blob: 302c8e459a974d7877512333cafb01e1382531cc [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
Václav Kubernát26b56082020-02-03 18:28:56 +01009#include "trompeloeil_doctest.hpp"
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át3a99f002020-03-31 02:27:41 +020012#include "leaf_data_helpers.hpp"
Václav Kubernát07204242018-06-04 18:12:09 +020013#include "parser.hpp"
Václav Kubernáted824d02020-06-09 15:48:30 +020014#include "pretty_printers.hpp"
Václav Kubernátbddbb172018-06-13 16:27:39 +020015#include "static_schema.hpp"
Václav Kubernát509ce652019-05-29 19:46:44 +020016#include "utils.hpp"
Václav Kubernát07204242018-06-04 18:12:09 +020017
18TEST_CASE("leaf editing")
19{
Václav Kubernátbddbb172018-06-13 16:27:39 +020020 auto schema = std::make_shared<StaticSchema>();
Václav Kubernát744f57f2018-06-29 22:46:26 +020021 schema->addModule("mod");
Václav Kubernáteeb38842019-03-20 19:46:05 +010022 schema->addModule("pizza-module");
Václav Kubernátefcac932020-01-10 15:26:32 +010023 schema->addContainer("/", "mod:contA");
Václav Kubernát3a99f002020-03-31 02:27:41 +020024 schema->addLeaf("/", "mod:leafString", yang::String{});
25 schema->addLeaf("/", "mod:leafDecimal", yang::Decimal{});
26 schema->addLeaf("/", "mod:leafBool", yang::Bool{});
27 schema->addLeaf("/", "mod:leafInt8", yang::Int8{});
28 schema->addLeaf("/", "mod:leafInt16", yang::Int16{});
29 schema->addLeaf("/", "mod:leafInt32", yang::Int32{});
30 schema->addLeaf("/", "mod:leafInt64", yang::Int64{});
31 schema->addLeaf("/", "mod:leafUint8", yang::Uint8{});
32 schema->addLeaf("/", "mod:leafUint16", yang::Uint16{});
33 schema->addLeaf("/", "mod:leafUint32", yang::Uint32{});
34 schema->addLeaf("/", "mod:leafUint64", yang::Uint64{});
35 schema->addLeaf("/", "mod:leafBinary", yang::Binary{});
Václav Kubernát222ecff2020-05-14 23:14:35 +020036 schema->addIdentity(std::nullopt, identityRef_{"mod", "food"});
37 schema->addIdentity(std::nullopt, identityRef_{"mod", "vehicle"});
38 schema->addIdentity(identityRef_{"mod", "food"}, identityRef_{"mod", "pizza"});
39 schema->addIdentity(identityRef_{"mod", "food"}, identityRef_{"mod", "spaghetti"});
40 schema->addIdentity(identityRef_{"mod", "pizza"}, identityRef_{"pizza-module", "hawaii"});
Václav Kubernát3a99f002020-03-31 02:27:41 +020041 schema->addLeaf("/", "mod:foodIdentRef", yang::IdentityRef{schema->validIdentities("mod", "food")});
42 schema->addLeaf("/", "mod:pizzaIdentRef", yang::IdentityRef{schema->validIdentities("mod", "pizza")});
43 schema->addLeaf("/mod:contA", "mod:identInCont", yang::IdentityRef{schema->validIdentities("mod", "pizza")});
44 schema->addLeaf("/", "mod:leafEnum", createEnum({"lol", "data", "coze"}));
45 schema->addLeaf("/mod:contA", "mod:leafInCont", yang::String{});
Václav Kubernátefcac932020-01-10 15:26:32 +010046 schema->addList("/", "mod:list", {"number"});
Václav Kubernát3a99f002020-03-31 02:27:41 +020047 schema->addLeaf("/mod:list", "mod:number", yang::Int32{});
48 schema->addLeaf("/mod:list", "mod:leafInList", yang::String{});
Václav Kubernát13b23d72020-04-16 21:49:51 +020049 schema->addLeaf("/", "mod:refToString", yang::LeafRef{"/mod:leafString", std::make_unique<yang::TypeInfo>(schema->leafType("/mod:leafString"))});
50 schema->addLeaf("/", "mod:refToInt8", yang::LeafRef{"/mod:leafInt8", std::make_unique<yang::TypeInfo>(schema->leafType("/mod:leafInt8"))});
51 schema->addLeaf("/", "mod:refToLeafInCont", yang::LeafRef{"/mod:contA/identInCont", std::make_unique<yang::TypeInfo>(schema->leafType("/mod:contA/mod:identInCont"))});
52 schema->addLeaf("/", "mod:intOrString", yang::Union{{yang::TypeInfo{yang::Int32{}}, yang::TypeInfo{yang::String{}}}});
53 schema->addLeaf("/", "mod:twoInts", yang::Union{{yang::TypeInfo{yang::Uint8{}}, yang::TypeInfo{yang::Int16{}}}});
Václav Kubernát2984f442020-02-20 17:43:35 +010054 schema->addLeaf("/", "mod:unionStringEnumLeafref", yang::Union{{
Václav Kubernát13b23d72020-04-16 21:49:51 +020055 yang::LeafDataType{yang::String{}},
56 yang::LeafDataType{createEnum({"foo", "bar"})},
57 yang::LeafDataType{yang::LeafRef{"/mod:leafEnum", std::make_unique<yang::TypeInfo>(schema->leafType("/mod:leafEnum"))}}
Václav Kubernát2984f442020-02-20 17:43:35 +010058 }});
59
60 schema->addList("/", "mod:portSettings", {"port"});
61 schema->addLeaf("/mod:portSettings", "mod:port", createEnum({"eth0", "eth1", "eth2"}));
62 schema->addList("/", "mod:portMapping", {"port"});
63 schema->addLeaf("/mod:portMapping", "mod:port", createEnum({"utf1", "utf2", "utf3"}));
Václav Kubernát13b23d72020-04-16 21:49:51 +020064 schema->addLeaf("/", "mod:activeMappedPort", yang::LeafRef{"/mod:portMapping/mod:port", std::make_unique<yang::TypeInfo>(schema->leafType("/mod:portMapping/mod:port"))});
Václav Kubernát2984f442020-02-20 17:43:35 +010065 schema->addLeaf("/", "mod:activePort", yang::Union{{
Václav Kubernát13b23d72020-04-16 21:49:51 +020066 yang::TypeInfo{createEnum({"wlan0", "wlan1"})},
67 yang::TypeInfo{yang::LeafRef{"/mod:portSettings/mod:port", std::make_unique<yang::TypeInfo>(schema->leafType("/mod:portSettings/mod:port"))}},
68 yang::TypeInfo{yang::LeafRef{"/mod:activeMappedPort", std::make_unique<yang::TypeInfo>(schema->leafType("/mod:activeMappedPort"))}},
Jan Kundrát379bb572020-05-07 03:23:13 +020069 yang::TypeInfo{yang::Empty{}},
Václav Kubernát2984f442020-02-20 17:43:35 +010070 }});
Jan Kundrát379bb572020-05-07 03:23:13 +020071 schema->addLeaf("/", "mod:dummy", yang::Empty{});
Václav Kubernátabf52802020-05-19 01:31:17 +020072 schema->addLeaf("/", "mod:readonly", yang::Int32{}, yang::AccessType::ReadOnly);
Václav Kubernát2984f442020-02-20 17:43:35 +010073
Václav Kubernátdab73ca2020-10-26 23:44:43 +010074 schema->addLeaf("/", "mod:flags", yang::Bits{{"carry", "sign"}});
Jan Kundrátbb7aa852023-08-30 11:51:43 +020075 schema->addLeaf("/", "mod:iid", yang::InstanceIdentifier{});
76 schema->addLeaf("/mod:contA", "mod:x", yang::String{});
77 schema->addLeaf("/mod:contA", "mod:iid", yang::InstanceIdentifier{});
Václav Kubernátdab73ca2020-10-26 23:44:43 +010078
Václav Kubernát07204242018-06-04 18:12:09 +020079 Parser parser(schema);
80 std::string input;
81 std::ostringstream errorStream;
82
83 SECTION("valid input")
84 {
85 set_ expected;
Jan Kundrát03c77a12023-08-28 14:23:05 +020086 dataPath_ cwd;
Václav Kubernát07204242018-06-04 18:12:09 +020087
Václav Kubernát1bbac7c2020-01-15 17:45:41 +010088 SECTION("set mod:leafString \"some_data\"")
Václav Kubernát07204242018-06-04 18:12:09 +020089 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +010090 input = "set mod:leafString \'some_data\'";
Václav Kubernátfaacd022020-07-08 16:44:38 +020091 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafString"));
Václav Kubernátebca2552018-06-08 19:06:02 +020092 expected.m_data = std::string("some_data");
Václav Kubernát07204242018-06-04 18:12:09 +020093 }
94
Jan Kundrát03c77a12023-08-28 14:23:05 +020095 SECTION("cd mod:contA; set leafInCont 'x'")
96 {
97 input = "set leafInCont 'x'";
98 cwd.m_nodes.emplace_back(module_{"mod"}, container_{"contA"});
99 expected.m_path.m_nodes.emplace_back(leaf_("leafInCont"));
100 expected.m_data = std::string("x");
101 }
102
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100103 SECTION("set mod:contA/leafInCont 'more_data'")
Václav Kubernát07204242018-06-04 18:12:09 +0200104 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100105 input = "set mod:contA/leafInCont 'more_data'";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200106 expected.m_path.m_nodes.emplace_back(module_{"mod"}, container_("contA"));
107 expected.m_path.m_nodes.emplace_back(leaf_("leafInCont"));
Václav Kubernátebca2552018-06-08 19:06:02 +0200108 expected.m_data = std::string("more_data");
109 }
110
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100111 SECTION("set mod:contA/leafInCont \"data with' a quote\"")
Václav Kubernátc18ffac2020-01-10 16:06:06 +0100112 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100113 input = "set mod:contA/leafInCont \"data with' a quote\"";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200114 expected.m_path.m_nodes.emplace_back(module_{"mod"}, container_("contA"));
115 expected.m_path.m_nodes.emplace_back(leaf_("leafInCont"));
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100116 expected.m_data = std::string("data with' a quote");
117 }
118
119 SECTION("set mod:contA/leafInCont 'data with\" a quote'")
120 {
121 input = "set mod:contA/leafInCont 'data with\" a quote'";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200122 expected.m_path.m_nodes.emplace_back(module_{"mod"}, container_("contA"));
123 expected.m_path.m_nodes.emplace_back(leaf_("leafInCont"));
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100124 expected.m_data = std::string("data with\" a quote");
125 }
126
127 SECTION("set mod:contA/leafInCont 'more d\tata'") // spaces in string
128 {
129 input = "set mod:contA/leafInCont 'more d\tata'";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200130 expected.m_path.m_nodes.emplace_back(module_{"mod"}, container_("contA"));
131 expected.m_path.m_nodes.emplace_back(leaf_("leafInCont"));
Václav Kubernátc18ffac2020-01-10 16:06:06 +0100132 expected.m_data = std::string("more d\tata");
133 }
134
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100135 SECTION("set mod:list[number=1]/leafInList \"another_data\"")
Václav Kubernátebca2552018-06-08 19:06:02 +0200136 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100137 input = "set mod:list[number=1]/leafInList \"another_data\"";
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100138 auto keys = ListInstance{
Václav Kubernát7707cae2020-01-16 12:04:53 +0100139 {"number", int32_t{1}}};
Václav Kubernátfaacd022020-07-08 16:44:38 +0200140 expected.m_path.m_nodes.emplace_back(module_{"mod"}, listElement_("list", keys));
141 expected.m_path.m_nodes.emplace_back(leaf_("leafInList"));
Václav Kubernátebca2552018-06-08 19:06:02 +0200142 expected.m_data = std::string("another_data");
143 }
144
145 SECTION("data types")
146 {
147 SECTION("string")
148 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100149 input = "set mod:leafString \"somedata\"";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200150 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafString"));
Václav Kubernátebca2552018-06-08 19:06:02 +0200151 expected.m_data = std::string("somedata");
152 }
153
Václav Kubernátb8bad062021-02-04 00:02:45 +0100154 SECTION("more spaces before value")
155 {
156 input = "set mod:leafString \"somedata\"";
157 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafString"));
158 expected.m_data = std::string("somedata");
159 }
160
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200161 SECTION("int8")
Václav Kubernátebca2552018-06-08 19:06:02 +0200162 {
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200163 input = "set mod:leafInt8 2";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200164 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafInt8"));
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200165 expected.m_data = int8_t{2};
166 }
167
168 SECTION("negative int8")
169 {
170 input = "set mod:leafInt8 -10";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200171 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafInt8"));
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200172 expected.m_data = int8_t{-10};
173 }
174
175 SECTION("uint8")
176 {
177 input = "set mod:leafUint8 2";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200178 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafUint8"));
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200179 expected.m_data = uint8_t{2};
180 }
181
182 SECTION("int16")
183 {
184 input = "set mod:leafInt16 30000";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200185 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafInt16"));
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200186 expected.m_data = int16_t{30'000};
187 }
188
189 SECTION("uint16")
190 {
191 input = "set mod:leafUint16 30000";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200192 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafUint16"));
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200193 expected.m_data = uint16_t{30'000};
194 }
195
196 SECTION("int32")
197 {
198 input = "set mod:leafInt32 30000";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200199 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafInt32"));
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200200 expected.m_data = int32_t{30'000};
201 }
202
203 SECTION("uint32")
204 {
205 input = "set mod:leafUint32 30000";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200206 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafUint32"));
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200207 expected.m_data = uint32_t{30'000};
208 }
209
210 SECTION("int32")
211 {
212 input = "set mod:leafInt32 30000";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200213 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafInt32"));
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200214 expected.m_data = int32_t{30'000};
215 }
216
217 SECTION("uint64")
218 {
219 input = "set mod:leafUint64 30000";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200220 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafUint64"));
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200221 expected.m_data = uint64_t{30'000};
Václav Kubernátebca2552018-06-08 19:06:02 +0200222 }
223
224 SECTION("decimal")
225 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200226 input = "set mod:leafDecimal 3.14159";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200227 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafDecimal"));
Václav Kubernátebca2552018-06-08 19:06:02 +0200228 expected.m_data = 3.14159;
229 }
230
231 SECTION("enum")
232 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200233 input = "set mod:leafEnum coze";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200234 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafEnum"));
Václav Kubernátebca2552018-06-08 19:06:02 +0200235 expected.m_data = enum_("coze");
236 }
237
238 SECTION("bool")
239 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200240 input = "set mod:leafBool true";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200241 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafBool"));
Václav Kubernátebca2552018-06-08 19:06:02 +0200242 expected.m_data = true;
243 }
Václav Kubernátab538992019-03-06 15:30:50 +0100244
Václav Kubernát2984f442020-02-20 17:43:35 +0100245 SECTION("union")
246 {
247 SECTION("int")
248 {
Václav Kubernátfaacd022020-07-08 16:44:38 +0200249 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("intOrString"));
Václav Kubernát2984f442020-02-20 17:43:35 +0100250 input = "set mod:intOrString 90";
251 expected.m_data = int32_t{90};
252 }
253 SECTION("string")
254 {
Václav Kubernátfaacd022020-07-08 16:44:38 +0200255 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("intOrString"));
Václav Kubernát2984f442020-02-20 17:43:35 +0100256 input = "set mod:intOrString \"test\"";
257 expected.m_data = std::string{"test"};
258 }
259
260 SECTION("union with two integral types")
261 {
Václav Kubernátfaacd022020-07-08 16:44:38 +0200262 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("twoInts"));
Václav Kubernát2984f442020-02-20 17:43:35 +0100263 SECTION("uint8")
264 {
265 input = "set mod:twoInts 100";
266 expected.m_data = uint8_t{100};
267 }
268 SECTION("int16")
269 {
270 input = "set mod:twoInts 6666";
271 expected.m_data = int16_t{6666};
272 }
273 }
274
275 SECTION("union with enum and leafref to enum")
276 {
Václav Kubernátfaacd022020-07-08 16:44:38 +0200277 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("unionStringEnumLeafref"));
Václav Kubernát2984f442020-02-20 17:43:35 +0100278 SECTION("string")
279 {
280 input = "set mod:unionStringEnumLeafref \"AHOJ\"";
281 expected.m_data = std::string{"AHOJ"};
282 }
283 SECTION("enum")
284 {
285 input = "set mod:unionStringEnumLeafref bar";
286 expected.m_data = enum_("bar");
287 }
288 SECTION("enum leafref")
289 {
290 input = "set mod:unionStringEnumLeafref coze";
291 expected.m_data = enum_("coze");
292 }
293 }
294
295 SECTION("activePort")
296 {
Václav Kubernátfaacd022020-07-08 16:44:38 +0200297 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("activePort"));
Václav Kubernát2984f442020-02-20 17:43:35 +0100298 input = "set mod:activePort ";
299 SECTION("1. anonymous enum")
300 {
301 SECTION("wlan0")
302 {
303 input += "wlan0";
304 expected.m_data = enum_("wlan0");
305 }
306 SECTION("wlan1")
307 {
308 input += "wlan1";
309 expected.m_data = enum_("wlan1");
310 }
311 }
312 SECTION("2. leafref to enum")
313 {
314 SECTION("eth0")
315 {
316 input += "eth0";
317 expected.m_data = enum_("eth0");
318 }
319 SECTION("eth1")
320 {
321 input += "eth1";
322 expected.m_data = enum_("eth1");
323 }
324 SECTION("eth2")
325 {
326 input += "eth2";
327 expected.m_data = enum_("eth2");
328 }
329 }
330 SECTION("3. leafref to leafref")
331 {
332 SECTION("utf1")
333 {
334 input += "utf1";
335 expected.m_data = enum_("utf1");
336 }
337 SECTION("utf2")
338 {
339 input += "utf2";
340 expected.m_data = enum_("utf2");
341 }
342 SECTION("utf3")
343 {
344 input += "utf3";
345 expected.m_data = enum_("utf3");
346 }
347 }
Jan Kundrát379bb572020-05-07 03:23:13 +0200348 SECTION("4. empty")
349 {
350 expected.m_data = empty_{};
351 }
Václav Kubernát2984f442020-02-20 17:43:35 +0100352 }
353 }
354
Václav Kubernátab538992019-03-06 15:30:50 +0100355 SECTION("binary")
356 {
357 SECTION("zero ending '='")
358 {
359 input = "set mod:leafBinary This/IsABase64EncodedSomething++/342431++";
360 expected.m_data = binary_{"This/IsABase64EncodedSomething++/342431++"};
361 }
362
363 SECTION("one ending '='")
364 {
365 input = "set mod:leafBinary This/IsABase64EncodedSomething++/342431++=";
366 expected.m_data = binary_{"This/IsABase64EncodedSomething++/342431++="};
367 }
368
369 SECTION("two ending '='")
370 {
371 input = "set mod:leafBinary This/IsABase64EncodedSomething++/342431++==";
372 expected.m_data = binary_{"This/IsABase64EncodedSomething++/342431++=="};
373 }
Václav Kubernátfaacd022020-07-08 16:44:38 +0200374 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafBinary"));
Václav Kubernátab538992019-03-06 15:30:50 +0100375 }
Václav Kubernáteeb38842019-03-20 19:46:05 +0100376
377 SECTION("identityRef")
378 {
379 SECTION("foodIdentRef")
380 {
381 input = "set mod:foodIdentRef ";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200382 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("foodIdentRef"));
Václav Kubernáteeb38842019-03-20 19:46:05 +0100383
384 SECTION("food")
385 {
386 input += "food";
387 expected.m_data = identityRef_("food");
388 }
389 SECTION("mod:food")
390 {
391 input += "mod:food";
392 expected.m_data = identityRef_("mod", "food");
393 }
394 SECTION("pizza")
395 {
396 input += "pizza";
397 expected.m_data = identityRef_("pizza");
398 }
399 SECTION("mod:pizza")
400 {
401 input += "mod:pizza";
402 expected.m_data = identityRef_("mod", "pizza");
403 }
404 SECTION("pizza-module:hawaii")
405 {
406 input += "pizza-module:hawaii";
407 expected.m_data = identityRef_("pizza-module", "hawaii");
408 }
409 }
410 SECTION("pizzaIdentRef")
411 {
412 input = "set mod:pizzaIdentRef ";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200413 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("pizzaIdentRef"));
Václav Kubernáteeb38842019-03-20 19:46:05 +0100414 SECTION("pizza")
415 {
416 input += "pizza";
417 expected.m_data = identityRef_("pizza");
418 }
419 SECTION("mod:pizza")
420 {
421 input += "mod:pizza";
422 expected.m_data = identityRef_("mod", "pizza");
423 }
424 SECTION("pizza-module:hawaii")
425 {
426 input += "pizza-module:hawaii";
427 expected.m_data = identityRef_("pizza-module", "hawaii");
428 }
429 }
430 SECTION("mod:contA/identInCont")
431 {
432 input = "set mod:contA/identInCont ";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200433 expected.m_path.m_nodes.emplace_back(module_{"mod"}, container_("contA"));
434 expected.m_path.m_nodes.emplace_back(leaf_("identInCont"));
Václav Kubernáteeb38842019-03-20 19:46:05 +0100435 SECTION("pizza")
436 {
437 input += "pizza";
438 expected.m_data = identityRef_("pizza");
439 }
440 SECTION("mod:pizza")
441 {
442 input += "mod:pizza";
443 expected.m_data = identityRef_("mod", "pizza");
444 }
445 SECTION("pizza-module:hawaii")
446 {
447 input += "pizza-module:hawaii";
448 expected.m_data = identityRef_("pizza-module", "hawaii");
449 }
450 }
451 }
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200452 SECTION("leafRef")
453 {
454 SECTION("refToString")
455 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100456 input = "set mod:refToString \"blabal\"";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200457 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("refToString"));
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200458 expected.m_data = std::string("blabal");
459 }
460
461 SECTION("refToInt8")
462 {
463 input = "set mod:refToInt8 42";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200464 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("refToInt8"));
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200465 expected.m_data = int8_t{42};
466 }
Václav Kubernát2d7b05c2020-04-05 14:02:37 +0200467
468 SECTION("refToLeafInCont")
469 {
470 input = "set mod:refToLeafInCont pizza";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200471 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("refToLeafInCont"));
Václav Kubernát2d7b05c2020-04-05 14:02:37 +0200472 expected.m_data = identityRef_{"pizza"};
473 }
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200474 }
Jan Kundrát379bb572020-05-07 03:23:13 +0200475 SECTION("empty")
476 {
477 input = "set mod:dummy ";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200478 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("dummy"));
Jan Kundrát379bb572020-05-07 03:23:13 +0200479 expected.m_data = empty_{};
480 }
Václav Kubernátdab73ca2020-10-26 23:44:43 +0100481
482 SECTION("bits")
483 {
484 input = "set mod:flags ";
485 decltype(bits_::m_bits) bits;
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100486 SECTION("<nothing>")
487 {
Václav Kubernátdab73ca2020-10-26 23:44:43 +0100488 bits = {};
489 }
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100490 SECTION("carry")
491 {
Václav Kubernátdab73ca2020-10-26 23:44:43 +0100492 input += "carry";
493 bits = {"carry"};
494 }
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100495 SECTION("sign")
496 {
Václav Kubernátdab73ca2020-10-26 23:44:43 +0100497 input += "sign";
498 bits = {"sign"};
499 }
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100500 SECTION("carry sign")
501 {
Václav Kubernátdab73ca2020-10-26 23:44:43 +0100502 input += "carry sign";
503 bits = {"carry", "sign"};
504 }
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100505 SECTION("sign carry")
506 {
Václav Kubernátdab73ca2020-10-26 23:44:43 +0100507 input += "sign carry";
508 bits = {"sign", "carry"};
509 }
510 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("flags"));
511 expected.m_data = bits_{bits};
512 }
Václav Kubernát07204242018-06-04 18:12:09 +0200513 }
514
Jan Kundrátbb7aa852023-08-30 11:51:43 +0200515 SECTION("instance-identifier") {
516 SECTION("toplevel")
517 {
518 input = "set mod:iid /mod:leafUint32";
519 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_{"iid"});
520 expected.m_data = instanceIdentifier_{"/mod:leafUint32"};
521 }
522
523 SECTION("deep to toplevel")
524 {
525 input = "set mod:contA/iid /mod:leafUint32";
526 expected.m_path.m_nodes.emplace_back(module_{"mod"}, container_{"contA"});
527 expected.m_path.m_nodes.emplace_back(leaf_{"iid"});
528 expected.m_data = instanceIdentifier_{"/mod:leafUint32"};
529 }
530
531 SECTION("deep to deep unprefixed")
532 {
533 input = "set mod:contA/iid /mod:contA/x";
534 expected.m_path.m_nodes.emplace_back(module_{"mod"}, container_{"contA"});
535 expected.m_path.m_nodes.emplace_back(leaf_{"iid"});
536 expected.m_data = instanceIdentifier_{"/mod:contA/x"};
537 }
538
539 SECTION("deep to deep mod-prefixed")
540 {
541 input = "set mod:contA/iid /mod:contA/mod:x";
542 expected.m_path.m_nodes.emplace_back(module_{"mod"}, container_{"contA"});
543 expected.m_path.m_nodes.emplace_back(leaf_{"iid"});
544 expected.m_data = instanceIdentifier_{"/mod:contA/mod:x"};
545 }
546
547 SECTION("absolute when nested")
548 {
549 cwd.m_nodes.emplace_back(module_{"mod"}, container_{"contA"});
550 input = "set iid /mod:contA/x";
551 expected.m_path.m_nodes.emplace_back(leaf_{"iid"});
552 expected.m_data = instanceIdentifier_{"/mod:contA/x"};
553 }
554 }
555
Jan Kundrát03c77a12023-08-28 14:23:05 +0200556 parser.changeNode(cwd);
Václav Kubernát07204242018-06-04 18:12:09 +0200557 command_ command = parser.parseCommand(input, errorStream);
558 REQUIRE(command.type() == typeid(set_));
559 REQUIRE(boost::get<set_>(command) == expected);
560 }
561
562 SECTION("invalid input")
563 {
Václav Kubernát777704d2020-01-15 18:48:06 +0100564 std::string expectedError;
Jan Kundrát03c77a12023-08-28 14:23:05 +0200565 dataPath_ cwd;
566
Václav Kubernát07204242018-06-04 18:12:09 +0200567 SECTION("missing space between a command and its arguments")
568 {
Václav Kubernát777704d2020-01-15 18:48:06 +0100569 SECTION("setmod:leafString some_data")
Václav Kubernát07204242018-06-04 18:12:09 +0200570 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100571 input = "setmod:leafString 'some_data'";
Václav Kubernát07204242018-06-04 18:12:09 +0200572 }
573 }
574
575 SECTION("missing space between arguments")
576 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100577 SECTION("set mod:leafString'lol'")
Václav Kubernát07204242018-06-04 18:12:09 +0200578 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100579 input = "set mod:leafString'lol'";
Václav Kubernát07204242018-06-04 18:12:09 +0200580 }
581 }
582
583 SECTION("non-leaf identifiers")
584 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100585 SECTION("set mod:nonexistent 'blabla'")
Václav Kubernát07204242018-06-04 18:12:09 +0200586 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100587 input = "set mod:nonexistent 'blabla'";
Václav Kubernát07204242018-06-04 18:12:09 +0200588 }
589
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100590 SECTION("set mod:contA 'abde'")
Václav Kubernát07204242018-06-04 18:12:09 +0200591 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100592 input = "set mod:contA 'abde'";
Václav Kubernát07204242018-06-04 18:12:09 +0200593 }
594 }
595
Václav Kubernátebca2552018-06-08 19:06:02 +0200596 SECTION("wrong types")
597 {
Václav Kubernát777704d2020-01-15 18:48:06 +0100598 expectedError = "leaf data type mismatch";
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100599 SECTION("set mod:leafBool 'blabla'")
Václav Kubernátebca2552018-06-08 19:06:02 +0200600 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100601 input = "set mod:leafBool 'blabla'";
Václav Kubernátebca2552018-06-08 19:06:02 +0200602 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100603 SECTION("set mod:leafUint8 'blabla'")
Václav Kubernátebca2552018-06-08 19:06:02 +0200604 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100605 input = "set mod:leafUint8 'blabla'";
Václav Kubernátebca2552018-06-08 19:06:02 +0200606 }
Václav Kubernát777704d2020-01-15 18:48:06 +0100607 SECTION("set mod:leafUint8 -5")
Václav Kubernátebca2552018-06-08 19:06:02 +0200608 {
Václav Kubernát777704d2020-01-15 18:48:06 +0100609 input = "set mod:leafUint8 -5";
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200610 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100611 SECTION("set mod:leafInt8 'blabla'")
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200612 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100613 input = "set mod:leafInt8 'blabla'";
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200614 }
Václav Kubernát777704d2020-01-15 18:48:06 +0100615 SECTION("set mod:leafInt8 130")
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200616 {
Václav Kubernát777704d2020-01-15 18:48:06 +0100617 input = "set mod:leafInt8 130";
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200618 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100619 SECTION("set mod:leafUint16 'blabla'")
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200620 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100621 input = "set mod:leafUint16 'blabla'";
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200622 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100623 SECTION("set mod:leafInt16 'blabla'")
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200624 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100625 input = "set mod:leafInt16 'blabla'";
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200626 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100627 SECTION("set mod:leafUint32 'blabla'")
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200628 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100629 input = "set mod:leafUint32 'blabla'";
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200630 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100631 SECTION("set mod:leafInt32 'blabla'")
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200632 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100633 input = "set mod:leafInt32 'blabla'";
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200634 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100635 SECTION("set mod:leafUint64 'blabla'")
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200636 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100637 input = "set mod:leafUint64 'blabla'";
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200638 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100639 SECTION("set mod:leafInt64 'blabla'")
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200640 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100641 input = "set mod:leafInt64 'blabla'";
Václav Kubernátebca2552018-06-08 19:06:02 +0200642 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100643 SECTION("set mod:leafEnum 'blabla'")
Václav Kubernátebca2552018-06-08 19:06:02 +0200644 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100645 input = "set mod:leafEnum 'blabla'";
Václav Kubernátebca2552018-06-08 19:06:02 +0200646 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100647 SECTION("set mod:refToInt8 'blabla'")
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200648 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100649 input = "set mod:refToInt8 'blabla'";
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200650 }
Václav Kubernátebca2552018-06-08 19:06:02 +0200651 }
652
Václav Kubernátab538992019-03-06 15:30:50 +0100653 SECTION("wrong base64 strings")
654 {
655 SECTION("invalid character")
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100656 {
Václav Kubernát777704d2020-01-15 18:48:06 +0100657 input = "set mod:leafBinary dbahj-";
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100658 }
Václav Kubernátab538992019-03-06 15:30:50 +0100659 SECTION("equal sign in the middle")
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100660 {
Václav Kubernát777704d2020-01-15 18:48:06 +0100661 input = "set mod:leafBinary db=ahj";
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100662 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100663 SECTION("enclosing in quotes")
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100664 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100665 input = "set mod:leafBinary 'dbahj'";
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100666 }
Václav Kubernátab538992019-03-06 15:30:50 +0100667 }
668
Václav Kubernáteeb38842019-03-20 19:46:05 +0100669 SECTION("non-existing identity")
670 {
671 input = "set mod:foodIdentRef identityBLABLA";
672 }
673
Václav Kubernátf365ecf2020-06-09 16:22:43 +0200674 SECTION("identity with non-existing module")
675 {
676 expectedError = "Invalid module name";
677 input = "set mod:foodIdentRef xd:haha";
678 }
679
Václav Kubernáteeb38842019-03-20 19:46:05 +0100680 SECTION("setting identities with wrong bases")
681 {
682 SECTION("set mod:foodIdentRef mod:vehicle")
683 {
684 input = "set mod:foodIdentRef mod:vehicle";
685 }
686 SECTION("set mod:pizzaIdentRef mod:food")
687 {
688 input = "set mod:pizzaIdentRef mod:food";
689 }
690 }
691 SECTION("setting different module identities without prefix")
692 {
693 input = "set mod:pizzaIdentRef hawaii";
694 }
695 SECTION("identity prefix without name")
696 {
697 input = "set mod:contA/identInCont pizza-module:";
698 }
699
Václav Kubernát2984f442020-02-20 17:43:35 +0100700 SECTION("set a union path to a wrong type")
701 {
702 input = "set mod:intOrString true";
703 }
704
Jan Kundrát379bb572020-05-07 03:23:13 +0200705 SECTION("no space for empty data")
706 {
707 input = "set mod:dummy";
708 }
709
Václav Kubernát0d42c512020-05-20 21:18:39 +0200710 SECTION("empty path")
711 {
712 input = "set ";
713 }
714
Václav Kubernátabf52802020-05-19 01:31:17 +0200715 SECTION("setting readonly data")
716 {
717 input = "set mod:readonly 123";
718 }
719
Václav Kubernátdab73ca2020-10-26 23:44:43 +0100720 SECTION("nonexistent bits")
721 {
722 input = "set mod:flags daw";
723 }
724
725 SECTION("same bit more than once")
726 {
727 input = "set mod:flags carry carry";
728 }
729
Jan Kundrátbb7aa852023-08-30 11:51:43 +0200730 SECTION("instance-identifier non-existing node")
731 {
732 input = "set mod:iid /mod:404";
733 }
734
735 SECTION("instance-identifier non-existing node without leading slash")
736 {
737 input = "set mod:iid mod:404";
738 }
739
740 SECTION("instance-identifier node without leading slash")
741 {
742 input = "set mod:iid mod:leafUint32";
743 }
744
745 SECTION("instance-identifier to pseudo-relative unprefixed")
746 {
747 cwd.m_nodes.emplace_back(module_{"mod"}, container_{"contA"});
748 input = "set iid x";
749 }
750
751 SECTION("instance-identifier to pseudo-relative prefixed")
752 {
753 cwd.m_nodes.emplace_back(module_{"mod"}, container_{"contA"});
754 input = "set iid mod:x";
755 }
756
757 SECTION("instance-identifier without leading slash when nested")
758 {
759 cwd.m_nodes.emplace_back(module_{"mod"}, container_{"contA"});
760 input = "set iid mod:contA/x";
761 }
762
Jan Kundrát03c77a12023-08-28 14:23:05 +0200763 parser.changeNode(cwd);
Jan Kundrátc381e632019-03-14 13:39:11 +0100764 REQUIRE_THROWS_AS(parser.parseCommand(input, errorStream), InvalidCommandException);
Václav Kubernát777704d2020-01-15 18:48:06 +0100765 REQUIRE(errorStream.str().find(expectedError) != std::string::npos);
Václav Kubernát07204242018-06-04 18:12:09 +0200766 }
Jan Kundrátb7206ad2020-06-18 21:08:14 +0200767
768 SECTION("deleting a leaf")
769 {
770 delete_ expected;
771 input = "delete mod:leafString";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200772 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafString"));
Jan Kundrátb7206ad2020-06-18 21:08:14 +0200773
774 command_ command = parser.parseCommand(input, errorStream);
775 REQUIRE(command.type() == typeid(delete_));
776 REQUIRE(boost::get<delete_>(command) == expected);
777 }
Václav Kubernát07204242018-06-04 18:12:09 +0200778}