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 | |
Jan Kundrát | a33cf08 | 2019-03-28 11:55:57 +0100 | [diff] [blame] | 9 | #include "trompeloeil_doctest.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"); |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 18 | schema->addModule("pizza-module"); |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 19 | schema->addContainer("", "mod:contA"); |
| 20 | schema->addLeaf("", "mod:leafString", yang::LeafDataTypes::String); |
| 21 | schema->addLeaf("", "mod:leafDecimal", yang::LeafDataTypes::Decimal); |
| 22 | schema->addLeaf("", "mod:leafBool", yang::LeafDataTypes::Bool); |
| 23 | schema->addLeaf("", "mod:leafInt", yang::LeafDataTypes::Int); |
| 24 | schema->addLeaf("", "mod:leafUint", yang::LeafDataTypes::Uint); |
Václav Kubernát | ab53899 | 2019-03-06 15:30:50 +0100 | [diff] [blame] | 25 | schema->addLeaf("", "mod:leafBinary", yang::LeafDataTypes::Binary); |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 26 | schema->addIdentity(std::nullopt, ModuleValuePair{"mod", "food"}); |
| 27 | schema->addIdentity(std::nullopt, ModuleValuePair{"mod", "vehicle"}); |
| 28 | schema->addIdentity(ModuleValuePair{"mod", "food"}, ModuleValuePair{"mod", "pizza"}); |
| 29 | schema->addIdentity(ModuleValuePair{"mod", "food"}, ModuleValuePair{"mod", "spaghetti"}); |
| 30 | schema->addIdentity(ModuleValuePair{"mod", "pizza"}, ModuleValuePair{"pizza-module", "hawaii"}); |
| 31 | schema->addLeafIdentityRef("", "mod:foodIdentRef", ModuleValuePair{"mod", "food"}); |
| 32 | schema->addLeafIdentityRef("", "mod:pizzaIdentRef", ModuleValuePair{"mod", "pizza"}); |
| 33 | schema->addLeafIdentityRef("mod:contA", "mod:identInCont", ModuleValuePair{"mod", "pizza"}); |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 34 | schema->addLeafEnum("", "mod:leafEnum", {"lol", "data", "coze"}); |
| 35 | schema->addLeaf("mod:contA", "mod:leafInCont", yang::LeafDataTypes::String); |
| 36 | schema->addList("", "mod:list", {"number"}); |
| 37 | schema->addLeaf("mod:list", "mod:leafInList", yang::LeafDataTypes::String); |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 38 | Parser parser(schema); |
| 39 | std::string input; |
| 40 | std::ostringstream errorStream; |
| 41 | |
| 42 | SECTION("valid input") |
| 43 | { |
| 44 | set_ expected; |
| 45 | |
Václav Kubernát | ab53899 | 2019-03-06 15:30:50 +0100 | [diff] [blame] | 46 | SECTION("set mod:leafString some_data") |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 47 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 48 | input = "set mod:leafString some_data"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 49 | 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] | 50 | expected.m_data = std::string("some_data"); |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 51 | } |
| 52 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 53 | SECTION("set mod:contA/leafInCont more_data") |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 54 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 55 | input = "set mod:contA/leafInCont more_data"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 56 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, container_("contA")}); |
| 57 | expected.m_path.m_nodes.push_back(dataNode_{leaf_("leafInCont")}); |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 58 | expected.m_data = std::string("more_data"); |
| 59 | } |
| 60 | |
Václav Kubernát | ab53899 | 2019-03-06 15:30:50 +0100 | [diff] [blame] | 61 | SECTION("set mod:list[number=1]/leafInList another_data") |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 62 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 63 | input = "set mod:list[number=1]/leafInList another_data"; |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 64 | auto keys = std::map<std::string, std::string>{ |
| 65 | {"number", "1"}}; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 66 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, listElement_("list", keys)}); |
| 67 | expected.m_path.m_nodes.push_back(dataNode_{leaf_("leafInList")}); |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 68 | expected.m_data = std::string("another_data"); |
| 69 | } |
| 70 | |
| 71 | SECTION("data types") |
| 72 | { |
| 73 | SECTION("string") |
| 74 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 75 | input = "set mod:leafString somedata"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 76 | 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] | 77 | expected.m_data = std::string("somedata"); |
| 78 | } |
| 79 | |
| 80 | SECTION("int") |
| 81 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 82 | input = "set mod:leafInt 2"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 83 | 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] | 84 | expected.m_data = 2; |
| 85 | } |
| 86 | |
| 87 | SECTION("decimal") |
| 88 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 89 | input = "set mod:leafDecimal 3.14159"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 90 | 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] | 91 | expected.m_data = 3.14159; |
| 92 | } |
| 93 | |
| 94 | SECTION("enum") |
| 95 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 96 | input = "set mod:leafEnum coze"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 97 | 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] | 98 | expected.m_data = enum_("coze"); |
| 99 | } |
| 100 | |
| 101 | SECTION("bool") |
| 102 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 103 | input = "set mod:leafBool true"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 104 | 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] | 105 | expected.m_data = true; |
| 106 | } |
Václav Kubernát | ab53899 | 2019-03-06 15:30:50 +0100 | [diff] [blame] | 107 | |
| 108 | SECTION("binary") |
| 109 | { |
| 110 | SECTION("zero ending '='") |
| 111 | { |
| 112 | input = "set mod:leafBinary This/IsABase64EncodedSomething++/342431++"; |
| 113 | expected.m_data = binary_{"This/IsABase64EncodedSomething++/342431++"}; |
| 114 | } |
| 115 | |
| 116 | SECTION("one ending '='") |
| 117 | { |
| 118 | input = "set mod:leafBinary This/IsABase64EncodedSomething++/342431++="; |
| 119 | expected.m_data = binary_{"This/IsABase64EncodedSomething++/342431++="}; |
| 120 | } |
| 121 | |
| 122 | SECTION("two ending '='") |
| 123 | { |
| 124 | input = "set mod:leafBinary This/IsABase64EncodedSomething++/342431++=="; |
| 125 | expected.m_data = binary_{"This/IsABase64EncodedSomething++/342431++=="}; |
| 126 | } |
| 127 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafBinary")}); |
| 128 | } |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 129 | |
| 130 | SECTION("identityRef") |
| 131 | { |
| 132 | SECTION("foodIdentRef") |
| 133 | { |
| 134 | input = "set mod:foodIdentRef "; |
| 135 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("foodIdentRef")}); |
| 136 | |
| 137 | SECTION("food") |
| 138 | { |
| 139 | input += "food"; |
| 140 | expected.m_data = identityRef_("food"); |
| 141 | } |
| 142 | SECTION("mod:food") |
| 143 | { |
| 144 | input += "mod:food"; |
| 145 | expected.m_data = identityRef_("mod", "food"); |
| 146 | } |
| 147 | SECTION("pizza") |
| 148 | { |
| 149 | input += "pizza"; |
| 150 | expected.m_data = identityRef_("pizza"); |
| 151 | } |
| 152 | SECTION("mod:pizza") |
| 153 | { |
| 154 | input += "mod:pizza"; |
| 155 | expected.m_data = identityRef_("mod", "pizza"); |
| 156 | } |
| 157 | SECTION("pizza-module:hawaii") |
| 158 | { |
| 159 | input += "pizza-module:hawaii"; |
| 160 | expected.m_data = identityRef_("pizza-module", "hawaii"); |
| 161 | } |
| 162 | } |
| 163 | SECTION("pizzaIdentRef") |
| 164 | { |
| 165 | input = "set mod:pizzaIdentRef "; |
| 166 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("pizzaIdentRef")}); |
| 167 | SECTION("pizza") |
| 168 | { |
| 169 | input += "pizza"; |
| 170 | expected.m_data = identityRef_("pizza"); |
| 171 | } |
| 172 | SECTION("mod:pizza") |
| 173 | { |
| 174 | input += "mod:pizza"; |
| 175 | expected.m_data = identityRef_("mod", "pizza"); |
| 176 | } |
| 177 | SECTION("pizza-module:hawaii") |
| 178 | { |
| 179 | input += "pizza-module:hawaii"; |
| 180 | expected.m_data = identityRef_("pizza-module", "hawaii"); |
| 181 | } |
| 182 | } |
| 183 | SECTION("mod:contA/identInCont") |
| 184 | { |
| 185 | input = "set mod:contA/identInCont "; |
| 186 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, container_("contA")}); |
| 187 | expected.m_path.m_nodes.push_back(dataNode_(leaf_("identInCont"))); |
| 188 | SECTION("pizza") |
| 189 | { |
| 190 | input += "pizza"; |
| 191 | expected.m_data = identityRef_("pizza"); |
| 192 | } |
| 193 | SECTION("mod:pizza") |
| 194 | { |
| 195 | input += "mod:pizza"; |
| 196 | expected.m_data = identityRef_("mod", "pizza"); |
| 197 | } |
| 198 | SECTION("pizza-module:hawaii") |
| 199 | { |
| 200 | input += "pizza-module:hawaii"; |
| 201 | expected.m_data = identityRef_("pizza-module", "hawaii"); |
| 202 | } |
| 203 | } |
| 204 | } |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | command_ command = parser.parseCommand(input, errorStream); |
| 208 | REQUIRE(command.type() == typeid(set_)); |
| 209 | REQUIRE(boost::get<set_>(command) == expected); |
| 210 | } |
| 211 | |
| 212 | SECTION("invalid input") |
| 213 | { |
| 214 | SECTION("missing space between a command and its arguments") |
| 215 | { |
| 216 | SECTION("setleaf some_data") |
| 217 | { |
| 218 | input = "setleaf some_data"; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | SECTION("missing space between arguments") |
| 223 | { |
| 224 | SECTION("set leaflol") |
| 225 | { |
| 226 | input = "set leaflol"; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | SECTION("non-leaf identifiers") |
| 231 | { |
| 232 | SECTION("set nonexistent blabla") |
| 233 | { |
| 234 | input = "set nonexistent blabla"; |
| 235 | } |
| 236 | |
| 237 | SECTION("set contA abde") |
| 238 | { |
| 239 | input = "set contA abde"; |
| 240 | } |
| 241 | } |
| 242 | |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 243 | SECTION("wrong types") |
| 244 | { |
| 245 | SECTION("set leafBool blabla") |
| 246 | { |
| 247 | input = "set leafBool blabla"; |
| 248 | } |
| 249 | SECTION("set leafUint blabla") |
| 250 | { |
| 251 | input = "set leafUint blabla"; |
| 252 | } |
| 253 | SECTION("set leafInt blabla") |
| 254 | { |
| 255 | input = "set leafInt blabla"; |
| 256 | } |
| 257 | SECTION("set leafEnum blabla") |
| 258 | { |
| 259 | input = "set leafEnum blabla"; |
| 260 | } |
| 261 | } |
| 262 | |
Václav Kubernát | ab53899 | 2019-03-06 15:30:50 +0100 | [diff] [blame] | 263 | SECTION("wrong base64 strings") |
| 264 | { |
| 265 | SECTION("invalid character") |
| 266 | input = "set leafBinary dbahj-"; |
| 267 | SECTION("equal sign in the middle") |
| 268 | input = "set leafBinary db=ahj"; |
| 269 | } |
| 270 | |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 271 | SECTION("non-existing identity") |
| 272 | { |
| 273 | input = "set mod:foodIdentRef identityBLABLA"; |
| 274 | } |
| 275 | |
| 276 | SECTION("setting identities with wrong bases") |
| 277 | { |
| 278 | SECTION("set mod:foodIdentRef mod:vehicle") |
| 279 | { |
| 280 | input = "set mod:foodIdentRef mod:vehicle"; |
| 281 | } |
| 282 | SECTION("set mod:pizzaIdentRef mod:food") |
| 283 | { |
| 284 | input = "set mod:pizzaIdentRef mod:food"; |
| 285 | } |
| 286 | } |
| 287 | SECTION("setting different module identities without prefix") |
| 288 | { |
| 289 | input = "set mod:pizzaIdentRef hawaii"; |
| 290 | } |
| 291 | SECTION("identity prefix without name") |
| 292 | { |
| 293 | input = "set mod:contA/identInCont pizza-module:"; |
| 294 | } |
| 295 | |
Jan Kundrát | c381e63 | 2019-03-14 13:39:11 +0100 | [diff] [blame] | 296 | REQUIRE_THROWS_AS(parser.parseCommand(input, errorStream), InvalidCommandException); |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 297 | } |
| 298 | } |