blob: 1ec4be4c8d57b670dee7bba01cfb23deeb3f3814 [file] [log] [blame]
Václav Kubernát07204242018-06-04 18:12:09 +02001/*
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áta33cf082019-03-28 11:55:57 +01009#include "trompeloeil_doctest.h"
Jan Kundrát76b3ddc2019-07-03 10:31:27 +020010#include <boost/core/demangle.hpp>
Václav Kubernát24df80e2018-06-06 15:18:03 +020011#include "ast_commands.hpp"
Václav Kubernát07204242018-06-04 18:12:09 +020012#include "parser.hpp"
Václav Kubernátbddbb172018-06-13 16:27:39 +020013#include "static_schema.hpp"
Václav Kubernát07204242018-06-04 18:12:09 +020014
Jan Kundrát76b3ddc2019-07-03 10:31:27 +020015std::ostream& operator<<(std::ostream& s, const set_ cmd)
16{
17 return s << "Command SET {path: " << pathToAbsoluteSchemaString(cmd.m_path) << ", type " << boost::core::demangle(cmd.m_data.type().name()) << ", data: " << leafDataToString(cmd.m_data) << "}";
18}
19
Václav Kubernát07204242018-06-04 18:12:09 +020020TEST_CASE("leaf editing")
21{
Václav Kubernátbddbb172018-06-13 16:27:39 +020022 auto schema = std::make_shared<StaticSchema>();
Václav Kubernát744f57f2018-06-29 22:46:26 +020023 schema->addModule("mod");
Václav Kubernáteeb38842019-03-20 19:46:05 +010024 schema->addModule("pizza-module");
Václav Kubernát744f57f2018-06-29 22:46:26 +020025 schema->addContainer("", "mod:contA");
26 schema->addLeaf("", "mod:leafString", yang::LeafDataTypes::String);
27 schema->addLeaf("", "mod:leafDecimal", yang::LeafDataTypes::Decimal);
28 schema->addLeaf("", "mod:leafBool", yang::LeafDataTypes::Bool);
Ivona Oboňová88c78ca2019-07-02 18:40:07 +020029 schema->addLeaf("", "mod:leafInt8", yang::LeafDataTypes::Int8);
30 schema->addLeaf("", "mod:leafInt16", yang::LeafDataTypes::Int16);
31 schema->addLeaf("", "mod:leafInt32", yang::LeafDataTypes::Int32);
32 schema->addLeaf("", "mod:leafInt64", yang::LeafDataTypes::Int64);
33 schema->addLeaf("", "mod:leafUint8", yang::LeafDataTypes::Uint8);
34 schema->addLeaf("", "mod:leafUint16", yang::LeafDataTypes::Uint16);
35 schema->addLeaf("", "mod:leafUint32", yang::LeafDataTypes::Uint32);
36 schema->addLeaf("", "mod:leafUint64", yang::LeafDataTypes::Uint64);
Václav Kubernátab538992019-03-06 15:30:50 +010037 schema->addLeaf("", "mod:leafBinary", yang::LeafDataTypes::Binary);
Václav Kubernáteeb38842019-03-20 19:46:05 +010038 schema->addIdentity(std::nullopt, ModuleValuePair{"mod", "food"});
39 schema->addIdentity(std::nullopt, ModuleValuePair{"mod", "vehicle"});
40 schema->addIdentity(ModuleValuePair{"mod", "food"}, ModuleValuePair{"mod", "pizza"});
41 schema->addIdentity(ModuleValuePair{"mod", "food"}, ModuleValuePair{"mod", "spaghetti"});
42 schema->addIdentity(ModuleValuePair{"mod", "pizza"}, ModuleValuePair{"pizza-module", "hawaii"});
43 schema->addLeafIdentityRef("", "mod:foodIdentRef", ModuleValuePair{"mod", "food"});
44 schema->addLeafIdentityRef("", "mod:pizzaIdentRef", ModuleValuePair{"mod", "pizza"});
45 schema->addLeafIdentityRef("mod:contA", "mod:identInCont", ModuleValuePair{"mod", "pizza"});
Václav Kubernát744f57f2018-06-29 22:46:26 +020046 schema->addLeafEnum("", "mod:leafEnum", {"lol", "data", "coze"});
47 schema->addLeaf("mod:contA", "mod:leafInCont", yang::LeafDataTypes::String);
48 schema->addList("", "mod:list", {"number"});
49 schema->addLeaf("mod:list", "mod:leafInList", yang::LeafDataTypes::String);
Václav Kubernát07204242018-06-04 18:12:09 +020050 Parser parser(schema);
51 std::string input;
52 std::ostringstream errorStream;
53
54 SECTION("valid input")
55 {
56 set_ expected;
57
Václav Kubernátab538992019-03-06 15:30:50 +010058 SECTION("set mod:leafString some_data")
Václav Kubernát07204242018-06-04 18:12:09 +020059 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020060 input = "set mod:leafString some_data";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020061 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafString")});
Václav Kubernátebca2552018-06-08 19:06:02 +020062 expected.m_data = std::string("some_data");
Václav Kubernát07204242018-06-04 18:12:09 +020063 }
64
Václav Kubernát744f57f2018-06-29 22:46:26 +020065 SECTION("set mod:contA/leafInCont more_data")
Václav Kubernát07204242018-06-04 18:12:09 +020066 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020067 input = "set mod:contA/leafInCont more_data";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020068 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, container_("contA")});
69 expected.m_path.m_nodes.push_back(dataNode_{leaf_("leafInCont")});
Václav Kubernátebca2552018-06-08 19:06:02 +020070 expected.m_data = std::string("more_data");
71 }
72
Václav Kubernátab538992019-03-06 15:30:50 +010073 SECTION("set mod:list[number=1]/leafInList another_data")
Václav Kubernátebca2552018-06-08 19:06:02 +020074 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020075 input = "set mod:list[number=1]/leafInList another_data";
Václav Kubernátebca2552018-06-08 19:06:02 +020076 auto keys = std::map<std::string, std::string>{
77 {"number", "1"}};
Václav Kubernát2eaceb82018-10-08 19:56:30 +020078 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, listElement_("list", keys)});
79 expected.m_path.m_nodes.push_back(dataNode_{leaf_("leafInList")});
Václav Kubernátebca2552018-06-08 19:06:02 +020080 expected.m_data = std::string("another_data");
81 }
82
83 SECTION("data types")
84 {
85 SECTION("string")
86 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020087 input = "set mod:leafString somedata";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020088 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafString")});
Václav Kubernátebca2552018-06-08 19:06:02 +020089 expected.m_data = std::string("somedata");
90 }
91
Ivona Oboňová88c78ca2019-07-02 18:40:07 +020092 SECTION("int8")
Václav Kubernátebca2552018-06-08 19:06:02 +020093 {
Ivona Oboňová88c78ca2019-07-02 18:40:07 +020094 input = "set mod:leafInt8 2";
95 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafInt8")});
96 expected.m_data = int8_t{2};
97 }
98
99 SECTION("negative int8")
100 {
101 input = "set mod:leafInt8 -10";
102 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafInt8")});
103 expected.m_data = int8_t{-10};
104 }
105
106 SECTION("uint8")
107 {
108 input = "set mod:leafUint8 2";
109 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafUint8")});
110 expected.m_data = uint8_t{2};
111 }
112
113 SECTION("int16")
114 {
115 input = "set mod:leafInt16 30000";
116 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafInt16")});
117 expected.m_data = int16_t{30'000};
118 }
119
120 SECTION("uint16")
121 {
122 input = "set mod:leafUint16 30000";
123 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafUint16")});
124 expected.m_data = uint16_t{30'000};
125 }
126
127 SECTION("int32")
128 {
129 input = "set mod:leafInt32 30000";
130 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafInt32")});
131 expected.m_data = int32_t{30'000};
132 }
133
134 SECTION("uint32")
135 {
136 input = "set mod:leafUint32 30000";
137 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafUint32")});
138 expected.m_data = uint32_t{30'000};
139 }
140
141 SECTION("int32")
142 {
143 input = "set mod:leafInt32 30000";
144 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafInt32")});
145 expected.m_data = int32_t{30'000};
146 }
147
148 SECTION("uint64")
149 {
150 input = "set mod:leafUint64 30000";
151 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafUint64")});
152 expected.m_data = uint64_t{30'000};
Václav Kubernátebca2552018-06-08 19:06:02 +0200153 }
154
155 SECTION("decimal")
156 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200157 input = "set mod:leafDecimal 3.14159";
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200158 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafDecimal")});
Václav Kubernátebca2552018-06-08 19:06:02 +0200159 expected.m_data = 3.14159;
160 }
161
162 SECTION("enum")
163 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200164 input = "set mod:leafEnum coze";
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200165 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafEnum")});
Václav Kubernátebca2552018-06-08 19:06:02 +0200166 expected.m_data = enum_("coze");
167 }
168
169 SECTION("bool")
170 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200171 input = "set mod:leafBool true";
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200172 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafBool")});
Václav Kubernátebca2552018-06-08 19:06:02 +0200173 expected.m_data = true;
174 }
Václav Kubernátab538992019-03-06 15:30:50 +0100175
176 SECTION("binary")
177 {
178 SECTION("zero ending '='")
179 {
180 input = "set mod:leafBinary This/IsABase64EncodedSomething++/342431++";
181 expected.m_data = binary_{"This/IsABase64EncodedSomething++/342431++"};
182 }
183
184 SECTION("one ending '='")
185 {
186 input = "set mod:leafBinary This/IsABase64EncodedSomething++/342431++=";
187 expected.m_data = binary_{"This/IsABase64EncodedSomething++/342431++="};
188 }
189
190 SECTION("two ending '='")
191 {
192 input = "set mod:leafBinary This/IsABase64EncodedSomething++/342431++==";
193 expected.m_data = binary_{"This/IsABase64EncodedSomething++/342431++=="};
194 }
195 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafBinary")});
196 }
Václav Kubernáteeb38842019-03-20 19:46:05 +0100197
198 SECTION("identityRef")
199 {
200 SECTION("foodIdentRef")
201 {
202 input = "set mod:foodIdentRef ";
203 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("foodIdentRef")});
204
205 SECTION("food")
206 {
207 input += "food";
208 expected.m_data = identityRef_("food");
209 }
210 SECTION("mod:food")
211 {
212 input += "mod:food";
213 expected.m_data = identityRef_("mod", "food");
214 }
215 SECTION("pizza")
216 {
217 input += "pizza";
218 expected.m_data = identityRef_("pizza");
219 }
220 SECTION("mod:pizza")
221 {
222 input += "mod:pizza";
223 expected.m_data = identityRef_("mod", "pizza");
224 }
225 SECTION("pizza-module:hawaii")
226 {
227 input += "pizza-module:hawaii";
228 expected.m_data = identityRef_("pizza-module", "hawaii");
229 }
230 }
231 SECTION("pizzaIdentRef")
232 {
233 input = "set mod:pizzaIdentRef ";
234 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("pizzaIdentRef")});
235 SECTION("pizza")
236 {
237 input += "pizza";
238 expected.m_data = identityRef_("pizza");
239 }
240 SECTION("mod:pizza")
241 {
242 input += "mod:pizza";
243 expected.m_data = identityRef_("mod", "pizza");
244 }
245 SECTION("pizza-module:hawaii")
246 {
247 input += "pizza-module:hawaii";
248 expected.m_data = identityRef_("pizza-module", "hawaii");
249 }
250 }
251 SECTION("mod:contA/identInCont")
252 {
253 input = "set mod:contA/identInCont ";
254 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, container_("contA")});
255 expected.m_path.m_nodes.push_back(dataNode_(leaf_("identInCont")));
256 SECTION("pizza")
257 {
258 input += "pizza";
259 expected.m_data = identityRef_("pizza");
260 }
261 SECTION("mod:pizza")
262 {
263 input += "mod:pizza";
264 expected.m_data = identityRef_("mod", "pizza");
265 }
266 SECTION("pizza-module:hawaii")
267 {
268 input += "pizza-module:hawaii";
269 expected.m_data = identityRef_("pizza-module", "hawaii");
270 }
271 }
272 }
Václav Kubernát07204242018-06-04 18:12:09 +0200273 }
274
275 command_ command = parser.parseCommand(input, errorStream);
276 REQUIRE(command.type() == typeid(set_));
277 REQUIRE(boost::get<set_>(command) == expected);
278 }
279
280 SECTION("invalid input")
281 {
282 SECTION("missing space between a command and its arguments")
283 {
284 SECTION("setleaf some_data")
285 {
286 input = "setleaf some_data";
287 }
288 }
289
290 SECTION("missing space between arguments")
291 {
292 SECTION("set leaflol")
293 {
294 input = "set leaflol";
295 }
296 }
297
298 SECTION("non-leaf identifiers")
299 {
300 SECTION("set nonexistent blabla")
301 {
302 input = "set nonexistent blabla";
303 }
304
305 SECTION("set contA abde")
306 {
307 input = "set contA abde";
308 }
309 }
310
Václav Kubernátebca2552018-06-08 19:06:02 +0200311 SECTION("wrong types")
312 {
313 SECTION("set leafBool blabla")
314 {
315 input = "set leafBool blabla";
316 }
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200317 SECTION("set leafUint8 blabla")
Václav Kubernátebca2552018-06-08 19:06:02 +0200318 {
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200319 input = "set leafUint8 blabla";
Václav Kubernátebca2552018-06-08 19:06:02 +0200320 }
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200321 SECTION("set leafUint8 -5")
Václav Kubernátebca2552018-06-08 19:06:02 +0200322 {
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200323 input = "set leafUint8 -5";
324 }
325 SECTION("set leafInt8 blabla")
326 {
327 input = "set leafInt8 blabla";
328 }
329 SECTION("set leafInt8 130")
330 {
331 input = "set leafInt8 130";
332 }
333 SECTION("set leafUint16 blabla")
334 {
335 input = "set leafUint16 blabla";
336 }
337 SECTION("set leafInt16 blabla")
338 {
339 input = "set leafInt16 blabla";
340 }
341 SECTION("set leafUint32 blabla")
342 {
343 input = "set leafUint32 blabla";
344 }
345 SECTION("set leafInt32 blabla")
346 {
347 input = "set leafInt32 blabla";
348 }
349 SECTION("set leafUint64 blabla")
350 {
351 input = "set leafUint64 blabla";
352 }
353 SECTION("set leafInt64 blabla")
354 {
355 input = "set leafInt64 blabla";
Václav Kubernátebca2552018-06-08 19:06:02 +0200356 }
357 SECTION("set leafEnum blabla")
358 {
359 input = "set leafEnum blabla";
360 }
361 }
362
Václav Kubernátab538992019-03-06 15:30:50 +0100363 SECTION("wrong base64 strings")
364 {
365 SECTION("invalid character")
366 input = "set leafBinary dbahj-";
367 SECTION("equal sign in the middle")
368 input = "set leafBinary db=ahj";
369 }
370
Václav Kubernáteeb38842019-03-20 19:46:05 +0100371 SECTION("non-existing identity")
372 {
373 input = "set mod:foodIdentRef identityBLABLA";
374 }
375
376 SECTION("setting identities with wrong bases")
377 {
378 SECTION("set mod:foodIdentRef mod:vehicle")
379 {
380 input = "set mod:foodIdentRef mod:vehicle";
381 }
382 SECTION("set mod:pizzaIdentRef mod:food")
383 {
384 input = "set mod:pizzaIdentRef mod:food";
385 }
386 }
387 SECTION("setting different module identities without prefix")
388 {
389 input = "set mod:pizzaIdentRef hawaii";
390 }
391 SECTION("identity prefix without name")
392 {
393 input = "set mod:contA/identInCont pizza-module:";
394 }
395
Jan Kundrátc381e632019-03-14 13:39:11 +0100396 REQUIRE_THROWS_AS(parser.parseCommand(input, errorStream), InvalidCommandException);
Václav Kubernát07204242018-06-04 18:12:09 +0200397 }
398}