blob: 0cda36d7fd3fb064526650714653877a09f5e2b1 [file] [log] [blame]
aPiecek20ddf8b2021-01-08 11:50:37 +01001/*
2 * @file test_printer_tree.c
3 * @author: Adam Piecek <piecek@cesnet.cz>
4 * @brief unit tests for functions from printer_tree.c
5 *
6 * Copyright (c) 2019-2021 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14#define _UTEST_MAIN_
15#include "utests.h"
16
17#include "common.h"
18#include "context.h"
19#include "out.h"
20#include "printer_schema.h"
21#include "tree_schema.h"
22
23#define TEST_LOCAL_SETUP \
24 char *printed; \
aPiecek20ddf8b2021-01-08 11:50:37 +010025 const struct lys_module *mod; \
26 const char *orig; \
aPieceke376b922021-04-19 08:08:14 +020027 const char *expect; \
28 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +010029
aPieceke376b922021-04-19 08:08:14 +020030#define TEST_LOCAL_PRINT(MOD, LINE_LENGTH) \
31 assert_int_equal(LY_SUCCESS, lys_print_module(UTEST_OUT, MOD, LYS_OUT_TREE, LINE_LENGTH, 0));
aPiecek20ddf8b2021-01-08 11:50:37 +010032
33#define TEST_LOCAL_TEARDOWN \
aPieceke376b922021-04-19 08:08:14 +020034 ly_out_free(UTEST_OUT, NULL, 1);
aPiecek20ddf8b2021-01-08 11:50:37 +010035
36static void
37base_sections(void **state)
38{
39 TEST_LOCAL_SETUP;
aPiecekdc8fd572021-04-19 10:47:23 +020040
41 orig =
42 "module a01xx {\n"
43 " yang-version 1.1;\n"
44 " namespace \"xx:y\";\n"
45 " prefix xx;\n"
46 " container c;\n"
47 " container d;\n"
48 "}\n";
49
50 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
51
52 /* module with import statement */
aPiecek20ddf8b2021-01-08 11:50:37 +010053 orig =
54 "module a01 {\n"
55 " yang-version 1.1;\n"
56 " namespace \"x:y\";\n"
57 " prefix x;\n"
58 "\n"
aPiecekdc8fd572021-04-19 10:47:23 +020059 " import a01xx {\n"
60 " prefix xx;\n"
61 " }\n"
62 "\n"
aPiecek20ddf8b2021-01-08 11:50:37 +010063 " grouping g1;\n"
64 "\n"
65 " grouping g2;\n"
aPiecekdc8fd572021-04-19 10:47:23 +020066 " container g;\n"
67 " augment \"/xx:c\" {\n"
68 " container e;\n"
aPiecek20ddf8b2021-01-08 11:50:37 +010069 " }\n"
aPiecekdc8fd572021-04-19 10:47:23 +020070 " augment \"/xx:d\" {\n"
71 " container f;\n"
aPiecek20ddf8b2021-01-08 11:50:37 +010072 " }\n"
73 " rpc rpc1;\n"
74 " rpc rpc2;\n"
75 " notification n1;\n"
76 " notification n2;\n"
77 "}\n";
78
aPiecekdc8fd572021-04-19 10:47:23 +020079 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
80
81 /* from pyang */
aPiecek20ddf8b2021-01-08 11:50:37 +010082 expect =
83 "module: a01\n"
aPiecekdc8fd572021-04-19 10:47:23 +020084 " +--rw g\n"
85 "\n"
86 " augment /xx:c:\n"
87 " +--rw e\n"
88 " augment /xx:d:\n"
89 " +--rw f\n"
aPiecek20ddf8b2021-01-08 11:50:37 +010090 "\n"
91 " rpcs:\n"
92 " +---x rpc1\n"
93 " +---x rpc2\n"
94 "\n"
95 " notifications:\n"
96 " +---n n1\n"
aPiecekdc8fd572021-04-19 10:47:23 +020097 " +---n n2\n"
98 "\n"
99 " grouping g1\n"
100 " grouping g2\n";
aPiecek20ddf8b2021-01-08 11:50:37 +0100101
aPieceke376b922021-04-19 08:08:14 +0200102 TEST_LOCAL_PRINT(mod, 72);
103 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100104 assert_string_equal(printed, expect);
105 TEST_LOCAL_TEARDOWN;
106}
107
108static void
109node_status(void **state)
110{
111 TEST_LOCAL_SETUP;
112 orig =
113 "module a02 {\n"
114 " yang-version 1.1;\n"
115 " namespace \"x:y\";\n"
116 " prefix x;\n"
117 " container l {\n"
118 " status current;\n"
119 " }\n"
120 " container m {\n"
121 " status deprecated;\n"
122 " }\n"
123 " container n {\n"
124 " status obsolete;\n"
125 " }\n"
126 "}\n";
127
128 /* from pyang */
129 expect =
130 "module: a02\n"
131 " +--rw l\n"
132 " x--rw m\n"
133 " o--rw n\n";
134
aPieceke376b922021-04-19 08:08:14 +0200135 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
136 TEST_LOCAL_PRINT(mod, 72);
137 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100138 assert_string_equal(printed, expect);
139 TEST_LOCAL_TEARDOWN;
140}
141
142static void
143node_config_flags(void **state)
144{
145 TEST_LOCAL_SETUP;
146 orig =
147 "module a03 {\n"
148 " yang-version 1.1;\n"
149 " namespace \"x:y\";\n"
150 " prefix x;\n"
151 " container l {\n"
152 " config true;\n"
153 " }\n"
154 " container m {\n"
155 " config false;\n"
156 " }\n"
157 "}\n";
158
159 /* from pyang */
160 expect =
161 "module: a03\n"
162 " +--rw l\n"
163 " +--ro m\n";
164
aPieceke376b922021-04-19 08:08:14 +0200165 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
166 TEST_LOCAL_PRINT(mod, 72);
167 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100168 assert_string_equal(printed, expect);
169 TEST_LOCAL_TEARDOWN;
170}
171
172static void
173node_rpcs_flags(void **state)
174{
175 TEST_LOCAL_SETUP;
176 orig =
177 "module a04 {\n"
178 " yang-version 1.1;\n"
179 " namespace \"x:y\";\n"
180 " prefix x;\n"
181 " container cont {\n"
182 " action rpc1 {\n"
183 "\n"
184 " input {\n"
185 " leaf in {\n"
186 " type string;\n"
187 " }\n"
188 " }\n"
189 " }\n"
190 " }\n"
191 "}\n";
192
193 /* from pyang */
194 expect =
195 "module: a04\n"
196 " +--rw cont\n"
197 " +---x rpc1\n"
198 " +---w input\n"
199 " +---w in? string\n";
200
aPieceke376b922021-04-19 08:08:14 +0200201 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
202 TEST_LOCAL_PRINT(mod, 72);
203 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100204 assert_string_equal(printed, expect);
205 TEST_LOCAL_TEARDOWN;
206}
207
208static void
209node_grouping_flags(void **state)
210{
211 TEST_LOCAL_SETUP;
212 orig =
213 "module a05 {\n"
214 " yang-version 1.1;\n"
215 " namespace \"x:y\";\n"
216 " prefix x;\n"
217 "\n"
218 " grouping g {\n"
aPiecekdc8fd572021-04-19 10:47:23 +0200219 " leaf a {\n"
220 " type string;\n"
221 " config true;\n"
222 " }\n"
223 " leaf b {\n"
224 " type string;\n"
225 " config false;\n"
226 " }\n"
227 " leaf c {\n"
228 " type string;\n"
229 " }\n"
230 " container d {\n"
231 " config false;\n"
232 " leaf e {\n"
233 " type string;\n"
234 " }\n"
235 " }\n"
236 " container f {\n"
237 " leaf g {\n"
238 " type string;\n"
239 " }\n"
240 " }\n"
aPiecek20ddf8b2021-01-08 11:50:37 +0100241 " }\n"
242 " container d {\n"
243 " uses g;\n"
244 " }\n"
245 "}\n";
246
aPiecekdc8fd572021-04-19 10:47:23 +0200247 /* from yanglint1 */
aPiecek20ddf8b2021-01-08 11:50:37 +0100248 expect =
249 "module: a05\n"
250 " +--rw d\n"
251 " +---u g\n"
252 "\n"
253 " grouping g:\n"
aPiecekdc8fd572021-04-19 10:47:23 +0200254 " +--rw a? string\n"
255 " +--ro b? string\n"
256 " +---- c? string\n"
257 " +--ro d\n"
258 " | +--ro e? string\n"
259 " +---- f\n"
260 " +---- g? string\n";
261
aPieceke376b922021-04-19 08:08:14 +0200262 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
263 TEST_LOCAL_PRINT(mod, 72);
264 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100265 assert_string_equal(printed, expect);
266 TEST_LOCAL_TEARDOWN;
267}
268
269static void
270notif_inside_container(void **state)
271{
272 TEST_LOCAL_SETUP;
273 orig =
274 "module a06 {\n"
275 " yang-version 1.1;\n"
276 " namespace \"x:y\";\n"
277 " prefix x;\n"
278 " container c {\n"
279 " notification notif;\n"
280 " }\n"
281 "}\n";
282
283 /* from pyang */
284 expect =
285 "module: a06\n"
286 " +--rw c\n"
287 " +---n notif\n";
288
aPieceke376b922021-04-19 08:08:14 +0200289 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
290 TEST_LOCAL_PRINT(mod, 72);
291 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100292 assert_string_equal(printed, expect);
293 TEST_LOCAL_TEARDOWN;
294}
295
296static void
297node_choice(void **state)
298{
299 TEST_LOCAL_SETUP;
300 orig =
301 "module a07 {\n"
302 " yang-version 1.1;\n"
303 " namespace \"x:y\";\n"
304 " prefix x;\n"
305 " choice my_choice;\n"
306 "}\n";
307
308 /* from pyang */
309 expect =
310 "module: a07\n"
311 " +--rw (my_choice)?\n";
312
aPieceke376b922021-04-19 08:08:14 +0200313 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
314 TEST_LOCAL_PRINT(mod, 72);
315 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100316 assert_string_equal(printed, expect);
317 TEST_LOCAL_TEARDOWN;
318}
319
320static void
321node_case(void **state)
322{
323 TEST_LOCAL_SETUP;
324 orig =
325 "module a08 {\n"
326 " yang-version 1.1;\n"
327 " namespace \"x:y\";\n"
328 " prefix x;\n"
329 " choice my_choice {\n"
330 " case my_case;\n"
331 " }\n"
332 "}\n";
333
334 /* from pyang */
335 expect =
336 "module: a08\n"
337 " +--rw (my_choice)?\n"
338 " +--:(my_case)\n";
339
aPieceke376b922021-04-19 08:08:14 +0200340 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
341 TEST_LOCAL_PRINT(mod, 72);
342 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100343 assert_string_equal(printed, expect);
344 TEST_LOCAL_TEARDOWN;
345}
346
347static void
348optional_opts(void **state)
349{
350 TEST_LOCAL_SETUP;
351 /* throws libyang warn: Use of anydata to define configuration data is not recommended... */
352 orig =
353 "module a09 {\n"
354 " yang-version 1.1;\n"
355 " namespace \"x:y\";\n"
356 " prefix x;\n"
357 " leaf l1 {\n"
358 " type string;\n"
359 " mandatory true;\n"
360 " }\n"
361 " leaf l2 {\n"
362 " type string;\n"
363 " mandatory false;\n"
364 " }\n"
365 " choice c1 {\n"
366 " mandatory true;\n"
367 " }\n"
368 " choice c2 {\n"
369 " mandatory false;\n"
370 " }\n"
371 " anydata a1 {\n"
372 " mandatory true;\n"
373 " }\n"
374 " anydata a2 {\n"
375 " mandatory false;\n"
376 " }\n"
377 " anyxml x1 {\n"
378 " mandatory true;\n"
379 " }\n"
380 " anyxml x2 {\n"
381 " mandatory false;\n"
382 " }\n"
383 "}\n";
384
385 /* from yanglint 1 */
386 expect =
387 "module: a09\n"
388 " +--rw l1 string\n"
389 " +--rw l2? string\n"
390 " +--rw (c1)\n"
391 " +--rw (c2)?\n"
392 " +--rw a1 anydata\n"
393 " +--rw a2? anydata\n"
394 " +--rw x1 anyxml\n"
395 " +--rw x2? anyxml\n";
396
aPieceke376b922021-04-19 08:08:14 +0200397 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
398 TEST_LOCAL_PRINT(mod, 72);
399 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100400 assert_string_equal(printed, expect);
401 TEST_LOCAL_TEARDOWN;
402}
403
404static void
405presence_container(void **state)
406{
407 TEST_LOCAL_SETUP;
408 orig =
409 "module a10 {\n"
410 " yang-version 1.1;\n"
411 " namespace \"x:y\";\n"
412 " prefix x;\n"
413 " container c;\n"
414 " container d {\n"
415 " presence \"str1\";\n"
416 " }\n"
417 "}\n";
418
419 /* from pyang */
420 expect =
421 "module: a10\n"
422 " +--rw c\n"
423 " +--rw d!\n";
424
aPieceke376b922021-04-19 08:08:14 +0200425 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
426 TEST_LOCAL_PRINT(mod, 72);
427 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100428 assert_string_equal(printed, expect);
429 TEST_LOCAL_TEARDOWN;
430}
431
432static void
433node_keys(void **state)
434{
435 TEST_LOCAL_SETUP;
436 orig =
437 "module a11 {\n"
438 " yang-version 1.1;\n"
439 " namespace \"x:y\";\n"
440 " prefix x;\n"
441 " list l1 {\n"
442 " key \"a\";\n"
443 " leaf a {\n"
444 " type string;\n"
445 " }\n"
446 " }\n"
447 " list l2 {\n"
448 " key \"a b\";\n"
449 " leaf a {\n"
450 " type string;\n"
451 " }\n"
452 " leaf b {\n"
453 " type string;\n"
454 " }\n"
455 " }\n"
456 " leaf-list ll {\n"
457 " type string;\n"
458 " }\n"
459 "}\n";
460
461 /* from pyang */
462 expect =
463 "module: a11\n"
464 " +--rw l1* [a]\n"
465 " | +--rw a string\n"
466 " +--rw l2* [a b]\n"
467 " | +--rw a string\n"
468 " | +--rw b string\n"
469 " +--rw ll* string\n";
470
aPieceke376b922021-04-19 08:08:14 +0200471 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
472 TEST_LOCAL_PRINT(mod, 72);
473 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100474 assert_string_equal(printed, expect);
475 TEST_LOCAL_TEARDOWN;
476}
477
478static void
479node_type_target(void **state)
480{
481 TEST_LOCAL_SETUP;
482 orig =
483 "module a12 {\n"
484 " yang-version 1.1;\n"
485 " namespace \"x:y\";\n"
486 " prefix x;\n"
487 " leaf a {\n"
488 " type leafref {\n"
489 " path \"/x:b\";\n"
490 " }\n"
491 " }\n"
492 " leaf b {\n"
493 " type string;\n"
494 " }\n"
495 "}\n";
496
497 /* from yanglint 1 */
498 expect =
499 "module: a12\n"
500 " +--rw a? -> /x:b\n"
501 " +--rw b? string\n";
502
aPieceke376b922021-04-19 08:08:14 +0200503 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
504 TEST_LOCAL_PRINT(mod, 72);
505 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100506 assert_string_equal(printed, expect);
507 TEST_LOCAL_TEARDOWN;
508}
509
510static void
511node_type_leafref(void **state)
512{
513 TEST_LOCAL_SETUP;
514 orig =
515 "module a13 {\n"
516 " yang-version 1.1;\n"
517 " namespace \"x:y\";\n"
518 " prefix x;\n"
519 " leaf pretty-long-identifier-name-which-should-exceed-the-limit-of-72-characters {\n"
520 " type string;\n"
521 " }\n"
522 " leaf a {\n"
523 " type leafref {\n"
524 " path \"/x:pretty-long-identifier-name-which-should-exceed-the-limit-of-72-characters\";\n"
525 " }\n"
526 " }\n"
527 "}\n";
528
529 /* yanglint --tree-no-leafref-target --tree-line-length=72 */
530 expect =
531 "module: a13\n"
532 " +--rw pretty-long-identifier-name-which-should-exceed-the-limit-of-72-characters?\n"
533 " | string\n"
534 " +--rw a?\n"
535 " leafref\n";
536
aPieceke376b922021-04-19 08:08:14 +0200537 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
538 TEST_LOCAL_PRINT(mod, 72);
539 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100540 assert_string_equal(printed, expect);
541 TEST_LOCAL_TEARDOWN;
542}
543
544static void
545node_iffeatures(void **state)
546{
547 TEST_LOCAL_SETUP;
548 orig =
549 "module a14 {\n"
550 " yang-version 1.1;\n"
551 " namespace \"x:y\";\n"
552 " prefix x;\n"
553 "\n"
554 " feature foo;\n"
555 "\n"
556 " feature bar;\n"
557 " container c {\n"
558 " if-feature \"foo or bar\";\n"
559 " }\n"
560 "}\n";
561
562 /* from pyang */
563 expect =
564 "module: a14\n"
565 " +--rw c {foo or bar}?\n";
566
aPieceke376b922021-04-19 08:08:14 +0200567 const char *feats[] = {"foo", NULL};
568
569 UTEST_ADD_MODULE(orig, LYS_IN_YANG, feats, &mod);
570 TEST_LOCAL_PRINT(mod, 72);
571 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100572 assert_string_equal(printed, expect);
573 TEST_LOCAL_TEARDOWN;
574}
575
576static void
577indent_wrapper(void **state)
578{
579 TEST_LOCAL_SETUP;
580 orig =
581 "module a15 {\n"
582 " yang-version 1.1;\n"
583 " namespace \"x:y\";\n"
584 " prefix x;\n"
585 " container a {\n"
586 " container b;\n"
587 " }\n"
588 " container c {\n"
589 " container d {\n"
590 " container e;\n"
591 " }\n"
592 " container f {\n"
593 " container g;\n"
594 " }\n"
595 " }\n"
596 " container h;\n"
597 " container i {\n"
598 " container j;\n"
599 " container k;\n"
600 " }\n"
601 "}\n";
602
603 /* from pyang */
604 expect =
605 "module: a15\n"
606 " +--rw a\n"
607 " | +--rw b\n"
608 " +--rw c\n"
609 " | +--rw d\n"
610 " | | +--rw e\n"
611 " | +--rw f\n"
612 " | +--rw g\n"
613 " +--rw h\n"
614 " +--rw i\n"
615 " +--rw j\n"
616 " +--rw k\n";
617
aPieceke376b922021-04-19 08:08:14 +0200618 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
619 TEST_LOCAL_PRINT(mod, 72);
620 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100621 assert_string_equal(printed, expect);
622 TEST_LOCAL_TEARDOWN;
623}
624
625static void
626line_length_twiddling(void **state)
627{
628 TEST_LOCAL_SETUP;
629 /* node_fits_tight */
630
631 orig =
632 "module a16 {\n"
633 " yang-version 1.1;\n"
634 " namespace \"x:y\";\n"
635 " prefix x;\n"
636 "\n"
637 " feature f;\n"
638 "\n"
639 " typedef some-long-type {\n"
640 " type string;\n"
641 " }\n"
642 " list my-list-name {\n"
643 " key \"key\";\n"
644 " leaf key {\n"
645 " type string;\n"
646 " }\n"
647 " leaf nod-leaf {\n"
648 " if-feature \"f\";\n"
649 " type some-long-type;\n"
650 " }\n"
651 " leaf nos-leaf {\n"
652 " if-feature \"f\";\n"
653 " type int32;\n"
654 " }\n"
655 " }\n"
656 "}\n";
657
658 /* pyang --tree-line-length 42 */
659 expect =
660 "module: a16\n"
661 " +--rw my-list-name* [key]\n"
662 " +--rw key string\n"
663 " +--rw nod-leaf? some-long-type {f}?\n"
664 " +--rw nos-leaf? int32 {f}?\n";
665
aPieceke376b922021-04-19 08:08:14 +0200666 const char *feats[] = {"f", NULL};
667
668 UTEST_ADD_MODULE(orig, LYS_IN_YANG, feats, &mod);
669 TEST_LOCAL_PRINT(mod, 42);
670 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100671 assert_string_equal(printed, expect);
672
aPieceke376b922021-04-19 08:08:14 +0200673 ly_out_reset(UTEST_OUT);
aPiecek20ddf8b2021-01-08 11:50:37 +0100674 /* break_before_iffeature */
675
676 /* pyang --tree-line-length 41 */
677 expect =
678 "module: a16\n"
679 " +--rw my-list-name* [key]\n"
680 " +--rw key string\n"
681 " +--rw nod-leaf? some-long-type\n"
682 " | {f}?\n"
683 " +--rw nos-leaf? int32 {f}?\n";
684
aPieceke376b922021-04-19 08:08:14 +0200685 UTEST_ADD_MODULE(orig, LYS_IN_YANG, feats, &mod);
686 TEST_LOCAL_PRINT(mod, 41);
687 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100688 assert_string_equal(printed, expect);
689
aPieceke376b922021-04-19 08:08:14 +0200690 ly_out_reset(UTEST_OUT);
aPiecek20ddf8b2021-01-08 11:50:37 +0100691 /* break_before_type */
692
693 /* pyang --tree-line-length 29 */
694 expect =
695 "module: a16\n"
696 " +--rw my-list-name* [key]\n"
697 " +--rw key string\n"
698 " +--rw nod-leaf?\n"
699 " | some-long-type\n"
700 " | {f}?\n"
701 " +--rw nos-leaf? int32\n"
702 " {f}?\n";
703
aPieceke376b922021-04-19 08:08:14 +0200704 UTEST_ADD_MODULE(orig, LYS_IN_YANG, feats, &mod);
705 TEST_LOCAL_PRINT(mod, 29);
706 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100707 assert_string_equal(printed, expect);
708
aPieceke376b922021-04-19 08:08:14 +0200709 ly_out_reset(UTEST_OUT);
aPiecek20ddf8b2021-01-08 11:50:37 +0100710 /* break_before_keys */
711
712 /* pyang --tree-line-length 23 */
713 expect =
714 "module: a16\n"
715 " +--rw my-list-name*\n"
716 " [key]\n"
717 " +--rw key\n"
718 " | string\n"
719 " +--rw nod-leaf?\n"
720 " | some-long-type\n"
721 " | {f}?\n"
722 " +--rw nos-leaf?\n"
723 " int32 {f}?\n";
724
aPieceke376b922021-04-19 08:08:14 +0200725 UTEST_ADD_MODULE(orig, LYS_IN_YANG, feats, &mod);
726 TEST_LOCAL_PRINT(mod, 23);
727 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100728 assert_string_equal(printed, expect);
729
aPieceke376b922021-04-19 08:08:14 +0200730 ly_out_reset(UTEST_OUT);
aPiecek20ddf8b2021-01-08 11:50:37 +0100731 /* every_node_name_is_too_long */
732
733 /* pyang --tree-line-length 14 */
734 expect =
735 "module: a16\n"
736 " +--rw my-list-name*\n"
737 " [key]\n"
738 " +--rw key\n"
739 " | string\n"
740 " +--rw nod-leaf?\n"
741 " | some-long-type\n"
742 " | {f}?\n"
743 " +--rw nos-leaf?\n"
744 " int32\n"
745 " {f}?\n";
746
aPieceke376b922021-04-19 08:08:14 +0200747 UTEST_ADD_MODULE(orig, LYS_IN_YANG, feats, &mod);
748 TEST_LOCAL_PRINT(mod, 14);
749 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100750 assert_string_equal(printed, expect);
751
752 TEST_LOCAL_TEARDOWN;
753}
754
755static void
756break_before_leafref(void **state)
757{
758 TEST_LOCAL_SETUP;
759 orig =
760 "module a17 {\n"
761 " yang-version 1.1;\n"
762 " namespace \"x:y\";\n"
763 " prefix x;\n"
764 " leaf e {\n"
765 " type string;\n"
766 " }\n"
767 " leaf abcd {\n"
768 " type leafref {\n"
769 " path \"/x:e\";\n"
770 " }\n"
771 " }\n"
772 "}\n";
773
774 /* yanglint --tree-line-length 14 */
775 expect =
776 "module: a17\n"
777 " +--rw e?\n"
778 " | string\n"
779 " +--rw abcd?\n"
780 " -> /x:e\n";
781
aPieceke376b922021-04-19 08:08:14 +0200782 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
783 TEST_LOCAL_PRINT(mod, 14);
784 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100785 assert_string_equal(printed, expect);
786 TEST_LOCAL_TEARDOWN;
787}
788
789static void
790break_before_leafref_and_iffeature(void **state)
791{
792 TEST_LOCAL_SETUP;
793 orig =
794 "module a18 {\n"
795 " yang-version 1.1;\n"
796 " namespace \"x:y\";\n"
797 " prefix x;\n"
798 "\n"
799 " feature f;\n"
800 " leaf some-long-id {\n"
801 " type string;\n"
802 " }\n"
803 " leaf a {\n"
804 " if-feature \"f\";\n"
805 " type leafref {\n"
806 " path \"/x:some-long-id\";\n"
807 " }\n"
808 " }\n"
809 "}\n";
810
811 /* yanglint --tree-no-leafref-target --tree-line-length=20 */
812 expect =
813 "module: a18\n"
814 " +--rw some-long-id?\n"
815 " | string\n"
816 " +--rw a?\n"
817 " leafref\n"
818 " {f}?\n";
819
aPieceke376b922021-04-19 08:08:14 +0200820 const char *feats[] = {"f", NULL};
821
822 UTEST_ADD_MODULE(orig, LYS_IN_YANG, feats, &mod);
823 TEST_LOCAL_PRINT(mod, 20);
824 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100825 assert_string_equal(printed, expect);
826 TEST_LOCAL_TEARDOWN;
827}
828
829static void
830basic_unified_indent_before_type(void **state)
831{
832 TEST_LOCAL_SETUP;
833 orig =
834 "module a19 {\n"
835 " yang-version 1.1;\n"
836 " namespace \"x:y\";\n"
837 " prefix x;\n"
838 "\n"
839 " typedef longType {\n"
840 " type string;\n"
841 " }\n"
842 " container A {\n"
843 " leaf Bnode {\n"
844 " type int8;\n"
845 " }\n"
846 " leaf Cnode {\n"
847 " type int8;\n"
848 " mandatory true;\n"
849 " }\n"
850 " leaf Dnode {\n"
851 " type int8;\n"
852 " mandatory true;\n"
853 " }\n"
854 " leaf E {\n"
855 " type longType;\n"
856 " mandatory true;\n"
857 " }\n"
858 " leaf G {\n"
859 " type int8;\n"
860 " }\n"
861 " }\n"
862 "}\n";
863
864 /* from pyang */
865 expect =
866 "module: a19\n"
867 " +--rw A\n"
868 " +--rw Bnode? int8\n"
869 " +--rw Cnode int8\n"
870 " +--rw Dnode int8\n"
871 " +--rw E longType\n"
872 " +--rw G? int8\n";
873
aPieceke376b922021-04-19 08:08:14 +0200874 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
875 TEST_LOCAL_PRINT(mod, 72);
876 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100877 assert_string_equal(printed, expect);
878 TEST_LOCAL_TEARDOWN;
879}
880
881static void
882twiddling_unified_indent_before_type(void **state)
883{
884 TEST_LOCAL_SETUP;
885 /* basic_functionality */
886
887 orig =
888 "module a20 {\n"
889 " yang-version 1.1;\n"
890 " namespace \"x:y\";\n"
891 " prefix x;\n"
892 "\n"
893 " typedef longType {\n"
894 " type string;\n"
895 " }\n"
896 " container A {\n"
897 " leaf Bnode {\n"
898 " type int8;\n"
899 " }\n"
900 " leaf CnodeIsBigger {\n"
901 " type int8;\n"
902 " mandatory true;\n"
903 " }\n"
904 " leaf Dnode {\n"
905 " type int8;\n"
906 " mandatory true;\n"
907 " }\n"
908 " leaf E {\n"
909 " type longType;\n"
910 " mandatory true;\n"
911 " }\n"
912 " leaf G {\n"
913 " type int8;\n"
914 " }\n"
915 " }\n"
916 "}\n";
917
918 /* pyang --tree-line-length 36 */
919 expect =
920 "module: a20\n"
921 " +--rw A\n"
922 " +--rw Bnode? int8\n"
923 " +--rw CnodeIsBigger int8\n"
924 " +--rw Dnode int8\n"
925 " +--rw E longType\n"
926 " +--rw G? int8\n";
927
aPieceke376b922021-04-19 08:08:14 +0200928 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
929 TEST_LOCAL_PRINT(mod, 36);
930 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100931 assert_string_equal(printed, expect);
932
aPieceke376b922021-04-19 08:08:14 +0200933 ly_out_reset(UTEST_OUT);
aPiecek20ddf8b2021-01-08 11:50:37 +0100934 /* unified_indent_before_type_long_node_name */
935
936 /* pyang --tree-line-length 32 */
937 expect =
938 "module: a20\n"
939 " +--rw A\n"
940 " +--rw Bnode? int8\n"
941 " +--rw CnodeIsBigger int8\n"
942 " +--rw Dnode int8\n"
943 " +--rw E\n"
944 " | longType\n"
945 " +--rw G? int8\n";
946
aPieceke376b922021-04-19 08:08:14 +0200947 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
948 TEST_LOCAL_PRINT(mod, 32);
949 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100950 assert_string_equal(printed, expect);
951
aPieceke376b922021-04-19 08:08:14 +0200952 ly_out_reset(UTEST_OUT);
aPiecek20ddf8b2021-01-08 11:50:37 +0100953 /* unified_indent_before_type_long_node_type */
954
955 /* pyang --tree-line-length 31 */
956 expect =
957 "module: a20\n"
958 " +--rw A\n"
959 " +--rw Bnode?\n"
960 " | int8\n"
961 " +--rw CnodeIsBigger\n"
962 " | int8\n"
963 " +--rw Dnode\n"
964 " | int8\n"
965 " +--rw E\n"
966 " | longType\n"
967 " +--rw G?\n"
968 " int8\n";
969
aPieceke376b922021-04-19 08:08:14 +0200970 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
971 TEST_LOCAL_PRINT(mod, 31);
972 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100973 assert_string_equal(printed, expect);
974
975 TEST_LOCAL_TEARDOWN;
976}
977
978static void
979inheritance_of_config_flag(void **state)
980{
981 TEST_LOCAL_SETUP;
982 orig =
983 "module a21 {\n"
984 " yang-version 1.1;\n"
985 " namespace \"x:y\";\n"
986 " prefix x;\n"
987 " container a {\n"
988 " config false;\n"
989 " leaf b {\n"
990 " type string;\n"
991 " }\n"
992 " }\n"
993 "}\n";
994
995 /* from pyang */
996 expect =
997 "module: a21\n"
998 " +--ro a\n"
999 " +--ro b? string\n";
1000
aPieceke376b922021-04-19 08:08:14 +02001001 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1002 TEST_LOCAL_PRINT(mod, 72);
1003 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +01001004 assert_string_equal(printed, expect);
1005 TEST_LOCAL_TEARDOWN;
1006}
1007
1008static void
1009inheritance_of_status_flag(void **state)
1010{
1011 TEST_LOCAL_SETUP;
1012 /* throws libyang warn: Missing explicit "..." status that was already specified in parent, inheriting. */
1013 orig =
1014 "module a22 {\n"
1015 " yang-version 1.1;\n"
1016 " namespace \"x:y\";\n"
1017 " prefix x;\n"
1018 " container a {\n"
1019 " status current;\n"
1020 " container b {\n"
1021 " status deprecated;\n"
1022 " leaf f {\n"
1023 " type string;\n"
1024 " }\n"
1025 " }\n"
1026 " container c {\n"
1027 " status obsolete;\n"
1028 " leaf g {\n"
1029 " type string;\n"
1030 " }\n"
1031 " }\n"
1032 " }\n"
1033 " container d {\n"
1034 " status deprecated;\n"
1035 " container h {\n"
1036 " status obsolete;\n"
1037 " leaf e {\n"
1038 " type string;\n"
1039 " }\n"
1040 " }\n"
1041 " }\n"
1042 "}\n";
1043
1044 /* from yanglint 1 */
1045 expect =
1046 "module: a22\n"
1047 " +--rw a\n"
1048 " | x--rw b\n"
1049 " | | x--rw f? string\n"
1050 " | o--rw c\n"
1051 " | o--rw g? string\n"
1052 " x--rw d\n"
1053 " o--rw h\n"
1054 " o--rw e? string\n";
1055
aPieceke376b922021-04-19 08:08:14 +02001056 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1057 TEST_LOCAL_PRINT(mod, 72);
1058 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +01001059 assert_string_equal(printed, expect);
1060 TEST_LOCAL_TEARDOWN;
1061}
1062
1063static void
1064key_leaf_is_always_mandatory_true(void **state)
1065{
1066 TEST_LOCAL_SETUP;
1067 orig =
1068 "module a23 {\n"
1069 " yang-version 1.1;\n"
1070 " namespace \"x:y\";\n"
1071 " prefix x;\n"
1072 " list a {\n"
1073 " key \"k1\";\n"
1074 " list b {\n"
1075 " key \"k2\";\n"
1076 " leaf k1 {\n"
1077 " type string;\n"
1078 " }\n"
1079 " leaf k2 {\n"
1080 " type string;\n"
1081 " }\n"
1082 " }\n"
1083 " leaf k1 {\n"
1084 " type string;\n"
1085 " }\n"
1086 " }\n"
1087 "}\n";
1088
1089 /* from pyang */
1090 expect =
1091 "module: a23\n"
1092 " +--rw a* [k1]\n"
1093 " +--rw b* [k2]\n"
1094 " | +--rw k1? string\n"
1095 " | +--rw k2 string\n"
1096 " +--rw k1 string\n";
1097
aPieceke376b922021-04-19 08:08:14 +02001098 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1099 TEST_LOCAL_PRINT(mod, 72);
1100 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +01001101 assert_string_equal(printed, expect);
1102 TEST_LOCAL_TEARDOWN;
1103}
1104
1105static void
1106transition_between_rpc_and_notif(void **state)
1107{
1108 TEST_LOCAL_SETUP;
1109 orig =
1110 "module a24 {\n"
1111 " yang-version 1.1;\n"
1112 " namespace \"x:y\";\n"
1113 " prefix x;\n"
1114 " container top {\n"
1115 " action rpc1 {\n"
1116 "\n"
1117 " input {\n"
1118 " leaf in {\n"
1119 " type string;\n"
1120 " }\n"
1121 " }\n"
1122 " }\n"
1123 " action rpc2 {\n"
1124 "\n"
1125 " input {\n"
1126 " leaf in {\n"
1127 " type string;\n"
1128 " }\n"
1129 " }\n"
1130 "\n"
1131 " output {\n"
1132 " leaf out {\n"
1133 " type string;\n"
1134 " }\n"
1135 " }\n"
1136 " }\n"
1137 " notification n1;\n"
1138 " notification n2;\n"
1139 " }\n"
1140 "}\n";
1141
1142 /* from pyang but edited -> n1 node is moved to n2 */
1143 expect =
1144 "module: a24\n"
1145 " +--rw top\n"
1146 " +---x rpc1\n"
1147 " | +---w input\n"
1148 " | +---w in? string\n"
1149 " +---x rpc2\n"
1150 " | +---w input\n"
1151 " | | +---w in? string\n"
1152 " | +--ro output\n"
1153 " | +--ro out? string\n"
1154 " +---n n1\n"
1155 " +---n n2\n";
1156
aPieceke376b922021-04-19 08:08:14 +02001157 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1158 TEST_LOCAL_PRINT(mod, 72);
1159 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +01001160 assert_string_equal(printed, expect);
1161 TEST_LOCAL_TEARDOWN;
1162}
1163
1164int
1165main(void)
1166{
1167 const struct CMUnitTest tests[] = {
1168 UTEST(base_sections),
1169 UTEST(node_status),
1170 UTEST(node_config_flags),
1171 UTEST(node_rpcs_flags),
1172 UTEST(node_grouping_flags),
1173 UTEST(notif_inside_container),
1174 UTEST(node_choice),
1175 UTEST(node_case),
1176 UTEST(optional_opts),
1177 UTEST(presence_container),
1178 UTEST(node_keys),
1179 UTEST(node_type_target),
1180 UTEST(node_type_leafref),
1181 UTEST(node_iffeatures),
1182 UTEST(indent_wrapper),
1183 UTEST(line_length_twiddling),
1184 UTEST(break_before_leafref),
1185 UTEST(break_before_leafref_and_iffeature),
1186 UTEST(basic_unified_indent_before_type),
1187 UTEST(twiddling_unified_indent_before_type),
1188 UTEST(inheritance_of_config_flag),
1189 UTEST(inheritance_of_status_flag),
1190 UTEST(key_leaf_is_always_mandatory_true),
1191 UTEST(transition_between_rpc_and_notif),
1192 };
1193
1194 return cmocka_run_group_tests(tests, NULL, NULL);
1195}