blob: c3ceb255bc3382e9db97a045ec3b357df8ad0870 [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át3a823f42020-04-29 23:40:21 +0200787 std::set<std::string> set;
788
789 SECTION("<root>")
790 {
791 set = {"example-schema:a", "example-schema:b", "example-schema:leafString",
792 "example-schema:leafDecimal", "example-schema:leafBool",
793 "example-schema:leafInt8", "example-schema:leafUint8",
794 "example-schema:leafInt16", "example-schema:leafUint16",
795 "example-schema:leafInt32", "example-schema:leafUint32",
796 "example-schema:leafInt64", "example-schema:leafUint64",
797 "example-schema:leafEnum", "example-schema:leafEnumTypedef",
798 "example-schema:leafEnumTypedefRestricted", "example-schema:leafEnumTypedefRestricted2",
799 "example-schema:foodIdentLeaf", "example-schema:pizzaIdentLeaf", "example-schema:foodDrinkIdentLeaf",
800 "example-schema:_list", "example-schema:twoKeyList", "second-schema:bla",
801 "example-schema:carry", "example-schema:zero", "example-schema:direction",
802 "example-schema:interrupt",
803 "example-schema:ethernet", "example-schema:loopback",
804 "example-schema:pizzaSize",
805 "example-schema:length", "example-schema:wavelength",
806 "example-schema:duration", "example-schema:another-duration",
807 "example-schema:activeNumber",
808 "example-schema:numberOrString",
809 "example-schema:portSettings",
810 "example-schema:portMapping",
811 "example-schema:activeMappedPort",
812 "example-schema:activePort",
813 "example-schema:clockSpeed",
814 "example-schema:deprecatedLeaf",
815 "example-schema:obsoleteLeaf",
816 "example-schema:obsoleteLeafWithDeprecatedType",
817 "example-schema:obsoleteLeafWithObsoleteType",
818 "example-schema:systemStats"};
819 }
820
821 SECTION("example-schema:a")
822 {
823 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, container_("a")));
824 set = {"a2", "leafa", "second-schema:augmentedContainer"};
825 }
826
827 SECTION("second-schema:bla")
828 {
829 path.m_nodes.push_back(schemaNode_(module_{"second-schema"}, container_("bla")));
830 set = {"bla2"};
831 }
832
833 SECTION("example-schema:ethernet")
834 {
835 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, container_("ethernet")));
836 set = {"ip"};
837 }
838
839 REQUIRE(ys.availableNodes(path, Recursion::NonRecursive) == set);
Václav Kubernát0d4db442018-07-18 17:18:43 +0200840 }
841
Václav Kubernát3a823f42020-04-29 23:40:21 +0200842 SECTION("modules")
Václav Kubernát0d4db442018-07-18 17:18:43 +0200843 {
Václav Kubernát3a823f42020-04-29 23:40:21 +0200844 std::string module;
845 std::set<std::string> expectedNonRecursive;
846 std::set<std::string> expectedRecursive;
847 SECTION("example-schema")
848 {
849 module = "example-schema";
850 expectedNonRecursive = {
851 "example-schema:_list",
852 "example-schema:a",
853 "example-schema:activeMappedPort",
854 "example-schema:activeNumber",
855 "example-schema:activePort",
856 "example-schema:another-duration",
857 "example-schema:b",
858 "example-schema:carry",
859 "example-schema:clockSpeed",
860 "example-schema:deprecatedLeaf",
861 "example-schema:direction",
862 "example-schema:duration",
863 "example-schema:ethernet",
864 "example-schema:foodDrinkIdentLeaf",
865 "example-schema:foodIdentLeaf",
866 "example-schema:interrupt",
867 "example-schema:leafBool",
868 "example-schema:leafDecimal",
869 "example-schema:leafEnum",
870 "example-schema:leafEnumTypedef",
871 "example-schema:leafEnumTypedefRestricted",
872 "example-schema:leafEnumTypedefRestricted2",
873 "example-schema:leafInt16",
874 "example-schema:leafInt32",
875 "example-schema:leafInt64",
876 "example-schema:leafInt8",
877 "example-schema:leafString",
878 "example-schema:leafUint16",
879 "example-schema:leafUint32",
880 "example-schema:leafUint64",
881 "example-schema:leafUint8",
882 "example-schema:length",
883 "example-schema:loopback",
884 "example-schema:numberOrString",
885 "example-schema:obsoleteLeaf",
886 "example-schema:obsoleteLeafWithDeprecatedType",
887 "example-schema:obsoleteLeafWithObsoleteType",
888 "example-schema:pizzaIdentLeaf",
889 "example-schema:pizzaSize",
890 "example-schema:portMapping",
891 "example-schema:portSettings",
892 "example-schema:systemStats",
893 "example-schema:twoKeyList",
894 "example-schema:wavelength",
895 "example-schema:zero"
896 };
897 expectedRecursive = {
898 "/example-schema:_list",
899 "/example-schema:_list/contInList",
900 "/example-schema:_list/number",
901 "/example-schema:a",
902 "/example-schema:a/a2",
903 "/example-schema:a/a2/a3",
904 "/example-schema:a/leafa",
905 "/example-schema:a/second-schema:augmentedContainer",
906 "/example-schema:activeMappedPort",
907 "/example-schema:activeNumber",
908 "/example-schema:activePort",
909 "/example-schema:another-duration",
910 "/example-schema:b",
911 "/example-schema:b/b2",
912 "/example-schema:b/b2/b3",
913 "/example-schema:carry",
914 "/example-schema:clockSpeed",
915 "/example-schema:deprecatedLeaf",
916 "/example-schema:direction",
917 "/example-schema:duration",
918 "/example-schema:foodDrinkIdentLeaf",
919 "/example-schema:foodIdentLeaf",
920 "/example-schema:interface/caseEthernet/ethernet",
921 "/example-schema:interface/caseEthernet/ethernet/ip",
922 "/example-schema:interface/caseLoopback/loopback",
923 "/example-schema:interface/caseLoopback/loopback/ip",
924 "/example-schema:interrupt",
925 "/example-schema:leafBool",
926 "/example-schema:leafDecimal",
927 "/example-schema:leafEnum",
928 "/example-schema:leafEnumTypedef",
929 "/example-schema:leafEnumTypedefRestricted",
930 "/example-schema:leafEnumTypedefRestricted2",
931 "/example-schema:leafInt16",
932 "/example-schema:leafInt32",
933 "/example-schema:leafInt64",
934 "/example-schema:leafInt8",
935 "/example-schema:leafString",
936 "/example-schema:leafUint16",
937 "/example-schema:leafUint32",
938 "/example-schema:leafUint64",
939 "/example-schema:leafUint8",
940 "/example-schema:length",
941 "/example-schema:numberOrString",
942 "/example-schema:obsoleteLeaf",
943 "/example-schema:obsoleteLeafWithDeprecatedType",
944 "/example-schema:obsoleteLeafWithObsoleteType",
945 "/example-schema:pizzaIdentLeaf",
946 "/example-schema:pizzaSize",
947 "/example-schema:portMapping",
948 "/example-schema:portMapping/port",
949 "/example-schema:portSettings",
950 "/example-schema:portSettings/port",
951 "/example-schema:systemStats",
952 "/example-schema:systemStats/upTime",
953 "/example-schema:twoKeyList",
954 "/example-schema:twoKeyList/name",
955 "/example-schema:twoKeyList/number",
956 "/example-schema:wavelength",
957 "/example-schema:zero"
958 };
959 }
Václav Kubernát4f77a252019-02-19 16:51:30 +0100960
Václav Kubernát3a823f42020-04-29 23:40:21 +0200961 SECTION("second-schema")
962 {
963 module = "second-schema";
964 expectedNonRecursive = {
965 "second-schema:bla"
966 };
967 expectedRecursive = {
968 "/second-schema:bla", "/second-schema:bla/bla2"
969 };
970 }
Václav Kubernát0d4db442018-07-18 17:18:43 +0200971
Václav Kubernát3a823f42020-04-29 23:40:21 +0200972 REQUIRE(ys.availableNodes(module_{module}, Recursion::NonRecursive) == expectedNonRecursive);
973 REQUIRE(ys.availableNodes(module_{module}, Recursion::Recursive) == expectedRecursive);
Václav Kubernát47a3f672019-11-08 15:42:43 +0100974 }
Václav Kubernát0d4db442018-07-18 17:18:43 +0200975 }
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100976 SECTION("nodeType")
977 {
978 yang::NodeTypes expected;
979 SECTION("leafInt32")
980 {
981 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, leaf_("leafInt32")));
982 expected = yang::NodeTypes::Leaf;
983 }
984
985 SECTION("a")
986 {
987 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, container_("a")));
988 expected = yang::NodeTypes::Container;
989 }
990
991 SECTION("a/a2/a3")
992 {
993 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, container_("a")));
994 path.m_nodes.push_back(schemaNode_(container_("a2")));
995 path.m_nodes.push_back(schemaNode_(container_("a3")));
996 expected = yang::NodeTypes::PresenceContainer;
997 }
998
999 SECTION("_list")
1000 {
1001 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, list_("_list")));
1002 expected = yang::NodeTypes::List;
1003 }
1004
1005 REQUIRE(ys.nodeType(pathToSchemaString(path, Prefixes::WhenNeeded)) == expected);
1006 }
Václav Kubernát1e09bd62020-02-17 15:13:38 +01001007
1008 SECTION("description")
1009 {
1010 std::optional<std::string> expected;
1011 SECTION("leafInt32")
1012 {
1013 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, leaf_("leafInt32")));
1014 expected = "A 32-bit integer leaf.";
1015 }
1016
1017 SECTION("leafString")
1018 {
1019 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, leaf_("leafString")));
1020 }
1021
Václav Kubernát2984f442020-02-20 17:43:35 +01001022 SECTION("numberOrString")
1023 {
1024 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, leaf_("numberOrString")));
1025 expected = "Can be an int32 or a string.";
1026 }
1027
Václav Kubernát1e09bd62020-02-17 15:13:38 +01001028 REQUIRE(ys.description(pathToSchemaString(path, Prefixes::WhenNeeded)) == expected);
1029 }
1030
Václav Kubernáta1c4c9e2020-04-22 00:37:52 +02001031 SECTION("status")
1032 {
1033 REQUIRE(ys.status("/example-schema:leafUint64") == yang::Status::Current);
1034 REQUIRE(ys.status("/example-schema:obsoleteLeaf") == yang::Status::Obsolete);
1035 REQUIRE(ys.status("/example-schema:deprecatedLeaf") == yang::Status::Deprecated);
1036 REQUIRE(ys.status("/example-schema:obsoleteLeafWithDeprecatedType") == yang::Status::Obsolete);
1037 REQUIRE(ys.status("/example-schema:obsoleteLeafWithObsoleteType") == yang::Status::Obsolete);
1038 }
1039
Václav Kubernát1e09bd62020-02-17 15:13:38 +01001040 SECTION("units")
1041 {
Václav Kubernát13b23d72020-04-16 21:49:51 +02001042 yang::LeafDataType expectedType;
1043 std::optional<std::string> expectedUnits;
Václav Kubernát1e09bd62020-02-17 15:13:38 +01001044 SECTION("length")
1045 {
1046 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, leaf_("length")));
Václav Kubernát13b23d72020-04-16 21:49:51 +02001047 expectedType.emplace<yang::Int32>();
1048 expectedUnits = "m";
Václav Kubernát1e09bd62020-02-17 15:13:38 +01001049 }
1050
1051 SECTION("wavelength")
1052 {
1053 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, leaf_("wavelength")));
Václav Kubernát13b23d72020-04-16 21:49:51 +02001054 expectedType.emplace<yang::Decimal>();
1055 expectedUnits = "nm";
Václav Kubernát1e09bd62020-02-17 15:13:38 +01001056 }
1057
1058 SECTION("leafInt32")
1059 {
1060 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, leaf_("leafInt32")));
Václav Kubernát13b23d72020-04-16 21:49:51 +02001061 expectedType.emplace<yang::Int32>();
Václav Kubernát1e09bd62020-02-17 15:13:38 +01001062 }
1063
1064 SECTION("duration")
1065 {
1066 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, leaf_("duration")));
Václav Kubernát13b23d72020-04-16 21:49:51 +02001067 expectedType.emplace<yang::Int32>();
1068 expectedUnits = "s";
Václav Kubernát1e09bd62020-02-17 15:13:38 +01001069 }
1070
1071 SECTION("another-duration")
1072 {
1073 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, leaf_("another-duration")));
Václav Kubernát13b23d72020-04-16 21:49:51 +02001074 expectedType.emplace<yang::Int32>();
1075 expectedUnits = "vt";
Václav Kubernát1e09bd62020-02-17 15:13:38 +01001076 }
Václav Kubernát13b23d72020-04-16 21:49:51 +02001077 REQUIRE(ys.leafType(pathToSchemaString(path, Prefixes::WhenNeeded)) == yang::TypeInfo{expectedType, expectedUnits});
Václav Kubernát1e09bd62020-02-17 15:13:38 +01001078 }
1079
1080 SECTION("nodeType")
1081 {
1082 yang::NodeTypes expected;
1083 SECTION("leafInt32")
1084 {
1085 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, leaf_("leafInt32")));
1086 expected = yang::NodeTypes::Leaf;
1087 }
1088
1089 SECTION("a")
1090 {
1091 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, container_("a")));
1092 expected = yang::NodeTypes::Container;
1093 }
1094
1095 SECTION("a/a2/a3")
1096 {
1097 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, container_("a")));
1098 path.m_nodes.push_back(schemaNode_(container_("a2")));
1099 path.m_nodes.push_back(schemaNode_(container_("a3")));
1100 expected = yang::NodeTypes::PresenceContainer;
1101 }
1102
1103 SECTION("_list")
1104 {
1105 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, list_("_list")));
1106 expected = yang::NodeTypes::List;
1107 }
1108
1109 REQUIRE(ys.nodeType(pathToSchemaString(path, Prefixes::WhenNeeded)) == expected);
1110 }
Václav Kubernátbd5e3c22020-02-19 15:22:00 +01001111
1112 SECTION("leafrefPath")
1113 {
1114 REQUIRE(ys.leafrefPath("/example-schema:activeNumber") == "/example-schema:_list/number");
1115 }
Václav Kubernát0599e9f2020-04-21 09:51:33 +02001116
1117 SECTION("isConfig")
1118 {
1119 REQUIRE(ys.isConfig("/example-schema:leafInt32"));
1120 REQUIRE_FALSE(ys.isConfig("/example-schema:clockSpeed"));
1121 REQUIRE_FALSE(ys.isConfig("/example-schema:systemStats"));
1122 REQUIRE_FALSE(ys.isConfig("/example-schema:systemStats/upTime"));
1123 }
Václav Kubernátbd0d5c82020-04-21 10:22:03 +02001124
Václav Kubernátb1a75c62020-04-21 15:20:16 +02001125 SECTION("defaultValue")
1126 {
1127 REQUIRE(ys.defaultValue("/example-schema:leafUint64") == "9001");
1128 REQUIRE(ys.defaultValue("/example-schema:leafEnumTypedefRestricted") == "data");
1129 REQUIRE(ys.defaultValue("/example-schema:leafInt32") == std::nullopt);
1130 }
1131
Václav Kubernátbd0d5c82020-04-21 10:22:03 +02001132 SECTION("moduleNodes")
1133 {
Václav Kubernátbd0d5c82020-04-21 10:22:03 +02001134 }
Václav Kubernát0d4db442018-07-18 17:18:43 +02001135 }
1136
1137 SECTION("negative")
1138 {
1139 SECTION("nonexistent nodes")
1140 {
1141 SECTION("example-schema:coze")
1142 {
1143 node.first = "example-schema";
1144 node.second = "coze";
1145 }
1146
1147 SECTION("example-schema:a/nevim")
1148 {
Václav Kubernát2eaceb82018-10-08 19:56:30 +02001149 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, container_("a")));
Václav Kubernát0d4db442018-07-18 17:18:43 +02001150 node.second = "nevim";
1151 }
1152
1153 SECTION("modul:a/nevim")
1154 {
Václav Kubernát2eaceb82018-10-08 19:56:30 +02001155 path.m_nodes.push_back(schemaNode_(module_{"modul"}, container_("a")));
Václav Kubernát0d4db442018-07-18 17:18:43 +02001156 node.second = "nevim";
1157 }
1158
1159 REQUIRE(!ys.isPresenceContainer(path, node));
1160 REQUIRE(!ys.isList(path, node));
1161 REQUIRE(!ys.isLeaf(path, node));
1162 REQUIRE(!ys.isContainer(path, node));
1163 }
1164
1165 SECTION("\"is\" methods return false for existing nodes for different nodetypes")
1166 {
1167 SECTION("example-schema:a")
1168 {
1169 node.first = "example-schema";
1170 node.second = "a";
1171 }
1172
1173 SECTION("example-schema:a/a2")
1174 {
Václav Kubernát2eaceb82018-10-08 19:56:30 +02001175 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, container_("a")));
Václav Kubernát0d4db442018-07-18 17:18:43 +02001176 node.second = "a2";
1177 }
1178
1179 REQUIRE(!ys.isPresenceContainer(path, node));
1180 REQUIRE(!ys.isList(path, node));
1181 REQUIRE(!ys.isLeaf(path, node));
1182 }
1183
1184 SECTION("nodetype-specific methods called with different nodetypes")
1185 {
Václav Kubernát2eaceb82018-10-08 19:56:30 +02001186 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, container_("a")));
Václav Kubernát0d4db442018-07-18 17:18:43 +02001187 node.second = "a2";
1188
Václav Kubernát0d4db442018-07-18 17:18:43 +02001189 REQUIRE(!ys.listHasKey(path, node, "chacha"));
1190 }
1191
1192 SECTION("nonexistent module")
1193 {
Václav Kubernát75877de2019-11-20 17:43:02 +01001194 REQUIRE(!ys.isModule("notAModule"));
Václav Kubernát0d4db442018-07-18 17:18:43 +02001195 }
Václav Kubernáteeb38842019-03-20 19:46:05 +01001196
Václav Kubernát7d82da72019-04-11 15:16:38 +02001197 SECTION("grouping is not a node")
1198 {
1199 SECTION("example-schema:arithmeticFlags")
1200 {
1201 node.first = "example-schema";
1202 node.second = "arithmeticFlags";
1203 }
1204
1205 SECTION("example-schema:flags")
1206 {
1207 node.first = "example-schema";
1208 node.second = "startAndStop";
1209 }
1210
1211 REQUIRE(!ys.isPresenceContainer(path, node));
1212 REQUIRE(!ys.isList(path, node));
1213 REQUIRE(!ys.isLeaf(path, node));
1214 REQUIRE(!ys.isContainer(path, node));
1215 }
Václav Kubernát280df4a2019-11-01 14:46:34 +01001216
1217 SECTION("choice is not a node")
1218 {
1219 SECTION("example-schema:interface")
1220 {
1221 node.first = "example-schema";
1222 node.second = "interface";
1223 }
1224
1225 REQUIRE(!ys.isPresenceContainer(path, node));
1226 REQUIRE(!ys.isList(path, node));
1227 REQUIRE(!ys.isLeaf(path, node));
1228 REQUIRE(!ys.isContainer(path, node));
1229 }
1230
1231 SECTION("case is not a node")
1232 {
1233 SECTION("example-schema:caseLoopback")
1234 {
1235 node.first = "example-schema";
1236 node.second = "caseLoopback";
1237 }
1238
1239 SECTION("example-schema:caseEthernet")
1240 {
1241 node.first = "example-schema";
1242 node.second = "caseEthernet";
1243 }
1244
1245 REQUIRE(!ys.isPresenceContainer(path, node));
1246 REQUIRE(!ys.isList(path, node));
1247 REQUIRE(!ys.isLeaf(path, node));
1248 REQUIRE(!ys.isContainer(path, node));
1249 }
Václav Kubernát0d4db442018-07-18 17:18:43 +02001250 }
1251}