blob: c1639b267bb7b4fc5f24929e1a7ab440ec847474 [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
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200142 SECTION("int8")
Václav Kubernátebca2552018-06-08 19:06:02 +0200143 {
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200144 input = "set mod:leafInt8 2";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200145 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafInt8"));
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200146 expected.m_data = int8_t{2};
147 }
148
149 SECTION("negative int8")
150 {
151 input = "set mod:leafInt8 -10";
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{-10};
154 }
155
156 SECTION("uint8")
157 {
158 input = "set mod:leafUint8 2";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200159 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafUint8"));
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200160 expected.m_data = uint8_t{2};
161 }
162
163 SECTION("int16")
164 {
165 input = "set mod:leafInt16 30000";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200166 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafInt16"));
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200167 expected.m_data = int16_t{30'000};
168 }
169
170 SECTION("uint16")
171 {
172 input = "set mod:leafUint16 30000";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200173 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafUint16"));
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200174 expected.m_data = uint16_t{30'000};
175 }
176
177 SECTION("int32")
178 {
179 input = "set mod:leafInt32 30000";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200180 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafInt32"));
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200181 expected.m_data = int32_t{30'000};
182 }
183
184 SECTION("uint32")
185 {
186 input = "set mod:leafUint32 30000";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200187 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafUint32"));
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200188 expected.m_data = uint32_t{30'000};
189 }
190
191 SECTION("int32")
192 {
193 input = "set mod:leafInt32 30000";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200194 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafInt32"));
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200195 expected.m_data = int32_t{30'000};
196 }
197
198 SECTION("uint64")
199 {
200 input = "set mod:leafUint64 30000";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200201 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafUint64"));
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200202 expected.m_data = uint64_t{30'000};
Václav Kubernátebca2552018-06-08 19:06:02 +0200203 }
204
205 SECTION("decimal")
206 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200207 input = "set mod:leafDecimal 3.14159";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200208 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafDecimal"));
Václav Kubernátebca2552018-06-08 19:06:02 +0200209 expected.m_data = 3.14159;
210 }
211
212 SECTION("enum")
213 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200214 input = "set mod:leafEnum coze";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200215 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafEnum"));
Václav Kubernátebca2552018-06-08 19:06:02 +0200216 expected.m_data = enum_("coze");
217 }
218
219 SECTION("bool")
220 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200221 input = "set mod:leafBool true";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200222 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafBool"));
Václav Kubernátebca2552018-06-08 19:06:02 +0200223 expected.m_data = true;
224 }
Václav Kubernátab538992019-03-06 15:30:50 +0100225
Václav Kubernát2984f442020-02-20 17:43:35 +0100226 SECTION("union")
227 {
228 SECTION("int")
229 {
Václav Kubernátfaacd022020-07-08 16:44:38 +0200230 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("intOrString"));
Václav Kubernát2984f442020-02-20 17:43:35 +0100231 input = "set mod:intOrString 90";
232 expected.m_data = int32_t{90};
233 }
234 SECTION("string")
235 {
Václav Kubernátfaacd022020-07-08 16:44:38 +0200236 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("intOrString"));
Václav Kubernát2984f442020-02-20 17:43:35 +0100237 input = "set mod:intOrString \"test\"";
238 expected.m_data = std::string{"test"};
239 }
240
241 SECTION("union with two integral types")
242 {
Václav Kubernátfaacd022020-07-08 16:44:38 +0200243 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("twoInts"));
Václav Kubernát2984f442020-02-20 17:43:35 +0100244 SECTION("uint8")
245 {
246 input = "set mod:twoInts 100";
247 expected.m_data = uint8_t{100};
248 }
249 SECTION("int16")
250 {
251 input = "set mod:twoInts 6666";
252 expected.m_data = int16_t{6666};
253 }
254 }
255
256 SECTION("union with enum and leafref to enum")
257 {
Václav Kubernátfaacd022020-07-08 16:44:38 +0200258 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("unionStringEnumLeafref"));
Václav Kubernát2984f442020-02-20 17:43:35 +0100259 SECTION("string")
260 {
261 input = "set mod:unionStringEnumLeafref \"AHOJ\"";
262 expected.m_data = std::string{"AHOJ"};
263 }
264 SECTION("enum")
265 {
266 input = "set mod:unionStringEnumLeafref bar";
267 expected.m_data = enum_("bar");
268 }
269 SECTION("enum leafref")
270 {
271 input = "set mod:unionStringEnumLeafref coze";
272 expected.m_data = enum_("coze");
273 }
274 }
275
276 SECTION("activePort")
277 {
Václav Kubernátfaacd022020-07-08 16:44:38 +0200278 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("activePort"));
Václav Kubernát2984f442020-02-20 17:43:35 +0100279 input = "set mod:activePort ";
280 SECTION("1. anonymous enum")
281 {
282 SECTION("wlan0")
283 {
284 input += "wlan0";
285 expected.m_data = enum_("wlan0");
286 }
287 SECTION("wlan1")
288 {
289 input += "wlan1";
290 expected.m_data = enum_("wlan1");
291 }
292 }
293 SECTION("2. leafref to enum")
294 {
295 SECTION("eth0")
296 {
297 input += "eth0";
298 expected.m_data = enum_("eth0");
299 }
300 SECTION("eth1")
301 {
302 input += "eth1";
303 expected.m_data = enum_("eth1");
304 }
305 SECTION("eth2")
306 {
307 input += "eth2";
308 expected.m_data = enum_("eth2");
309 }
310 }
311 SECTION("3. leafref to leafref")
312 {
313 SECTION("utf1")
314 {
315 input += "utf1";
316 expected.m_data = enum_("utf1");
317 }
318 SECTION("utf2")
319 {
320 input += "utf2";
321 expected.m_data = enum_("utf2");
322 }
323 SECTION("utf3")
324 {
325 input += "utf3";
326 expected.m_data = enum_("utf3");
327 }
328 }
Jan Kundrát379bb572020-05-07 03:23:13 +0200329 SECTION("4. empty")
330 {
331 expected.m_data = empty_{};
332 }
Václav Kubernát2984f442020-02-20 17:43:35 +0100333 }
334 }
335
Václav Kubernátab538992019-03-06 15:30:50 +0100336 SECTION("binary")
337 {
338 SECTION("zero ending '='")
339 {
340 input = "set mod:leafBinary This/IsABase64EncodedSomething++/342431++";
341 expected.m_data = binary_{"This/IsABase64EncodedSomething++/342431++"};
342 }
343
344 SECTION("one ending '='")
345 {
346 input = "set mod:leafBinary This/IsABase64EncodedSomething++/342431++=";
347 expected.m_data = binary_{"This/IsABase64EncodedSomething++/342431++="};
348 }
349
350 SECTION("two ending '='")
351 {
352 input = "set mod:leafBinary This/IsABase64EncodedSomething++/342431++==";
353 expected.m_data = binary_{"This/IsABase64EncodedSomething++/342431++=="};
354 }
Václav Kubernátfaacd022020-07-08 16:44:38 +0200355 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafBinary"));
Václav Kubernátab538992019-03-06 15:30:50 +0100356 }
Václav Kubernáteeb38842019-03-20 19:46:05 +0100357
358 SECTION("identityRef")
359 {
360 SECTION("foodIdentRef")
361 {
362 input = "set mod:foodIdentRef ";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200363 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("foodIdentRef"));
Václav Kubernáteeb38842019-03-20 19:46:05 +0100364
365 SECTION("food")
366 {
367 input += "food";
368 expected.m_data = identityRef_("food");
369 }
370 SECTION("mod:food")
371 {
372 input += "mod:food";
373 expected.m_data = identityRef_("mod", "food");
374 }
375 SECTION("pizza")
376 {
377 input += "pizza";
378 expected.m_data = identityRef_("pizza");
379 }
380 SECTION("mod:pizza")
381 {
382 input += "mod:pizza";
383 expected.m_data = identityRef_("mod", "pizza");
384 }
385 SECTION("pizza-module:hawaii")
386 {
387 input += "pizza-module:hawaii";
388 expected.m_data = identityRef_("pizza-module", "hawaii");
389 }
390 }
391 SECTION("pizzaIdentRef")
392 {
393 input = "set mod:pizzaIdentRef ";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200394 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("pizzaIdentRef"));
Václav Kubernáteeb38842019-03-20 19:46:05 +0100395 SECTION("pizza")
396 {
397 input += "pizza";
398 expected.m_data = identityRef_("pizza");
399 }
400 SECTION("mod:pizza")
401 {
402 input += "mod:pizza";
403 expected.m_data = identityRef_("mod", "pizza");
404 }
405 SECTION("pizza-module:hawaii")
406 {
407 input += "pizza-module:hawaii";
408 expected.m_data = identityRef_("pizza-module", "hawaii");
409 }
410 }
411 SECTION("mod:contA/identInCont")
412 {
413 input = "set mod:contA/identInCont ";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200414 expected.m_path.m_nodes.emplace_back(module_{"mod"}, container_("contA"));
415 expected.m_path.m_nodes.emplace_back(leaf_("identInCont"));
Václav Kubernáteeb38842019-03-20 19:46:05 +0100416 SECTION("pizza")
417 {
418 input += "pizza";
419 expected.m_data = identityRef_("pizza");
420 }
421 SECTION("mod:pizza")
422 {
423 input += "mod:pizza";
424 expected.m_data = identityRef_("mod", "pizza");
425 }
426 SECTION("pizza-module:hawaii")
427 {
428 input += "pizza-module:hawaii";
429 expected.m_data = identityRef_("pizza-module", "hawaii");
430 }
431 }
432 }
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200433 SECTION("leafRef")
434 {
435 SECTION("refToString")
436 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100437 input = "set mod:refToString \"blabal\"";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200438 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("refToString"));
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200439 expected.m_data = std::string("blabal");
440 }
441
442 SECTION("refToInt8")
443 {
444 input = "set mod:refToInt8 42";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200445 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("refToInt8"));
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200446 expected.m_data = int8_t{42};
447 }
Václav Kubernát2d7b05c2020-04-05 14:02:37 +0200448
449 SECTION("refToLeafInCont")
450 {
451 input = "set mod:refToLeafInCont pizza";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200452 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("refToLeafInCont"));
Václav Kubernát2d7b05c2020-04-05 14:02:37 +0200453 expected.m_data = identityRef_{"pizza"};
454 }
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200455 }
Jan Kundrát379bb572020-05-07 03:23:13 +0200456 SECTION("empty")
457 {
458 input = "set mod:dummy ";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200459 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("dummy"));
Jan Kundrát379bb572020-05-07 03:23:13 +0200460 expected.m_data = empty_{};
461 }
Václav Kubernátdab73ca2020-10-26 23:44:43 +0100462
463 SECTION("bits")
464 {
465 input = "set mod:flags ";
466 decltype(bits_::m_bits) bits;
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100467 SECTION("<nothing>")
468 {
Václav Kubernátdab73ca2020-10-26 23:44:43 +0100469 bits = {};
470 }
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100471 SECTION("carry")
472 {
Václav Kubernátdab73ca2020-10-26 23:44:43 +0100473 input += "carry";
474 bits = {"carry"};
475 }
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100476 SECTION("sign")
477 {
Václav Kubernátdab73ca2020-10-26 23:44:43 +0100478 input += "sign";
479 bits = {"sign"};
480 }
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100481 SECTION("carry sign")
482 {
Václav Kubernátdab73ca2020-10-26 23:44:43 +0100483 input += "carry sign";
484 bits = {"carry", "sign"};
485 }
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100486 SECTION("sign carry")
487 {
Václav Kubernátdab73ca2020-10-26 23:44:43 +0100488 input += "sign carry";
489 bits = {"sign", "carry"};
490 }
491 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("flags"));
492 expected.m_data = bits_{bits};
493 }
Václav Kubernát07204242018-06-04 18:12:09 +0200494 }
495
496 command_ command = parser.parseCommand(input, errorStream);
497 REQUIRE(command.type() == typeid(set_));
498 REQUIRE(boost::get<set_>(command) == expected);
499 }
500
501 SECTION("invalid input")
502 {
Václav Kubernát777704d2020-01-15 18:48:06 +0100503 std::string expectedError;
Václav Kubernát07204242018-06-04 18:12:09 +0200504 SECTION("missing space between a command and its arguments")
505 {
Václav Kubernát777704d2020-01-15 18:48:06 +0100506 SECTION("setmod:leafString some_data")
Václav Kubernát07204242018-06-04 18:12:09 +0200507 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100508 input = "setmod:leafString 'some_data'";
Václav Kubernát07204242018-06-04 18:12:09 +0200509 }
510 }
511
512 SECTION("missing space between arguments")
513 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100514 SECTION("set mod:leafString'lol'")
Václav Kubernát07204242018-06-04 18:12:09 +0200515 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100516 input = "set mod:leafString'lol'";
Václav Kubernát07204242018-06-04 18:12:09 +0200517 }
518 }
519
520 SECTION("non-leaf identifiers")
521 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100522 SECTION("set mod:nonexistent 'blabla'")
Václav Kubernát07204242018-06-04 18:12:09 +0200523 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100524 input = "set mod:nonexistent 'blabla'";
Václav Kubernát07204242018-06-04 18:12:09 +0200525 }
526
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100527 SECTION("set mod:contA 'abde'")
Václav Kubernát07204242018-06-04 18:12:09 +0200528 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100529 input = "set mod:contA 'abde'";
Václav Kubernát07204242018-06-04 18:12:09 +0200530 }
531 }
532
Václav Kubernátebca2552018-06-08 19:06:02 +0200533 SECTION("wrong types")
534 {
Václav Kubernát777704d2020-01-15 18:48:06 +0100535 expectedError = "leaf data type mismatch";
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100536 SECTION("set mod:leafBool 'blabla'")
Václav Kubernátebca2552018-06-08 19:06:02 +0200537 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100538 input = "set mod:leafBool 'blabla'";
Václav Kubernátebca2552018-06-08 19:06:02 +0200539 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100540 SECTION("set mod:leafUint8 'blabla'")
Václav Kubernátebca2552018-06-08 19:06:02 +0200541 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100542 input = "set mod:leafUint8 'blabla'";
Václav Kubernátebca2552018-06-08 19:06:02 +0200543 }
Václav Kubernát777704d2020-01-15 18:48:06 +0100544 SECTION("set mod:leafUint8 -5")
Václav Kubernátebca2552018-06-08 19:06:02 +0200545 {
Václav Kubernát777704d2020-01-15 18:48:06 +0100546 input = "set mod:leafUint8 -5";
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200547 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100548 SECTION("set mod:leafInt8 'blabla'")
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200549 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100550 input = "set mod:leafInt8 'blabla'";
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200551 }
Václav Kubernát777704d2020-01-15 18:48:06 +0100552 SECTION("set mod:leafInt8 130")
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200553 {
Václav Kubernát777704d2020-01-15 18:48:06 +0100554 input = "set mod:leafInt8 130";
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200555 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100556 SECTION("set mod:leafUint16 'blabla'")
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200557 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100558 input = "set mod:leafUint16 'blabla'";
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200559 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100560 SECTION("set mod:leafInt16 'blabla'")
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200561 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100562 input = "set mod:leafInt16 'blabla'";
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200563 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100564 SECTION("set mod:leafUint32 'blabla'")
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200565 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100566 input = "set mod:leafUint32 'blabla'";
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200567 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100568 SECTION("set mod:leafInt32 'blabla'")
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200569 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100570 input = "set mod:leafInt32 'blabla'";
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200571 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100572 SECTION("set mod:leafUint64 'blabla'")
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200573 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100574 input = "set mod:leafUint64 'blabla'";
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200575 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100576 SECTION("set mod:leafInt64 'blabla'")
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200577 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100578 input = "set mod:leafInt64 'blabla'";
Václav Kubernátebca2552018-06-08 19:06:02 +0200579 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100580 SECTION("set mod:leafEnum 'blabla'")
Václav Kubernátebca2552018-06-08 19:06:02 +0200581 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100582 input = "set mod:leafEnum 'blabla'";
Václav Kubernátebca2552018-06-08 19:06:02 +0200583 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100584 SECTION("set mod:refToInt8 'blabla'")
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200585 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100586 input = "set mod:refToInt8 'blabla'";
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200587 }
Václav Kubernátebca2552018-06-08 19:06:02 +0200588 }
589
Václav Kubernátab538992019-03-06 15:30:50 +0100590 SECTION("wrong base64 strings")
591 {
592 SECTION("invalid character")
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100593 {
Václav Kubernát777704d2020-01-15 18:48:06 +0100594 input = "set mod:leafBinary dbahj-";
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100595 }
Václav Kubernátab538992019-03-06 15:30:50 +0100596 SECTION("equal sign in the middle")
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100597 {
Václav Kubernát777704d2020-01-15 18:48:06 +0100598 input = "set mod:leafBinary db=ahj";
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100599 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100600 SECTION("enclosing in quotes")
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100601 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100602 input = "set mod:leafBinary 'dbahj'";
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100603 }
Václav Kubernátab538992019-03-06 15:30:50 +0100604 }
605
Václav Kubernáteeb38842019-03-20 19:46:05 +0100606 SECTION("non-existing identity")
607 {
608 input = "set mod:foodIdentRef identityBLABLA";
609 }
610
Václav Kubernátf365ecf2020-06-09 16:22:43 +0200611 SECTION("identity with non-existing module")
612 {
613 expectedError = "Invalid module name";
614 input = "set mod:foodIdentRef xd:haha";
615 }
616
Václav Kubernáteeb38842019-03-20 19:46:05 +0100617 SECTION("setting identities with wrong bases")
618 {
619 SECTION("set mod:foodIdentRef mod:vehicle")
620 {
621 input = "set mod:foodIdentRef mod:vehicle";
622 }
623 SECTION("set mod:pizzaIdentRef mod:food")
624 {
625 input = "set mod:pizzaIdentRef mod:food";
626 }
627 }
628 SECTION("setting different module identities without prefix")
629 {
630 input = "set mod:pizzaIdentRef hawaii";
631 }
632 SECTION("identity prefix without name")
633 {
634 input = "set mod:contA/identInCont pizza-module:";
635 }
636
Václav Kubernát2984f442020-02-20 17:43:35 +0100637 SECTION("set a union path to a wrong type")
638 {
639 input = "set mod:intOrString true";
640 }
641
Jan Kundrát379bb572020-05-07 03:23:13 +0200642 SECTION("no space for empty data")
643 {
644 input = "set mod:dummy";
645 }
646
Václav Kubernát0d42c512020-05-20 21:18:39 +0200647 SECTION("empty path")
648 {
649 input = "set ";
650 }
651
Václav Kubernátabf52802020-05-19 01:31:17 +0200652 SECTION("setting readonly data")
653 {
654 input = "set mod:readonly 123";
655 }
656
Václav Kubernátdab73ca2020-10-26 23:44:43 +0100657 SECTION("nonexistent bits")
658 {
659 input = "set mod:flags daw";
660 }
661
662 SECTION("same bit more than once")
663 {
664 input = "set mod:flags carry carry";
665 }
666
Jan Kundrátc381e632019-03-14 13:39:11 +0100667 REQUIRE_THROWS_AS(parser.parseCommand(input, errorStream), InvalidCommandException);
Václav Kubernát777704d2020-01-15 18:48:06 +0100668 REQUIRE(errorStream.str().find(expectedError) != std::string::npos);
Václav Kubernát07204242018-06-04 18:12:09 +0200669 }
Jan Kundrátb7206ad2020-06-18 21:08:14 +0200670
671 SECTION("deleting a leaf")
672 {
673 delete_ expected;
674 input = "delete mod:leafString";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200675 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafString"));
Jan Kundrátb7206ad2020-06-18 21:08:14 +0200676
677 command_ command = parser.parseCommand(input, errorStream);
678 REQUIRE(command.type() == typeid(delete_));
679 REQUIRE(boost::get<delete_>(command) == expected);
680 }
Václav Kubernát07204242018-06-04 18:12:09 +0200681}