blob: cdb200e3bc47a03601930f632cc0820ef4f5aaff [file] [log] [blame]
Václav Kubernát0d4db442018-07-18 17:18:43 +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át47a3f672019-11-08 15:42:43 +01009#include <experimental/iterator>
Václav Kubernát3a99f002020-03-31 02:27:41 +020010#include "leaf_data_helpers.hpp"
Václav Kubernát1e09bd62020-02-17 15:13:38 +010011#include "pretty_printers.hpp"
Václav Kubernát26b56082020-02-03 18:28:56 +010012#include "trompeloeil_doctest.hpp"
Václav Kubernát0d4db442018-07-18 17:18:43 +020013#include "yang_schema.hpp"
14
Václav Kubernát4f77a252019-02-19 16:51:30 +010015const char* second_schema = R"(
16module second-schema {
17 namespace "http://example.com/nevim";
18 prefix second;
19
20 import example-schema {
21 prefix "example";
22 }
23
Václav Kubernáteeb38842019-03-20 19:46:05 +010024 identity pineapple {
25 base "example:fruit";
26 }
27
Václav Kubernát4f77a252019-02-19 16:51:30 +010028 augment /example:a {
29 container augmentedContainer {
30 }
31 }
32
33 container bla {
34 container bla2 {
35 }
36 }
37}
38)";
39
40const char* example_schema = R"(
Václav Kubernát0d4db442018-07-18 17:18:43 +020041module example-schema {
Václav Kubernát6a713d62018-10-03 18:47:34 +020042 yang-version 1.1;
Václav Kubernát0d4db442018-07-18 17:18:43 +020043 namespace "http://example.com/example-sports";
44 prefix coze;
45
Václav Kubernáteeb38842019-03-20 19:46:05 +010046 identity drink {
47 }
48
49 identity voda {
50 base "drink";
51 }
52
53 identity food {
54 }
55
56 identity fruit {
57 base "food";
58 }
59
60 identity pizza {
61 base "food";
62 }
63
64 identity hawaii {
65 base "pizza";
66 }
67
Václav Kubernát0d4db442018-07-18 17:18:43 +020068 container a {
69 container a2 {
70 container a3 {
71 presence true;
72 }
73 }
74
75 leaf leafa {
76 type string;
77 }
78 }
79
80 container b {
81 container b2 {
82 presence true;
83 container b3 {
84 }
85 }
86 }
87
88 leaf leafString {
89 type string;
90 }
91
92 leaf leafDecimal {
93 type decimal64 {
94 fraction-digits 5;
95 }
96 }
97
98 leaf leafBool {
99 type boolean;
100 }
101
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200102 leaf leafInt8 {
103 type int8;
104 }
105
106 leaf leafUint8 {
107 type uint8;
108 }
109
110 leaf leafInt16 {
111 type int16;
112 }
113
114 leaf leafUint16 {
115 type uint16;
116 }
117
118 leaf leafInt32 {
Václav Kubernát1e09bd62020-02-17 15:13:38 +0100119 description "A 32-bit integer leaf.";
Václav Kubernát0d4db442018-07-18 17:18:43 +0200120 type int32;
121 }
122
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200123 leaf leafUint32 {
Václav Kubernát0d4db442018-07-18 17:18:43 +0200124 type uint32;
125 }
126
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200127 leaf leafInt64 {
128 type int64;
129 }
130
131 leaf leafUint64 {
132 type uint64;
Václav Kubernátb1a75c62020-04-21 15:20:16 +0200133 default 9001;
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200134 }
135
Václav Kubernát0d4db442018-07-18 17:18:43 +0200136 leaf leafEnum {
137 type enumeration {
138 enum lol;
139 enum data;
140 enum coze;
141 }
142 }
143
Václav Kubernát6a713d62018-10-03 18:47:34 +0200144 typedef enumTypedef {
145 type enumeration {
146 enum lol;
147 enum data;
148 enum coze;
149 }
Václav Kubernátb1a75c62020-04-21 15:20:16 +0200150 default data;
Václav Kubernát6a713d62018-10-03 18:47:34 +0200151 }
152
153 typedef enumTypedefRestricted {
154 type enumTypedef {
155 enum lol;
156 enum data;
157 }
158 }
159
160 leaf leafEnumTypedef {
161 type enumTypedef;
162 }
163
164 leaf leafEnumTypedefRestricted {
165 type enumTypedef {
166 enum data;
167 enum coze;
168 }
169 }
170
171 leaf leafEnumTypedefRestricted2 {
172 type enumTypedefRestricted;
173 }
174
Václav Kubernáteeb38842019-03-20 19:46:05 +0100175 leaf foodIdentLeaf {
176 type identityref {
177 base "food";
178 }
179 }
180
181 leaf pizzaIdentLeaf {
182 type identityref {
183 base "pizza";
184 }
185 }
186
187 leaf foodDrinkIdentLeaf {
188 type identityref {
189 base "food";
190 base "drink";
191 }
192 }
193
Václav Kubernát0d4db442018-07-18 17:18:43 +0200194 list _list {
195 key number;
196
197 leaf number {
198 type int32;
199 }
200
201 container contInList {
202 presence true;
203 }
204 }
205
206 list twoKeyList {
207 key "name number";
208
209 leaf number {
210 type int32;
211 }
212
213 leaf name {
214 type string;
215 }
216 }
Václav Kubernát7d82da72019-04-11 15:16:38 +0200217
218 grouping arithmeticFlags {
219 leaf carry {
220 type boolean;
221 }
222 leaf zero {
223 type boolean;
224 }
225 }
226
227 grouping flags {
228 leaf direction {
229 type boolean;
230 }
231 leaf interrupt {
232 type boolean;
233 }
234
235 uses arithmeticFlags;
236 }
237
238 uses flags;
Václav Kubernát280df4a2019-11-01 14:46:34 +0100239
240 choice interface {
241 case caseLoopback {
242 container loopback {
243 leaf ip {
244 type string;
245 }
246 }
247 }
248
249 case caseEthernet {
250 container ethernet {
251 leaf ip {
252 type string;
253 }
254 }
255 }
256 }
257
Václav Kubernáta38d4172019-11-04 12:36:39 +0100258 feature bigPizzas;
259
260 leaf pizzaSize {
261 type enumeration {
262 enum large {
263 if-feature "bigPizzas";
264 }
265 enum medium;
266 enum small;
267 }
268 }
269
Václav Kubernát1e09bd62020-02-17 15:13:38 +0100270 leaf length {
271 type int32;
272 units "m";
273 }
274
275 leaf wavelength {
276 type decimal64 {
277 fraction-digits 10;
278 }
279 units "nm";
280 }
281
282 typedef seconds {
283 type int32;
284 units "s";
285 }
286
287 leaf duration {
288 type seconds;
289 }
290
291 leaf another-duration {
292 type seconds;
293 units "vt";
294 }
295
Václav Kubernátbd5e3c22020-02-19 15:22:00 +0100296 leaf activeNumber {
297 type leafref {
298 path "/_list/number";
299 }
300 }
301
Václav Kubernátfa81c8c2020-02-13 17:22:46 +0100302 rpc myRpc {}
303
Václav Kubernát2984f442020-02-20 17:43:35 +0100304 leaf numberOrString {
305 type union {
306 type int32;
307 type string;
308 }
309 description "Can be an int32 or a string.";
310 }
311
312 list portSettings {
313 key "port";
314 leaf port {
315 type enumeration {
316 enum eth0;
317 enum eth1;
318 enum eth2;
319 }
320 }
321 }
322
323 feature weirdPortNames;
324
325 list portMapping {
326 key "port";
327 leaf port {
328 type enumeration {
329 enum WEIRD {
330 if-feature "weirdPortNames";
331 }
332 enum utf2;
333 enum utf3;
334 }
335 }
336 }
337
338 leaf activeMappedPort {
339 type leafref {
340 path "../portMapping/port";
341 }
342 }
343
344 leaf activePort {
345 type union {
346 type enumeration {
347 enum wlan0;
348 enum wlan1;
349 }
350 type leafref {
351 path "../portSettings/port";
352 }
353 type leafref {
354 path "../activeMappedPort";
355 }
356 }
357 }
358
Václav Kubernát0599e9f2020-04-21 09:51:33 +0200359 leaf clockSpeed {
360 type int64;
361 config false;
362 }
363
364 container systemStats {
365 config false;
366 leaf upTime {
367 type uint64;
368 }
369 }
370
Václav Kubernáta1c4c9e2020-04-22 00:37:52 +0200371 leaf obsoleteLeaf {
372 type int32;
373 status obsolete;
374 }
375
376 leaf deprecatedLeaf {
377 type int32;
378 status deprecated;
379 }
380
381 typedef deprecatedType {
382 type int32;
383 status deprecated;
384 }
385
386 leaf obsoleteLeafWithDeprecatedType {
387 type deprecatedType;
388 status obsolete;
389 }
390
391 typedef obsoleteType {
392 type int32;
393 status obsolete;
394 }
395
396 leaf obsoleteLeafWithObsoleteType {
397 type deprecatedType;
398 status obsolete;
399 }
400
Václav Kubernát0d4db442018-07-18 17:18:43 +0200401})";
402
Václav Kubernát47a3f672019-11-08 15:42:43 +0100403namespace std {
404std::ostream& operator<<(std::ostream& s, const std::set<std::string> set)
405{
406 s << std::endl << "{";
407 std::copy(set.begin(), set.end(), std::experimental::make_ostream_joiner(s, ", "));
408 s << "}" << std::endl;
409 return s;
410}
411}
412
Václav Kubernát0d4db442018-07-18 17:18:43 +0200413TEST_CASE("yangschema")
414{
Václav Kubernát4f77a252019-02-19 16:51:30 +0100415 using namespace std::string_view_literals;
Václav Kubernát0d4db442018-07-18 17:18:43 +0200416 YangSchema ys;
Václav Kubernátb52dc252019-12-04 13:03:39 +0100417 ys.registerModuleCallback([]([[maybe_unused]] auto modName, auto, auto, auto) {
Václav Kubernát4f77a252019-02-19 16:51:30 +0100418 assert("example-schema"sv == modName);
419 return example_schema;
420 });
421 ys.addSchemaString(second_schema);
422
Václav Kubernátefcac932020-01-10 15:26:32 +0100423 schemaPath_ path{Scope::Absolute, {}};
Václav Kubernát0d4db442018-07-18 17:18:43 +0200424 ModuleNodePair node;
425
426 SECTION("positive")
427 {
428 SECTION("isContainer")
429 {
430 SECTION("example-schema:a")
431 {
432 node.first = "example-schema";
433 node.second = "a";
434 }
435
436 SECTION("example-schema:a/a2")
437 {
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200438 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, container_("a")));
Václav Kubernát0d4db442018-07-18 17:18:43 +0200439 node.second = "a2";
440 }
441
Václav Kubernát280df4a2019-11-01 14:46:34 +0100442 SECTION("example-schema:ethernet")
443 {
444 node.first = "example-schema";
445 node.second = "ethernet";
446 }
447
448 SECTION("example-schema:loopback")
449 {
450 node.first = "example-schema";
451 node.second = "loopback";
452 }
453
Václav Kubernát0d4db442018-07-18 17:18:43 +0200454 REQUIRE(ys.isContainer(path, node));
455 }
456 SECTION("isLeaf")
457 {
458 SECTION("example-schema:leafString")
459 {
460 node.first = "example-schema";
461 node.second = "leafString";
462 }
463
464 SECTION("example-schema:a/leafa")
465 {
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200466 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, container_("a")));
Václav Kubernát0d4db442018-07-18 17:18:43 +0200467 node.first = "example-schema";
468 node.second = "leafa";
469 }
470
Václav Kubernát7d82da72019-04-11 15:16:38 +0200471 SECTION("example-schema:carry")
472 {
473 node.first = "example-schema";
474 node.second = "carry";
475 }
476
477 SECTION("example-schema:zero")
478 {
479 node.first = "example-schema";
480 node.second = "zero";
481 }
482
483 SECTION("example-schema:direction")
484 {
485 node.first = "example-schema";
486 node.second = "direction";
487 }
488
489 SECTION("example-schema:interrupt")
490 {
491 node.first = "example-schema";
492 node.second = "interrupt";
493 }
494
Václav Kubernát0d4db442018-07-18 17:18:43 +0200495 REQUIRE(ys.isLeaf(path, node));
496 }
497 SECTION("isModule")
498 {
Václav Kubernát75877de2019-11-20 17:43:02 +0100499 REQUIRE(ys.isModule("example-schema"));
Václav Kubernát0d4db442018-07-18 17:18:43 +0200500 }
501 SECTION("isList")
502 {
503 SECTION("example-schema:_list")
504 {
505 node.first = "example-schema";
506 node.second = "_list";
507 }
508
509 SECTION("example-schema:twoKeyList")
510 {
511 node.first = "example-schema";
512 node.second = "twoKeyList";
513 }
514
515 REQUIRE(ys.isList(path, node));
516 }
517 SECTION("isPresenceContainer")
518 {
519 SECTION("example-schema:a/a2/a3")
520 {
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200521 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, container_("a")));
522 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, container_("a2")));
Václav Kubernát0d4db442018-07-18 17:18:43 +0200523 node.second = "a3";
524 }
525
526 REQUIRE(ys.isPresenceContainer(path, node));
527 }
Václav Kubernáteeb38842019-03-20 19:46:05 +0100528
Václav Kubernát0d4db442018-07-18 17:18:43 +0200529 SECTION("listHasKey")
530 {
531 std::string key;
532
533 SECTION("_list")
534 {
535 node.first = "example-schema";
536 node.second = "_list";
537 SECTION("number")
538 key = "number";
539 }
540
541 SECTION("twoKeyList")
542 {
543 node.first = "example-schema";
544 node.second = "twoKeyList";
545 SECTION("number")
546 key = "number";
547 SECTION("name")
548 key = "name";
549 }
550
551 REQUIRE(ys.listHasKey(path, node, key));
552 }
553 SECTION("listKeys")
554 {
555 std::set<std::string> set;
556
557 SECTION("_list")
558 {
559 set = {"number"};
560 node.first = "example-schema";
561 node.second = "_list";
562 }
563
564 SECTION("twoKeyList")
565 {
566 set = {"number", "name"};
567 node.first = "example-schema";
568 node.second = "twoKeyList";
569 }
570
571 REQUIRE(ys.listKeys(path, node) == set);
572 }
573 SECTION("leafType")
574 {
Václav Kubernát3a99f002020-03-31 02:27:41 +0200575 yang::LeafDataType type;
Václav Kubernát0d4db442018-07-18 17:18:43 +0200576
577 SECTION("leafString")
578 {
579 node.first = "example-schema";
580 node.second = "leafString";
Václav Kubernát3a99f002020-03-31 02:27:41 +0200581 type = yang::String{};
Václav Kubernát0d4db442018-07-18 17:18:43 +0200582 }
583
584 SECTION("leafDecimal")
585 {
586 node.first = "example-schema";
587 node.second = "leafDecimal";
Václav Kubernát3a99f002020-03-31 02:27:41 +0200588 type = yang::Decimal{};
Václav Kubernát0d4db442018-07-18 17:18:43 +0200589 }
590
591 SECTION("leafBool")
592 {
593 node.first = "example-schema";
594 node.second = "leafBool";
Václav Kubernát3a99f002020-03-31 02:27:41 +0200595 type = yang::Bool{};
Václav Kubernát0d4db442018-07-18 17:18:43 +0200596 }
597
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200598 SECTION("leafInt8")
Václav Kubernát0d4db442018-07-18 17:18:43 +0200599 {
600 node.first = "example-schema";
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200601 node.second = "leafInt8";
Václav Kubernát3a99f002020-03-31 02:27:41 +0200602 type = yang::Int8{};
Václav Kubernát0d4db442018-07-18 17:18:43 +0200603 }
604
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200605 SECTION("leafUint8")
Václav Kubernát0d4db442018-07-18 17:18:43 +0200606 {
607 node.first = "example-schema";
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200608 node.second = "leafUint8";
Václav Kubernát3a99f002020-03-31 02:27:41 +0200609 type = yang::Uint8{};
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200610 }
611
Václav Kubernátb6d02752020-04-03 00:25:10 +0200612 SECTION("leafInt16")
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200613 {
614 node.first = "example-schema";
615 node.second = "leafInt16";
Václav Kubernát3a99f002020-03-31 02:27:41 +0200616 type = yang::Int16{};
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200617 }
618
619 SECTION("leafUint16")
620 {
621 node.first = "example-schema";
622 node.second = "leafUint16";
Václav Kubernát3a99f002020-03-31 02:27:41 +0200623 type = yang::Uint16{};
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200624 }
625
626 SECTION("leafInt32")
627 {
628 node.first = "example-schema";
629 node.second = "leafInt32";
Václav Kubernát3a99f002020-03-31 02:27:41 +0200630 type = yang::Int32{};
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200631 }
632
633 SECTION("leafUint32")
634 {
635 node.first = "example-schema";
636 node.second = "leafUint32";
Václav Kubernát3a99f002020-03-31 02:27:41 +0200637 type = yang::Uint32{};
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200638 }
639
640 SECTION("leafInt64")
641 {
642 node.first = "example-schema";
643 node.second = "leafInt64";
Václav Kubernát3a99f002020-03-31 02:27:41 +0200644 type = yang::Int64{};
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200645 }
646
647 SECTION("leafUint64")
648 {
649 node.first = "example-schema";
650 node.second = "leafUint64";
Václav Kubernát3a99f002020-03-31 02:27:41 +0200651 type = yang::Uint64{};
Václav Kubernát0d4db442018-07-18 17:18:43 +0200652 }
653
654 SECTION("leafEnum")
655 {
656 node.first = "example-schema";
657 node.second = "leafEnum";
Václav Kubernát3a99f002020-03-31 02:27:41 +0200658 type = createEnum({"lol", "data", "coze"});
659 }
660
661 SECTION("leafEnumTypedef")
662 {
663 node.first = "example-schema";
664 node.second = "leafEnumTypedef";
665 type = createEnum({"lol", "data", "coze"});
666 }
667
668 SECTION("leafEnumTypedefRestricted")
669 {
670 node.first = "example-schema";
671 node.second = "leafEnumTypedefRestricted";
672 type = createEnum({"data", "coze"});
673 }
674
675 SECTION("leafEnumTypedefRestricted2")
676 {
677 node.first = "example-schema";
678 node.second = "leafEnumTypedefRestricted2";
679 type = createEnum({"lol", "data"});
680 }
681
682 SECTION("pizzaSize")
683 {
684 node.first = "example-schema";
685 node.second = "pizzaSize";
686
687 SECTION("bigPizzas disabled")
688 {
689 type = createEnum({"small", "medium"});
690 }
691 SECTION("bigPizzas enabled")
692 {
693 ys.enableFeature("example-schema", "bigPizzas");
694 type = createEnum({"small", "medium", "large"});
695 }
696 }
697
698 SECTION("foodIdentLeaf")
699 {
700 node.first = "example-schema";
701 node.second = "foodIdentLeaf";
702 type = yang::IdentityRef{{{"second-schema", "pineapple"},
703 {"example-schema", "food"},
704 {"example-schema", "pizza"},
705 {"example-schema", "hawaii"},
706 {"example-schema", "fruit"}}};
707 }
708
709 SECTION("pizzaIdentLeaf")
710 {
711 node.first = "example-schema";
712 node.second = "pizzaIdentLeaf";
713
714 type = yang::IdentityRef{{
715 {"example-schema", "pizza"},
716 {"example-schema", "hawaii"},
717 }};
718 }
719
720 SECTION("foodDrinkIdentLeaf")
721 {
722 node.first = "example-schema";
723 node.second = "foodDrinkIdentLeaf";
724
725 type = yang::IdentityRef{{
726 {"example-schema", "food"},
727 {"example-schema", "drink"},
728 {"example-schema", "fruit"},
729 {"example-schema", "hawaii"},
730 {"example-schema", "pizza"},
731 {"example-schema", "voda"},
732 {"second-schema", "pineapple"},
733 }};
Václav Kubernát0d4db442018-07-18 17:18:43 +0200734 }
735
Václav Kubernátb6d02752020-04-03 00:25:10 +0200736 SECTION("activeNumber")
737 {
738 node.first = "example-schema";
739 node.second = "activeNumber";
Václav Kubernát3a99f002020-03-31 02:27:41 +0200740 type.emplace<yang::LeafRef>(
741 "/example-schema:_list/number",
Václav Kubernát13b23d72020-04-16 21:49:51 +0200742 std::make_unique<yang::TypeInfo>(ys.leafType("/example-schema:_list/number"))
Václav Kubernát3a99f002020-03-31 02:27:41 +0200743 );
Václav Kubernátb6d02752020-04-03 00:25:10 +0200744 }
745
Václav Kubernát2984f442020-02-20 17:43:35 +0100746 SECTION("activePort")
747 {
748 node.first = "example-schema";
749 node.second = "activePort";
750
751 yang::Enum enums = [&ys]() {
752 SECTION("weird ports disabled")
753 {
754 return createEnum({"utf2", "utf3"});
755 }
756 SECTION("weird ports enabled")
757 {
758 ys.enableFeature("example-schema", "weirdPortNames");
759 return createEnum({"WEIRD", "utf2", "utf3"});
760 }
761 __builtin_unreachable();
762 }();
763
764 type = yang::Union{{
Václav Kubernát13b23d72020-04-16 21:49:51 +0200765 yang::TypeInfo{createEnum({"wlan0", "wlan1"})},
766 yang::TypeInfo{yang::LeafRef{
Václav Kubernát2984f442020-02-20 17:43:35 +0100767 "/example-schema:portSettings/port",
Václav Kubernát13b23d72020-04-16 21:49:51 +0200768 std::make_unique<yang::TypeInfo>(createEnum({"eth0", "eth1", "eth2"}))
769 }},
770 yang::TypeInfo{yang::LeafRef{
Václav Kubernát2984f442020-02-20 17:43:35 +0100771 "/example-schema:activeMappedPort",
Václav Kubernát13b23d72020-04-16 21:49:51 +0200772 std::make_unique<yang::TypeInfo>(yang::LeafRef{
Václav Kubernát2984f442020-02-20 17:43:35 +0100773 "/example-schema:portMapping/port",
Václav Kubernát13b23d72020-04-16 21:49:51 +0200774 std::make_unique<yang::TypeInfo>(enums)
775 })
776 }},
Václav Kubernát2984f442020-02-20 17:43:35 +0100777 }};
778 }
779
Václav Kubernát0d4db442018-07-18 17:18:43 +0200780 REQUIRE(ys.leafType(path, node) == type);
781 }
Václav Kubernát3a823f42020-04-29 23:40:21 +0200782 SECTION("availableNodes")
Václav Kubernát0d4db442018-07-18 17:18:43 +0200783 {
Václav Kubernát3a823f42020-04-29 23:40:21 +0200784 // TODO: merge "path" and "module" sections and add recursive versions to the path section
785 SECTION("paths")
Václav Kubernát0d4db442018-07-18 17:18:43 +0200786 {
Václav Kubernát95b08872020-04-28 01:04:17 +0200787 std::set<ModuleNodePair> set;
Václav Kubernát3a823f42020-04-29 23:40:21 +0200788
Václav Kubernát95b08872020-04-28 01:04:17 +0200789 using namespace std::string_literals;
Václav Kubernát3a823f42020-04-29 23:40:21 +0200790 SECTION("<root>")
791 {
Václav Kubernát95b08872020-04-28 01:04:17 +0200792 set = {{"example-schema"s, "a"}, {"example-schema"s, "b"}, {"example-schema"s, "leafString"},
793 {"example-schema"s, "leafDecimal"}, {"example-schema"s, "leafBool"},
794 {"example-schema"s, "leafInt8"}, {"example-schema"s, "leafUint8"},
795 {"example-schema"s, "leafInt16"}, {"example-schema"s, "leafUint16"},
796 {"example-schema"s, "leafInt32"}, {"example-schema"s, "leafUint32"},
797 {"example-schema"s, "leafInt64"}, {"example-schema"s, "leafUint64"},
798 {"example-schema"s, "leafEnum"}, {"example-schema"s, "leafEnumTypedef"},
799 {"example-schema"s, "leafEnumTypedefRestricted"}, {"example-schema"s, "leafEnumTypedefRestricted2"},
800 {"example-schema"s, "foodIdentLeaf"}, {"example-schema"s, "pizzaIdentLeaf"}, {"example-schema"s, "foodDrinkIdentLeaf"},
801 {"example-schema"s, "_list"}, {"example-schema"s, "twoKeyList"}, {"second-schema"s, "bla"},
802 {"example-schema"s, "carry"}, {"example-schema"s, "zero"}, {"example-schema"s, "direction"},
803 {"example-schema"s, "interrupt"},
804 {"example-schema"s, "ethernet"}, {"example-schema"s, "loopback"},
805 {"example-schema"s, "pizzaSize"},
806 {"example-schema"s, "length"}, {"example-schema"s, "wavelength"},
807 {"example-schema"s, "duration"}, {"example-schema"s, "another-duration"},
808 {"example-schema"s, "activeNumber"},
809 {"example-schema"s, "numberOrString"},
810 {"example-schema"s, "portSettings"},
811 {"example-schema"s, "portMapping"},
812 {"example-schema"s, "activeMappedPort"},
813 {"example-schema"s, "activePort"},
814 {"example-schema"s, "clockSpeed"},
815 {"example-schema"s, "deprecatedLeaf"},
816 {"example-schema"s, "obsoleteLeaf"},
817 {"example-schema"s, "obsoleteLeafWithDeprecatedType"},
818 {"example-schema"s, "obsoleteLeafWithObsoleteType"},
819 {"example-schema"s, "systemStats"}};
Václav Kubernát3a823f42020-04-29 23:40:21 +0200820 }
821
822 SECTION("example-schema:a")
823 {
824 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, container_("a")));
Václav Kubernát95b08872020-04-28 01:04:17 +0200825 set = {
826 {boost::none, "a2"},
827 {boost::none, "leafa"},
828 {"second-schema"s, "augmentedContainer"}
829 };
Václav Kubernát3a823f42020-04-29 23:40:21 +0200830 }
831
832 SECTION("example-schema:ethernet")
833 {
834 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, container_("ethernet")));
Václav Kubernát95b08872020-04-28 01:04:17 +0200835 set = {{boost::none, "ip"}};
836 }
837
838 SECTION("second-schema:bla")
839 {
840 path.m_nodes.push_back(schemaNode_(module_{"second-schema"}, container_("bla")));
841 set = {{boost::none, "bla2"}};
Václav Kubernát3a823f42020-04-29 23:40:21 +0200842 }
843
844 REQUIRE(ys.availableNodes(path, Recursion::NonRecursive) == set);
Václav Kubernát0d4db442018-07-18 17:18:43 +0200845 }
846
Václav Kubernát3a823f42020-04-29 23:40:21 +0200847 SECTION("modules")
Václav Kubernát0d4db442018-07-18 17:18:43 +0200848 {
Václav Kubernát3a823f42020-04-29 23:40:21 +0200849 std::string module;
Václav Kubernát95b08872020-04-28 01:04:17 +0200850 std::set<ModuleNodePair> expectedNonRecursive;
851 std::set<ModuleNodePair> expectedRecursive;
852 using namespace std::string_literals;
Václav Kubernát3a823f42020-04-29 23:40:21 +0200853 SECTION("example-schema")
854 {
855 module = "example-schema";
856 expectedNonRecursive = {
Václav Kubernát95b08872020-04-28 01:04:17 +0200857 {"example-schema"s, "_list"},
858 {"example-schema"s, "a"},
859 {"example-schema"s, "activeMappedPort"},
860 {"example-schema"s, "activeNumber"},
861 {"example-schema"s, "activePort"},
862 {"example-schema"s, "another-duration"},
863 {"example-schema"s, "b"},
864 {"example-schema"s, "carry"},
865 {"example-schema"s, "clockSpeed"},
866 {"example-schema"s, "deprecatedLeaf"},
867 {"example-schema"s, "direction"},
868 {"example-schema"s, "duration"},
869 {"example-schema"s, "ethernet"},
870 {"example-schema"s, "foodDrinkIdentLeaf"},
871 {"example-schema"s, "foodIdentLeaf"},
872 {"example-schema"s, "interrupt"},
873 {"example-schema"s, "leafBool"},
874 {"example-schema"s, "leafDecimal"},
875 {"example-schema"s, "leafEnum"},
876 {"example-schema"s, "leafEnumTypedef"},
877 {"example-schema"s, "leafEnumTypedefRestricted"},
878 {"example-schema"s, "leafEnumTypedefRestricted2"},
879 {"example-schema"s, "leafInt16"},
880 {"example-schema"s, "leafInt32"},
881 {"example-schema"s, "leafInt64"},
882 {"example-schema"s, "leafInt8"},
883 {"example-schema"s, "leafString"},
884 {"example-schema"s, "leafUint16"},
885 {"example-schema"s, "leafUint32"},
886 {"example-schema"s, "leafUint64"},
887 {"example-schema"s, "leafUint8"},
888 {"example-schema"s, "length"},
889 {"example-schema"s, "loopback"},
890 {"example-schema"s, "numberOrString"},
891 {"example-schema"s, "obsoleteLeaf"},
892 {"example-schema"s, "obsoleteLeafWithDeprecatedType"},
893 {"example-schema"s, "obsoleteLeafWithObsoleteType"},
894 {"example-schema"s, "pizzaIdentLeaf"},
895 {"example-schema"s, "pizzaSize"},
896 {"example-schema"s, "portMapping"},
897 {"example-schema"s, "portSettings"},
898 {"example-schema"s, "systemStats"},
899 {"example-schema"s, "twoKeyList"},
900 {"example-schema"s, "wavelength"},
901 {"example-schema"s, "zero"}
Václav Kubernát3a823f42020-04-29 23:40:21 +0200902 };
903 expectedRecursive = {
Václav Kubernát95b08872020-04-28 01:04:17 +0200904 {boost::none, "/example-schema:_list"},
905 {boost::none, "/example-schema:_list/contInList"},
906 {boost::none, "/example-schema:_list/number"},
907 {boost::none, "/example-schema:a"},
908 {boost::none, "/example-schema:a/a2"},
909 {boost::none, "/example-schema:a/a2/a3"},
910 {boost::none, "/example-schema:a/leafa"},
911 {boost::none, "/example-schema:a/second-schema:augmentedContainer"},
912 {boost::none, "/example-schema:activeMappedPort"},
913 {boost::none, "/example-schema:activeNumber"},
914 {boost::none, "/example-schema:activePort"},
915 {boost::none, "/example-schema:another-duration"},
916 {boost::none, "/example-schema:b"},
917 {boost::none, "/example-schema:b/b2"},
918 {boost::none, "/example-schema:b/b2/b3"},
919 {boost::none, "/example-schema:carry"},
920 {boost::none, "/example-schema:clockSpeed"},
921 {boost::none, "/example-schema:deprecatedLeaf"},
922 {boost::none, "/example-schema:direction"},
923 {boost::none, "/example-schema:duration"},
924 {boost::none, "/example-schema:foodDrinkIdentLeaf"},
925 {boost::none, "/example-schema:foodIdentLeaf"},
926 {boost::none, "/example-schema:interface/caseEthernet/ethernet"},
927 {boost::none, "/example-schema:interface/caseEthernet/ethernet/ip"},
928 {boost::none, "/example-schema:interface/caseLoopback/loopback"},
929 {boost::none, "/example-schema:interface/caseLoopback/loopback/ip"},
930 {boost::none, "/example-schema:interrupt"},
931 {boost::none, "/example-schema:leafBool"},
932 {boost::none, "/example-schema:leafDecimal"},
933 {boost::none, "/example-schema:leafEnum"},
934 {boost::none, "/example-schema:leafEnumTypedef"},
935 {boost::none, "/example-schema:leafEnumTypedefRestricted"},
936 {boost::none, "/example-schema:leafEnumTypedefRestricted2"},
937 {boost::none, "/example-schema:leafInt16"},
938 {boost::none, "/example-schema:leafInt32"},
939 {boost::none, "/example-schema:leafInt64"},
940 {boost::none, "/example-schema:leafInt8"},
941 {boost::none, "/example-schema:leafString"},
942 {boost::none, "/example-schema:leafUint16"},
943 {boost::none, "/example-schema:leafUint32"},
944 {boost::none, "/example-schema:leafUint64"},
945 {boost::none, "/example-schema:leafUint8"},
946 {boost::none, "/example-schema:length"},
947 {boost::none, "/example-schema:numberOrString"},
948 {boost::none, "/example-schema:obsoleteLeaf"},
949 {boost::none, "/example-schema:obsoleteLeafWithDeprecatedType"},
950 {boost::none, "/example-schema:obsoleteLeafWithObsoleteType"},
951 {boost::none, "/example-schema:pizzaIdentLeaf"},
952 {boost::none, "/example-schema:pizzaSize"},
953 {boost::none, "/example-schema:portMapping"},
954 {boost::none, "/example-schema:portMapping/port"},
955 {boost::none, "/example-schema:portSettings"},
956 {boost::none, "/example-schema:portSettings/port"},
957 {boost::none, "/example-schema:systemStats"},
958 {boost::none, "/example-schema:systemStats/upTime"},
959 {boost::none, "/example-schema:twoKeyList"},
960 {boost::none, "/example-schema:twoKeyList/name"},
961 {boost::none, "/example-schema:twoKeyList/number"},
962 {boost::none, "/example-schema:wavelength"},
963 {boost::none, "/example-schema:zero"}
Václav Kubernát3a823f42020-04-29 23:40:21 +0200964 };
965 }
Václav Kubernát3a823f42020-04-29 23:40:21 +0200966 SECTION("second-schema")
967 {
968 module = "second-schema";
969 expectedNonRecursive = {
Václav Kubernát95b08872020-04-28 01:04:17 +0200970 {"second-schema"s, "bla"}
Václav Kubernát3a823f42020-04-29 23:40:21 +0200971 };
972 expectedRecursive = {
Václav Kubernát95b08872020-04-28 01:04:17 +0200973 {boost::none, "/second-schema:bla"},
974 {boost::none, "/second-schema:bla/bla2"}
Václav Kubernát3a823f42020-04-29 23:40:21 +0200975 };
976 }
Václav Kubernát0d4db442018-07-18 17:18:43 +0200977
Václav Kubernát3a823f42020-04-29 23:40:21 +0200978 REQUIRE(ys.availableNodes(module_{module}, Recursion::NonRecursive) == expectedNonRecursive);
979 REQUIRE(ys.availableNodes(module_{module}, Recursion::Recursive) == expectedRecursive);
Václav Kubernát47a3f672019-11-08 15:42:43 +0100980 }
Václav Kubernát0d4db442018-07-18 17:18:43 +0200981 }
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100982 SECTION("nodeType")
983 {
984 yang::NodeTypes expected;
985 SECTION("leafInt32")
986 {
987 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, leaf_("leafInt32")));
988 expected = yang::NodeTypes::Leaf;
989 }
990
991 SECTION("a")
992 {
993 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, container_("a")));
994 expected = yang::NodeTypes::Container;
995 }
996
997 SECTION("a/a2/a3")
998 {
999 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, container_("a")));
1000 path.m_nodes.push_back(schemaNode_(container_("a2")));
1001 path.m_nodes.push_back(schemaNode_(container_("a3")));
1002 expected = yang::NodeTypes::PresenceContainer;
1003 }
1004
1005 SECTION("_list")
1006 {
1007 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, list_("_list")));
1008 expected = yang::NodeTypes::List;
1009 }
1010
1011 REQUIRE(ys.nodeType(pathToSchemaString(path, Prefixes::WhenNeeded)) == expected);
1012 }
Václav Kubernát1e09bd62020-02-17 15:13:38 +01001013
1014 SECTION("description")
1015 {
1016 std::optional<std::string> expected;
1017 SECTION("leafInt32")
1018 {
1019 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, leaf_("leafInt32")));
1020 expected = "A 32-bit integer leaf.";
1021 }
1022
1023 SECTION("leafString")
1024 {
1025 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, leaf_("leafString")));
1026 }
1027
Václav Kubernát2984f442020-02-20 17:43:35 +01001028 SECTION("numberOrString")
1029 {
1030 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, leaf_("numberOrString")));
1031 expected = "Can be an int32 or a string.";
1032 }
1033
Václav Kubernát1e09bd62020-02-17 15:13:38 +01001034 REQUIRE(ys.description(pathToSchemaString(path, Prefixes::WhenNeeded)) == expected);
1035 }
1036
Václav Kubernáta1c4c9e2020-04-22 00:37:52 +02001037 SECTION("status")
1038 {
1039 REQUIRE(ys.status("/example-schema:leafUint64") == yang::Status::Current);
1040 REQUIRE(ys.status("/example-schema:obsoleteLeaf") == yang::Status::Obsolete);
1041 REQUIRE(ys.status("/example-schema:deprecatedLeaf") == yang::Status::Deprecated);
1042 REQUIRE(ys.status("/example-schema:obsoleteLeafWithDeprecatedType") == yang::Status::Obsolete);
1043 REQUIRE(ys.status("/example-schema:obsoleteLeafWithObsoleteType") == yang::Status::Obsolete);
1044 }
1045
Václav Kubernát1e09bd62020-02-17 15:13:38 +01001046 SECTION("units")
1047 {
Václav Kubernát13b23d72020-04-16 21:49:51 +02001048 yang::LeafDataType expectedType;
1049 std::optional<std::string> expectedUnits;
Václav Kubernát1e09bd62020-02-17 15:13:38 +01001050 SECTION("length")
1051 {
1052 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, leaf_("length")));
Václav Kubernát13b23d72020-04-16 21:49:51 +02001053 expectedType.emplace<yang::Int32>();
1054 expectedUnits = "m";
Václav Kubernát1e09bd62020-02-17 15:13:38 +01001055 }
1056
1057 SECTION("wavelength")
1058 {
1059 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, leaf_("wavelength")));
Václav Kubernát13b23d72020-04-16 21:49:51 +02001060 expectedType.emplace<yang::Decimal>();
1061 expectedUnits = "nm";
Václav Kubernát1e09bd62020-02-17 15:13:38 +01001062 }
1063
1064 SECTION("leafInt32")
1065 {
1066 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, leaf_("leafInt32")));
Václav Kubernát13b23d72020-04-16 21:49:51 +02001067 expectedType.emplace<yang::Int32>();
Václav Kubernát1e09bd62020-02-17 15:13:38 +01001068 }
1069
1070 SECTION("duration")
1071 {
1072 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, leaf_("duration")));
Václav Kubernát13b23d72020-04-16 21:49:51 +02001073 expectedType.emplace<yang::Int32>();
1074 expectedUnits = "s";
Václav Kubernát1e09bd62020-02-17 15:13:38 +01001075 }
1076
1077 SECTION("another-duration")
1078 {
1079 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, leaf_("another-duration")));
Václav Kubernát13b23d72020-04-16 21:49:51 +02001080 expectedType.emplace<yang::Int32>();
1081 expectedUnits = "vt";
Václav Kubernát1e09bd62020-02-17 15:13:38 +01001082 }
Václav Kubernát13b23d72020-04-16 21:49:51 +02001083 REQUIRE(ys.leafType(pathToSchemaString(path, Prefixes::WhenNeeded)) == yang::TypeInfo{expectedType, expectedUnits});
Václav Kubernát1e09bd62020-02-17 15:13:38 +01001084 }
1085
1086 SECTION("nodeType")
1087 {
1088 yang::NodeTypes expected;
1089 SECTION("leafInt32")
1090 {
1091 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, leaf_("leafInt32")));
1092 expected = yang::NodeTypes::Leaf;
1093 }
1094
1095 SECTION("a")
1096 {
1097 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, container_("a")));
1098 expected = yang::NodeTypes::Container;
1099 }
1100
1101 SECTION("a/a2/a3")
1102 {
1103 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, container_("a")));
1104 path.m_nodes.push_back(schemaNode_(container_("a2")));
1105 path.m_nodes.push_back(schemaNode_(container_("a3")));
1106 expected = yang::NodeTypes::PresenceContainer;
1107 }
1108
1109 SECTION("_list")
1110 {
1111 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, list_("_list")));
1112 expected = yang::NodeTypes::List;
1113 }
1114
1115 REQUIRE(ys.nodeType(pathToSchemaString(path, Prefixes::WhenNeeded)) == expected);
1116 }
Václav Kubernátbd5e3c22020-02-19 15:22:00 +01001117
1118 SECTION("leafrefPath")
1119 {
1120 REQUIRE(ys.leafrefPath("/example-schema:activeNumber") == "/example-schema:_list/number");
1121 }
Václav Kubernát0599e9f2020-04-21 09:51:33 +02001122
1123 SECTION("isConfig")
1124 {
1125 REQUIRE(ys.isConfig("/example-schema:leafInt32"));
1126 REQUIRE_FALSE(ys.isConfig("/example-schema:clockSpeed"));
1127 REQUIRE_FALSE(ys.isConfig("/example-schema:systemStats"));
1128 REQUIRE_FALSE(ys.isConfig("/example-schema:systemStats/upTime"));
1129 }
Václav Kubernátbd0d5c82020-04-21 10:22:03 +02001130
Václav Kubernátb1a75c62020-04-21 15:20:16 +02001131 SECTION("defaultValue")
1132 {
1133 REQUIRE(ys.defaultValue("/example-schema:leafUint64") == "9001");
1134 REQUIRE(ys.defaultValue("/example-schema:leafEnumTypedefRestricted") == "data");
1135 REQUIRE(ys.defaultValue("/example-schema:leafInt32") == std::nullopt);
1136 }
1137
Václav Kubernátbd0d5c82020-04-21 10:22:03 +02001138 SECTION("moduleNodes")
1139 {
Václav Kubernátbd0d5c82020-04-21 10:22:03 +02001140 }
Václav Kubernát0d4db442018-07-18 17:18:43 +02001141 }
1142
1143 SECTION("negative")
1144 {
1145 SECTION("nonexistent nodes")
1146 {
1147 SECTION("example-schema:coze")
1148 {
1149 node.first = "example-schema";
1150 node.second = "coze";
1151 }
1152
1153 SECTION("example-schema:a/nevim")
1154 {
Václav Kubernát2eaceb82018-10-08 19:56:30 +02001155 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, container_("a")));
Václav Kubernát0d4db442018-07-18 17:18:43 +02001156 node.second = "nevim";
1157 }
1158
1159 SECTION("modul:a/nevim")
1160 {
Václav Kubernát2eaceb82018-10-08 19:56:30 +02001161 path.m_nodes.push_back(schemaNode_(module_{"modul"}, container_("a")));
Václav Kubernát0d4db442018-07-18 17:18:43 +02001162 node.second = "nevim";
1163 }
1164
1165 REQUIRE(!ys.isPresenceContainer(path, node));
1166 REQUIRE(!ys.isList(path, node));
1167 REQUIRE(!ys.isLeaf(path, node));
1168 REQUIRE(!ys.isContainer(path, node));
1169 }
1170
1171 SECTION("\"is\" methods return false for existing nodes for different nodetypes")
1172 {
1173 SECTION("example-schema:a")
1174 {
1175 node.first = "example-schema";
1176 node.second = "a";
1177 }
1178
1179 SECTION("example-schema:a/a2")
1180 {
Václav Kubernát2eaceb82018-10-08 19:56:30 +02001181 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, container_("a")));
Václav Kubernát0d4db442018-07-18 17:18:43 +02001182 node.second = "a2";
1183 }
1184
1185 REQUIRE(!ys.isPresenceContainer(path, node));
1186 REQUIRE(!ys.isList(path, node));
1187 REQUIRE(!ys.isLeaf(path, node));
1188 }
1189
1190 SECTION("nodetype-specific methods called with different nodetypes")
1191 {
Václav Kubernát2eaceb82018-10-08 19:56:30 +02001192 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, container_("a")));
Václav Kubernát0d4db442018-07-18 17:18:43 +02001193 node.second = "a2";
1194
Václav Kubernát0d4db442018-07-18 17:18:43 +02001195 REQUIRE(!ys.listHasKey(path, node, "chacha"));
1196 }
1197
1198 SECTION("nonexistent module")
1199 {
Václav Kubernát75877de2019-11-20 17:43:02 +01001200 REQUIRE(!ys.isModule("notAModule"));
Václav Kubernát0d4db442018-07-18 17:18:43 +02001201 }
Václav Kubernáteeb38842019-03-20 19:46:05 +01001202
Václav Kubernát7d82da72019-04-11 15:16:38 +02001203 SECTION("grouping is not a node")
1204 {
1205 SECTION("example-schema:arithmeticFlags")
1206 {
1207 node.first = "example-schema";
1208 node.second = "arithmeticFlags";
1209 }
1210
1211 SECTION("example-schema:flags")
1212 {
1213 node.first = "example-schema";
1214 node.second = "startAndStop";
1215 }
1216
1217 REQUIRE(!ys.isPresenceContainer(path, node));
1218 REQUIRE(!ys.isList(path, node));
1219 REQUIRE(!ys.isLeaf(path, node));
1220 REQUIRE(!ys.isContainer(path, node));
1221 }
Václav Kubernát280df4a2019-11-01 14:46:34 +01001222
1223 SECTION("choice is not a node")
1224 {
1225 SECTION("example-schema:interface")
1226 {
1227 node.first = "example-schema";
1228 node.second = "interface";
1229 }
1230
1231 REQUIRE(!ys.isPresenceContainer(path, node));
1232 REQUIRE(!ys.isList(path, node));
1233 REQUIRE(!ys.isLeaf(path, node));
1234 REQUIRE(!ys.isContainer(path, node));
1235 }
1236
1237 SECTION("case is not a node")
1238 {
1239 SECTION("example-schema:caseLoopback")
1240 {
1241 node.first = "example-schema";
1242 node.second = "caseLoopback";
1243 }
1244
1245 SECTION("example-schema:caseEthernet")
1246 {
1247 node.first = "example-schema";
1248 node.second = "caseEthernet";
1249 }
1250
1251 REQUIRE(!ys.isPresenceContainer(path, node));
1252 REQUIRE(!ys.isList(path, node));
1253 REQUIRE(!ys.isLeaf(path, node));
1254 REQUIRE(!ys.isContainer(path, node));
1255 }
Václav Kubernát0d4db442018-07-18 17:18:43 +02001256 }
1257}