blob: 3e798389341e7bd82e43343ed47fd54090550a86 [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);
aPiecek9e505922021-04-19 13:45:08 +0200105
106 ly_out_reset(UTEST_OUT);
107
108 /* from pyang */
109 expect =
110 "module: a01\n"
111 " +--rw g\n"
112 "\n"
113 " augment /xx:c:\n"
114 " +--rw e\n"
115 " augment /xx:d:\n"
116 " +--rw f\n"
117 "\n"
118 " rpcs:\n"
119 " +---x rpc1\n"
120 " +---x rpc2\n"
121 "\n"
122 " notifications:\n"
123 " +---n n1\n"
124 " +---n n2\n";
125
126 /* using lysc tree */
127 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
128 TEST_LOCAL_PRINT(mod, 72);
129 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
130 assert_string_equal(printed, expect);
131 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
132
aPiecek20ddf8b2021-01-08 11:50:37 +0100133 TEST_LOCAL_TEARDOWN;
134}
135
136static void
137node_status(void **state)
138{
139 TEST_LOCAL_SETUP;
140 orig =
141 "module a02 {\n"
142 " yang-version 1.1;\n"
143 " namespace \"x:y\";\n"
144 " prefix x;\n"
145 " container l {\n"
146 " status current;\n"
147 " }\n"
148 " container m {\n"
149 " status deprecated;\n"
150 " }\n"
151 " container n {\n"
152 " status obsolete;\n"
153 " }\n"
154 "}\n";
155
156 /* from pyang */
157 expect =
158 "module: a02\n"
159 " +--rw l\n"
160 " x--rw m\n"
161 " o--rw n\n";
162
aPieceke376b922021-04-19 08:08:14 +0200163 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
164 TEST_LOCAL_PRINT(mod, 72);
165 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100166 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +0200167
168 ly_out_reset(UTEST_OUT);
169 /* using lysc tree */
170 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
171 TEST_LOCAL_PRINT(mod, 72);
172 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
173 assert_string_equal(printed, expect);
174 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
175
aPiecek20ddf8b2021-01-08 11:50:37 +0100176 TEST_LOCAL_TEARDOWN;
177}
178
179static void
180node_config_flags(void **state)
181{
182 TEST_LOCAL_SETUP;
183 orig =
184 "module a03 {\n"
185 " yang-version 1.1;\n"
186 " namespace \"x:y\";\n"
187 " prefix x;\n"
188 " container l {\n"
189 " config true;\n"
190 " }\n"
191 " container m {\n"
192 " config false;\n"
193 " }\n"
194 "}\n";
195
196 /* from pyang */
197 expect =
198 "module: a03\n"
199 " +--rw l\n"
200 " +--ro m\n";
201
aPieceke376b922021-04-19 08:08:14 +0200202 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
203 TEST_LOCAL_PRINT(mod, 72);
204 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100205 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +0200206
207 ly_out_reset(UTEST_OUT);
208 /* using lysc tree */
209 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
210 TEST_LOCAL_PRINT(mod, 72);
211 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
212 assert_string_equal(printed, expect);
213 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
214
aPiecek20ddf8b2021-01-08 11:50:37 +0100215 TEST_LOCAL_TEARDOWN;
216}
217
218static void
219node_rpcs_flags(void **state)
220{
221 TEST_LOCAL_SETUP;
222 orig =
223 "module a04 {\n"
224 " yang-version 1.1;\n"
225 " namespace \"x:y\";\n"
226 " prefix x;\n"
227 " container cont {\n"
228 " action rpc1 {\n"
229 "\n"
230 " input {\n"
231 " leaf in {\n"
232 " type string;\n"
233 " }\n"
234 " }\n"
aPiecek9e505922021-04-19 13:45:08 +0200235 "\n"
236 " output {\n"
237 " leaf out {\n"
238 " type string;\n"
239 " }\n"
240 " }\n"
aPiecek20ddf8b2021-01-08 11:50:37 +0100241 " }\n"
242 " }\n"
243 "}\n";
244
245 /* from pyang */
246 expect =
247 "module: a04\n"
248 " +--rw cont\n"
249 " +---x rpc1\n"
250 " +---w input\n"
aPiecek9e505922021-04-19 13:45:08 +0200251 " | +---w in? string\n"
252 " +--ro output\n"
253 " +--ro out? string\n";
aPiecek20ddf8b2021-01-08 11:50:37 +0100254
aPieceke376b922021-04-19 08:08:14 +0200255 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
256 TEST_LOCAL_PRINT(mod, 72);
257 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100258 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +0200259
260 ly_out_reset(UTEST_OUT);
261 /* using lysc tree */
262 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
263 TEST_LOCAL_PRINT(mod, 72);
264 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
265 assert_string_equal(printed, expect);
266 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
267
aPiecek20ddf8b2021-01-08 11:50:37 +0100268 TEST_LOCAL_TEARDOWN;
269}
270
271static void
272node_grouping_flags(void **state)
273{
274 TEST_LOCAL_SETUP;
275 orig =
276 "module a05 {\n"
277 " yang-version 1.1;\n"
278 " namespace \"x:y\";\n"
279 " prefix x;\n"
280 "\n"
281 " grouping g {\n"
aPiecekdc8fd572021-04-19 10:47:23 +0200282 " leaf a {\n"
283 " type string;\n"
284 " config true;\n"
285 " }\n"
286 " leaf b {\n"
287 " type string;\n"
288 " config false;\n"
289 " }\n"
290 " leaf c {\n"
291 " type string;\n"
292 " }\n"
293 " container d {\n"
294 " config false;\n"
295 " leaf e {\n"
296 " type string;\n"
297 " }\n"
298 " }\n"
299 " container f {\n"
300 " leaf g {\n"
301 " type string;\n"
302 " }\n"
303 " }\n"
aPiecek20ddf8b2021-01-08 11:50:37 +0100304 " }\n"
305 " container d {\n"
306 " uses g;\n"
307 " }\n"
308 "}\n";
309
aPiecekdc8fd572021-04-19 10:47:23 +0200310 /* from yanglint1 */
aPiecek20ddf8b2021-01-08 11:50:37 +0100311 expect =
312 "module: a05\n"
313 " +--rw d\n"
314 " +---u g\n"
315 "\n"
316 " grouping g:\n"
aPiecekdc8fd572021-04-19 10:47:23 +0200317 " +--rw a? string\n"
318 " +--ro b? string\n"
319 " +---- c? string\n"
320 " +--ro d\n"
321 " | +--ro e? string\n"
322 " +---- f\n"
323 " +---- g? string\n";
324
aPieceke376b922021-04-19 08:08:14 +0200325 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
326 TEST_LOCAL_PRINT(mod, 72);
327 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100328 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +0200329
330 /* from pyang */
331 expect =
332 "module: a05\n"
333 " +--rw d\n"
334 " +--rw a? string\n"
335 " +--ro b? string\n"
336 " +--rw c? string\n"
337 " +--ro d\n"
338 " | +--ro e? string\n"
339 " +--rw f\n"
340 " +--rw g? string\n";
341
342 ly_out_reset(UTEST_OUT);
343 /* using lysc tree */
344 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
345 TEST_LOCAL_PRINT(mod, 72);
346 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
347 assert_string_equal(printed, expect);
348 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
349
aPiecek20ddf8b2021-01-08 11:50:37 +0100350 TEST_LOCAL_TEARDOWN;
351}
352
353static void
354notif_inside_container(void **state)
355{
356 TEST_LOCAL_SETUP;
357 orig =
358 "module a06 {\n"
359 " yang-version 1.1;\n"
360 " namespace \"x:y\";\n"
361 " prefix x;\n"
362 " container c {\n"
363 " notification notif;\n"
364 " }\n"
365 "}\n";
366
367 /* from pyang */
368 expect =
369 "module: a06\n"
370 " +--rw c\n"
371 " +---n notif\n";
372
aPieceke376b922021-04-19 08:08:14 +0200373 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
374 TEST_LOCAL_PRINT(mod, 72);
375 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100376 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +0200377
378 ly_out_reset(UTEST_OUT);
379 /* using lysc tree */
380 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
381 TEST_LOCAL_PRINT(mod, 72);
382 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
383 assert_string_equal(printed, expect);
384 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
385
aPiecek20ddf8b2021-01-08 11:50:37 +0100386 TEST_LOCAL_TEARDOWN;
387}
388
389static void
390node_choice(void **state)
391{
392 TEST_LOCAL_SETUP;
393 orig =
394 "module a07 {\n"
395 " yang-version 1.1;\n"
396 " namespace \"x:y\";\n"
397 " prefix x;\n"
398 " choice my_choice;\n"
399 "}\n";
400
401 /* from pyang */
402 expect =
403 "module: a07\n"
404 " +--rw (my_choice)?\n";
405
aPieceke376b922021-04-19 08:08:14 +0200406 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
407 TEST_LOCAL_PRINT(mod, 72);
408 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100409 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +0200410
411 ly_out_reset(UTEST_OUT);
412 /* using lysc tree */
413 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
414 TEST_LOCAL_PRINT(mod, 72);
415 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
416 assert_string_equal(printed, expect);
417 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
418
aPiecek20ddf8b2021-01-08 11:50:37 +0100419 TEST_LOCAL_TEARDOWN;
420}
421
422static void
423node_case(void **state)
424{
425 TEST_LOCAL_SETUP;
426 orig =
427 "module a08 {\n"
428 " yang-version 1.1;\n"
429 " namespace \"x:y\";\n"
430 " prefix x;\n"
431 " choice my_choice {\n"
432 " case my_case;\n"
433 " }\n"
434 "}\n";
435
436 /* from pyang */
437 expect =
438 "module: a08\n"
439 " +--rw (my_choice)?\n"
440 " +--:(my_case)\n";
441
aPieceke376b922021-04-19 08:08:14 +0200442 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
443 TEST_LOCAL_PRINT(mod, 72);
444 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100445 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +0200446
447 ly_out_reset(UTEST_OUT);
448 /* using lysc tree */
449 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
450 TEST_LOCAL_PRINT(mod, 72);
451 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
452 assert_string_equal(printed, expect);
453 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
454
aPiecek20ddf8b2021-01-08 11:50:37 +0100455 TEST_LOCAL_TEARDOWN;
456}
457
458static void
459optional_opts(void **state)
460{
461 TEST_LOCAL_SETUP;
462 /* throws libyang warn: Use of anydata to define configuration data is not recommended... */
463 orig =
464 "module a09 {\n"
465 " yang-version 1.1;\n"
466 " namespace \"x:y\";\n"
467 " prefix x;\n"
468 " leaf l1 {\n"
469 " type string;\n"
470 " mandatory true;\n"
471 " }\n"
472 " leaf l2 {\n"
473 " type string;\n"
474 " mandatory false;\n"
475 " }\n"
476 " choice c1 {\n"
477 " mandatory true;\n"
478 " }\n"
479 " choice c2 {\n"
480 " mandatory false;\n"
481 " }\n"
482 " anydata a1 {\n"
483 " mandatory true;\n"
484 " }\n"
485 " anydata a2 {\n"
486 " mandatory false;\n"
487 " }\n"
488 " anyxml x1 {\n"
489 " mandatory true;\n"
490 " }\n"
491 " anyxml x2 {\n"
492 " mandatory false;\n"
493 " }\n"
494 "}\n";
495
496 /* from yanglint 1 */
497 expect =
498 "module: a09\n"
499 " +--rw l1 string\n"
500 " +--rw l2? string\n"
501 " +--rw (c1)\n"
502 " +--rw (c2)?\n"
503 " +--rw a1 anydata\n"
504 " +--rw a2? anydata\n"
505 " +--rw x1 anyxml\n"
506 " +--rw x2? anyxml\n";
507
aPieceke376b922021-04-19 08:08:14 +0200508 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
509 TEST_LOCAL_PRINT(mod, 72);
510 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100511 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +0200512
513 ly_out_reset(UTEST_OUT);
514 /* using lysc tree */
515 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
516 TEST_LOCAL_PRINT(mod, 72);
517 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
518 assert_string_equal(printed, expect);
519 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
520
aPiecek20ddf8b2021-01-08 11:50:37 +0100521 TEST_LOCAL_TEARDOWN;
522}
523
524static void
525presence_container(void **state)
526{
527 TEST_LOCAL_SETUP;
528 orig =
529 "module a10 {\n"
530 " yang-version 1.1;\n"
531 " namespace \"x:y\";\n"
532 " prefix x;\n"
533 " container c;\n"
534 " container d {\n"
535 " presence \"str1\";\n"
536 " }\n"
537 "}\n";
538
539 /* from pyang */
540 expect =
541 "module: a10\n"
542 " +--rw c\n"
543 " +--rw d!\n";
544
aPieceke376b922021-04-19 08:08:14 +0200545 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
546 TEST_LOCAL_PRINT(mod, 72);
547 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100548 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +0200549
550 ly_out_reset(UTEST_OUT);
551 /* using lysc tree */
552 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
553 TEST_LOCAL_PRINT(mod, 72);
554 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
555 assert_string_equal(printed, expect);
556 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
557
aPiecek20ddf8b2021-01-08 11:50:37 +0100558 TEST_LOCAL_TEARDOWN;
559}
560
561static void
562node_keys(void **state)
563{
564 TEST_LOCAL_SETUP;
565 orig =
566 "module a11 {\n"
567 " yang-version 1.1;\n"
568 " namespace \"x:y\";\n"
569 " prefix x;\n"
570 " list l1 {\n"
571 " key \"a\";\n"
572 " leaf a {\n"
573 " type string;\n"
574 " }\n"
575 " }\n"
576 " list l2 {\n"
577 " key \"a b\";\n"
578 " leaf a {\n"
579 " type string;\n"
580 " }\n"
581 " leaf b {\n"
582 " type string;\n"
583 " }\n"
584 " }\n"
585 " leaf-list ll {\n"
586 " type string;\n"
587 " }\n"
588 "}\n";
589
590 /* from pyang */
591 expect =
592 "module: a11\n"
593 " +--rw l1* [a]\n"
594 " | +--rw a string\n"
595 " +--rw l2* [a b]\n"
596 " | +--rw a string\n"
597 " | +--rw b string\n"
598 " +--rw ll* string\n";
599
aPieceke376b922021-04-19 08:08:14 +0200600 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
601 TEST_LOCAL_PRINT(mod, 72);
602 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100603 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +0200604
605 ly_out_reset(UTEST_OUT);
606 /* using lysc tree */
607 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
608 TEST_LOCAL_PRINT(mod, 72);
609 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
610 assert_string_equal(printed, expect);
611 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
612
aPiecek20ddf8b2021-01-08 11:50:37 +0100613 TEST_LOCAL_TEARDOWN;
614}
615
616static void
617node_type_target(void **state)
618{
619 TEST_LOCAL_SETUP;
620 orig =
621 "module a12 {\n"
622 " yang-version 1.1;\n"
623 " namespace \"x:y\";\n"
624 " prefix x;\n"
625 " leaf a {\n"
626 " type leafref {\n"
627 " path \"/x:b\";\n"
628 " }\n"
629 " }\n"
630 " leaf b {\n"
631 " type string;\n"
632 " }\n"
633 "}\n";
634
635 /* from yanglint 1 */
636 expect =
637 "module: a12\n"
638 " +--rw a? -> /x:b\n"
639 " +--rw b? string\n";
640
aPieceke376b922021-04-19 08:08:14 +0200641 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
642 TEST_LOCAL_PRINT(mod, 72);
643 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100644 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +0200645
646 ly_out_reset(UTEST_OUT);
647 /* using lysc tree */
648 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
649 TEST_LOCAL_PRINT(mod, 72);
650 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
651 assert_string_equal(printed, expect);
652 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
653
aPiecek20ddf8b2021-01-08 11:50:37 +0100654 TEST_LOCAL_TEARDOWN;
655}
656
657static void
658node_type_leafref(void **state)
659{
660 TEST_LOCAL_SETUP;
661 orig =
662 "module a13 {\n"
663 " yang-version 1.1;\n"
664 " namespace \"x:y\";\n"
665 " prefix x;\n"
666 " leaf pretty-long-identifier-name-which-should-exceed-the-limit-of-72-characters {\n"
667 " type string;\n"
668 " }\n"
669 " leaf a {\n"
670 " type leafref {\n"
671 " path \"/x:pretty-long-identifier-name-which-should-exceed-the-limit-of-72-characters\";\n"
672 " }\n"
673 " }\n"
674 "}\n";
675
676 /* yanglint --tree-no-leafref-target --tree-line-length=72 */
677 expect =
678 "module: a13\n"
679 " +--rw pretty-long-identifier-name-which-should-exceed-the-limit-of-72-characters?\n"
680 " | string\n"
681 " +--rw a?\n"
682 " leafref\n";
683
aPieceke376b922021-04-19 08:08:14 +0200684 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
685 TEST_LOCAL_PRINT(mod, 72);
686 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100687 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +0200688
689 ly_out_reset(UTEST_OUT);
690 /* using lysc tree */
691 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
692 TEST_LOCAL_PRINT(mod, 72);
693 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
694 assert_string_equal(printed, expect);
695 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
696
aPiecek20ddf8b2021-01-08 11:50:37 +0100697 TEST_LOCAL_TEARDOWN;
698}
699
700static void
701node_iffeatures(void **state)
702{
703 TEST_LOCAL_SETUP;
704 orig =
705 "module a14 {\n"
706 " yang-version 1.1;\n"
707 " namespace \"x:y\";\n"
708 " prefix x;\n"
709 "\n"
710 " feature foo;\n"
711 "\n"
712 " feature bar;\n"
713 " container c {\n"
714 " if-feature \"foo or bar\";\n"
715 " }\n"
716 "}\n";
717
718 /* from pyang */
719 expect =
720 "module: a14\n"
721 " +--rw c {foo or bar}?\n";
722
aPieceke376b922021-04-19 08:08:14 +0200723 const char *feats[] = {"foo", NULL};
724
725 UTEST_ADD_MODULE(orig, LYS_IN_YANG, feats, &mod);
726 TEST_LOCAL_PRINT(mod, 72);
727 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100728 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +0200729
730 ly_out_reset(UTEST_OUT);
731 /* using lysc tree */
732 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
733 TEST_LOCAL_PRINT(mod, 72);
734 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
735 assert_string_equal(printed, expect);
736 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
737
aPiecek20ddf8b2021-01-08 11:50:37 +0100738 TEST_LOCAL_TEARDOWN;
739}
740
741static void
742indent_wrapper(void **state)
743{
744 TEST_LOCAL_SETUP;
745 orig =
746 "module a15 {\n"
747 " yang-version 1.1;\n"
748 " namespace \"x:y\";\n"
749 " prefix x;\n"
750 " container a {\n"
751 " container b;\n"
752 " }\n"
753 " container c {\n"
754 " container d {\n"
755 " container e;\n"
756 " }\n"
757 " container f {\n"
758 " container g;\n"
759 " }\n"
760 " }\n"
761 " container h;\n"
762 " container i {\n"
763 " container j;\n"
764 " container k;\n"
765 " }\n"
766 "}\n";
767
768 /* from pyang */
769 expect =
770 "module: a15\n"
771 " +--rw a\n"
772 " | +--rw b\n"
773 " +--rw c\n"
774 " | +--rw d\n"
775 " | | +--rw e\n"
776 " | +--rw f\n"
777 " | +--rw g\n"
778 " +--rw h\n"
779 " +--rw i\n"
780 " +--rw j\n"
781 " +--rw k\n";
782
aPieceke376b922021-04-19 08:08:14 +0200783 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
784 TEST_LOCAL_PRINT(mod, 72);
785 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100786 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +0200787
788 ly_out_reset(UTEST_OUT);
789 /* using lysc tree */
790 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
791 TEST_LOCAL_PRINT(mod, 72);
792 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
793 assert_string_equal(printed, expect);
794 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
795
aPiecek20ddf8b2021-01-08 11:50:37 +0100796 TEST_LOCAL_TEARDOWN;
797}
798
799static void
800line_length_twiddling(void **state)
801{
802 TEST_LOCAL_SETUP;
803 /* node_fits_tight */
804
805 orig =
806 "module a16 {\n"
807 " yang-version 1.1;\n"
808 " namespace \"x:y\";\n"
809 " prefix x;\n"
810 "\n"
811 " feature f;\n"
812 "\n"
813 " typedef some-long-type {\n"
814 " type string;\n"
815 " }\n"
816 " list my-list-name {\n"
817 " key \"key\";\n"
818 " leaf key {\n"
819 " type string;\n"
820 " }\n"
821 " leaf nod-leaf {\n"
822 " if-feature \"f\";\n"
823 " type some-long-type;\n"
824 " }\n"
825 " leaf nos-leaf {\n"
826 " if-feature \"f\";\n"
827 " type int32;\n"
828 " }\n"
829 " }\n"
830 "}\n";
831
832 /* pyang --tree-line-length 42 */
833 expect =
834 "module: a16\n"
835 " +--rw my-list-name* [key]\n"
836 " +--rw key string\n"
837 " +--rw nod-leaf? some-long-type {f}?\n"
838 " +--rw nos-leaf? int32 {f}?\n";
839
aPieceke376b922021-04-19 08:08:14 +0200840 const char *feats[] = {"f", NULL};
841
842 UTEST_ADD_MODULE(orig, LYS_IN_YANG, feats, &mod);
843 TEST_LOCAL_PRINT(mod, 42);
844 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100845 assert_string_equal(printed, expect);
846
aPieceke376b922021-04-19 08:08:14 +0200847 ly_out_reset(UTEST_OUT);
aPiecek9e505922021-04-19 13:45:08 +0200848 /* using lysc tree */
849 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
850 TEST_LOCAL_PRINT(mod, 42);
851 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
852 assert_string_equal(printed, expect);
853 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
854
855 ly_out_reset(UTEST_OUT);
aPiecek20ddf8b2021-01-08 11:50:37 +0100856 /* break_before_iffeature */
857
858 /* pyang --tree-line-length 41 */
859 expect =
860 "module: a16\n"
861 " +--rw my-list-name* [key]\n"
862 " +--rw key string\n"
863 " +--rw nod-leaf? some-long-type\n"
864 " | {f}?\n"
865 " +--rw nos-leaf? int32 {f}?\n";
866
aPieceke376b922021-04-19 08:08:14 +0200867 UTEST_ADD_MODULE(orig, LYS_IN_YANG, feats, &mod);
868 TEST_LOCAL_PRINT(mod, 41);
869 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100870 assert_string_equal(printed, expect);
871
aPieceke376b922021-04-19 08:08:14 +0200872 ly_out_reset(UTEST_OUT);
aPiecek9e505922021-04-19 13:45:08 +0200873 /* using lysc tree */
874 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
875 TEST_LOCAL_PRINT(mod, 41);
876 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
877 assert_string_equal(printed, expect);
878 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
879
880 ly_out_reset(UTEST_OUT);
aPiecek20ddf8b2021-01-08 11:50:37 +0100881 /* break_before_type */
882
883 /* pyang --tree-line-length 29 */
884 expect =
885 "module: a16\n"
886 " +--rw my-list-name* [key]\n"
887 " +--rw key string\n"
888 " +--rw nod-leaf?\n"
889 " | some-long-type\n"
890 " | {f}?\n"
891 " +--rw nos-leaf? int32\n"
892 " {f}?\n";
893
aPieceke376b922021-04-19 08:08:14 +0200894 UTEST_ADD_MODULE(orig, LYS_IN_YANG, feats, &mod);
895 TEST_LOCAL_PRINT(mod, 29);
896 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100897 assert_string_equal(printed, expect);
898
aPieceke376b922021-04-19 08:08:14 +0200899 ly_out_reset(UTEST_OUT);
aPiecek9e505922021-04-19 13:45:08 +0200900 /* using lysc tree */
901 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
902 TEST_LOCAL_PRINT(mod, 29);
903 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
904 assert_string_equal(printed, expect);
905 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
906
907 ly_out_reset(UTEST_OUT);
aPiecek20ddf8b2021-01-08 11:50:37 +0100908 /* break_before_keys */
909
910 /* pyang --tree-line-length 23 */
911 expect =
912 "module: a16\n"
913 " +--rw my-list-name*\n"
914 " [key]\n"
915 " +--rw key\n"
916 " | string\n"
917 " +--rw nod-leaf?\n"
918 " | some-long-type\n"
919 " | {f}?\n"
920 " +--rw nos-leaf?\n"
921 " int32 {f}?\n";
922
aPieceke376b922021-04-19 08:08:14 +0200923 UTEST_ADD_MODULE(orig, LYS_IN_YANG, feats, &mod);
924 TEST_LOCAL_PRINT(mod, 23);
925 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100926 assert_string_equal(printed, expect);
927
aPieceke376b922021-04-19 08:08:14 +0200928 ly_out_reset(UTEST_OUT);
aPiecek9e505922021-04-19 13:45:08 +0200929 /* using lysc tree */
930 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
931 TEST_LOCAL_PRINT(mod, 23);
932 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
933 assert_string_equal(printed, expect);
934 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
935
936 ly_out_reset(UTEST_OUT);
aPiecek20ddf8b2021-01-08 11:50:37 +0100937 /* every_node_name_is_too_long */
938
939 /* pyang --tree-line-length 14 */
940 expect =
941 "module: a16\n"
942 " +--rw my-list-name*\n"
943 " [key]\n"
944 " +--rw key\n"
945 " | string\n"
946 " +--rw nod-leaf?\n"
947 " | some-long-type\n"
948 " | {f}?\n"
949 " +--rw nos-leaf?\n"
950 " int32\n"
951 " {f}?\n";
952
aPieceke376b922021-04-19 08:08:14 +0200953 UTEST_ADD_MODULE(orig, LYS_IN_YANG, feats, &mod);
954 TEST_LOCAL_PRINT(mod, 14);
955 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100956 assert_string_equal(printed, expect);
957
aPiecek9e505922021-04-19 13:45:08 +0200958 ly_out_reset(UTEST_OUT);
959 /* using lysc tree */
960 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
961 TEST_LOCAL_PRINT(mod, 14);
962 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
963 assert_string_equal(printed, expect);
964 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
965
aPiecek20ddf8b2021-01-08 11:50:37 +0100966 TEST_LOCAL_TEARDOWN;
967}
968
969static void
970break_before_leafref(void **state)
971{
972 TEST_LOCAL_SETUP;
973 orig =
974 "module a17 {\n"
975 " yang-version 1.1;\n"
976 " namespace \"x:y\";\n"
977 " prefix x;\n"
978 " leaf e {\n"
979 " type string;\n"
980 " }\n"
981 " leaf abcd {\n"
982 " type leafref {\n"
983 " path \"/x:e\";\n"
984 " }\n"
985 " }\n"
986 "}\n";
987
988 /* yanglint --tree-line-length 14 */
989 expect =
990 "module: a17\n"
991 " +--rw e?\n"
992 " | string\n"
993 " +--rw abcd?\n"
994 " -> /x:e\n";
995
aPieceke376b922021-04-19 08:08:14 +0200996 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
997 TEST_LOCAL_PRINT(mod, 14);
998 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100999 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +02001000
1001 ly_out_reset(UTEST_OUT);
1002 /* using lysc tree */
1003 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1004 TEST_LOCAL_PRINT(mod, 14);
1005 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1006 assert_string_equal(printed, expect);
1007 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1008
aPiecek20ddf8b2021-01-08 11:50:37 +01001009 TEST_LOCAL_TEARDOWN;
1010}
1011
1012static void
1013break_before_leafref_and_iffeature(void **state)
1014{
1015 TEST_LOCAL_SETUP;
1016 orig =
1017 "module a18 {\n"
1018 " yang-version 1.1;\n"
1019 " namespace \"x:y\";\n"
1020 " prefix x;\n"
1021 "\n"
1022 " feature f;\n"
1023 " leaf some-long-id {\n"
1024 " type string;\n"
1025 " }\n"
1026 " leaf a {\n"
1027 " if-feature \"f\";\n"
1028 " type leafref {\n"
1029 " path \"/x:some-long-id\";\n"
1030 " }\n"
1031 " }\n"
1032 "}\n";
1033
1034 /* yanglint --tree-no-leafref-target --tree-line-length=20 */
1035 expect =
1036 "module: a18\n"
1037 " +--rw some-long-id?\n"
1038 " | string\n"
1039 " +--rw a?\n"
1040 " leafref\n"
1041 " {f}?\n";
1042
aPieceke376b922021-04-19 08:08:14 +02001043 const char *feats[] = {"f", NULL};
1044
1045 UTEST_ADD_MODULE(orig, LYS_IN_YANG, feats, &mod);
1046 TEST_LOCAL_PRINT(mod, 20);
1047 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +01001048 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +02001049
1050 ly_out_reset(UTEST_OUT);
1051 /* using lysc tree */
1052 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1053 TEST_LOCAL_PRINT(mod, 20);
1054 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1055 assert_string_equal(printed, expect);
1056 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1057
aPiecek20ddf8b2021-01-08 11:50:37 +01001058 TEST_LOCAL_TEARDOWN;
1059}
1060
1061static void
1062basic_unified_indent_before_type(void **state)
1063{
1064 TEST_LOCAL_SETUP;
1065 orig =
1066 "module a19 {\n"
1067 " yang-version 1.1;\n"
1068 " namespace \"x:y\";\n"
1069 " prefix x;\n"
1070 "\n"
1071 " typedef longType {\n"
1072 " type string;\n"
1073 " }\n"
1074 " container A {\n"
1075 " leaf Bnode {\n"
1076 " type int8;\n"
1077 " }\n"
1078 " leaf Cnode {\n"
1079 " type int8;\n"
1080 " mandatory true;\n"
1081 " }\n"
1082 " leaf Dnode {\n"
1083 " type int8;\n"
1084 " mandatory true;\n"
1085 " }\n"
1086 " leaf E {\n"
1087 " type longType;\n"
1088 " mandatory true;\n"
1089 " }\n"
1090 " leaf G {\n"
1091 " type int8;\n"
1092 " }\n"
1093 " }\n"
1094 "}\n";
1095
1096 /* from pyang */
1097 expect =
1098 "module: a19\n"
1099 " +--rw A\n"
1100 " +--rw Bnode? int8\n"
1101 " +--rw Cnode int8\n"
1102 " +--rw Dnode int8\n"
1103 " +--rw E longType\n"
1104 " +--rw G? int8\n";
1105
aPieceke376b922021-04-19 08:08:14 +02001106 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1107 TEST_LOCAL_PRINT(mod, 72);
1108 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +01001109 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +02001110
1111 ly_out_reset(UTEST_OUT);
1112 /* using lysc tree */
1113 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1114 TEST_LOCAL_PRINT(mod, 72);
1115 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1116 assert_string_equal(printed, expect);
1117 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1118
aPiecek20ddf8b2021-01-08 11:50:37 +01001119 TEST_LOCAL_TEARDOWN;
1120}
1121
1122static void
1123twiddling_unified_indent_before_type(void **state)
1124{
1125 TEST_LOCAL_SETUP;
1126 /* basic_functionality */
1127
1128 orig =
1129 "module a20 {\n"
1130 " yang-version 1.1;\n"
1131 " namespace \"x:y\";\n"
1132 " prefix x;\n"
1133 "\n"
1134 " typedef longType {\n"
1135 " type string;\n"
1136 " }\n"
1137 " container A {\n"
1138 " leaf Bnode {\n"
1139 " type int8;\n"
1140 " }\n"
1141 " leaf CnodeIsBigger {\n"
1142 " type int8;\n"
1143 " mandatory true;\n"
1144 " }\n"
1145 " leaf Dnode {\n"
1146 " type int8;\n"
1147 " mandatory true;\n"
1148 " }\n"
1149 " leaf E {\n"
1150 " type longType;\n"
1151 " mandatory true;\n"
1152 " }\n"
1153 " leaf G {\n"
1154 " type int8;\n"
1155 " }\n"
1156 " }\n"
1157 "}\n";
1158
1159 /* pyang --tree-line-length 36 */
1160 expect =
1161 "module: a20\n"
1162 " +--rw A\n"
1163 " +--rw Bnode? int8\n"
1164 " +--rw CnodeIsBigger int8\n"
1165 " +--rw Dnode int8\n"
1166 " +--rw E longType\n"
1167 " +--rw G? int8\n";
1168
aPieceke376b922021-04-19 08:08:14 +02001169 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1170 TEST_LOCAL_PRINT(mod, 36);
1171 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +01001172 assert_string_equal(printed, expect);
1173
aPieceke376b922021-04-19 08:08:14 +02001174 ly_out_reset(UTEST_OUT);
aPiecek9e505922021-04-19 13:45:08 +02001175 /* using lysc tree */
1176 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1177 TEST_LOCAL_PRINT(mod, 36);
1178 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1179 assert_string_equal(printed, expect);
1180 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1181
1182 ly_out_reset(UTEST_OUT);
aPiecek20ddf8b2021-01-08 11:50:37 +01001183 /* unified_indent_before_type_long_node_name */
1184
1185 /* pyang --tree-line-length 32 */
1186 expect =
1187 "module: a20\n"
1188 " +--rw A\n"
1189 " +--rw Bnode? int8\n"
1190 " +--rw CnodeIsBigger int8\n"
1191 " +--rw Dnode int8\n"
1192 " +--rw E\n"
1193 " | longType\n"
1194 " +--rw G? int8\n";
1195
aPieceke376b922021-04-19 08:08:14 +02001196 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1197 TEST_LOCAL_PRINT(mod, 32);
1198 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +01001199 assert_string_equal(printed, expect);
1200
aPieceke376b922021-04-19 08:08:14 +02001201 ly_out_reset(UTEST_OUT);
aPiecek9e505922021-04-19 13:45:08 +02001202 /* using lysc tree */
1203 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1204 TEST_LOCAL_PRINT(mod, 32);
1205 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1206 assert_string_equal(printed, expect);
1207 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1208
1209 ly_out_reset(UTEST_OUT);
aPiecek20ddf8b2021-01-08 11:50:37 +01001210 /* unified_indent_before_type_long_node_type */
1211
1212 /* pyang --tree-line-length 31 */
1213 expect =
1214 "module: a20\n"
1215 " +--rw A\n"
1216 " +--rw Bnode?\n"
1217 " | int8\n"
1218 " +--rw CnodeIsBigger\n"
1219 " | int8\n"
1220 " +--rw Dnode\n"
1221 " | int8\n"
1222 " +--rw E\n"
1223 " | longType\n"
1224 " +--rw G?\n"
1225 " int8\n";
1226
aPieceke376b922021-04-19 08:08:14 +02001227 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1228 TEST_LOCAL_PRINT(mod, 31);
1229 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +01001230 assert_string_equal(printed, expect);
1231
aPiecek9e505922021-04-19 13:45:08 +02001232 ly_out_reset(UTEST_OUT);
1233 /* using lysc tree */
1234 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1235 TEST_LOCAL_PRINT(mod, 31);
1236 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1237 assert_string_equal(printed, expect);
1238 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1239
aPiecek20ddf8b2021-01-08 11:50:37 +01001240 TEST_LOCAL_TEARDOWN;
1241}
1242
1243static void
1244inheritance_of_config_flag(void **state)
1245{
1246 TEST_LOCAL_SETUP;
1247 orig =
1248 "module a21 {\n"
1249 " yang-version 1.1;\n"
1250 " namespace \"x:y\";\n"
1251 " prefix x;\n"
1252 " container a {\n"
1253 " config false;\n"
1254 " leaf b {\n"
1255 " type string;\n"
1256 " }\n"
1257 " }\n"
1258 "}\n";
1259
1260 /* from pyang */
1261 expect =
1262 "module: a21\n"
1263 " +--ro a\n"
1264 " +--ro b? string\n";
1265
aPieceke376b922021-04-19 08:08:14 +02001266 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1267 TEST_LOCAL_PRINT(mod, 72);
1268 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +01001269 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +02001270
1271 ly_out_reset(UTEST_OUT);
1272 /* using lysc tree */
1273 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1274 TEST_LOCAL_PRINT(mod, 72);
1275 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1276 assert_string_equal(printed, expect);
1277 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1278
aPiecek20ddf8b2021-01-08 11:50:37 +01001279 TEST_LOCAL_TEARDOWN;
1280}
1281
1282static void
1283inheritance_of_status_flag(void **state)
1284{
1285 TEST_LOCAL_SETUP;
1286 /* throws libyang warn: Missing explicit "..." status that was already specified in parent, inheriting. */
1287 orig =
1288 "module a22 {\n"
1289 " yang-version 1.1;\n"
1290 " namespace \"x:y\";\n"
1291 " prefix x;\n"
1292 " container a {\n"
1293 " status current;\n"
1294 " container b {\n"
1295 " status deprecated;\n"
1296 " leaf f {\n"
1297 " type string;\n"
1298 " }\n"
1299 " }\n"
1300 " container c {\n"
1301 " status obsolete;\n"
1302 " leaf g {\n"
1303 " type string;\n"
1304 " }\n"
1305 " }\n"
1306 " }\n"
1307 " container d {\n"
1308 " status deprecated;\n"
1309 " container h {\n"
1310 " status obsolete;\n"
1311 " leaf e {\n"
1312 " type string;\n"
1313 " }\n"
1314 " }\n"
1315 " }\n"
1316 "}\n";
1317
1318 /* from yanglint 1 */
1319 expect =
1320 "module: a22\n"
1321 " +--rw a\n"
1322 " | x--rw b\n"
1323 " | | x--rw f? string\n"
1324 " | o--rw c\n"
1325 " | o--rw g? string\n"
1326 " x--rw d\n"
1327 " o--rw h\n"
1328 " o--rw e? string\n";
1329
aPieceke376b922021-04-19 08:08:14 +02001330 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1331 TEST_LOCAL_PRINT(mod, 72);
1332 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +01001333 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +02001334
1335 ly_out_reset(UTEST_OUT);
1336 /* using lysc tree */
1337 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1338 TEST_LOCAL_PRINT(mod, 72);
1339 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1340 assert_string_equal(printed, expect);
1341 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1342
aPiecek20ddf8b2021-01-08 11:50:37 +01001343 TEST_LOCAL_TEARDOWN;
1344}
1345
1346static void
1347key_leaf_is_always_mandatory_true(void **state)
1348{
1349 TEST_LOCAL_SETUP;
1350 orig =
1351 "module a23 {\n"
1352 " yang-version 1.1;\n"
1353 " namespace \"x:y\";\n"
1354 " prefix x;\n"
1355 " list a {\n"
1356 " key \"k1\";\n"
1357 " list b {\n"
1358 " key \"k2\";\n"
1359 " leaf k1 {\n"
1360 " type string;\n"
1361 " }\n"
1362 " leaf k2 {\n"
1363 " type string;\n"
1364 " }\n"
1365 " }\n"
1366 " leaf k1 {\n"
1367 " type string;\n"
1368 " }\n"
1369 " }\n"
1370 "}\n";
1371
1372 /* from pyang */
1373 expect =
1374 "module: a23\n"
1375 " +--rw a* [k1]\n"
1376 " +--rw b* [k2]\n"
1377 " | +--rw k1? string\n"
1378 " | +--rw k2 string\n"
1379 " +--rw k1 string\n";
1380
aPieceke376b922021-04-19 08:08:14 +02001381 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1382 TEST_LOCAL_PRINT(mod, 72);
1383 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +01001384 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +02001385
1386 ly_out_reset(UTEST_OUT);
1387
1388 /* from pyang but with some swapped lines */
1389 expect =
1390 "module: a23\n"
1391 " +--rw a* [k1]\n"
1392 " +--rw k1 string\n"
1393 " +--rw b* [k2]\n"
1394 " +--rw k2 string\n"
1395 " +--rw k1? string\n";
1396
1397 /* using lysc tree */
1398 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1399 TEST_LOCAL_PRINT(mod, 72);
1400 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1401 assert_string_equal(printed, expect);
1402 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1403
aPiecek20ddf8b2021-01-08 11:50:37 +01001404 TEST_LOCAL_TEARDOWN;
1405}
1406
1407static void
1408transition_between_rpc_and_notif(void **state)
1409{
1410 TEST_LOCAL_SETUP;
1411 orig =
1412 "module a24 {\n"
1413 " yang-version 1.1;\n"
1414 " namespace \"x:y\";\n"
1415 " prefix x;\n"
1416 " container top {\n"
aPiecek9e505922021-04-19 13:45:08 +02001417 " leaf g {\n"
1418 " type string;\n"
1419 " }\n"
aPiecek20ddf8b2021-01-08 11:50:37 +01001420 " action rpc1 {\n"
1421 "\n"
1422 " input {\n"
1423 " leaf in {\n"
1424 " type string;\n"
1425 " }\n"
1426 " }\n"
1427 " }\n"
1428 " action rpc2 {\n"
1429 "\n"
1430 " input {\n"
1431 " leaf in {\n"
1432 " type string;\n"
1433 " }\n"
1434 " }\n"
1435 "\n"
1436 " output {\n"
1437 " leaf out {\n"
1438 " type string;\n"
1439 " }\n"
1440 " }\n"
1441 " }\n"
1442 " notification n1;\n"
1443 " notification n2;\n"
1444 " }\n"
1445 "}\n";
1446
1447 /* from pyang but edited -> n1 node is moved to n2 */
1448 expect =
1449 "module: a24\n"
1450 " +--rw top\n"
aPiecek9e505922021-04-19 13:45:08 +02001451 " +--rw g? string\n"
aPiecek20ddf8b2021-01-08 11:50:37 +01001452 " +---x rpc1\n"
1453 " | +---w input\n"
1454 " | +---w in? string\n"
1455 " +---x rpc2\n"
1456 " | +---w input\n"
1457 " | | +---w in? string\n"
1458 " | +--ro output\n"
1459 " | +--ro out? string\n"
1460 " +---n n1\n"
1461 " +---n n2\n";
1462
aPieceke376b922021-04-19 08:08:14 +02001463 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1464 TEST_LOCAL_PRINT(mod, 72);
1465 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +01001466 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +02001467
1468 ly_out_reset(UTEST_OUT);
1469 /* using lysc tree */
1470 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1471 TEST_LOCAL_PRINT(mod, 72);
1472 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1473 assert_string_equal(printed, expect);
1474 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1475
1476 TEST_LOCAL_TEARDOWN;
1477}
1478
1479static void
1480local_augment(void **state)
1481{
1482 TEST_LOCAL_SETUP;
1483
1484 orig =
1485 "module a25 {\n"
1486 " yang-version 1.1;\n"
1487 " namespace \"x:y\";\n"
1488 " prefix x;\n"
1489 " container g;\n"
1490 " augment \"/x:g\" {\n"
1491 " container e;\n"
1492 " }\n"
1493 "}\n";
1494
1495 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1496
1497 /* from pyang */
1498 expect =
1499 "module: a25\n"
1500 " +--rw g\n"
1501 " +--rw e\n";
1502
1503 /* using lysc tree */
1504 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1505 TEST_LOCAL_PRINT(mod, 72);
1506 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1507 assert_string_equal(printed, expect);
1508 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1509
aPiecek20ddf8b2021-01-08 11:50:37 +01001510 TEST_LOCAL_TEARDOWN;
1511}
1512
aPiecek7745b282021-04-20 13:54:52 +02001513static void
1514print_compiled_node(void **state)
1515{
1516 TEST_LOCAL_SETUP;
1517 const struct lysc_node *node;
1518
1519 orig =
1520 "module a26 {\n"
1521 " yang-version 1.1;\n"
1522 " namespace \"x:y\";\n"
1523 " prefix x;\n"
1524 " container g {\n"
1525 " leaf a {\n"
1526 " type string;\n"
1527 " }\n"
1528 " container h {\n"
1529 " leaf b {\n"
1530 " type string;\n"
1531 " mandatory true;\n"
1532 " }\n"
1533 " leaf c {\n"
1534 " type string;\n"
1535 " }\n"
1536 " }\n"
1537 " }\n"
1538 "}\n";
1539
1540 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1541
1542 /* pyang -f tree --tree-path /g/h/c */
1543 expect =
1544 "module: a26\n"
1545 " +--rw g\n"
1546 " +--rw h\n"
1547 " +--rw c? string\n";
1548
1549 /* using lysc tree */
1550 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1551
1552 node = lys_find_path(UTEST_LYCTX, NULL, "/a26:g/h/c", 0);
1553 CHECK_POINTER(node, 1);
1554 assert_int_equal(LY_SUCCESS, lys_print_node(UTEST_OUT, node, LYS_OUT_TREE, 72, 0));
1555 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1556 assert_string_equal(printed, expect);
1557
1558 ly_out_reset(UTEST_OUT);
1559
1560 /* pyang -f tree --tree-path /g/h */
1561 expect =
1562 "module: a26\n"
1563 " +--rw g\n"
1564 " +--rw h\n"
1565 " +--rw b string\n"
1566 " +--rw c? string\n";
1567
1568 node = lys_find_path(UTEST_LYCTX, NULL, "/a26:g/h", 0);
1569 CHECK_POINTER(node, 1);
1570 assert_int_equal(LY_SUCCESS, lys_print_node(UTEST_OUT, node, LYS_OUT_TREE, 72, 0));
1571 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1572 assert_string_equal(printed, expect);
1573
1574 ly_out_reset(UTEST_OUT);
1575
1576 /* pyang whose output is adjusted manually */
1577 expect =
1578 "module: a26\n"
1579 " +--rw g\n"
1580 " +--rw h\n";
1581
1582 node = lys_find_path(UTEST_LYCTX, NULL, "/a26:g/h", 0);
1583 CHECK_POINTER(node, 1);
1584 assert_int_equal(LY_SUCCESS, lys_print_node(UTEST_OUT, node, LYS_OUT_TREE, 72, LYS_PRINT_NO_SUBSTMT));
1585 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1586 assert_string_equal(printed, expect);
1587
1588 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1589
1590 TEST_LOCAL_TEARDOWN;
1591}
1592
aPiecekaecf7512021-04-21 08:34:35 +02001593static LY_ERR
1594local_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
1595 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
1596 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
1597{
1598 *module_data = user_data;
1599 *format = LYS_IN_YANG;
1600 *free_module_data = NULL;
1601 return LY_SUCCESS;
1602}
1603
1604static void
1605print_parsed_submodule(void **state)
1606{
1607 TEST_LOCAL_SETUP;
1608
1609 orig = "module a27 {\n"
1610 " yang-version 1.1;\n"
1611 " namespace \"x:y\";\n"
1612 " prefix x;\n"
1613 "\n"
1614 " include \"a27-sub\";\n"
1615 "}\n";
1616
1617 char *submodule =
1618 "submodule a27-sub {\n"
1619 " yang-version 1.1;\n"
1620 " belongs-to a27 {\n"
1621 " prefix x;\n"
1622 " }\n"
1623 "\n"
1624 " container h {\n"
1625 " leaf b {\n"
1626 " type string;\n"
1627 " }\n"
1628 " }\n"
1629 "}\n";
1630
1631 /* edited pyang output */
1632 expect =
1633 "submodule: a27-sub\n"
1634 " +--rw h\n"
1635 " +--rw b? string\n";
1636
1637 ly_ctx_set_module_imp_clb(UTEST_LYCTX, local_imp_clb, submodule);
1638 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1639 assert_int_equal(LY_SUCCESS, lys_print_submodule(UTEST_OUT, mod->parsed->includes[0].submodule, LYS_OUT_TREE, 72, 0));
1640 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1641 assert_string_equal(printed, expect);
1642
1643 TEST_LOCAL_TEARDOWN;
1644}
1645
aPiecek20ddf8b2021-01-08 11:50:37 +01001646int
1647main(void)
1648{
1649 const struct CMUnitTest tests[] = {
1650 UTEST(base_sections),
1651 UTEST(node_status),
1652 UTEST(node_config_flags),
1653 UTEST(node_rpcs_flags),
1654 UTEST(node_grouping_flags),
1655 UTEST(notif_inside_container),
1656 UTEST(node_choice),
1657 UTEST(node_case),
1658 UTEST(optional_opts),
1659 UTEST(presence_container),
1660 UTEST(node_keys),
1661 UTEST(node_type_target),
1662 UTEST(node_type_leafref),
1663 UTEST(node_iffeatures),
1664 UTEST(indent_wrapper),
1665 UTEST(line_length_twiddling),
1666 UTEST(break_before_leafref),
1667 UTEST(break_before_leafref_and_iffeature),
1668 UTEST(basic_unified_indent_before_type),
1669 UTEST(twiddling_unified_indent_before_type),
1670 UTEST(inheritance_of_config_flag),
1671 UTEST(inheritance_of_status_flag),
1672 UTEST(key_leaf_is_always_mandatory_true),
1673 UTEST(transition_between_rpc_and_notif),
aPiecek9e505922021-04-19 13:45:08 +02001674 UTEST(local_augment),
aPiecek7745b282021-04-20 13:54:52 +02001675 UTEST(print_compiled_node),
aPiecekaecf7512021-04-21 08:34:35 +02001676 UTEST(print_parsed_submodule),
aPiecek20ddf8b2021-01-08 11:50:37 +01001677 };
1678
1679 return cmocka_run_group_tests(tests, NULL, NULL);
1680}