blob: 7b203f4c9f7176476dff984e00c92060f30e0bd6 [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átc15fe822020-06-04 11:28:39 +0200126 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;
467 SECTION("<nothing>") {
468 bits = {};
469 }
470 SECTION("carry") {
471 input += "carry";
472 bits = {"carry"};
473 }
474 SECTION("sign") {
475 input += "sign";
476 bits = {"sign"};
477 }
478 SECTION("carry sign") {
479 input += "carry sign";
480 bits = {"carry", "sign"};
481 }
482 SECTION("sign carry") {
483 input += "sign carry";
484 bits = {"sign", "carry"};
485 }
486 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("flags"));
487 expected.m_data = bits_{bits};
488 }
Václav Kubernát07204242018-06-04 18:12:09 +0200489 }
490
491 command_ command = parser.parseCommand(input, errorStream);
492 REQUIRE(command.type() == typeid(set_));
493 REQUIRE(boost::get<set_>(command) == expected);
494 }
495
496 SECTION("invalid input")
497 {
Václav Kubernát777704d2020-01-15 18:48:06 +0100498 std::string expectedError;
Václav Kubernát07204242018-06-04 18:12:09 +0200499 SECTION("missing space between a command and its arguments")
500 {
Václav Kubernát777704d2020-01-15 18:48:06 +0100501 SECTION("setmod:leafString some_data")
Václav Kubernát07204242018-06-04 18:12:09 +0200502 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100503 input = "setmod:leafString 'some_data'";
Václav Kubernát07204242018-06-04 18:12:09 +0200504 }
505 }
506
507 SECTION("missing space between arguments")
508 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100509 SECTION("set mod:leafString'lol'")
Václav Kubernát07204242018-06-04 18:12:09 +0200510 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100511 input = "set mod:leafString'lol'";
Václav Kubernát07204242018-06-04 18:12:09 +0200512 }
513 }
514
515 SECTION("non-leaf identifiers")
516 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100517 SECTION("set mod:nonexistent 'blabla'")
Václav Kubernát07204242018-06-04 18:12:09 +0200518 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100519 input = "set mod:nonexistent 'blabla'";
Václav Kubernát07204242018-06-04 18:12:09 +0200520 }
521
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100522 SECTION("set mod:contA 'abde'")
Václav Kubernát07204242018-06-04 18:12:09 +0200523 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100524 input = "set mod:contA 'abde'";
Václav Kubernát07204242018-06-04 18:12:09 +0200525 }
526 }
527
Václav Kubernátebca2552018-06-08 19:06:02 +0200528 SECTION("wrong types")
529 {
Václav Kubernát777704d2020-01-15 18:48:06 +0100530 expectedError = "leaf data type mismatch";
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100531 SECTION("set mod:leafBool 'blabla'")
Václav Kubernátebca2552018-06-08 19:06:02 +0200532 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100533 input = "set mod:leafBool 'blabla'";
Václav Kubernátebca2552018-06-08 19:06:02 +0200534 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100535 SECTION("set mod:leafUint8 'blabla'")
Václav Kubernátebca2552018-06-08 19:06:02 +0200536 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100537 input = "set mod:leafUint8 'blabla'";
Václav Kubernátebca2552018-06-08 19:06:02 +0200538 }
Václav Kubernát777704d2020-01-15 18:48:06 +0100539 SECTION("set mod:leafUint8 -5")
Václav Kubernátebca2552018-06-08 19:06:02 +0200540 {
Václav Kubernát777704d2020-01-15 18:48:06 +0100541 input = "set mod:leafUint8 -5";
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200542 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100543 SECTION("set mod:leafInt8 'blabla'")
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200544 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100545 input = "set mod:leafInt8 'blabla'";
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200546 }
Václav Kubernát777704d2020-01-15 18:48:06 +0100547 SECTION("set mod:leafInt8 130")
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200548 {
Václav Kubernát777704d2020-01-15 18:48:06 +0100549 input = "set mod:leafInt8 130";
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200550 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100551 SECTION("set mod:leafUint16 'blabla'")
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200552 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100553 input = "set mod:leafUint16 'blabla'";
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200554 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100555 SECTION("set mod:leafInt16 'blabla'")
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200556 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100557 input = "set mod:leafInt16 'blabla'";
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200558 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100559 SECTION("set mod:leafUint32 'blabla'")
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200560 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100561 input = "set mod:leafUint32 'blabla'";
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200562 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100563 SECTION("set mod:leafInt32 'blabla'")
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200564 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100565 input = "set mod:leafInt32 'blabla'";
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200566 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100567 SECTION("set mod:leafUint64 'blabla'")
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200568 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100569 input = "set mod:leafUint64 'blabla'";
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200570 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100571 SECTION("set mod:leafInt64 'blabla'")
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200572 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100573 input = "set mod:leafInt64 'blabla'";
Václav Kubernátebca2552018-06-08 19:06:02 +0200574 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100575 SECTION("set mod:leafEnum 'blabla'")
Václav Kubernátebca2552018-06-08 19:06:02 +0200576 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100577 input = "set mod:leafEnum 'blabla'";
Václav Kubernátebca2552018-06-08 19:06:02 +0200578 }
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100579 SECTION("set mod:refToInt8 'blabla'")
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200580 {
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100581 input = "set mod:refToInt8 'blabla'";
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200582 }
Václav Kubernátebca2552018-06-08 19:06:02 +0200583 }
584
Václav Kubernátab538992019-03-06 15:30:50 +0100585 SECTION("wrong base64 strings")
586 {
587 SECTION("invalid character")
Václav Kubernát777704d2020-01-15 18:48:06 +0100588 input = "set mod:leafBinary dbahj-";
Václav Kubernátab538992019-03-06 15:30:50 +0100589 SECTION("equal sign in the middle")
Václav Kubernát777704d2020-01-15 18:48:06 +0100590 input = "set mod:leafBinary db=ahj";
Václav Kubernát1bbac7c2020-01-15 17:45:41 +0100591 SECTION("enclosing in quotes")
592 input = "set mod:leafBinary 'dbahj'";
Václav Kubernátab538992019-03-06 15:30:50 +0100593 }
594
Václav Kubernáteeb38842019-03-20 19:46:05 +0100595 SECTION("non-existing identity")
596 {
597 input = "set mod:foodIdentRef identityBLABLA";
598 }
599
Václav Kubernátf365ecf2020-06-09 16:22:43 +0200600 SECTION("identity with non-existing module")
601 {
602 expectedError = "Invalid module name";
603 input = "set mod:foodIdentRef xd:haha";
604 }
605
Václav Kubernáteeb38842019-03-20 19:46:05 +0100606 SECTION("setting identities with wrong bases")
607 {
608 SECTION("set mod:foodIdentRef mod:vehicle")
609 {
610 input = "set mod:foodIdentRef mod:vehicle";
611 }
612 SECTION("set mod:pizzaIdentRef mod:food")
613 {
614 input = "set mod:pizzaIdentRef mod:food";
615 }
616 }
617 SECTION("setting different module identities without prefix")
618 {
619 input = "set mod:pizzaIdentRef hawaii";
620 }
621 SECTION("identity prefix without name")
622 {
623 input = "set mod:contA/identInCont pizza-module:";
624 }
625
Václav Kubernát2984f442020-02-20 17:43:35 +0100626 SECTION("set a union path to a wrong type")
627 {
628 input = "set mod:intOrString true";
629 }
630
Jan Kundrát379bb572020-05-07 03:23:13 +0200631 SECTION("no space for empty data")
632 {
633 input = "set mod:dummy";
634 }
635
Václav Kubernát0d42c512020-05-20 21:18:39 +0200636 SECTION("empty path")
637 {
638 input = "set ";
639 }
640
Václav Kubernátabf52802020-05-19 01:31:17 +0200641 SECTION("setting readonly data")
642 {
643 input = "set mod:readonly 123";
644 }
645
Václav Kubernátdab73ca2020-10-26 23:44:43 +0100646 SECTION("nonexistent bits")
647 {
648 input = "set mod:flags daw";
649 }
650
651 SECTION("same bit more than once")
652 {
653 input = "set mod:flags carry carry";
654 }
655
Jan Kundrátc381e632019-03-14 13:39:11 +0100656 REQUIRE_THROWS_AS(parser.parseCommand(input, errorStream), InvalidCommandException);
Václav Kubernát777704d2020-01-15 18:48:06 +0100657 REQUIRE(errorStream.str().find(expectedError) != std::string::npos);
Václav Kubernát07204242018-06-04 18:12:09 +0200658 }
Jan Kundrátb7206ad2020-06-18 21:08:14 +0200659
660 SECTION("deleting a leaf")
661 {
662 delete_ expected;
663 input = "delete mod:leafString";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200664 expected.m_path.m_nodes.emplace_back(module_{"mod"}, leaf_("leafString"));
Jan Kundrátb7206ad2020-06-18 21:08:14 +0200665
666 command_ command = parser.parseCommand(input, errorStream);
667 REQUIRE(command.type() == typeid(delete_));
668 REQUIRE(boost::get<delete_>(command) == expected);
669 }
Václav Kubernát07204242018-06-04 18:12:09 +0200670}