Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/ |
| 3 | * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/ |
| 4 | * |
| 5 | * Written by Václav Kubernát <kubervac@fit.cvut.cz> |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #include "trompeloeil_catch.h" |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 10 | #include "ast_commands.hpp" |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 11 | #include "parser.hpp" |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 12 | #include "static_schema.hpp" |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 13 | |
| 14 | TEST_CASE("leaf editing") |
| 15 | { |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 16 | auto schema = std::make_shared<StaticSchema>(); |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 17 | schema->addModule("mod"); |
| 18 | schema->addContainer("", "mod:contA"); |
| 19 | schema->addLeaf("", "mod:leafString", yang::LeafDataTypes::String); |
| 20 | schema->addLeaf("", "mod:leafDecimal", yang::LeafDataTypes::Decimal); |
| 21 | schema->addLeaf("", "mod:leafBool", yang::LeafDataTypes::Bool); |
| 22 | schema->addLeaf("", "mod:leafInt", yang::LeafDataTypes::Int); |
| 23 | schema->addLeaf("", "mod:leafUint", yang::LeafDataTypes::Uint); |
Václav Kubernát | ab53899 | 2019-03-06 15:30:50 +0100 | [diff] [blame^] | 24 | schema->addLeaf("", "mod:leafBinary", yang::LeafDataTypes::Binary); |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 25 | schema->addLeafEnum("", "mod:leafEnum", {"lol", "data", "coze"}); |
| 26 | schema->addLeaf("mod:contA", "mod:leafInCont", yang::LeafDataTypes::String); |
| 27 | schema->addList("", "mod:list", {"number"}); |
| 28 | schema->addLeaf("mod:list", "mod:leafInList", yang::LeafDataTypes::String); |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 29 | Parser parser(schema); |
| 30 | std::string input; |
| 31 | std::ostringstream errorStream; |
| 32 | |
| 33 | SECTION("valid input") |
| 34 | { |
| 35 | set_ expected; |
| 36 | |
Václav Kubernát | ab53899 | 2019-03-06 15:30:50 +0100 | [diff] [blame^] | 37 | SECTION("set mod:leafString some_data") |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 38 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 39 | input = "set mod:leafString some_data"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 40 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafString")}); |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 41 | expected.m_data = std::string("some_data"); |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 42 | } |
| 43 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 44 | SECTION("set mod:contA/leafInCont more_data") |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 45 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 46 | input = "set mod:contA/leafInCont more_data"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 47 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, container_("contA")}); |
| 48 | expected.m_path.m_nodes.push_back(dataNode_{leaf_("leafInCont")}); |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 49 | expected.m_data = std::string("more_data"); |
| 50 | } |
| 51 | |
Václav Kubernát | ab53899 | 2019-03-06 15:30:50 +0100 | [diff] [blame^] | 52 | SECTION("set mod:list[number=1]/leafInList another_data") |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 53 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 54 | input = "set mod:list[number=1]/leafInList another_data"; |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 55 | auto keys = std::map<std::string, std::string>{ |
| 56 | {"number", "1"}}; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 57 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, listElement_("list", keys)}); |
| 58 | expected.m_path.m_nodes.push_back(dataNode_{leaf_("leafInList")}); |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 59 | expected.m_data = std::string("another_data"); |
| 60 | } |
| 61 | |
| 62 | SECTION("data types") |
| 63 | { |
| 64 | SECTION("string") |
| 65 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 66 | input = "set mod:leafString somedata"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 67 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafString")}); |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 68 | expected.m_data = std::string("somedata"); |
| 69 | } |
| 70 | |
| 71 | SECTION("int") |
| 72 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 73 | input = "set mod:leafInt 2"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 74 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafInt")}); |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 75 | expected.m_data = 2; |
| 76 | } |
| 77 | |
| 78 | SECTION("decimal") |
| 79 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 80 | input = "set mod:leafDecimal 3.14159"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 81 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafDecimal")}); |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 82 | expected.m_data = 3.14159; |
| 83 | } |
| 84 | |
| 85 | SECTION("enum") |
| 86 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 87 | input = "set mod:leafEnum coze"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 88 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafEnum")}); |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 89 | expected.m_data = enum_("coze"); |
| 90 | } |
| 91 | |
| 92 | SECTION("bool") |
| 93 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 94 | input = "set mod:leafBool true"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 95 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafBool")}); |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 96 | expected.m_data = true; |
| 97 | } |
Václav Kubernát | ab53899 | 2019-03-06 15:30:50 +0100 | [diff] [blame^] | 98 | |
| 99 | SECTION("binary") |
| 100 | { |
| 101 | SECTION("zero ending '='") |
| 102 | { |
| 103 | input = "set mod:leafBinary This/IsABase64EncodedSomething++/342431++"; |
| 104 | expected.m_data = binary_{"This/IsABase64EncodedSomething++/342431++"}; |
| 105 | } |
| 106 | |
| 107 | SECTION("one ending '='") |
| 108 | { |
| 109 | input = "set mod:leafBinary This/IsABase64EncodedSomething++/342431++="; |
| 110 | expected.m_data = binary_{"This/IsABase64EncodedSomething++/342431++="}; |
| 111 | } |
| 112 | |
| 113 | SECTION("two ending '='") |
| 114 | { |
| 115 | input = "set mod:leafBinary This/IsABase64EncodedSomething++/342431++=="; |
| 116 | expected.m_data = binary_{"This/IsABase64EncodedSomething++/342431++=="}; |
| 117 | } |
| 118 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafBinary")}); |
| 119 | } |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | command_ command = parser.parseCommand(input, errorStream); |
| 123 | REQUIRE(command.type() == typeid(set_)); |
| 124 | REQUIRE(boost::get<set_>(command) == expected); |
| 125 | } |
| 126 | |
| 127 | SECTION("invalid input") |
| 128 | { |
| 129 | SECTION("missing space between a command and its arguments") |
| 130 | { |
| 131 | SECTION("setleaf some_data") |
| 132 | { |
| 133 | input = "setleaf some_data"; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | SECTION("missing space between arguments") |
| 138 | { |
| 139 | SECTION("set leaflol") |
| 140 | { |
| 141 | input = "set leaflol"; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | SECTION("non-leaf identifiers") |
| 146 | { |
| 147 | SECTION("set nonexistent blabla") |
| 148 | { |
| 149 | input = "set nonexistent blabla"; |
| 150 | } |
| 151 | |
| 152 | SECTION("set contA abde") |
| 153 | { |
| 154 | input = "set contA abde"; |
| 155 | } |
| 156 | } |
| 157 | |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 158 | SECTION("wrong types") |
| 159 | { |
| 160 | SECTION("set leafBool blabla") |
| 161 | { |
| 162 | input = "set leafBool blabla"; |
| 163 | } |
| 164 | SECTION("set leafUint blabla") |
| 165 | { |
| 166 | input = "set leafUint blabla"; |
| 167 | } |
| 168 | SECTION("set leafInt blabla") |
| 169 | { |
| 170 | input = "set leafInt blabla"; |
| 171 | } |
| 172 | SECTION("set leafEnum blabla") |
| 173 | { |
| 174 | input = "set leafEnum blabla"; |
| 175 | } |
| 176 | } |
| 177 | |
Václav Kubernát | ab53899 | 2019-03-06 15:30:50 +0100 | [diff] [blame^] | 178 | SECTION("wrong base64 strings") |
| 179 | { |
| 180 | SECTION("invalid character") |
| 181 | input = "set leafBinary dbahj-"; |
| 182 | SECTION("equal sign in the middle") |
| 183 | input = "set leafBinary db=ahj"; |
| 184 | } |
| 185 | |
Václav Kubernát | b7a279d | 2018-10-04 15:27:50 +0200 | [diff] [blame] | 186 | REQUIRE_THROWS_AS(parser.parseCommand(input, errorStream), InvalidCommandException&); |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 187 | } |
| 188 | } |