blob: 40ce08de4c7c88a706983371f71af58d702f9a5c [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"}});
75
Václav Kubernát07204242018-06-04 18:12:09 +020076 Parser parser(schema);
77 std::string input;
78 std::ostringstream errorStream;
79
80 SECTION("valid input")
81 {
82 set_ expected;
83
Václav Kubernát1bbac7c2020-01-15 17:45:41 +010084 SECTION("set mod:leafString \"some_data\"")
Václav Kubernát07204242018-06-04 18:12:09 +020085 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +010086 input = "set mod:leafString \'some_data\'";
Václav Kubernátfaacd022020-07-08 16:44:38 +020087 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafString"));
Václav Kubernátebca2552018-06-08 19:06:02 +020088 expected.m_data = std::string("some_data");
Václav Kubernát07204242018-06-04 18:12:09 +020089 }
90
Václav Kubernát1bbac7c2020-01-15 17:45:41 +010091 SECTION("set mod:contA/leafInCont 'more_data'")
Václav Kubernát07204242018-06-04 18:12:09 +020092 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +010093 input = "set mod:contA/leafInCont 'more_data'";
Václav Kubernátfaacd022020-07-08 16:44:38 +020094 expected.m_path.m_nodes.emplace_back(module_{"mod"}, container_("contA"));
95 expected.m_path.m_nodes.emplace_back(leaf_("leafInCont"));
Václav Kubernátebca2552018-06-08 19:06:02 +020096 expected.m_data = std::string("more_data");
97 }
98
Václav Kubernát1bbac7c2020-01-15 17:45:41 +010099 SECTION("set mod:contA/leafInCont \"data with' a quote\"")
Václav Kubernátc18ffac2020-01-10 16:06:06 +0100100 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100101 input = "set mod:contA/leafInCont \"data with' a quote\"";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200102 expected.m_path.m_nodes.emplace_back(module_{"mod"}, container_("contA"));
103 expected.m_path.m_nodes.emplace_back(leaf_("leafInCont"));
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100104 expected.m_data = std::string("data with' a quote");
105 }
106
107 SECTION("set mod:contA/leafInCont 'data with\" a quote'")
108 {
109 input = "set mod:contA/leafInCont 'data with\" a quote'";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200110 expected.m_path.m_nodes.emplace_back(module_{"mod"}, container_("contA"));
111 expected.m_path.m_nodes.emplace_back(leaf_("leafInCont"));
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100112 expected.m_data = std::string("data with\" a quote");
113 }
114
115 SECTION("set mod:contA/leafInCont 'more d\tata'") // spaces in string
116 {
117 input = "set mod:contA/leafInCont 'more d\tata'";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200118 expected.m_path.m_nodes.emplace_back(module_{"mod"}, container_("contA"));
119 expected.m_path.m_nodes.emplace_back(leaf_("leafInCont"));
Václav Kubernátc18ffac2020-01-10 16:06:06 +0100120 expected.m_data = std::string("more d\tata");
121 }
122
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100123 SECTION("set mod:list[number=1]/leafInList \"another_data\"")
Václav Kubernátebca2552018-06-08 19:06:02 +0200124 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100125 input = "set mod:list[number=1]/leafInList \"another_data\"";
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100126 auto keys = ListInstance{
Václav Kubernát7707cae2020-01-16 12:04:53 +0100127 {"number", int32_t{1}}};
Václav Kubernátfaacd022020-07-08 16:44:38 +0200128 expected.m_path.m_nodes.emplace_back(module_{"mod"}, listElement_("list", keys));
129 expected.m_path.m_nodes.emplace_back(leaf_("leafInList"));
Václav Kubernátebca2552018-06-08 19:06:02 +0200130 expected.m_data = std::string("another_data");
131 }
132
133 SECTION("data types")
134 {
135 SECTION("string")
136 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100137 input = "set mod:leafString \"somedata\"";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200138 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafString"));
Václav Kubernátebca2552018-06-08 19:06:02 +0200139 expected.m_data = std::string("somedata");
140 }
141
Václav Kubernátb8bad062021-02-04 00:02:45 +0100142 SECTION("more spaces before value")
143 {
144 input = "set mod:leafString \"somedata\"";
145 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafString"));
146 expected.m_data = std::string("somedata");
147 }
148
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200149 SECTION("int8")
Václav Kubernátebca2552018-06-08 19:06:02 +0200150 {
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200151 input = "set mod:leafInt8 2";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200152 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafInt8"));
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200153 expected.m_data = int8_t{2};
154 }
155
156 SECTION("negative int8")
157 {
158 input = "set mod:leafInt8 -10";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200159 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafInt8"));
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200160 expected.m_data = int8_t{-10};
161 }
162
163 SECTION("uint8")
164 {
165 input = "set mod:leafUint8 2";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200166 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafUint8"));
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200167 expected.m_data = uint8_t{2};
168 }
169
170 SECTION("int16")
171 {
172 input = "set mod:leafInt16 30000";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200173 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafInt16"));
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200174 expected.m_data = int16_t{30'000};
175 }
176
177 SECTION("uint16")
178 {
179 input = "set mod:leafUint16 30000";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200180 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafUint16"));
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200181 expected.m_data = uint16_t{30'000};
182 }
183
184 SECTION("int32")
185 {
186 input = "set mod:leafInt32 30000";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200187 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafInt32"));
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200188 expected.m_data = int32_t{30'000};
189 }
190
191 SECTION("uint32")
192 {
193 input = "set mod:leafUint32 30000";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200194 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafUint32"));
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200195 expected.m_data = uint32_t{30'000};
196 }
197
198 SECTION("int32")
199 {
200 input = "set mod:leafInt32 30000";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200201 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafInt32"));
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200202 expected.m_data = int32_t{30'000};
203 }
204
205 SECTION("uint64")
206 {
207 input = "set mod:leafUint64 30000";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200208 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafUint64"));
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200209 expected.m_data = uint64_t{30'000};
Václav Kubernátebca2552018-06-08 19:06:02 +0200210 }
211
212 SECTION("decimal")
213 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200214 input = "set mod:leafDecimal 3.14159";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200215 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafDecimal"));
Václav Kubernátebca2552018-06-08 19:06:02 +0200216 expected.m_data = 3.14159;
217 }
218
219 SECTION("enum")
220 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200221 input = "set mod:leafEnum coze";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200222 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafEnum"));
Václav Kubernátebca2552018-06-08 19:06:02 +0200223 expected.m_data = enum_("coze");
224 }
225
226 SECTION("bool")
227 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200228 input = "set mod:leafBool true";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200229 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafBool"));
Václav Kubernátebca2552018-06-08 19:06:02 +0200230 expected.m_data = true;
231 }
Václav Kubernátab538992019-03-06 15:30:50 +0100232
Václav Kubernát2984f442020-02-20 17:43:35 +0100233 SECTION("union")
234 {
235 SECTION("int")
236 {
Václav Kubernátfaacd022020-07-08 16:44:38 +0200237 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("intOrString"));
Václav Kubernát2984f442020-02-20 17:43:35 +0100238 input = "set mod:intOrString 90";
239 expected.m_data = int32_t{90};
240 }
241 SECTION("string")
242 {
Václav Kubernátfaacd022020-07-08 16:44:38 +0200243 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("intOrString"));
Václav Kubernát2984f442020-02-20 17:43:35 +0100244 input = "set mod:intOrString \"test\"";
245 expected.m_data = std::string{"test"};
246 }
247
248 SECTION("union with two integral types")
249 {
Václav Kubernátfaacd022020-07-08 16:44:38 +0200250 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("twoInts"));
Václav Kubernát2984f442020-02-20 17:43:35 +0100251 SECTION("uint8")
252 {
253 input = "set mod:twoInts 100";
254 expected.m_data = uint8_t{100};
255 }
256 SECTION("int16")
257 {
258 input = "set mod:twoInts 6666";
259 expected.m_data = int16_t{6666};
260 }
261 }
262
263 SECTION("union with enum and leafref to enum")
264 {
Václav Kubernátfaacd022020-07-08 16:44:38 +0200265 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("unionStringEnumLeafref"));
Václav Kubernát2984f442020-02-20 17:43:35 +0100266 SECTION("string")
267 {
268 input = "set mod:unionStringEnumLeafref \"AHOJ\"";
269 expected.m_data = std::string{"AHOJ"};
270 }
271 SECTION("enum")
272 {
273 input = "set mod:unionStringEnumLeafref bar";
274 expected.m_data = enum_("bar");
275 }
276 SECTION("enum leafref")
277 {
278 input = "set mod:unionStringEnumLeafref coze";
279 expected.m_data = enum_("coze");
280 }
281 }
282
283 SECTION("activePort")
284 {
Václav Kubernátfaacd022020-07-08 16:44:38 +0200285 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("activePort"));
Václav Kubernát2984f442020-02-20 17:43:35 +0100286 input = "set mod:activePort ";
287 SECTION("1. anonymous enum")
288 {
289 SECTION("wlan0")
290 {
291 input += "wlan0";
292 expected.m_data = enum_("wlan0");
293 }
294 SECTION("wlan1")
295 {
296 input += "wlan1";
297 expected.m_data = enum_("wlan1");
298 }
299 }
300 SECTION("2. leafref to enum")
301 {
302 SECTION("eth0")
303 {
304 input += "eth0";
305 expected.m_data = enum_("eth0");
306 }
307 SECTION("eth1")
308 {
309 input += "eth1";
310 expected.m_data = enum_("eth1");
311 }
312 SECTION("eth2")
313 {
314 input += "eth2";
315 expected.m_data = enum_("eth2");
316 }
317 }
318 SECTION("3. leafref to leafref")
319 {
320 SECTION("utf1")
321 {
322 input += "utf1";
323 expected.m_data = enum_("utf1");
324 }
325 SECTION("utf2")
326 {
327 input += "utf2";
328 expected.m_data = enum_("utf2");
329 }
330 SECTION("utf3")
331 {
332 input += "utf3";
333 expected.m_data = enum_("utf3");
334 }
335 }
Jan Kundrát379bb572020-05-07 03:23:13 +0200336 SECTION("4. empty")
337 {
338 expected.m_data = empty_{};
339 }
Václav Kubernát2984f442020-02-20 17:43:35 +0100340 }
341 }
342
Václav Kubernátab538992019-03-06 15:30:50 +0100343 SECTION("binary")
344 {
345 SECTION("zero ending '='")
346 {
347 input = "set mod:leafBinary This/IsABase64EncodedSomething++/342431++";
348 expected.m_data = binary_{"This/IsABase64EncodedSomething++/342431++"};
349 }
350
351 SECTION("one ending '='")
352 {
353 input = "set mod:leafBinary This/IsABase64EncodedSomething++/342431++=";
354 expected.m_data = binary_{"This/IsABase64EncodedSomething++/342431++="};
355 }
356
357 SECTION("two ending '='")
358 {
359 input = "set mod:leafBinary This/IsABase64EncodedSomething++/342431++==";
360 expected.m_data = binary_{"This/IsABase64EncodedSomething++/342431++=="};
361 }
Václav Kubernátfaacd022020-07-08 16:44:38 +0200362 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafBinary"));
Václav Kubernátab538992019-03-06 15:30:50 +0100363 }
Václav Kubernáteeb38842019-03-20 19:46:05 +0100364
365 SECTION("identityRef")
366 {
367 SECTION("foodIdentRef")
368 {
369 input = "set mod:foodIdentRef ";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200370 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("foodIdentRef"));
Václav Kubernáteeb38842019-03-20 19:46:05 +0100371
372 SECTION("food")
373 {
374 input += "food";
375 expected.m_data = identityRef_("food");
376 }
377 SECTION("mod:food")
378 {
379 input += "mod:food";
380 expected.m_data = identityRef_("mod", "food");
381 }
382 SECTION("pizza")
383 {
384 input += "pizza";
385 expected.m_data = identityRef_("pizza");
386 }
387 SECTION("mod:pizza")
388 {
389 input += "mod:pizza";
390 expected.m_data = identityRef_("mod", "pizza");
391 }
392 SECTION("pizza-module:hawaii")
393 {
394 input += "pizza-module:hawaii";
395 expected.m_data = identityRef_("pizza-module", "hawaii");
396 }
397 }
398 SECTION("pizzaIdentRef")
399 {
400 input = "set mod:pizzaIdentRef ";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200401 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("pizzaIdentRef"));
Václav Kubernáteeb38842019-03-20 19:46:05 +0100402 SECTION("pizza")
403 {
404 input += "pizza";
405 expected.m_data = identityRef_("pizza");
406 }
407 SECTION("mod:pizza")
408 {
409 input += "mod:pizza";
410 expected.m_data = identityRef_("mod", "pizza");
411 }
412 SECTION("pizza-module:hawaii")
413 {
414 input += "pizza-module:hawaii";
415 expected.m_data = identityRef_("pizza-module", "hawaii");
416 }
417 }
418 SECTION("mod:contA/identInCont")
419 {
420 input = "set mod:contA/identInCont ";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200421 expected.m_path.m_nodes.emplace_back(module_{"mod"}, container_("contA"));
422 expected.m_path.m_nodes.emplace_back(leaf_("identInCont"));
Václav Kubernáteeb38842019-03-20 19:46:05 +0100423 SECTION("pizza")
424 {
425 input += "pizza";
426 expected.m_data = identityRef_("pizza");
427 }
428 SECTION("mod:pizza")
429 {
430 input += "mod:pizza";
431 expected.m_data = identityRef_("mod", "pizza");
432 }
433 SECTION("pizza-module:hawaii")
434 {
435 input += "pizza-module:hawaii";
436 expected.m_data = identityRef_("pizza-module", "hawaii");
437 }
438 }
439 }
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200440 SECTION("leafRef")
441 {
442 SECTION("refToString")
443 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100444 input = "set mod:refToString \"blabal\"";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200445 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("refToString"));
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200446 expected.m_data = std::string("blabal");
447 }
448
449 SECTION("refToInt8")
450 {
451 input = "set mod:refToInt8 42";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200452 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("refToInt8"));
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200453 expected.m_data = int8_t{42};
454 }
Václav Kubernát2d7b05c2020-04-05 14:02:37 +0200455
456 SECTION("refToLeafInCont")
457 {
458 input = "set mod:refToLeafInCont pizza";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200459 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("refToLeafInCont"));
Václav Kubernát2d7b05c2020-04-05 14:02:37 +0200460 expected.m_data = identityRef_{"pizza"};
461 }
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200462 }
Jan Kundrát379bb572020-05-07 03:23:13 +0200463 SECTION("empty")
464 {
465 input = "set mod:dummy ";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200466 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("dummy"));
Jan Kundrát379bb572020-05-07 03:23:13 +0200467 expected.m_data = empty_{};
468 }
Václav Kubernátdab73ca2020-10-26 23:44:43 +0100469
470 SECTION("bits")
471 {
472 input = "set mod:flags ";
473 decltype(bits_::m_bits) bits;
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100474 SECTION("<nothing>")
475 {
Václav Kubernátdab73ca2020-10-26 23:44:43 +0100476 bits = {};
477 }
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100478 SECTION("carry")
479 {
Václav Kubernátdab73ca2020-10-26 23:44:43 +0100480 input += "carry";
481 bits = {"carry"};
482 }
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100483 SECTION("sign")
484 {
Václav Kubernátdab73ca2020-10-26 23:44:43 +0100485 input += "sign";
486 bits = {"sign"};
487 }
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100488 SECTION("carry sign")
489 {
Václav Kubernátdab73ca2020-10-26 23:44:43 +0100490 input += "carry sign";
491 bits = {"carry", "sign"};
492 }
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100493 SECTION("sign carry")
494 {
Václav Kubernátdab73ca2020-10-26 23:44:43 +0100495 input += "sign carry";
496 bits = {"sign", "carry"};
497 }
498 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("flags"));
499 expected.m_data = bits_{bits};
500 }
Václav Kubernát07204242018-06-04 18:12:09 +0200501 }
502
503 command_ command = parser.parseCommand(input, errorStream);
504 REQUIRE(command.type() == typeid(set_));
505 REQUIRE(boost::get<set_>(command) == expected);
506 }
507
508 SECTION("invalid input")
509 {
Václav Kubernát777704d2020-01-15 18:48:06 +0100510 std::string expectedError;
Václav Kubernát07204242018-06-04 18:12:09 +0200511 SECTION("missing space between a command and its arguments")
512 {
Václav Kubernát777704d2020-01-15 18:48:06 +0100513 SECTION("setmod:leafString some_data")
Václav Kubernát07204242018-06-04 18:12:09 +0200514 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100515 input = "setmod:leafString 'some_data'";
Václav Kubernát07204242018-06-04 18:12:09 +0200516 }
517 }
518
519 SECTION("missing space between arguments")
520 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100521 SECTION("set mod:leafString'lol'")
Václav Kubernát07204242018-06-04 18:12:09 +0200522 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100523 input = "set mod:leafString'lol'";
Václav Kubernát07204242018-06-04 18:12:09 +0200524 }
525 }
526
527 SECTION("non-leaf identifiers")
528 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100529 SECTION("set mod:nonexistent 'blabla'")
Václav Kubernát07204242018-06-04 18:12:09 +0200530 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100531 input = "set mod:nonexistent 'blabla'";
Václav Kubernát07204242018-06-04 18:12:09 +0200532 }
533
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100534 SECTION("set mod:contA 'abde'")
Václav Kubernát07204242018-06-04 18:12:09 +0200535 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100536 input = "set mod:contA 'abde'";
Václav Kubernát07204242018-06-04 18:12:09 +0200537 }
538 }
539
Václav Kubernátebca2552018-06-08 19:06:02 +0200540 SECTION("wrong types")
541 {
Václav Kubernát777704d2020-01-15 18:48:06 +0100542 expectedError = "leaf data type mismatch";
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100543 SECTION("set mod:leafBool 'blabla'")
Václav Kubernátebca2552018-06-08 19:06:02 +0200544 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100545 input = "set mod:leafBool 'blabla'";
Václav Kubernátebca2552018-06-08 19:06:02 +0200546 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100547 SECTION("set mod:leafUint8 'blabla'")
Václav Kubernátebca2552018-06-08 19:06:02 +0200548 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100549 input = "set mod:leafUint8 'blabla'";
Václav Kubernátebca2552018-06-08 19:06:02 +0200550 }
Václav Kubernát777704d2020-01-15 18:48:06 +0100551 SECTION("set mod:leafUint8 -5")
Václav Kubernátebca2552018-06-08 19:06:02 +0200552 {
Václav Kubernát777704d2020-01-15 18:48:06 +0100553 input = "set mod:leafUint8 -5";
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200554 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100555 SECTION("set mod:leafInt8 'blabla'")
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200556 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100557 input = "set mod:leafInt8 'blabla'";
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200558 }
Václav Kubernát777704d2020-01-15 18:48:06 +0100559 SECTION("set mod:leafInt8 130")
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200560 {
Václav Kubernát777704d2020-01-15 18:48:06 +0100561 input = "set mod:leafInt8 130";
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200562 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100563 SECTION("set mod:leafUint16 'blabla'")
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200564 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100565 input = "set mod:leafUint16 'blabla'";
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200566 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100567 SECTION("set mod:leafInt16 'blabla'")
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200568 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100569 input = "set mod:leafInt16 'blabla'";
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200570 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100571 SECTION("set mod:leafUint32 'blabla'")
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200572 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100573 input = "set mod:leafUint32 'blabla'";
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200574 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100575 SECTION("set mod:leafInt32 'blabla'")
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200576 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100577 input = "set mod:leafInt32 'blabla'";
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200578 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100579 SECTION("set mod:leafUint64 'blabla'")
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200580 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100581 input = "set mod:leafUint64 'blabla'";
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200582 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100583 SECTION("set mod:leafInt64 'blabla'")
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200584 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100585 input = "set mod:leafInt64 'blabla'";
Václav Kubernátebca2552018-06-08 19:06:02 +0200586 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100587 SECTION("set mod:leafEnum 'blabla'")
Václav Kubernátebca2552018-06-08 19:06:02 +0200588 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100589 input = "set mod:leafEnum 'blabla'";
Václav Kubernátebca2552018-06-08 19:06:02 +0200590 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100591 SECTION("set mod:refToInt8 'blabla'")
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200592 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100593 input = "set mod:refToInt8 'blabla'";
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200594 }
Václav Kubernátebca2552018-06-08 19:06:02 +0200595 }
596
Václav Kubernátab538992019-03-06 15:30:50 +0100597 SECTION("wrong base64 strings")
598 {
599 SECTION("invalid character")
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100600 {
Václav Kubernát777704d2020-01-15 18:48:06 +0100601 input = "set mod:leafBinary dbahj-";
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100602 }
Václav Kubernátab538992019-03-06 15:30:50 +0100603 SECTION("equal sign in the middle")
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100604 {
Václav Kubernát777704d2020-01-15 18:48:06 +0100605 input = "set mod:leafBinary db=ahj";
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100606 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100607 SECTION("enclosing in quotes")
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100608 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100609 input = "set mod:leafBinary 'dbahj'";
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100610 }
Václav Kubernátab538992019-03-06 15:30:50 +0100611 }
612
Václav Kubernáteeb38842019-03-20 19:46:05 +0100613 SECTION("non-existing identity")
614 {
615 input = "set mod:foodIdentRef identityBLABLA";
616 }
617
Václav Kubernátf365ecf2020-06-09 16:22:43 +0200618 SECTION("identity with non-existing module")
619 {
620 expectedError = "Invalid module name";
621 input = "set mod:foodIdentRef xd:haha";
622 }
623
Václav Kubernáteeb38842019-03-20 19:46:05 +0100624 SECTION("setting identities with wrong bases")
625 {
626 SECTION("set mod:foodIdentRef mod:vehicle")
627 {
628 input = "set mod:foodIdentRef mod:vehicle";
629 }
630 SECTION("set mod:pizzaIdentRef mod:food")
631 {
632 input = "set mod:pizzaIdentRef mod:food";
633 }
634 }
635 SECTION("setting different module identities without prefix")
636 {
637 input = "set mod:pizzaIdentRef hawaii";
638 }
639 SECTION("identity prefix without name")
640 {
641 input = "set mod:contA/identInCont pizza-module:";
642 }
643
Václav Kubernát2984f442020-02-20 17:43:35 +0100644 SECTION("set a union path to a wrong type")
645 {
646 input = "set mod:intOrString true";
647 }
648
Jan Kundrát379bb572020-05-07 03:23:13 +0200649 SECTION("no space for empty data")
650 {
651 input = "set mod:dummy";
652 }
653
Václav Kubernát0d42c512020-05-20 21:18:39 +0200654 SECTION("empty path")
655 {
656 input = "set ";
657 }
658
Václav Kubernátabf52802020-05-19 01:31:17 +0200659 SECTION("setting readonly data")
660 {
661 input = "set mod:readonly 123";
662 }
663
Václav Kubernátdab73ca2020-10-26 23:44:43 +0100664 SECTION("nonexistent bits")
665 {
666 input = "set mod:flags daw";
667 }
668
669 SECTION("same bit more than once")
670 {
671 input = "set mod:flags carry carry";
672 }
673
Jan Kundrátc381e632019-03-14 13:39:11 +0100674 REQUIRE_THROWS_AS(parser.parseCommand(input, errorStream), InvalidCommandException);
Václav Kubernát777704d2020-01-15 18:48:06 +0100675 REQUIRE(errorStream.str().find(expectedError) != std::string::npos);
Václav Kubernát07204242018-06-04 18:12:09 +0200676 }
Jan Kundrátb7206ad2020-06-18 21:08:14 +0200677
678 SECTION("deleting a leaf")
679 {
680 delete_ expected;
681 input = "delete mod:leafString";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200682 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafString"));
Jan Kundrátb7206ad2020-06-18 21:08:14 +0200683
684 command_ command = parser.parseCommand(input, errorStream);
685 REQUIRE(command.type() == typeid(delete_));
686 REQUIRE(boost::get<delete_>(command) == expected);
687 }
Václav Kubernát07204242018-06-04 18:12:09 +0200688}