blob: 2c92bd97be62297bb214441641559af9284b2e54 [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;
40 orig =
41 "module a01 {\n"
42 " yang-version 1.1;\n"
43 " namespace \"x:y\";\n"
44 " prefix x;\n"
45 "\n"
46 " grouping g1;\n"
47 "\n"
48 " grouping g2;\n"
49 " container c;\n"
50 " container d;\n"
51 " augment \"/x:c\" {\n"
52 " }\n"
53 " augment \"/x:d\" {\n"
54 " }\n"
55 " rpc rpc1;\n"
56 " rpc rpc2;\n"
57 " notification n1;\n"
58 " notification n2;\n"
59 "}\n";
60
61 /* from yanglint 1 */
62 expect =
63 "module: a01\n"
64 " +--rw c\n"
65 " +--rw d\n"
66 "\n"
67 " rpcs:\n"
68 " +---x rpc1\n"
69 " +---x rpc2\n"
70 "\n"
71 " notifications:\n"
72 " +---n n1\n"
73 " +---n n2\n";
74
aPieceke376b922021-04-19 08:08:14 +020075 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
76 TEST_LOCAL_PRINT(mod, 72);
77 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +010078 assert_string_equal(printed, expect);
79 TEST_LOCAL_TEARDOWN;
80}
81
82static void
83node_status(void **state)
84{
85 TEST_LOCAL_SETUP;
86 orig =
87 "module a02 {\n"
88 " yang-version 1.1;\n"
89 " namespace \"x:y\";\n"
90 " prefix x;\n"
91 " container l {\n"
92 " status current;\n"
93 " }\n"
94 " container m {\n"
95 " status deprecated;\n"
96 " }\n"
97 " container n {\n"
98 " status obsolete;\n"
99 " }\n"
100 "}\n";
101
102 /* from pyang */
103 expect =
104 "module: a02\n"
105 " +--rw l\n"
106 " x--rw m\n"
107 " o--rw n\n";
108
aPieceke376b922021-04-19 08:08:14 +0200109 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
110 TEST_LOCAL_PRINT(mod, 72);
111 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100112 assert_string_equal(printed, expect);
113 TEST_LOCAL_TEARDOWN;
114}
115
116static void
117node_config_flags(void **state)
118{
119 TEST_LOCAL_SETUP;
120 orig =
121 "module a03 {\n"
122 " yang-version 1.1;\n"
123 " namespace \"x:y\";\n"
124 " prefix x;\n"
125 " container l {\n"
126 " config true;\n"
127 " }\n"
128 " container m {\n"
129 " config false;\n"
130 " }\n"
131 "}\n";
132
133 /* from pyang */
134 expect =
135 "module: a03\n"
136 " +--rw l\n"
137 " +--ro m\n";
138
aPieceke376b922021-04-19 08:08:14 +0200139 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
140 TEST_LOCAL_PRINT(mod, 72);
141 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100142 assert_string_equal(printed, expect);
143 TEST_LOCAL_TEARDOWN;
144}
145
146static void
147node_rpcs_flags(void **state)
148{
149 TEST_LOCAL_SETUP;
150 orig =
151 "module a04 {\n"
152 " yang-version 1.1;\n"
153 " namespace \"x:y\";\n"
154 " prefix x;\n"
155 " container cont {\n"
156 " action rpc1 {\n"
157 "\n"
158 " input {\n"
159 " leaf in {\n"
160 " type string;\n"
161 " }\n"
162 " }\n"
163 " }\n"
164 " }\n"
165 "}\n";
166
167 /* from pyang */
168 expect =
169 "module: a04\n"
170 " +--rw cont\n"
171 " +---x rpc1\n"
172 " +---w input\n"
173 " +---w in? string\n";
174
aPieceke376b922021-04-19 08:08:14 +0200175 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
176 TEST_LOCAL_PRINT(mod, 72);
177 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100178 assert_string_equal(printed, expect);
179 TEST_LOCAL_TEARDOWN;
180}
181
182static void
183node_grouping_flags(void **state)
184{
185 TEST_LOCAL_SETUP;
186 orig =
187 "module a05 {\n"
188 " yang-version 1.1;\n"
189 " namespace \"x:y\";\n"
190 " prefix x;\n"
191 "\n"
192 " grouping g {\n"
193 " container c;\n"
194 " }\n"
195 " container d {\n"
196 " uses g;\n"
197 " }\n"
198 "}\n";
199
200 /* handwritten. Neither Pyang nor Yanglint1 provided the required output. */
201 expect =
202 "module: a05\n"
203 " +--rw d\n"
204 " +---u g\n"
205 "\n"
206 " grouping g:\n"
207 " +--rw c\n";
aPieceke376b922021-04-19 08:08:14 +0200208 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
209 TEST_LOCAL_PRINT(mod, 72);
210 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100211 assert_string_equal(printed, expect);
212 TEST_LOCAL_TEARDOWN;
213}
214
215static void
216notif_inside_container(void **state)
217{
218 TEST_LOCAL_SETUP;
219 orig =
220 "module a06 {\n"
221 " yang-version 1.1;\n"
222 " namespace \"x:y\";\n"
223 " prefix x;\n"
224 " container c {\n"
225 " notification notif;\n"
226 " }\n"
227 "}\n";
228
229 /* from pyang */
230 expect =
231 "module: a06\n"
232 " +--rw c\n"
233 " +---n notif\n";
234
aPieceke376b922021-04-19 08:08:14 +0200235 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
236 TEST_LOCAL_PRINT(mod, 72);
237 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100238 assert_string_equal(printed, expect);
239 TEST_LOCAL_TEARDOWN;
240}
241
242static void
243node_choice(void **state)
244{
245 TEST_LOCAL_SETUP;
246 orig =
247 "module a07 {\n"
248 " yang-version 1.1;\n"
249 " namespace \"x:y\";\n"
250 " prefix x;\n"
251 " choice my_choice;\n"
252 "}\n";
253
254 /* from pyang */
255 expect =
256 "module: a07\n"
257 " +--rw (my_choice)?\n";
258
aPieceke376b922021-04-19 08:08:14 +0200259 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
260 TEST_LOCAL_PRINT(mod, 72);
261 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100262 assert_string_equal(printed, expect);
263 TEST_LOCAL_TEARDOWN;
264}
265
266static void
267node_case(void **state)
268{
269 TEST_LOCAL_SETUP;
270 orig =
271 "module a08 {\n"
272 " yang-version 1.1;\n"
273 " namespace \"x:y\";\n"
274 " prefix x;\n"
275 " choice my_choice {\n"
276 " case my_case;\n"
277 " }\n"
278 "}\n";
279
280 /* from pyang */
281 expect =
282 "module: a08\n"
283 " +--rw (my_choice)?\n"
284 " +--:(my_case)\n";
285
aPieceke376b922021-04-19 08:08:14 +0200286 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
287 TEST_LOCAL_PRINT(mod, 72);
288 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100289 assert_string_equal(printed, expect);
290 TEST_LOCAL_TEARDOWN;
291}
292
293static void
294optional_opts(void **state)
295{
296 TEST_LOCAL_SETUP;
297 /* throws libyang warn: Use of anydata to define configuration data is not recommended... */
298 orig =
299 "module a09 {\n"
300 " yang-version 1.1;\n"
301 " namespace \"x:y\";\n"
302 " prefix x;\n"
303 " leaf l1 {\n"
304 " type string;\n"
305 " mandatory true;\n"
306 " }\n"
307 " leaf l2 {\n"
308 " type string;\n"
309 " mandatory false;\n"
310 " }\n"
311 " choice c1 {\n"
312 " mandatory true;\n"
313 " }\n"
314 " choice c2 {\n"
315 " mandatory false;\n"
316 " }\n"
317 " anydata a1 {\n"
318 " mandatory true;\n"
319 " }\n"
320 " anydata a2 {\n"
321 " mandatory false;\n"
322 " }\n"
323 " anyxml x1 {\n"
324 " mandatory true;\n"
325 " }\n"
326 " anyxml x2 {\n"
327 " mandatory false;\n"
328 " }\n"
329 "}\n";
330
331 /* from yanglint 1 */
332 expect =
333 "module: a09\n"
334 " +--rw l1 string\n"
335 " +--rw l2? string\n"
336 " +--rw (c1)\n"
337 " +--rw (c2)?\n"
338 " +--rw a1 anydata\n"
339 " +--rw a2? anydata\n"
340 " +--rw x1 anyxml\n"
341 " +--rw x2? anyxml\n";
342
aPieceke376b922021-04-19 08:08:14 +0200343 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
344 TEST_LOCAL_PRINT(mod, 72);
345 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100346 assert_string_equal(printed, expect);
347 TEST_LOCAL_TEARDOWN;
348}
349
350static void
351presence_container(void **state)
352{
353 TEST_LOCAL_SETUP;
354 orig =
355 "module a10 {\n"
356 " yang-version 1.1;\n"
357 " namespace \"x:y\";\n"
358 " prefix x;\n"
359 " container c;\n"
360 " container d {\n"
361 " presence \"str1\";\n"
362 " }\n"
363 "}\n";
364
365 /* from pyang */
366 expect =
367 "module: a10\n"
368 " +--rw c\n"
369 " +--rw d!\n";
370
aPieceke376b922021-04-19 08:08:14 +0200371 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
372 TEST_LOCAL_PRINT(mod, 72);
373 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100374 assert_string_equal(printed, expect);
375 TEST_LOCAL_TEARDOWN;
376}
377
378static void
379node_keys(void **state)
380{
381 TEST_LOCAL_SETUP;
382 orig =
383 "module a11 {\n"
384 " yang-version 1.1;\n"
385 " namespace \"x:y\";\n"
386 " prefix x;\n"
387 " list l1 {\n"
388 " key \"a\";\n"
389 " leaf a {\n"
390 " type string;\n"
391 " }\n"
392 " }\n"
393 " list l2 {\n"
394 " key \"a b\";\n"
395 " leaf a {\n"
396 " type string;\n"
397 " }\n"
398 " leaf b {\n"
399 " type string;\n"
400 " }\n"
401 " }\n"
402 " leaf-list ll {\n"
403 " type string;\n"
404 " }\n"
405 "}\n";
406
407 /* from pyang */
408 expect =
409 "module: a11\n"
410 " +--rw l1* [a]\n"
411 " | +--rw a string\n"
412 " +--rw l2* [a b]\n"
413 " | +--rw a string\n"
414 " | +--rw b string\n"
415 " +--rw ll* string\n";
416
aPieceke376b922021-04-19 08:08:14 +0200417 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
418 TEST_LOCAL_PRINT(mod, 72);
419 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100420 assert_string_equal(printed, expect);
421 TEST_LOCAL_TEARDOWN;
422}
423
424static void
425node_type_target(void **state)
426{
427 TEST_LOCAL_SETUP;
428 orig =
429 "module a12 {\n"
430 " yang-version 1.1;\n"
431 " namespace \"x:y\";\n"
432 " prefix x;\n"
433 " leaf a {\n"
434 " type leafref {\n"
435 " path \"/x:b\";\n"
436 " }\n"
437 " }\n"
438 " leaf b {\n"
439 " type string;\n"
440 " }\n"
441 "}\n";
442
443 /* from yanglint 1 */
444 expect =
445 "module: a12\n"
446 " +--rw a? -> /x:b\n"
447 " +--rw b? string\n";
448
aPieceke376b922021-04-19 08:08:14 +0200449 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
450 TEST_LOCAL_PRINT(mod, 72);
451 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100452 assert_string_equal(printed, expect);
453 TEST_LOCAL_TEARDOWN;
454}
455
456static void
457node_type_leafref(void **state)
458{
459 TEST_LOCAL_SETUP;
460 orig =
461 "module a13 {\n"
462 " yang-version 1.1;\n"
463 " namespace \"x:y\";\n"
464 " prefix x;\n"
465 " leaf pretty-long-identifier-name-which-should-exceed-the-limit-of-72-characters {\n"
466 " type string;\n"
467 " }\n"
468 " leaf a {\n"
469 " type leafref {\n"
470 " path \"/x:pretty-long-identifier-name-which-should-exceed-the-limit-of-72-characters\";\n"
471 " }\n"
472 " }\n"
473 "}\n";
474
475 /* yanglint --tree-no-leafref-target --tree-line-length=72 */
476 expect =
477 "module: a13\n"
478 " +--rw pretty-long-identifier-name-which-should-exceed-the-limit-of-72-characters?\n"
479 " | string\n"
480 " +--rw a?\n"
481 " leafref\n";
482
aPieceke376b922021-04-19 08:08:14 +0200483 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
484 TEST_LOCAL_PRINT(mod, 72);
485 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100486 assert_string_equal(printed, expect);
487 TEST_LOCAL_TEARDOWN;
488}
489
490static void
491node_iffeatures(void **state)
492{
493 TEST_LOCAL_SETUP;
494 orig =
495 "module a14 {\n"
496 " yang-version 1.1;\n"
497 " namespace \"x:y\";\n"
498 " prefix x;\n"
499 "\n"
500 " feature foo;\n"
501 "\n"
502 " feature bar;\n"
503 " container c {\n"
504 " if-feature \"foo or bar\";\n"
505 " }\n"
506 "}\n";
507
508 /* from pyang */
509 expect =
510 "module: a14\n"
511 " +--rw c {foo or bar}?\n";
512
aPieceke376b922021-04-19 08:08:14 +0200513 const char *feats[] = {"foo", NULL};
514
515 UTEST_ADD_MODULE(orig, LYS_IN_YANG, feats, &mod);
516 TEST_LOCAL_PRINT(mod, 72);
517 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100518 assert_string_equal(printed, expect);
519 TEST_LOCAL_TEARDOWN;
520}
521
522static void
523indent_wrapper(void **state)
524{
525 TEST_LOCAL_SETUP;
526 orig =
527 "module a15 {\n"
528 " yang-version 1.1;\n"
529 " namespace \"x:y\";\n"
530 " prefix x;\n"
531 " container a {\n"
532 " container b;\n"
533 " }\n"
534 " container c {\n"
535 " container d {\n"
536 " container e;\n"
537 " }\n"
538 " container f {\n"
539 " container g;\n"
540 " }\n"
541 " }\n"
542 " container h;\n"
543 " container i {\n"
544 " container j;\n"
545 " container k;\n"
546 " }\n"
547 "}\n";
548
549 /* from pyang */
550 expect =
551 "module: a15\n"
552 " +--rw a\n"
553 " | +--rw b\n"
554 " +--rw c\n"
555 " | +--rw d\n"
556 " | | +--rw e\n"
557 " | +--rw f\n"
558 " | +--rw g\n"
559 " +--rw h\n"
560 " +--rw i\n"
561 " +--rw j\n"
562 " +--rw k\n";
563
aPieceke376b922021-04-19 08:08:14 +0200564 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
565 TEST_LOCAL_PRINT(mod, 72);
566 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100567 assert_string_equal(printed, expect);
568 TEST_LOCAL_TEARDOWN;
569}
570
571static void
572line_length_twiddling(void **state)
573{
574 TEST_LOCAL_SETUP;
575 /* node_fits_tight */
576
577 orig =
578 "module a16 {\n"
579 " yang-version 1.1;\n"
580 " namespace \"x:y\";\n"
581 " prefix x;\n"
582 "\n"
583 " feature f;\n"
584 "\n"
585 " typedef some-long-type {\n"
586 " type string;\n"
587 " }\n"
588 " list my-list-name {\n"
589 " key \"key\";\n"
590 " leaf key {\n"
591 " type string;\n"
592 " }\n"
593 " leaf nod-leaf {\n"
594 " if-feature \"f\";\n"
595 " type some-long-type;\n"
596 " }\n"
597 " leaf nos-leaf {\n"
598 " if-feature \"f\";\n"
599 " type int32;\n"
600 " }\n"
601 " }\n"
602 "}\n";
603
604 /* pyang --tree-line-length 42 */
605 expect =
606 "module: a16\n"
607 " +--rw my-list-name* [key]\n"
608 " +--rw key string\n"
609 " +--rw nod-leaf? some-long-type {f}?\n"
610 " +--rw nos-leaf? int32 {f}?\n";
611
aPieceke376b922021-04-19 08:08:14 +0200612 const char *feats[] = {"f", NULL};
613
614 UTEST_ADD_MODULE(orig, LYS_IN_YANG, feats, &mod);
615 TEST_LOCAL_PRINT(mod, 42);
616 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100617 assert_string_equal(printed, expect);
618
aPieceke376b922021-04-19 08:08:14 +0200619 ly_out_reset(UTEST_OUT);
aPiecek20ddf8b2021-01-08 11:50:37 +0100620 /* break_before_iffeature */
621
622 /* pyang --tree-line-length 41 */
623 expect =
624 "module: a16\n"
625 " +--rw my-list-name* [key]\n"
626 " +--rw key string\n"
627 " +--rw nod-leaf? some-long-type\n"
628 " | {f}?\n"
629 " +--rw nos-leaf? int32 {f}?\n";
630
aPieceke376b922021-04-19 08:08:14 +0200631 UTEST_ADD_MODULE(orig, LYS_IN_YANG, feats, &mod);
632 TEST_LOCAL_PRINT(mod, 41);
633 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100634 assert_string_equal(printed, expect);
635
aPieceke376b922021-04-19 08:08:14 +0200636 ly_out_reset(UTEST_OUT);
aPiecek20ddf8b2021-01-08 11:50:37 +0100637 /* break_before_type */
638
639 /* pyang --tree-line-length 29 */
640 expect =
641 "module: a16\n"
642 " +--rw my-list-name* [key]\n"
643 " +--rw key string\n"
644 " +--rw nod-leaf?\n"
645 " | some-long-type\n"
646 " | {f}?\n"
647 " +--rw nos-leaf? int32\n"
648 " {f}?\n";
649
aPieceke376b922021-04-19 08:08:14 +0200650 UTEST_ADD_MODULE(orig, LYS_IN_YANG, feats, &mod);
651 TEST_LOCAL_PRINT(mod, 29);
652 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100653 assert_string_equal(printed, expect);
654
aPieceke376b922021-04-19 08:08:14 +0200655 ly_out_reset(UTEST_OUT);
aPiecek20ddf8b2021-01-08 11:50:37 +0100656 /* break_before_keys */
657
658 /* pyang --tree-line-length 23 */
659 expect =
660 "module: a16\n"
661 " +--rw my-list-name*\n"
662 " [key]\n"
663 " +--rw key\n"
664 " | string\n"
665 " +--rw nod-leaf?\n"
666 " | some-long-type\n"
667 " | {f}?\n"
668 " +--rw nos-leaf?\n"
669 " int32 {f}?\n";
670
aPieceke376b922021-04-19 08:08:14 +0200671 UTEST_ADD_MODULE(orig, LYS_IN_YANG, feats, &mod);
672 TEST_LOCAL_PRINT(mod, 23);
673 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100674 assert_string_equal(printed, expect);
675
aPieceke376b922021-04-19 08:08:14 +0200676 ly_out_reset(UTEST_OUT);
aPiecek20ddf8b2021-01-08 11:50:37 +0100677 /* every_node_name_is_too_long */
678
679 /* pyang --tree-line-length 14 */
680 expect =
681 "module: a16\n"
682 " +--rw my-list-name*\n"
683 " [key]\n"
684 " +--rw key\n"
685 " | string\n"
686 " +--rw nod-leaf?\n"
687 " | some-long-type\n"
688 " | {f}?\n"
689 " +--rw nos-leaf?\n"
690 " int32\n"
691 " {f}?\n";
692
aPieceke376b922021-04-19 08:08:14 +0200693 UTEST_ADD_MODULE(orig, LYS_IN_YANG, feats, &mod);
694 TEST_LOCAL_PRINT(mod, 14);
695 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100696 assert_string_equal(printed, expect);
697
698 TEST_LOCAL_TEARDOWN;
699}
700
701static void
702break_before_leafref(void **state)
703{
704 TEST_LOCAL_SETUP;
705 orig =
706 "module a17 {\n"
707 " yang-version 1.1;\n"
708 " namespace \"x:y\";\n"
709 " prefix x;\n"
710 " leaf e {\n"
711 " type string;\n"
712 " }\n"
713 " leaf abcd {\n"
714 " type leafref {\n"
715 " path \"/x:e\";\n"
716 " }\n"
717 " }\n"
718 "}\n";
719
720 /* yanglint --tree-line-length 14 */
721 expect =
722 "module: a17\n"
723 " +--rw e?\n"
724 " | string\n"
725 " +--rw abcd?\n"
726 " -> /x:e\n";
727
aPieceke376b922021-04-19 08:08:14 +0200728 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
729 TEST_LOCAL_PRINT(mod, 14);
730 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100731 assert_string_equal(printed, expect);
732 TEST_LOCAL_TEARDOWN;
733}
734
735static void
736break_before_leafref_and_iffeature(void **state)
737{
738 TEST_LOCAL_SETUP;
739 orig =
740 "module a18 {\n"
741 " yang-version 1.1;\n"
742 " namespace \"x:y\";\n"
743 " prefix x;\n"
744 "\n"
745 " feature f;\n"
746 " leaf some-long-id {\n"
747 " type string;\n"
748 " }\n"
749 " leaf a {\n"
750 " if-feature \"f\";\n"
751 " type leafref {\n"
752 " path \"/x:some-long-id\";\n"
753 " }\n"
754 " }\n"
755 "}\n";
756
757 /* yanglint --tree-no-leafref-target --tree-line-length=20 */
758 expect =
759 "module: a18\n"
760 " +--rw some-long-id?\n"
761 " | string\n"
762 " +--rw a?\n"
763 " leafref\n"
764 " {f}?\n";
765
aPieceke376b922021-04-19 08:08:14 +0200766 const char *feats[] = {"f", NULL};
767
768 UTEST_ADD_MODULE(orig, LYS_IN_YANG, feats, &mod);
769 TEST_LOCAL_PRINT(mod, 20);
770 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100771 assert_string_equal(printed, expect);
772 TEST_LOCAL_TEARDOWN;
773}
774
775static void
776basic_unified_indent_before_type(void **state)
777{
778 TEST_LOCAL_SETUP;
779 orig =
780 "module a19 {\n"
781 " yang-version 1.1;\n"
782 " namespace \"x:y\";\n"
783 " prefix x;\n"
784 "\n"
785 " typedef longType {\n"
786 " type string;\n"
787 " }\n"
788 " container A {\n"
789 " leaf Bnode {\n"
790 " type int8;\n"
791 " }\n"
792 " leaf Cnode {\n"
793 " type int8;\n"
794 " mandatory true;\n"
795 " }\n"
796 " leaf Dnode {\n"
797 " type int8;\n"
798 " mandatory true;\n"
799 " }\n"
800 " leaf E {\n"
801 " type longType;\n"
802 " mandatory true;\n"
803 " }\n"
804 " leaf G {\n"
805 " type int8;\n"
806 " }\n"
807 " }\n"
808 "}\n";
809
810 /* from pyang */
811 expect =
812 "module: a19\n"
813 " +--rw A\n"
814 " +--rw Bnode? int8\n"
815 " +--rw Cnode int8\n"
816 " +--rw Dnode int8\n"
817 " +--rw E longType\n"
818 " +--rw G? int8\n";
819
aPieceke376b922021-04-19 08:08:14 +0200820 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
821 TEST_LOCAL_PRINT(mod, 72);
822 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100823 assert_string_equal(printed, expect);
824 TEST_LOCAL_TEARDOWN;
825}
826
827static void
828twiddling_unified_indent_before_type(void **state)
829{
830 TEST_LOCAL_SETUP;
831 /* basic_functionality */
832
833 orig =
834 "module a20 {\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 CnodeIsBigger {\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 /* pyang --tree-line-length 36 */
865 expect =
866 "module: a20\n"
867 " +--rw A\n"
868 " +--rw Bnode? int8\n"
869 " +--rw CnodeIsBigger 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, 36);
876 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100877 assert_string_equal(printed, expect);
878
aPieceke376b922021-04-19 08:08:14 +0200879 ly_out_reset(UTEST_OUT);
aPiecek20ddf8b2021-01-08 11:50:37 +0100880 /* unified_indent_before_type_long_node_name */
881
882 /* pyang --tree-line-length 32 */
883 expect =
884 "module: a20\n"
885 " +--rw A\n"
886 " +--rw Bnode? int8\n"
887 " +--rw CnodeIsBigger int8\n"
888 " +--rw Dnode int8\n"
889 " +--rw E\n"
890 " | longType\n"
891 " +--rw G? int8\n";
892
aPieceke376b922021-04-19 08:08:14 +0200893 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
894 TEST_LOCAL_PRINT(mod, 32);
895 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100896 assert_string_equal(printed, expect);
897
aPieceke376b922021-04-19 08:08:14 +0200898 ly_out_reset(UTEST_OUT);
aPiecek20ddf8b2021-01-08 11:50:37 +0100899 /* unified_indent_before_type_long_node_type */
900
901 /* pyang --tree-line-length 31 */
902 expect =
903 "module: a20\n"
904 " +--rw A\n"
905 " +--rw Bnode?\n"
906 " | int8\n"
907 " +--rw CnodeIsBigger\n"
908 " | int8\n"
909 " +--rw Dnode\n"
910 " | int8\n"
911 " +--rw E\n"
912 " | longType\n"
913 " +--rw G?\n"
914 " int8\n";
915
aPieceke376b922021-04-19 08:08:14 +0200916 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
917 TEST_LOCAL_PRINT(mod, 31);
918 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100919 assert_string_equal(printed, expect);
920
921 TEST_LOCAL_TEARDOWN;
922}
923
924static void
925inheritance_of_config_flag(void **state)
926{
927 TEST_LOCAL_SETUP;
928 orig =
929 "module a21 {\n"
930 " yang-version 1.1;\n"
931 " namespace \"x:y\";\n"
932 " prefix x;\n"
933 " container a {\n"
934 " config false;\n"
935 " leaf b {\n"
936 " type string;\n"
937 " }\n"
938 " }\n"
939 "}\n";
940
941 /* from pyang */
942 expect =
943 "module: a21\n"
944 " +--ro a\n"
945 " +--ro b? string\n";
946
aPieceke376b922021-04-19 08:08:14 +0200947 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
948 TEST_LOCAL_PRINT(mod, 72);
949 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100950 assert_string_equal(printed, expect);
951 TEST_LOCAL_TEARDOWN;
952}
953
954static void
955inheritance_of_status_flag(void **state)
956{
957 TEST_LOCAL_SETUP;
958 /* throws libyang warn: Missing explicit "..." status that was already specified in parent, inheriting. */
959 orig =
960 "module a22 {\n"
961 " yang-version 1.1;\n"
962 " namespace \"x:y\";\n"
963 " prefix x;\n"
964 " container a {\n"
965 " status current;\n"
966 " container b {\n"
967 " status deprecated;\n"
968 " leaf f {\n"
969 " type string;\n"
970 " }\n"
971 " }\n"
972 " container c {\n"
973 " status obsolete;\n"
974 " leaf g {\n"
975 " type string;\n"
976 " }\n"
977 " }\n"
978 " }\n"
979 " container d {\n"
980 " status deprecated;\n"
981 " container h {\n"
982 " status obsolete;\n"
983 " leaf e {\n"
984 " type string;\n"
985 " }\n"
986 " }\n"
987 " }\n"
988 "}\n";
989
990 /* from yanglint 1 */
991 expect =
992 "module: a22\n"
993 " +--rw a\n"
994 " | x--rw b\n"
995 " | | x--rw f? string\n"
996 " | o--rw c\n"
997 " | o--rw g? string\n"
998 " x--rw d\n"
999 " o--rw h\n"
1000 " o--rw e? string\n";
1001
aPieceke376b922021-04-19 08:08:14 +02001002 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1003 TEST_LOCAL_PRINT(mod, 72);
1004 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +01001005 assert_string_equal(printed, expect);
1006 TEST_LOCAL_TEARDOWN;
1007}
1008
1009static void
1010key_leaf_is_always_mandatory_true(void **state)
1011{
1012 TEST_LOCAL_SETUP;
1013 orig =
1014 "module a23 {\n"
1015 " yang-version 1.1;\n"
1016 " namespace \"x:y\";\n"
1017 " prefix x;\n"
1018 " list a {\n"
1019 " key \"k1\";\n"
1020 " list b {\n"
1021 " key \"k2\";\n"
1022 " leaf k1 {\n"
1023 " type string;\n"
1024 " }\n"
1025 " leaf k2 {\n"
1026 " type string;\n"
1027 " }\n"
1028 " }\n"
1029 " leaf k1 {\n"
1030 " type string;\n"
1031 " }\n"
1032 " }\n"
1033 "}\n";
1034
1035 /* from pyang */
1036 expect =
1037 "module: a23\n"
1038 " +--rw a* [k1]\n"
1039 " +--rw b* [k2]\n"
1040 " | +--rw k1? string\n"
1041 " | +--rw k2 string\n"
1042 " +--rw k1 string\n";
1043
aPieceke376b922021-04-19 08:08:14 +02001044 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1045 TEST_LOCAL_PRINT(mod, 72);
1046 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +01001047 assert_string_equal(printed, expect);
1048 TEST_LOCAL_TEARDOWN;
1049}
1050
1051static void
1052transition_between_rpc_and_notif(void **state)
1053{
1054 TEST_LOCAL_SETUP;
1055 orig =
1056 "module a24 {\n"
1057 " yang-version 1.1;\n"
1058 " namespace \"x:y\";\n"
1059 " prefix x;\n"
1060 " container top {\n"
1061 " action rpc1 {\n"
1062 "\n"
1063 " input {\n"
1064 " leaf in {\n"
1065 " type string;\n"
1066 " }\n"
1067 " }\n"
1068 " }\n"
1069 " action rpc2 {\n"
1070 "\n"
1071 " input {\n"
1072 " leaf in {\n"
1073 " type string;\n"
1074 " }\n"
1075 " }\n"
1076 "\n"
1077 " output {\n"
1078 " leaf out {\n"
1079 " type string;\n"
1080 " }\n"
1081 " }\n"
1082 " }\n"
1083 " notification n1;\n"
1084 " notification n2;\n"
1085 " }\n"
1086 "}\n";
1087
1088 /* from pyang but edited -> n1 node is moved to n2 */
1089 expect =
1090 "module: a24\n"
1091 " +--rw top\n"
1092 " +---x rpc1\n"
1093 " | +---w input\n"
1094 " | +---w in? string\n"
1095 " +---x rpc2\n"
1096 " | +---w input\n"
1097 " | | +---w in? string\n"
1098 " | +--ro output\n"
1099 " | +--ro out? string\n"
1100 " +---n n1\n"
1101 " +---n n2\n";
1102
aPieceke376b922021-04-19 08:08:14 +02001103 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1104 TEST_LOCAL_PRINT(mod, 72);
1105 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +01001106 assert_string_equal(printed, expect);
1107 TEST_LOCAL_TEARDOWN;
1108}
1109
1110int
1111main(void)
1112{
1113 const struct CMUnitTest tests[] = {
1114 UTEST(base_sections),
1115 UTEST(node_status),
1116 UTEST(node_config_flags),
1117 UTEST(node_rpcs_flags),
1118 UTEST(node_grouping_flags),
1119 UTEST(notif_inside_container),
1120 UTEST(node_choice),
1121 UTEST(node_case),
1122 UTEST(optional_opts),
1123 UTEST(presence_container),
1124 UTEST(node_keys),
1125 UTEST(node_type_target),
1126 UTEST(node_type_leafref),
1127 UTEST(node_iffeatures),
1128 UTEST(indent_wrapper),
1129 UTEST(line_length_twiddling),
1130 UTEST(break_before_leafref),
1131 UTEST(break_before_leafref_and_iffeature),
1132 UTEST(basic_unified_indent_before_type),
1133 UTEST(twiddling_unified_indent_before_type),
1134 UTEST(inheritance_of_config_flag),
1135 UTEST(inheritance_of_status_flag),
1136 UTEST(key_leaf_is_always_mandatory_true),
1137 UTEST(transition_between_rpc_and_notif),
1138 };
1139
1140 return cmocka_run_group_tests(tests, NULL, NULL);
1141}