blob: 5438c6114b8220e2ccea803a2b059773f28a23de [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
aPiecek20ddf8b2021-01-08 11:50:37 +010017#include "context.h"
Michal Vasko8f702ee2024-02-20 15:44:24 +010018#include "ly_common.h"
aPiecek20ddf8b2021-01-08 11:50:37 +010019#include "out.h"
20#include "printer_schema.h"
21#include "tree_schema.h"
22
23#define TEST_LOCAL_SETUP \
24 char *printed; \
Michal Vasko4de7d072021-07-09 09:13:18 +020025 struct lys_module *mod; \
aPiecek20ddf8b2021-01-08 11:50:37 +010026 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;
Michal Vasko4de7d072021-07-09 09:13:18 +020040 struct lys_module *modxx;
aPiecekdc8fd572021-04-19 10:47:23 +020041
42 orig =
43 "module a01xx {\n"
44 " yang-version 1.1;\n"
45 " namespace \"xx:y\";\n"
46 " prefix xx;\n"
47 " container c;\n"
48 " container d;\n"
49 "}\n";
50
aPiecek34fa3772021-05-21 12:35:46 +020051 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &modxx);
aPiecekdc8fd572021-04-19 10:47:23 +020052
53 /* module with import statement */
aPiecek20ddf8b2021-01-08 11:50:37 +010054 orig =
55 "module a01 {\n"
56 " yang-version 1.1;\n"
57 " namespace \"x:y\";\n"
58 " prefix x;\n"
59 "\n"
aPiecekdc8fd572021-04-19 10:47:23 +020060 " import a01xx {\n"
61 " prefix xx;\n"
62 " }\n"
63 "\n"
aPiecek20ddf8b2021-01-08 11:50:37 +010064 " grouping g1;\n"
65 "\n"
66 " grouping g2;\n"
aPiecekdc8fd572021-04-19 10:47:23 +020067 " container g;\n"
68 " augment \"/xx:c\" {\n"
69 " container e;\n"
aPiecek20ddf8b2021-01-08 11:50:37 +010070 " }\n"
aPiecekdc8fd572021-04-19 10:47:23 +020071 " augment \"/xx:d\" {\n"
72 " container f;\n"
aPiecek20ddf8b2021-01-08 11:50:37 +010073 " }\n"
74 " rpc rpc1;\n"
75 " rpc rpc2;\n"
76 " notification n1;\n"
77 " notification n2;\n"
78 "}\n";
79
aPiecekdc8fd572021-04-19 10:47:23 +020080 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
81
82 /* from pyang */
aPiecek20ddf8b2021-01-08 11:50:37 +010083 expect =
84 "module: a01\n"
aPiecekdc8fd572021-04-19 10:47:23 +020085 " +--rw g\n"
86 "\n"
87 " augment /xx:c:\n"
88 " +--rw e\n"
89 " augment /xx:d:\n"
90 " +--rw f\n"
aPiecek20ddf8b2021-01-08 11:50:37 +010091 "\n"
92 " rpcs:\n"
93 " +---x rpc1\n"
94 " +---x rpc2\n"
95 "\n"
96 " notifications:\n"
97 " +---n n1\n"
aPiecekdc8fd572021-04-19 10:47:23 +020098 " +---n n2\n"
99 "\n"
100 " grouping g1\n"
101 " grouping g2\n";
aPiecek20ddf8b2021-01-08 11:50:37 +0100102
aPieceke376b922021-04-19 08:08:14 +0200103 TEST_LOCAL_PRINT(mod, 72);
104 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100105 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +0200106
107 ly_out_reset(UTEST_OUT);
108
109 /* from pyang */
110 expect =
111 "module: a01\n"
112 " +--rw g\n"
113 "\n"
114 " augment /xx:c:\n"
115 " +--rw e\n"
116 " augment /xx:d:\n"
117 " +--rw f\n"
118 "\n"
119 " rpcs:\n"
120 " +---x rpc1\n"
121 " +---x rpc2\n"
122 "\n"
123 " notifications:\n"
124 " +---n n1\n"
125 " +---n n2\n";
126
127 /* using lysc tree */
128 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
129 TEST_LOCAL_PRINT(mod, 72);
130 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
131 assert_string_equal(printed, expect);
aPiecek34fa3772021-05-21 12:35:46 +0200132
133 ly_out_reset(UTEST_OUT);
134
135 /* from pyang */
136 expect =
137 "module: a01xx\n"
138 " +--rw c\n"
139 " | +--rw x:e\n"
140 " +--rw d\n"
141 " +--rw x:f\n";
142
143 TEST_LOCAL_PRINT(modxx, 72);
144 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
145 assert_string_equal(printed, expect);
146
aPiecek9e505922021-04-19 13:45:08 +0200147 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
148
aPiecek20ddf8b2021-01-08 11:50:37 +0100149 TEST_LOCAL_TEARDOWN;
150}
151
152static void
153node_status(void **state)
154{
155 TEST_LOCAL_SETUP;
156 orig =
157 "module a02 {\n"
158 " yang-version 1.1;\n"
159 " namespace \"x:y\";\n"
160 " prefix x;\n"
161 " container l {\n"
162 " status current;\n"
163 " }\n"
164 " container m {\n"
165 " status deprecated;\n"
166 " }\n"
167 " container n {\n"
168 " status obsolete;\n"
169 " }\n"
170 "}\n";
171
172 /* from pyang */
173 expect =
174 "module: a02\n"
175 " +--rw l\n"
176 " x--rw m\n"
177 " o--rw n\n";
178
aPieceke376b922021-04-19 08:08:14 +0200179 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
180 TEST_LOCAL_PRINT(mod, 72);
181 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100182 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +0200183
184 ly_out_reset(UTEST_OUT);
185 /* using lysc tree */
186 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
187 TEST_LOCAL_PRINT(mod, 72);
188 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
189 assert_string_equal(printed, expect);
190 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
191
aPiecek20ddf8b2021-01-08 11:50:37 +0100192 TEST_LOCAL_TEARDOWN;
193}
194
195static void
196node_config_flags(void **state)
197{
198 TEST_LOCAL_SETUP;
199 orig =
200 "module a03 {\n"
201 " yang-version 1.1;\n"
202 " namespace \"x:y\";\n"
203 " prefix x;\n"
204 " container l {\n"
205 " config true;\n"
206 " }\n"
207 " container m {\n"
208 " config false;\n"
209 " }\n"
210 "}\n";
211
212 /* from pyang */
213 expect =
214 "module: a03\n"
215 " +--rw l\n"
216 " +--ro m\n";
217
aPieceke376b922021-04-19 08:08:14 +0200218 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
219 TEST_LOCAL_PRINT(mod, 72);
220 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100221 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +0200222
223 ly_out_reset(UTEST_OUT);
224 /* using lysc tree */
225 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
226 TEST_LOCAL_PRINT(mod, 72);
227 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
228 assert_string_equal(printed, expect);
229 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
230
aPiecek20ddf8b2021-01-08 11:50:37 +0100231 TEST_LOCAL_TEARDOWN;
232}
233
234static void
235node_rpcs_flags(void **state)
236{
237 TEST_LOCAL_SETUP;
238 orig =
239 "module a04 {\n"
240 " yang-version 1.1;\n"
241 " namespace \"x:y\";\n"
242 " prefix x;\n"
243 " container cont {\n"
244 " action rpc1 {\n"
245 "\n"
246 " input {\n"
247 " leaf in {\n"
248 " type string;\n"
249 " }\n"
250 " }\n"
aPiecek9e505922021-04-19 13:45:08 +0200251 "\n"
252 " output {\n"
253 " leaf out {\n"
254 " type string;\n"
255 " }\n"
256 " }\n"
aPiecek20ddf8b2021-01-08 11:50:37 +0100257 " }\n"
258 " }\n"
259 "}\n";
260
261 /* from pyang */
262 expect =
263 "module: a04\n"
264 " +--rw cont\n"
265 " +---x rpc1\n"
266 " +---w input\n"
aPiecek9e505922021-04-19 13:45:08 +0200267 " | +---w in? string\n"
268 " +--ro output\n"
269 " +--ro out? string\n";
aPiecek20ddf8b2021-01-08 11:50:37 +0100270
aPieceke376b922021-04-19 08:08:14 +0200271 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
272 TEST_LOCAL_PRINT(mod, 72);
273 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100274 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +0200275
276 ly_out_reset(UTEST_OUT);
277 /* using lysc tree */
278 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
279 TEST_LOCAL_PRINT(mod, 72);
280 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
281 assert_string_equal(printed, expect);
282 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
283
aPiecek20ddf8b2021-01-08 11:50:37 +0100284 TEST_LOCAL_TEARDOWN;
285}
286
287static void
288node_grouping_flags(void **state)
289{
290 TEST_LOCAL_SETUP;
291 orig =
292 "module a05 {\n"
293 " yang-version 1.1;\n"
294 " namespace \"x:y\";\n"
295 " prefix x;\n"
296 "\n"
297 " grouping g {\n"
aPiecekdc8fd572021-04-19 10:47:23 +0200298 " leaf a {\n"
299 " type string;\n"
300 " config true;\n"
301 " }\n"
302 " leaf b {\n"
303 " type string;\n"
304 " config false;\n"
305 " }\n"
306 " leaf c {\n"
307 " type string;\n"
308 " }\n"
309 " container d {\n"
310 " config false;\n"
311 " leaf e {\n"
312 " type string;\n"
313 " }\n"
314 " }\n"
315 " container f {\n"
316 " leaf g {\n"
317 " type string;\n"
318 " }\n"
319 " }\n"
aPiecek20ddf8b2021-01-08 11:50:37 +0100320 " }\n"
321 " container d {\n"
322 " uses g;\n"
323 " }\n"
324 "}\n";
325
aPiecekdc8fd572021-04-19 10:47:23 +0200326 /* from yanglint1 */
aPiecek20ddf8b2021-01-08 11:50:37 +0100327 expect =
328 "module: a05\n"
329 " +--rw d\n"
330 " +---u g\n"
331 "\n"
332 " grouping g:\n"
aPiecekdc8fd572021-04-19 10:47:23 +0200333 " +--rw a? string\n"
334 " +--ro b? string\n"
335 " +---- c? string\n"
336 " +--ro d\n"
337 " | +--ro e? string\n"
338 " +---- f\n"
339 " +---- g? string\n";
340
aPieceke376b922021-04-19 08:08:14 +0200341 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
342 TEST_LOCAL_PRINT(mod, 72);
343 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100344 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +0200345
346 /* from pyang */
347 expect =
348 "module: a05\n"
349 " +--rw d\n"
350 " +--rw a? string\n"
351 " +--ro b? string\n"
352 " +--rw c? string\n"
353 " +--ro d\n"
354 " | +--ro e? string\n"
355 " +--rw f\n"
356 " +--rw g? string\n";
357
358 ly_out_reset(UTEST_OUT);
359 /* using lysc tree */
360 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
361 TEST_LOCAL_PRINT(mod, 72);
362 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
363 assert_string_equal(printed, expect);
364 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
365
aPiecek20ddf8b2021-01-08 11:50:37 +0100366 TEST_LOCAL_TEARDOWN;
367}
368
369static void
370notif_inside_container(void **state)
371{
372 TEST_LOCAL_SETUP;
373 orig =
374 "module a06 {\n"
375 " yang-version 1.1;\n"
376 " namespace \"x:y\";\n"
377 " prefix x;\n"
378 " container c {\n"
379 " notification notif;\n"
380 " }\n"
381 "}\n";
382
383 /* from pyang */
384 expect =
385 "module: a06\n"
386 " +--rw c\n"
387 " +---n notif\n";
388
aPieceke376b922021-04-19 08:08:14 +0200389 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
390 TEST_LOCAL_PRINT(mod, 72);
391 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100392 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +0200393
394 ly_out_reset(UTEST_OUT);
395 /* using lysc tree */
396 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
397 TEST_LOCAL_PRINT(mod, 72);
398 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
399 assert_string_equal(printed, expect);
400 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
401
aPiecek20ddf8b2021-01-08 11:50:37 +0100402 TEST_LOCAL_TEARDOWN;
403}
404
405static void
406node_choice(void **state)
407{
408 TEST_LOCAL_SETUP;
409 orig =
410 "module a07 {\n"
411 " yang-version 1.1;\n"
412 " namespace \"x:y\";\n"
413 " prefix x;\n"
414 " choice my_choice;\n"
415 "}\n";
416
417 /* from pyang */
418 expect =
419 "module: a07\n"
420 " +--rw (my_choice)?\n";
421
aPieceke376b922021-04-19 08:08:14 +0200422 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
423 TEST_LOCAL_PRINT(mod, 72);
424 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100425 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +0200426
427 ly_out_reset(UTEST_OUT);
428 /* using lysc tree */
429 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
430 TEST_LOCAL_PRINT(mod, 72);
431 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
432 assert_string_equal(printed, expect);
433 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
434
aPiecek20ddf8b2021-01-08 11:50:37 +0100435 TEST_LOCAL_TEARDOWN;
436}
437
438static void
439node_case(void **state)
440{
441 TEST_LOCAL_SETUP;
442 orig =
443 "module a08 {\n"
444 " yang-version 1.1;\n"
445 " namespace \"x:y\";\n"
446 " prefix x;\n"
aPiecek7a28e2f2021-05-21 07:27:03 +0200447 "\n"
448 " feature foo;\n"
aPiecek20ddf8b2021-01-08 11:50:37 +0100449 " choice my_choice {\n"
450 " case my_case;\n"
451 " }\n"
aPiecek082c7dc2021-05-20 08:55:07 +0200452 " choice shorthand {\n"
aPiecek7a28e2f2021-05-21 07:27:03 +0200453 " container cont1 {\n"
454 " if-feature \"foo\";\n"
aPiecekaa320c92021-05-21 07:34:24 +0200455 " status obsolete;\n"
aPiecek7a28e2f2021-05-21 07:27:03 +0200456 " }\n"
aPiecek082c7dc2021-05-20 08:55:07 +0200457 " container cont2 {\n"
458 " container cont3;\n"
459 " }\n"
460 " }\n"
461 " container top {\n"
462 " choice shorthand1 {\n"
463 " container cont1;\n"
464 " }\n"
465 " choice shorthand2 {\n"
466 " container cont2 {\n"
467 " container cont3;\n"
468 " }\n"
469 " }\n"
470 " }\n"
aPiecek20ddf8b2021-01-08 11:50:37 +0100471 "}\n";
472
473 /* from pyang */
474 expect =
475 "module: a08\n"
476 " +--rw (my_choice)?\n"
aPiecek082c7dc2021-05-20 08:55:07 +0200477 " | +--:(my_case)\n"
478 " +--rw (shorthand)?\n"
aPiecekaa320c92021-05-21 07:34:24 +0200479 " | o--:(cont1)\n"
480 " | | o--rw cont1 {foo}?\n"
aPiecek082c7dc2021-05-20 08:55:07 +0200481 " | +--:(cont2)\n"
482 " | +--rw cont2\n"
483 " | +--rw cont3\n"
484 " +--rw top\n"
485 " +--rw (shorthand1)?\n"
486 " | +--:(cont1)\n"
487 " | +--rw cont1\n"
488 " +--rw (shorthand2)?\n"
489 " +--:(cont2)\n"
490 " +--rw cont2\n"
491 " +--rw cont3\n";
aPiecek20ddf8b2021-01-08 11:50:37 +0100492
aPiecek7a28e2f2021-05-21 07:27:03 +0200493 const char *feats[] = {"foo", NULL};
494
495 UTEST_ADD_MODULE(orig, LYS_IN_YANG, feats, &mod);
aPieceke376b922021-04-19 08:08:14 +0200496 TEST_LOCAL_PRINT(mod, 72);
497 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100498 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +0200499
500 ly_out_reset(UTEST_OUT);
501 /* using lysc tree */
502 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
503 TEST_LOCAL_PRINT(mod, 72);
504 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
505 assert_string_equal(printed, expect);
506 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
507
aPiecek20ddf8b2021-01-08 11:50:37 +0100508 TEST_LOCAL_TEARDOWN;
509}
510
511static void
512optional_opts(void **state)
513{
514 TEST_LOCAL_SETUP;
515 /* throws libyang warn: Use of anydata to define configuration data is not recommended... */
516 orig =
517 "module a09 {\n"
518 " yang-version 1.1;\n"
519 " namespace \"x:y\";\n"
520 " prefix x;\n"
521 " leaf l1 {\n"
522 " type string;\n"
523 " mandatory true;\n"
524 " }\n"
525 " leaf l2 {\n"
526 " type string;\n"
527 " mandatory false;\n"
528 " }\n"
529 " choice c1 {\n"
530 " mandatory true;\n"
531 " }\n"
532 " choice c2 {\n"
533 " mandatory false;\n"
534 " }\n"
535 " anydata a1 {\n"
536 " mandatory true;\n"
537 " }\n"
538 " anydata a2 {\n"
539 " mandatory false;\n"
540 " }\n"
541 " anyxml x1 {\n"
542 " mandatory true;\n"
543 " }\n"
544 " anyxml x2 {\n"
545 " mandatory false;\n"
546 " }\n"
547 "}\n";
548
549 /* from yanglint 1 */
550 expect =
551 "module: a09\n"
552 " +--rw l1 string\n"
553 " +--rw l2? string\n"
554 " +--rw (c1)\n"
555 " +--rw (c2)?\n"
556 " +--rw a1 anydata\n"
557 " +--rw a2? anydata\n"
558 " +--rw x1 anyxml\n"
559 " +--rw x2? anyxml\n";
560
aPieceke376b922021-04-19 08:08:14 +0200561 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
562 TEST_LOCAL_PRINT(mod, 72);
563 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100564 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +0200565
566 ly_out_reset(UTEST_OUT);
567 /* using lysc tree */
568 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
569 TEST_LOCAL_PRINT(mod, 72);
570 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
571 assert_string_equal(printed, expect);
572 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
573
aPiecek20ddf8b2021-01-08 11:50:37 +0100574 TEST_LOCAL_TEARDOWN;
575}
576
577static void
578presence_container(void **state)
579{
580 TEST_LOCAL_SETUP;
581 orig =
582 "module a10 {\n"
583 " yang-version 1.1;\n"
584 " namespace \"x:y\";\n"
585 " prefix x;\n"
586 " container c;\n"
587 " container d {\n"
588 " presence \"str1\";\n"
589 " }\n"
590 "}\n";
591
592 /* from pyang */
593 expect =
594 "module: a10\n"
595 " +--rw c\n"
596 " +--rw d!\n";
597
aPieceke376b922021-04-19 08:08:14 +0200598 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
599 TEST_LOCAL_PRINT(mod, 72);
600 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100601 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +0200602
603 ly_out_reset(UTEST_OUT);
604 /* using lysc tree */
605 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
606 TEST_LOCAL_PRINT(mod, 72);
607 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
608 assert_string_equal(printed, expect);
609 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
610
aPiecek20ddf8b2021-01-08 11:50:37 +0100611 TEST_LOCAL_TEARDOWN;
612}
613
614static void
615node_keys(void **state)
616{
617 TEST_LOCAL_SETUP;
618 orig =
619 "module a11 {\n"
620 " yang-version 1.1;\n"
621 " namespace \"x:y\";\n"
622 " prefix x;\n"
623 " list l1 {\n"
624 " key \"a\";\n"
625 " leaf a {\n"
626 " type string;\n"
627 " }\n"
628 " }\n"
629 " list l2 {\n"
630 " key \"a b\";\n"
631 " leaf a {\n"
632 " type string;\n"
633 " }\n"
634 " leaf b {\n"
635 " type string;\n"
636 " }\n"
637 " }\n"
638 " leaf-list ll {\n"
639 " type string;\n"
640 " }\n"
641 "}\n";
642
643 /* from pyang */
644 expect =
645 "module: a11\n"
646 " +--rw l1* [a]\n"
647 " | +--rw a string\n"
648 " +--rw l2* [a b]\n"
649 " | +--rw a string\n"
650 " | +--rw b string\n"
651 " +--rw ll* string\n";
652
aPieceke376b922021-04-19 08:08:14 +0200653 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
654 TEST_LOCAL_PRINT(mod, 72);
655 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100656 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +0200657
658 ly_out_reset(UTEST_OUT);
659 /* using lysc tree */
660 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
661 TEST_LOCAL_PRINT(mod, 72);
662 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
663 assert_string_equal(printed, expect);
664 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
665
aPiecek20ddf8b2021-01-08 11:50:37 +0100666 TEST_LOCAL_TEARDOWN;
667}
668
669static void
670node_type_target(void **state)
671{
672 TEST_LOCAL_SETUP;
673 orig =
674 "module a12 {\n"
675 " yang-version 1.1;\n"
676 " namespace \"x:y\";\n"
677 " prefix x;\n"
678 " leaf a {\n"
679 " type leafref {\n"
680 " path \"/x:b\";\n"
681 " }\n"
682 " }\n"
683 " leaf b {\n"
684 " type string;\n"
685 " }\n"
686 "}\n";
687
688 /* from yanglint 1 */
689 expect =
690 "module: a12\n"
691 " +--rw a? -> /x:b\n"
692 " +--rw b? string\n";
693
aPieceke376b922021-04-19 08:08:14 +0200694 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
695 TEST_LOCAL_PRINT(mod, 72);
696 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100697 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +0200698
699 ly_out_reset(UTEST_OUT);
700 /* using lysc tree */
701 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
702 TEST_LOCAL_PRINT(mod, 72);
703 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
704 assert_string_equal(printed, expect);
705 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
706
aPiecek20ddf8b2021-01-08 11:50:37 +0100707 TEST_LOCAL_TEARDOWN;
708}
709
710static void
711node_type_leafref(void **state)
712{
713 TEST_LOCAL_SETUP;
714 orig =
715 "module a13 {\n"
716 " yang-version 1.1;\n"
717 " namespace \"x:y\";\n"
718 " prefix x;\n"
719 " leaf pretty-long-identifier-name-which-should-exceed-the-limit-of-72-characters {\n"
720 " type string;\n"
721 " }\n"
722 " leaf a {\n"
723 " type leafref {\n"
724 " path \"/x:pretty-long-identifier-name-which-should-exceed-the-limit-of-72-characters\";\n"
725 " }\n"
726 " }\n"
727 "}\n";
728
729 /* yanglint --tree-no-leafref-target --tree-line-length=72 */
730 expect =
731 "module: a13\n"
732 " +--rw pretty-long-identifier-name-which-should-exceed-the-limit-of-72-characters?\n"
733 " | string\n"
734 " +--rw a?\n"
735 " leafref\n";
736
aPieceke376b922021-04-19 08:08:14 +0200737 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
738 TEST_LOCAL_PRINT(mod, 72);
739 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100740 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +0200741
742 ly_out_reset(UTEST_OUT);
743 /* using lysc tree */
744 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
745 TEST_LOCAL_PRINT(mod, 72);
746 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
747 assert_string_equal(printed, expect);
748 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
749
aPiecek20ddf8b2021-01-08 11:50:37 +0100750 TEST_LOCAL_TEARDOWN;
751}
752
753static void
754node_iffeatures(void **state)
755{
756 TEST_LOCAL_SETUP;
757 orig =
758 "module a14 {\n"
759 " yang-version 1.1;\n"
760 " namespace \"x:y\";\n"
761 " prefix x;\n"
762 "\n"
763 " feature foo;\n"
764 "\n"
765 " feature bar;\n"
766 " container c {\n"
767 " if-feature \"foo or bar\";\n"
768 " }\n"
769 "}\n";
770
771 /* from pyang */
772 expect =
773 "module: a14\n"
774 " +--rw c {foo or bar}?\n";
775
aPieceke376b922021-04-19 08:08:14 +0200776 const char *feats[] = {"foo", NULL};
777
778 UTEST_ADD_MODULE(orig, LYS_IN_YANG, feats, &mod);
779 TEST_LOCAL_PRINT(mod, 72);
780 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100781 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +0200782
783 ly_out_reset(UTEST_OUT);
784 /* using lysc tree */
785 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
786 TEST_LOCAL_PRINT(mod, 72);
787 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
788 assert_string_equal(printed, expect);
789 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
790
aPiecek20ddf8b2021-01-08 11:50:37 +0100791 TEST_LOCAL_TEARDOWN;
792}
793
794static void
795indent_wrapper(void **state)
796{
797 TEST_LOCAL_SETUP;
798 orig =
799 "module a15 {\n"
800 " yang-version 1.1;\n"
801 " namespace \"x:y\";\n"
802 " prefix x;\n"
803 " container a {\n"
804 " container b;\n"
805 " }\n"
806 " container c {\n"
807 " container d {\n"
808 " container e;\n"
809 " }\n"
810 " container f {\n"
811 " container g;\n"
812 " }\n"
813 " }\n"
814 " container h;\n"
815 " container i {\n"
816 " container j;\n"
817 " container k;\n"
818 " }\n"
819 "}\n";
820
821 /* from pyang */
822 expect =
823 "module: a15\n"
824 " +--rw a\n"
825 " | +--rw b\n"
826 " +--rw c\n"
827 " | +--rw d\n"
828 " | | +--rw e\n"
829 " | +--rw f\n"
830 " | +--rw g\n"
831 " +--rw h\n"
832 " +--rw i\n"
833 " +--rw j\n"
834 " +--rw k\n";
835
aPieceke376b922021-04-19 08:08:14 +0200836 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
837 TEST_LOCAL_PRINT(mod, 72);
838 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100839 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +0200840
841 ly_out_reset(UTEST_OUT);
842 /* using lysc tree */
843 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
844 TEST_LOCAL_PRINT(mod, 72);
845 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
846 assert_string_equal(printed, expect);
847 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
848
aPiecek20ddf8b2021-01-08 11:50:37 +0100849 TEST_LOCAL_TEARDOWN;
850}
851
852static void
853line_length_twiddling(void **state)
854{
855 TEST_LOCAL_SETUP;
856 /* node_fits_tight */
857
858 orig =
859 "module a16 {\n"
860 " yang-version 1.1;\n"
861 " namespace \"x:y\";\n"
862 " prefix x;\n"
863 "\n"
864 " feature f;\n"
865 "\n"
866 " typedef some-long-type {\n"
867 " type string;\n"
868 " }\n"
869 " list my-list-name {\n"
870 " key \"key\";\n"
871 " leaf key {\n"
872 " type string;\n"
873 " }\n"
874 " leaf nod-leaf {\n"
875 " if-feature \"f\";\n"
876 " type some-long-type;\n"
877 " }\n"
878 " leaf nos-leaf {\n"
879 " if-feature \"f\";\n"
880 " type int32;\n"
881 " }\n"
882 " }\n"
883 "}\n";
884
885 /* pyang --tree-line-length 42 */
886 expect =
887 "module: a16\n"
888 " +--rw my-list-name* [key]\n"
889 " +--rw key string\n"
890 " +--rw nod-leaf? some-long-type {f}?\n"
891 " +--rw nos-leaf? int32 {f}?\n";
892
aPieceke376b922021-04-19 08:08:14 +0200893 const char *feats[] = {"f", NULL};
894
895 UTEST_ADD_MODULE(orig, LYS_IN_YANG, feats, &mod);
896 TEST_LOCAL_PRINT(mod, 42);
897 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100898 assert_string_equal(printed, expect);
899
aPieceke376b922021-04-19 08:08:14 +0200900 ly_out_reset(UTEST_OUT);
aPiecek9e505922021-04-19 13:45:08 +0200901 /* using lysc tree */
902 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
903 TEST_LOCAL_PRINT(mod, 42);
904 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
905 assert_string_equal(printed, expect);
906 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
907
908 ly_out_reset(UTEST_OUT);
aPiecek20ddf8b2021-01-08 11:50:37 +0100909 /* break_before_iffeature */
910
911 /* pyang --tree-line-length 41 */
912 expect =
913 "module: a16\n"
914 " +--rw my-list-name* [key]\n"
915 " +--rw key string\n"
916 " +--rw nod-leaf? some-long-type\n"
917 " | {f}?\n"
918 " +--rw nos-leaf? int32 {f}?\n";
919
aPieceke376b922021-04-19 08:08:14 +0200920 UTEST_ADD_MODULE(orig, LYS_IN_YANG, feats, &mod);
921 TEST_LOCAL_PRINT(mod, 41);
922 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100923 assert_string_equal(printed, expect);
924
aPieceke376b922021-04-19 08:08:14 +0200925 ly_out_reset(UTEST_OUT);
aPiecek9e505922021-04-19 13:45:08 +0200926 /* using lysc tree */
927 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
928 TEST_LOCAL_PRINT(mod, 41);
929 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
930 assert_string_equal(printed, expect);
931 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
932
933 ly_out_reset(UTEST_OUT);
aPiecek20ddf8b2021-01-08 11:50:37 +0100934 /* break_before_type */
935
936 /* pyang --tree-line-length 29 */
937 expect =
938 "module: a16\n"
939 " +--rw my-list-name* [key]\n"
940 " +--rw key string\n"
941 " +--rw nod-leaf?\n"
942 " | some-long-type\n"
943 " | {f}?\n"
944 " +--rw nos-leaf? int32\n"
945 " {f}?\n";
946
aPieceke376b922021-04-19 08:08:14 +0200947 UTEST_ADD_MODULE(orig, LYS_IN_YANG, feats, &mod);
948 TEST_LOCAL_PRINT(mod, 29);
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);
aPiecek9e505922021-04-19 13:45:08 +0200953 /* using lysc tree */
954 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
955 TEST_LOCAL_PRINT(mod, 29);
956 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
957 assert_string_equal(printed, expect);
958 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
959
960 ly_out_reset(UTEST_OUT);
aPiecek20ddf8b2021-01-08 11:50:37 +0100961 /* break_before_keys */
962
963 /* pyang --tree-line-length 23 */
964 expect =
965 "module: a16\n"
966 " +--rw my-list-name*\n"
967 " [key]\n"
968 " +--rw key\n"
969 " | string\n"
970 " +--rw nod-leaf?\n"
971 " | some-long-type\n"
972 " | {f}?\n"
973 " +--rw nos-leaf?\n"
974 " int32 {f}?\n";
975
aPieceke376b922021-04-19 08:08:14 +0200976 UTEST_ADD_MODULE(orig, LYS_IN_YANG, feats, &mod);
977 TEST_LOCAL_PRINT(mod, 23);
978 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +0100979 assert_string_equal(printed, expect);
980
aPieceke376b922021-04-19 08:08:14 +0200981 ly_out_reset(UTEST_OUT);
aPiecek9e505922021-04-19 13:45:08 +0200982 /* using lysc tree */
983 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
984 TEST_LOCAL_PRINT(mod, 23);
985 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
986 assert_string_equal(printed, expect);
987 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
988
989 ly_out_reset(UTEST_OUT);
aPiecek20ddf8b2021-01-08 11:50:37 +0100990 /* every_node_name_is_too_long */
991
992 /* pyang --tree-line-length 14 */
993 expect =
994 "module: a16\n"
995 " +--rw my-list-name*\n"
996 " [key]\n"
997 " +--rw key\n"
998 " | string\n"
999 " +--rw nod-leaf?\n"
1000 " | some-long-type\n"
1001 " | {f}?\n"
1002 " +--rw nos-leaf?\n"
1003 " int32\n"
1004 " {f}?\n";
1005
aPieceke376b922021-04-19 08:08:14 +02001006 UTEST_ADD_MODULE(orig, LYS_IN_YANG, feats, &mod);
1007 TEST_LOCAL_PRINT(mod, 14);
1008 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +01001009 assert_string_equal(printed, expect);
1010
aPiecek9e505922021-04-19 13:45:08 +02001011 ly_out_reset(UTEST_OUT);
1012 /* using lysc tree */
1013 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1014 TEST_LOCAL_PRINT(mod, 14);
1015 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1016 assert_string_equal(printed, expect);
1017 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1018
aPiecek20ddf8b2021-01-08 11:50:37 +01001019 TEST_LOCAL_TEARDOWN;
1020}
1021
1022static void
1023break_before_leafref(void **state)
1024{
1025 TEST_LOCAL_SETUP;
1026 orig =
1027 "module a17 {\n"
1028 " yang-version 1.1;\n"
1029 " namespace \"x:y\";\n"
1030 " prefix x;\n"
1031 " leaf e {\n"
1032 " type string;\n"
1033 " }\n"
1034 " leaf abcd {\n"
1035 " type leafref {\n"
1036 " path \"/x:e\";\n"
1037 " }\n"
1038 " }\n"
1039 "}\n";
1040
1041 /* yanglint --tree-line-length 14 */
1042 expect =
1043 "module: a17\n"
1044 " +--rw e?\n"
1045 " | string\n"
1046 " +--rw abcd?\n"
1047 " -> /x:e\n";
1048
aPieceke376b922021-04-19 08:08:14 +02001049 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1050 TEST_LOCAL_PRINT(mod, 14);
1051 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +01001052 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +02001053
1054 ly_out_reset(UTEST_OUT);
1055 /* using lysc tree */
1056 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1057 TEST_LOCAL_PRINT(mod, 14);
1058 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1059 assert_string_equal(printed, expect);
1060 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1061
aPiecek20ddf8b2021-01-08 11:50:37 +01001062 TEST_LOCAL_TEARDOWN;
1063}
1064
1065static void
1066break_before_leafref_and_iffeature(void **state)
1067{
1068 TEST_LOCAL_SETUP;
1069 orig =
1070 "module a18 {\n"
1071 " yang-version 1.1;\n"
1072 " namespace \"x:y\";\n"
1073 " prefix x;\n"
1074 "\n"
1075 " feature f;\n"
1076 " leaf some-long-id {\n"
1077 " type string;\n"
1078 " }\n"
1079 " leaf a {\n"
1080 " if-feature \"f\";\n"
1081 " type leafref {\n"
1082 " path \"/x:some-long-id\";\n"
1083 " }\n"
1084 " }\n"
1085 "}\n";
1086
1087 /* yanglint --tree-no-leafref-target --tree-line-length=20 */
1088 expect =
1089 "module: a18\n"
1090 " +--rw some-long-id?\n"
1091 " | string\n"
1092 " +--rw a?\n"
1093 " leafref\n"
1094 " {f}?\n";
1095
aPieceke376b922021-04-19 08:08:14 +02001096 const char *feats[] = {"f", NULL};
1097
1098 UTEST_ADD_MODULE(orig, LYS_IN_YANG, feats, &mod);
1099 TEST_LOCAL_PRINT(mod, 20);
1100 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +01001101 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +02001102
1103 ly_out_reset(UTEST_OUT);
1104 /* using lysc tree */
1105 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1106 TEST_LOCAL_PRINT(mod, 20);
1107 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1108 assert_string_equal(printed, expect);
1109 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1110
aPiecek20ddf8b2021-01-08 11:50:37 +01001111 TEST_LOCAL_TEARDOWN;
1112}
1113
1114static void
1115basic_unified_indent_before_type(void **state)
1116{
1117 TEST_LOCAL_SETUP;
1118 orig =
1119 "module a19 {\n"
1120 " yang-version 1.1;\n"
1121 " namespace \"x:y\";\n"
1122 " prefix x;\n"
1123 "\n"
1124 " typedef longType {\n"
1125 " type string;\n"
1126 " }\n"
1127 " container A {\n"
1128 " leaf Bnode {\n"
1129 " type int8;\n"
1130 " }\n"
1131 " leaf Cnode {\n"
1132 " type int8;\n"
1133 " mandatory true;\n"
1134 " }\n"
1135 " leaf Dnode {\n"
1136 " type int8;\n"
1137 " mandatory true;\n"
1138 " }\n"
1139 " leaf E {\n"
1140 " type longType;\n"
1141 " mandatory true;\n"
1142 " }\n"
1143 " leaf G {\n"
1144 " type int8;\n"
1145 " }\n"
1146 " }\n"
1147 "}\n";
1148
1149 /* from pyang */
1150 expect =
1151 "module: a19\n"
1152 " +--rw A\n"
1153 " +--rw Bnode? int8\n"
1154 " +--rw Cnode int8\n"
1155 " +--rw Dnode int8\n"
1156 " +--rw E longType\n"
1157 " +--rw G? int8\n";
1158
aPieceke376b922021-04-19 08:08:14 +02001159 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1160 TEST_LOCAL_PRINT(mod, 72);
1161 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +01001162 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +02001163
1164 ly_out_reset(UTEST_OUT);
1165 /* using lysc tree */
1166 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1167 TEST_LOCAL_PRINT(mod, 72);
1168 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1169 assert_string_equal(printed, expect);
1170 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1171
aPiecek20ddf8b2021-01-08 11:50:37 +01001172 TEST_LOCAL_TEARDOWN;
1173}
1174
1175static void
1176twiddling_unified_indent_before_type(void **state)
1177{
1178 TEST_LOCAL_SETUP;
1179 /* basic_functionality */
1180
1181 orig =
1182 "module a20 {\n"
1183 " yang-version 1.1;\n"
1184 " namespace \"x:y\";\n"
1185 " prefix x;\n"
1186 "\n"
1187 " typedef longType {\n"
1188 " type string;\n"
1189 " }\n"
1190 " container A {\n"
1191 " leaf Bnode {\n"
1192 " type int8;\n"
1193 " }\n"
1194 " leaf CnodeIsBigger {\n"
1195 " type int8;\n"
1196 " mandatory true;\n"
1197 " }\n"
1198 " leaf Dnode {\n"
1199 " type int8;\n"
1200 " mandatory true;\n"
1201 " }\n"
1202 " leaf E {\n"
1203 " type longType;\n"
1204 " mandatory true;\n"
1205 " }\n"
1206 " leaf G {\n"
1207 " type int8;\n"
1208 " }\n"
1209 " }\n"
1210 "}\n";
1211
1212 /* pyang --tree-line-length 36 */
1213 expect =
1214 "module: a20\n"
1215 " +--rw A\n"
1216 " +--rw Bnode? int8\n"
1217 " +--rw CnodeIsBigger int8\n"
1218 " +--rw Dnode int8\n"
1219 " +--rw E longType\n"
1220 " +--rw G? int8\n";
1221
aPieceke376b922021-04-19 08:08:14 +02001222 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1223 TEST_LOCAL_PRINT(mod, 36);
1224 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +01001225 assert_string_equal(printed, expect);
1226
aPieceke376b922021-04-19 08:08:14 +02001227 ly_out_reset(UTEST_OUT);
aPiecek9e505922021-04-19 13:45:08 +02001228 /* using lysc tree */
1229 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1230 TEST_LOCAL_PRINT(mod, 36);
1231 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1232 assert_string_equal(printed, expect);
1233 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1234
1235 ly_out_reset(UTEST_OUT);
aPiecek20ddf8b2021-01-08 11:50:37 +01001236 /* unified_indent_before_type_long_node_name */
1237
1238 /* pyang --tree-line-length 32 */
1239 expect =
1240 "module: a20\n"
1241 " +--rw A\n"
1242 " +--rw Bnode? int8\n"
1243 " +--rw CnodeIsBigger int8\n"
1244 " +--rw Dnode int8\n"
1245 " +--rw E\n"
1246 " | longType\n"
1247 " +--rw G? int8\n";
1248
aPieceke376b922021-04-19 08:08:14 +02001249 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1250 TEST_LOCAL_PRINT(mod, 32);
1251 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +01001252 assert_string_equal(printed, expect);
1253
aPieceke376b922021-04-19 08:08:14 +02001254 ly_out_reset(UTEST_OUT);
aPiecek9e505922021-04-19 13:45:08 +02001255 /* using lysc tree */
1256 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1257 TEST_LOCAL_PRINT(mod, 32);
1258 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1259 assert_string_equal(printed, expect);
1260 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1261
1262 ly_out_reset(UTEST_OUT);
aPiecek20ddf8b2021-01-08 11:50:37 +01001263 /* unified_indent_before_type_long_node_type */
1264
1265 /* pyang --tree-line-length 31 */
1266 expect =
1267 "module: a20\n"
1268 " +--rw A\n"
1269 " +--rw Bnode?\n"
1270 " | int8\n"
1271 " +--rw CnodeIsBigger\n"
1272 " | int8\n"
1273 " +--rw Dnode\n"
1274 " | int8\n"
1275 " +--rw E\n"
1276 " | longType\n"
1277 " +--rw G?\n"
1278 " int8\n";
1279
aPieceke376b922021-04-19 08:08:14 +02001280 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1281 TEST_LOCAL_PRINT(mod, 31);
1282 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +01001283 assert_string_equal(printed, expect);
1284
aPiecek9e505922021-04-19 13:45:08 +02001285 ly_out_reset(UTEST_OUT);
1286 /* using lysc tree */
1287 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1288 TEST_LOCAL_PRINT(mod, 31);
1289 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1290 assert_string_equal(printed, expect);
1291 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1292
aPiecek20ddf8b2021-01-08 11:50:37 +01001293 TEST_LOCAL_TEARDOWN;
1294}
1295
1296static void
1297inheritance_of_config_flag(void **state)
1298{
1299 TEST_LOCAL_SETUP;
1300 orig =
1301 "module a21 {\n"
1302 " yang-version 1.1;\n"
1303 " namespace \"x:y\";\n"
1304 " prefix x;\n"
1305 " container a {\n"
1306 " config false;\n"
1307 " leaf b {\n"
1308 " type string;\n"
1309 " }\n"
1310 " }\n"
1311 "}\n";
1312
1313 /* from pyang */
1314 expect =
1315 "module: a21\n"
1316 " +--ro a\n"
1317 " +--ro b? string\n";
1318
aPieceke376b922021-04-19 08:08:14 +02001319 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1320 TEST_LOCAL_PRINT(mod, 72);
1321 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +01001322 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +02001323
1324 ly_out_reset(UTEST_OUT);
1325 /* using lysc tree */
1326 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1327 TEST_LOCAL_PRINT(mod, 72);
1328 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1329 assert_string_equal(printed, expect);
1330 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1331
aPiecek20ddf8b2021-01-08 11:50:37 +01001332 TEST_LOCAL_TEARDOWN;
1333}
1334
1335static void
1336inheritance_of_status_flag(void **state)
1337{
1338 TEST_LOCAL_SETUP;
1339 /* throws libyang warn: Missing explicit "..." status that was already specified in parent, inheriting. */
1340 orig =
1341 "module a22 {\n"
1342 " yang-version 1.1;\n"
1343 " namespace \"x:y\";\n"
1344 " prefix x;\n"
1345 " container a {\n"
1346 " status current;\n"
1347 " container b {\n"
1348 " status deprecated;\n"
1349 " leaf f {\n"
1350 " type string;\n"
1351 " }\n"
1352 " }\n"
1353 " container c {\n"
1354 " status obsolete;\n"
1355 " leaf g {\n"
1356 " type string;\n"
1357 " }\n"
1358 " }\n"
1359 " }\n"
1360 " container d {\n"
1361 " status deprecated;\n"
1362 " container h {\n"
1363 " status obsolete;\n"
1364 " leaf e {\n"
1365 " type string;\n"
1366 " }\n"
1367 " }\n"
1368 " }\n"
1369 "}\n";
1370
1371 /* from yanglint 1 */
1372 expect =
1373 "module: a22\n"
1374 " +--rw a\n"
1375 " | x--rw b\n"
1376 " | | x--rw f? string\n"
1377 " | o--rw c\n"
1378 " | o--rw g? string\n"
1379 " x--rw d\n"
1380 " o--rw h\n"
1381 " o--rw e? string\n";
1382
aPieceke376b922021-04-19 08:08:14 +02001383 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1384 TEST_LOCAL_PRINT(mod, 72);
1385 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +01001386 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +02001387
1388 ly_out_reset(UTEST_OUT);
1389 /* using lysc tree */
1390 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1391 TEST_LOCAL_PRINT(mod, 72);
1392 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1393 assert_string_equal(printed, expect);
1394 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1395
aPiecek20ddf8b2021-01-08 11:50:37 +01001396 TEST_LOCAL_TEARDOWN;
1397}
1398
1399static void
1400key_leaf_is_always_mandatory_true(void **state)
1401{
1402 TEST_LOCAL_SETUP;
1403 orig =
1404 "module a23 {\n"
1405 " yang-version 1.1;\n"
1406 " namespace \"x:y\";\n"
1407 " prefix x;\n"
1408 " list a {\n"
1409 " key \"k1\";\n"
1410 " list b {\n"
1411 " key \"k2\";\n"
1412 " leaf k1 {\n"
1413 " type string;\n"
1414 " }\n"
1415 " leaf k2 {\n"
1416 " type string;\n"
1417 " }\n"
1418 " }\n"
1419 " leaf k1 {\n"
1420 " type string;\n"
1421 " }\n"
1422 " }\n"
1423 "}\n";
1424
1425 /* from pyang */
1426 expect =
1427 "module: a23\n"
1428 " +--rw a* [k1]\n"
1429 " +--rw b* [k2]\n"
1430 " | +--rw k1? string\n"
1431 " | +--rw k2 string\n"
1432 " +--rw k1 string\n";
1433
aPieceke376b922021-04-19 08:08:14 +02001434 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1435 TEST_LOCAL_PRINT(mod, 72);
1436 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +01001437 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +02001438
1439 ly_out_reset(UTEST_OUT);
1440
1441 /* from pyang but with some swapped lines */
1442 expect =
1443 "module: a23\n"
1444 " +--rw a* [k1]\n"
1445 " +--rw k1 string\n"
1446 " +--rw b* [k2]\n"
1447 " +--rw k2 string\n"
1448 " +--rw k1? string\n";
1449
1450 /* using lysc tree */
1451 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1452 TEST_LOCAL_PRINT(mod, 72);
1453 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1454 assert_string_equal(printed, expect);
1455 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1456
aPiecek20ddf8b2021-01-08 11:50:37 +01001457 TEST_LOCAL_TEARDOWN;
1458}
1459
1460static void
1461transition_between_rpc_and_notif(void **state)
1462{
1463 TEST_LOCAL_SETUP;
1464 orig =
1465 "module a24 {\n"
1466 " yang-version 1.1;\n"
1467 " namespace \"x:y\";\n"
1468 " prefix x;\n"
1469 " container top {\n"
aPiecek9e505922021-04-19 13:45:08 +02001470 " leaf g {\n"
1471 " type string;\n"
1472 " }\n"
aPiecek20ddf8b2021-01-08 11:50:37 +01001473 " action rpc1 {\n"
1474 "\n"
1475 " input {\n"
1476 " leaf in {\n"
1477 " type string;\n"
1478 " }\n"
1479 " }\n"
1480 " }\n"
1481 " action rpc2 {\n"
1482 "\n"
1483 " input {\n"
1484 " leaf in {\n"
1485 " type string;\n"
1486 " }\n"
1487 " }\n"
1488 "\n"
1489 " output {\n"
1490 " leaf out {\n"
1491 " type string;\n"
1492 " }\n"
1493 " }\n"
1494 " }\n"
1495 " notification n1;\n"
1496 " notification n2;\n"
1497 " }\n"
1498 "}\n";
1499
aPiecekb352de72021-05-21 09:04:04 +02001500 /* from pyang */
aPiecek20ddf8b2021-01-08 11:50:37 +01001501 expect =
1502 "module: a24\n"
1503 " +--rw top\n"
aPiecek9e505922021-04-19 13:45:08 +02001504 " +--rw g? string\n"
aPiecek20ddf8b2021-01-08 11:50:37 +01001505 " +---x rpc1\n"
1506 " | +---w input\n"
1507 " | +---w in? string\n"
1508 " +---x rpc2\n"
1509 " | +---w input\n"
1510 " | | +---w in? string\n"
1511 " | +--ro output\n"
1512 " | +--ro out? string\n"
1513 " +---n n1\n"
1514 " +---n n2\n";
1515
aPieceke376b922021-04-19 08:08:14 +02001516 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1517 TEST_LOCAL_PRINT(mod, 72);
1518 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
aPiecek20ddf8b2021-01-08 11:50:37 +01001519 assert_string_equal(printed, expect);
aPiecek9e505922021-04-19 13:45:08 +02001520
1521 ly_out_reset(UTEST_OUT);
1522 /* using lysc tree */
1523 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1524 TEST_LOCAL_PRINT(mod, 72);
1525 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1526 assert_string_equal(printed, expect);
1527 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1528
1529 TEST_LOCAL_TEARDOWN;
1530}
1531
1532static void
1533local_augment(void **state)
1534{
1535 TEST_LOCAL_SETUP;
1536
1537 orig =
1538 "module a25 {\n"
1539 " yang-version 1.1;\n"
1540 " namespace \"x:y\";\n"
1541 " prefix x;\n"
1542 " container g;\n"
1543 " augment \"/x:g\" {\n"
1544 " container e;\n"
1545 " }\n"
1546 "}\n";
1547
1548 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1549
1550 /* from pyang */
1551 expect =
1552 "module: a25\n"
1553 " +--rw g\n"
1554 " +--rw e\n";
1555
1556 /* using lysc tree */
1557 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1558 TEST_LOCAL_PRINT(mod, 72);
1559 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1560 assert_string_equal(printed, expect);
1561 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1562
aPiecek20ddf8b2021-01-08 11:50:37 +01001563 TEST_LOCAL_TEARDOWN;
1564}
1565
aPiecek7745b282021-04-20 13:54:52 +02001566static void
1567print_compiled_node(void **state)
1568{
1569 TEST_LOCAL_SETUP;
1570 const struct lysc_node *node;
1571
1572 orig =
1573 "module a26 {\n"
1574 " yang-version 1.1;\n"
1575 " namespace \"x:y\";\n"
1576 " prefix x;\n"
aPiecekfa3f6872023-03-29 11:33:32 +02001577 "\n"
aPiecek7745b282021-04-20 13:54:52 +02001578 " container g {\n"
1579 " leaf a {\n"
1580 " type string;\n"
1581 " }\n"
1582 " container h {\n"
1583 " leaf b {\n"
1584 " type string;\n"
1585 " mandatory true;\n"
1586 " }\n"
1587 " leaf c {\n"
1588 " type string;\n"
1589 " }\n"
aPiecekfa3f6872023-03-29 11:33:32 +02001590 " list l {\n"
1591 " key \"ip\";\n"
1592 " leaf ip {\n"
1593 " type string;\n"
1594 " }\n"
1595 " }\n"
aPiecek7745b282021-04-20 13:54:52 +02001596 " }\n"
1597 " }\n"
1598 "}\n";
1599
1600 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1601
1602 /* pyang -f tree --tree-path /g/h/c */
1603 expect =
1604 "module: a26\n"
1605 " +--rw g\n"
1606 " +--rw h\n"
1607 " +--rw c? string\n";
1608
1609 /* using lysc tree */
1610 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1611
1612 node = lys_find_path(UTEST_LYCTX, NULL, "/a26:g/h/c", 0);
1613 CHECK_POINTER(node, 1);
1614 assert_int_equal(LY_SUCCESS, lys_print_node(UTEST_OUT, node, LYS_OUT_TREE, 72, 0));
1615 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1616 assert_string_equal(printed, expect);
1617
1618 ly_out_reset(UTEST_OUT);
1619
aPiecekfa3f6872023-03-29 11:33:32 +02001620 /* pyang -f tree --tree-path /g/h/l */
1621 expect =
1622 "module: a26\n"
1623 " +--rw g\n"
1624 " +--rw h\n"
1625 " +--rw l* [ip]\n"
1626 " +--rw ip string\n";
1627
1628 node = lys_find_path(UTEST_LYCTX, NULL, "/a26:g/h/l", 0);
1629 CHECK_POINTER(node, 1);
1630 assert_int_equal(LY_SUCCESS, lys_print_node(UTEST_OUT, node, LYS_OUT_TREE, 72, 0));
1631 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1632 assert_string_equal(printed, expect);
1633
1634 ly_out_reset(UTEST_OUT);
1635
aPiecek7745b282021-04-20 13:54:52 +02001636 /* pyang -f tree --tree-path /g/h */
1637 expect =
1638 "module: a26\n"
1639 " +--rw g\n"
1640 " +--rw h\n"
1641 " +--rw b string\n"
aPiecekfa3f6872023-03-29 11:33:32 +02001642 " +--rw c? string\n"
1643 " +--rw l* [ip]\n"
1644 " +--rw ip string\n";
aPiecek7745b282021-04-20 13:54:52 +02001645
1646 node = lys_find_path(UTEST_LYCTX, NULL, "/a26:g/h", 0);
1647 CHECK_POINTER(node, 1);
1648 assert_int_equal(LY_SUCCESS, lys_print_node(UTEST_OUT, node, LYS_OUT_TREE, 72, 0));
1649 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1650 assert_string_equal(printed, expect);
1651
1652 ly_out_reset(UTEST_OUT);
1653
1654 /* pyang whose output is adjusted manually */
1655 expect =
1656 "module: a26\n"
1657 " +--rw g\n"
1658 " +--rw h\n";
1659
1660 node = lys_find_path(UTEST_LYCTX, NULL, "/a26:g/h", 0);
1661 CHECK_POINTER(node, 1);
1662 assert_int_equal(LY_SUCCESS, lys_print_node(UTEST_OUT, node, LYS_OUT_TREE, 72, LYS_PRINT_NO_SUBSTMT));
1663 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1664 assert_string_equal(printed, expect);
1665
1666 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1667
1668 TEST_LOCAL_TEARDOWN;
1669}
1670
aPieceke4362cc2023-03-29 12:07:41 +02001671static void
1672print_compiled_node_augment(void **state)
1673{
1674 TEST_LOCAL_SETUP;
1675 const struct lysc_node *node;
1676
1677 orig =
1678 "module b26xx {\n"
1679 " yang-version 1.1;\n"
1680 " namespace \"xx:y\";\n"
1681 " prefix xx;\n"
1682 " container c;\n"
1683 "}\n";
1684
1685 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1686
1687 /* module with import statement */
1688 orig =
1689 "module b26 {\n"
1690 " yang-version 1.1;\n"
1691 " namespace \"x:y\";\n"
1692 " prefix x;\n"
1693 "\n"
1694 " import b26xx {\n"
1695 " prefix xx;\n"
1696 " }\n"
1697 "\n"
1698 " augment \"/xx:c\" {\n"
1699 " container e;\n"
1700 " }\n"
1701 "}\n";
1702
1703 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1704
1705 /* pyang -f tree --tree-path /c/e ... but prefixes modified */
1706 expect =
1707 "module: b26\n"
1708 " +--rw xx:c\n"
1709 " +--rw e\n";
1710
1711 /* using lysc tree */
1712 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1713 node = lys_find_path(UTEST_LYCTX, NULL, "/b26xx:c/b26:e", 0);
1714 CHECK_POINTER(node, 1);
1715 assert_int_equal(LY_SUCCESS, lys_print_node(UTEST_OUT, node, LYS_OUT_TREE, 72, 0));
1716 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1717 assert_string_equal(printed, expect);
1718 ly_out_reset(UTEST_OUT);
1719 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1720
1721 TEST_LOCAL_TEARDOWN;
1722}
1723
aPiecekaecf7512021-04-21 08:34:35 +02001724static LY_ERR
1725local_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
1726 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
1727 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
1728{
1729 *module_data = user_data;
1730 *format = LYS_IN_YANG;
1731 *free_module_data = NULL;
1732 return LY_SUCCESS;
1733}
1734
1735static void
1736print_parsed_submodule(void **state)
1737{
1738 TEST_LOCAL_SETUP;
1739
1740 orig = "module a27 {\n"
1741 " yang-version 1.1;\n"
1742 " namespace \"x:y\";\n"
1743 " prefix x;\n"
1744 "\n"
1745 " include \"a27-sub\";\n"
1746 "}\n";
1747
1748 char *submodule =
1749 "submodule a27-sub {\n"
1750 " yang-version 1.1;\n"
1751 " belongs-to a27 {\n"
1752 " prefix x;\n"
1753 " }\n"
1754 "\n"
1755 " container h {\n"
1756 " leaf b {\n"
1757 " type string;\n"
1758 " }\n"
1759 " }\n"
1760 "}\n";
1761
1762 /* edited pyang output */
1763 expect =
1764 "submodule: a27-sub\n"
1765 " +--rw h\n"
1766 " +--rw b? string\n";
1767
1768 ly_ctx_set_module_imp_clb(UTEST_LYCTX, local_imp_clb, submodule);
1769 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1770 assert_int_equal(LY_SUCCESS, lys_print_submodule(UTEST_OUT, mod->parsed->includes[0].submodule, LYS_OUT_TREE, 72, 0));
1771 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1772 assert_string_equal(printed, expect);
1773
1774 TEST_LOCAL_TEARDOWN;
1775}
1776
aPiecekb8e43f12021-04-23 12:52:22 +02001777static void
1778yang_data(void **state)
1779{
1780 TEST_LOCAL_SETUP;
1781
1782 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
1783 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-restconf", "2017-01-26", NULL));
1784
1785 orig =
1786 "module a28 {\n"
1787 " yang-version 1.1;\n"
1788 " namespace \"x:y\";\n"
1789 " prefix x;\n"
1790 "\n"
1791 " import ietf-restconf {\n"
1792 " prefix rc;\n"
1793 " revision-date 2017-01-26;\n"
1794 " }\n"
1795 "\n"
1796 " rc:yang-data \"tmp1\" {\n"
1797 " container cont1 {\n"
1798 " leaf lf {\n"
1799 " type string;\n"
1800 " }\n"
1801 " list l2 {\n"
1802 " key\n"
1803 " \"a b\";\n"
1804 " leaf a {\n"
1805 " type string;\n"
1806 " }\n"
1807 " leaf b {\n"
1808 " type string;\n"
1809 " }\n"
1810 " }\n"
1811 " }\n"
1812 " }\n"
1813 " rc:yang-data \"tmp2\" {\n"
1814 " container con2 {\n"
1815 " leaf lf {\n"
1816 " type string;\n"
1817 " }\n"
1818 " }\n"
1819 " }\n"
1820 " rc:yang-data \"tmp3\" {\n"
1821 " uses g1;\n"
1822 " uses g2;\n"
1823 " }\n"
1824 " rc:yang-data \"tmp4\" {\n"
1825 " choice x {\n"
1826 " case a {\n"
1827 " container z;\n"
1828 " }\n"
1829 " case b {\n"
1830 " container y;\n"
1831 " }\n"
1832 " }\n"
1833 " }\n"
1834 "\n"
1835 " grouping g1 {\n"
1836 " description\n"
1837 " \"Some Text\";\n"
1838 " }\n"
1839 "\n"
1840 " grouping g2 {\n"
1841 " container cont3;\n"
1842 " }\n"
1843 " container mont;\n"
1844 "}\n";
1845
aPiecekb352de72021-05-21 09:04:04 +02001846 /* from pyang (--tree-print-yang-data --tree-print-groupings -p "...")
1847 * but with these adjustments:
aPiecekb8e43f12021-04-23 12:52:22 +02001848 * - <flags> is always '--' for yang-data nodes
1849 * - yang-data tmp3 has two 'uses' nodes
1850 * - grouping g2 has ':' character at the end
1851 */
1852 expect =
1853 "module: a28\n"
1854 " +--rw mont\n"
1855 "\n"
1856 " grouping g1\n"
1857 " grouping g2:\n"
1858 " +---- cont3\n"
1859 "\n"
1860 " yang-data tmp1:\n"
1861 " +---- cont1\n"
1862 " +---- lf? string\n"
1863 " +---- l2* [a b]\n"
1864 " +---- a string\n"
1865 " +---- b string\n"
1866 " yang-data tmp2:\n"
1867 " +---- con2\n"
1868 " +---- lf? string\n"
1869 " yang-data tmp3:\n"
1870 " +---u g1\n"
1871 " +---u g2\n"
1872 " yang-data tmp4:\n"
1873 " +---- (x)?\n"
1874 " +--:(a)\n"
1875 " | +---- z\n"
1876 " +--:(b)\n"
1877 " +---- y\n";
1878
1879 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1880 TEST_LOCAL_PRINT(mod, 72);
1881 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1882 assert_string_equal(printed, expect);
1883
1884 ly_out_reset(UTEST_OUT);
1885
aPiecekb352de72021-05-21 09:04:04 +02001886 /* from pyang (--tree-print-yang-data -p "...")
1887 * but with these adjustments:
aPiecekb8e43f12021-04-23 12:52:22 +02001888 * <flags> is always '--' for yang-data nodes
1889 */
1890 expect =
1891 "module: a28\n"
1892 " +--rw mont\n"
1893 "\n"
1894 " yang-data tmp1:\n"
1895 " +---- cont1\n"
1896 " +---- lf? string\n"
1897 " +---- l2* [a b]\n"
1898 " +---- a string\n"
1899 " +---- b string\n"
1900 " yang-data tmp2:\n"
1901 " +---- con2\n"
1902 " +---- lf? string\n"
1903 " yang-data tmp3:\n"
1904 " +---- cont3\n"
1905 " yang-data tmp4:\n"
1906 " +---- (x)?\n"
1907 " +--:(a)\n"
1908 " | +---- z\n"
1909 " +--:(b)\n"
1910 " +---- y\n";
1911
1912 /* using lysc tree */
1913 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1914 TEST_LOCAL_PRINT(mod, 72);
1915 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1916 assert_string_equal(printed, expect);
1917 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1918
1919 TEST_LOCAL_TEARDOWN;
1920}
1921
aPiecek85cda2a2022-10-13 13:47:05 +02001922static LY_ERR
1923getter(const struct lysc_ext_instance *ext, void *user_data, void **ext_data, ly_bool *ext_data_free)
1924{
1925 struct ly_ctx *ctx;
1926 struct lyd_node *data = NULL;
1927
1928 ctx = ext->module->ctx;
1929 if (user_data) {
1930 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, user_data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &data));
1931 }
1932
1933 *ext_data = data;
1934 *ext_data_free = 1;
1935 return LY_SUCCESS;
1936}
1937
1938#define SM_MODNAME_EXT "sm-extension"
1939#define SM_MOD_EXT_NAMESPACE "urn:sm-ext"
1940#define SM_PREF "sm"
1941#define SCHEMA_REF_INLINE "<inline></inline>"
1942#define SCHEMA_REF_SHARED(REF) "<shared-schema>"REF"</shared-schema>"
1943
1944#define EXT_DATA(MPMOD_NAME, MODULES, SCHEMA_REF) \
1945 "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\"\n" \
1946 " xmlns:ds=\"urn:ietf:params:xml:ns:yang:ietf-datastores\">\n" \
1947 "<module-set>\n" \
1948 " <name>test-set</name>\n" \
1949 " <module>\n" \
1950 " <name>"SM_MODNAME_EXT"</name>\n" \
1951 " <namespace>"SM_MOD_EXT_NAMESPACE"</namespace>\n" \
1952 " </module>\n" \
1953 MODULES \
1954 "</module-set>\n" \
1955 "<content-id>1</content-id>\n" \
1956 "</yang-library>\n" \
1957 "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n" \
1958 "<module-set-id>1</module-set-id>\n" \
1959 "</modules-state>\n" \
1960 "<schema-mounts xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-schema-mount\">\n" \
1961 "<namespace>\n" \
1962 " <prefix>"SM_PREF"</prefix>\n" \
1963 " <uri>x:"MPMOD_NAME"</uri>\n" \
1964 "</namespace>\n" \
1965 "<mount-point>\n" \
1966 " <module>"MPMOD_NAME"</module>\n" \
1967 " <label>mnt-root</label>\n" \
1968 SCHEMA_REF \
1969 "</mount-point>\n" \
1970 "</schema-mounts>"
1971
1972#define SM_MOD_MAIN(NAME, BODY) \
1973 "module "NAME" {\n" \
1974 " yang-version 1.1;\n" \
1975 " namespace \"x:"NAME"\";\n" \
1976 " prefix \"x\";\n" \
1977 " import ietf-yang-schema-mount {\n" \
1978 " prefix yangmnt;\n" \
1979 " }\n" \
1980 BODY \
1981 "}"
1982
ekinzie0ab8b302022-10-10 03:03:57 -04001983static void
1984mount_point(void **state)
1985{
aPiecek85cda2a2022-10-13 13:47:05 +02001986 char *data;
1987
ekinzie0ab8b302022-10-10 03:03:57 -04001988 TEST_LOCAL_SETUP;
aPiecek85cda2a2022-10-13 13:47:05 +02001989 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
ekinzie0ab8b302022-10-10 03:03:57 -04001990
aPiecek85cda2a2022-10-13 13:47:05 +02001991 /* interested in sm-extension.yang and sm-mod.yang */
1992 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
ekinzie0ab8b302022-10-10 03:03:57 -04001993
aPiecek85cda2a2022-10-13 13:47:05 +02001994 /*
1995 * 'mp' flag for list and container
1996 */
1997 orig = SM_MOD_MAIN("a29",
1998 "list lt {\n"
1999 " key \"name\";\n"
2000 " leaf name {\n"
2001 " type string;\n"
2002 " }\n"
2003 " yangmnt:mount-point \"mnt-root\";\n"
2004 "}\n"
2005 "container cont {\n"
2006 " yangmnt:mount-point \"mnt-root\";\n"
2007 "}\n");
ekinzie0ab8b302022-10-10 03:03:57 -04002008 expect =
Michal Vasko8bb6e7e2022-10-10 09:05:14 +02002009 "module: a29\n"
aPiecek85cda2a2022-10-13 13:47:05 +02002010 " +--mp lt* [name]\n"
Michal Vasko8bb6e7e2022-10-10 09:05:14 +02002011 " | +--rw name string\n"
aPiecek85cda2a2022-10-13 13:47:05 +02002012 " +--mp cont\n";
ekinzie0ab8b302022-10-10 03:03:57 -04002013 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
2014 TEST_LOCAL_PRINT(mod, 72);
2015 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
2016 assert_string_equal(printed, expect);
ekinzie0ab8b302022-10-10 03:03:57 -04002017 ly_out_reset(UTEST_OUT);
2018
aPiecek85cda2a2022-10-13 13:47:05 +02002019 /*
2020 * mount schema by 'inline' schema-ref
2021 */
2022 orig = SM_MOD_MAIN("a30",
2023 "list lt {\n"
2024 " key \"name\";\n"
2025 " leaf name {\n"
2026 " type string;\n"
2027 " }\n"
2028 " yangmnt:mount-point \"mnt-root\";\n"
2029 "}\n");
2030 expect =
2031 "module: a30\n"
2032 " +--mp lt* [name]\n"
aPiecek03cb4872022-10-24 10:31:51 +02002033 " +--rw tlist/* [name]\n"
aPiecek85cda2a2022-10-13 13:47:05 +02002034 " | +--rw name uint32\n"
2035 " +--rw tcont/\n"
2036 " | +--rw tleaf? uint32\n"
2037 " +--rw name string\n";
2038 data = EXT_DATA("a30", "", SCHEMA_REF_INLINE);
2039 ly_ctx_set_ext_data_clb(UTEST_LYCTX, getter, data);
2040 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
ekinzie0ab8b302022-10-10 03:03:57 -04002041 TEST_LOCAL_PRINT(mod, 72);
2042 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
2043 assert_string_equal(printed, expect);
aPiecek85cda2a2022-10-13 13:47:05 +02002044 ly_out_reset(UTEST_OUT);
ekinzie0ab8b302022-10-10 03:03:57 -04002045
aPiecek85cda2a2022-10-13 13:47:05 +02002046 /*
2047 * mount schema into empty container
2048 */
2049 orig = SM_MOD_MAIN("a31",
2050 "container cont {\n"
2051 " yangmnt:mount-point \"mnt-root\";\n"
2052 "}\n"
2053 "leaf lf {\n"
2054 " type string;\n"
2055 "}\n");
2056 expect =
2057 "module: a31\n"
2058 " +--mp cont\n"
aPiecek03cb4872022-10-24 10:31:51 +02002059 " | +--rw tlist/* [name]\n"
aPiecek85cda2a2022-10-13 13:47:05 +02002060 " | | +--rw name uint32\n"
2061 " | +--rw tcont/\n"
2062 " | +--rw tleaf? uint32\n"
2063 " +--rw lf? string\n";
2064 data = EXT_DATA("a31", "", SCHEMA_REF_INLINE);
2065 ly_ctx_set_ext_data_clb(UTEST_LYCTX, getter, data);
2066 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
2067 TEST_LOCAL_PRINT(mod, 72);
2068 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
2069 assert_string_equal(printed, expect);
2070 ly_out_reset(UTEST_OUT);
2071
2072 /*
2073 * mount schema into non-empty container
2074 */
2075 orig = SM_MOD_MAIN("a32",
2076 "container cont {\n"
2077 " leaf lf1 {\n"
2078 " type string;\n"
2079 " }\n"
2080 " yangmnt:mount-point \"mnt-root\";\n"
2081 " leaf lf2 {\n"
2082 " type string;\n"
2083 " }\n"
2084 "}\n");
2085 expect =
2086 "module: a32\n"
2087 " +--mp cont\n"
aPiecek03cb4872022-10-24 10:31:51 +02002088 " +--rw tlist/* [name]\n"
aPiecek85cda2a2022-10-13 13:47:05 +02002089 " | +--rw name uint32\n"
2090 " +--rw tcont/\n"
2091 " | +--rw tleaf? uint32\n"
2092 " +--rw lf1? string\n"
2093 " +--rw lf2? string\n";
2094 data = EXT_DATA("a32", "", SCHEMA_REF_INLINE);
2095 ly_ctx_set_ext_data_clb(UTEST_LYCTX, getter, data);
2096 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
2097 TEST_LOCAL_PRINT(mod, 72);
2098 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
2099 assert_string_equal(printed, expect);
2100 ly_out_reset(UTEST_OUT);
2101
2102 /*
2103 * mounting with parent-reference
2104 */
2105 orig = SM_MOD_MAIN("a33",
2106 "list pr {\n"
2107 " key \"name\";\n"
2108 " leaf name {\n"
2109 " type string;\n"
2110 " }\n"
2111 " leaf prlf {\n"
2112 " type string;\n"
2113 " }\n"
2114 "}\n"
2115 "leaf lf {\n"
2116 " type string;\n"
2117 "}\n"
2118 "container cont {\n"
2119 " yangmnt:mount-point \"mnt-root\";\n"
2120 " list lt {\n"
2121 " key \"name\";\n"
2122 " leaf name {\n"
2123 " type string;\n"
2124 " }\n"
2125 " }\n"
2126 "}\n");
2127 expect =
2128 "module: a33\n"
2129 " +--rw pr* [name]\n"
2130 " | +--rw name string\n"
2131 " | +--rw prlf? string\n"
2132 " +--rw lf? string\n"
2133 " +--mp cont\n"
aPiecek03cb4872022-10-24 10:31:51 +02002134 " +--rw tlist/* [name]\n"
aPiecek85cda2a2022-10-13 13:47:05 +02002135 " | +--rw name uint32\n"
2136 " +--rw tcont/\n"
2137 " | +--rw tleaf? uint32\n"
aPiecek03cb4872022-10-24 10:31:51 +02002138 " +--rw pr@* [name]\n"
aPiecek85cda2a2022-10-13 13:47:05 +02002139 " | +--rw prlf? string\n"
aPiecek03cb4872022-10-24 10:31:51 +02002140 " +--rw lf@? string\n"
aPiecek85cda2a2022-10-13 13:47:05 +02002141 " +--rw lt* [name]\n"
2142 " +--rw name string\n";
2143 data = EXT_DATA("a33", "", SCHEMA_REF_SHARED(
2144 "<parent-reference>/"SM_PREF ":pr/"SM_PREF ":prlf</parent-reference>\n"
2145 "<parent-reference>/"SM_PREF ":lf</parent-reference>\n"));
2146 ly_ctx_set_ext_data_clb(UTEST_LYCTX, getter, data);
2147 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
2148 TEST_LOCAL_PRINT(mod, 72);
2149 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
2150 assert_string_equal(printed, expect);
2151 ly_out_reset(UTEST_OUT);
2152
2153 /*
2154 * mounting with parent-reference into empty container
2155 */
2156 orig = SM_MOD_MAIN("a34",
2157 "container cont {\n"
2158 " yangmnt:mount-point \"mnt-root\";\n"
2159 "}\n"
2160 "leaf lf {\n"
2161 " type string;\n"
2162 "}\n");
2163 expect =
2164 "module: a34\n"
2165 " +--mp cont\n"
aPiecek03cb4872022-10-24 10:31:51 +02002166 " | +--rw tlist/* [name]\n"
aPiecek85cda2a2022-10-13 13:47:05 +02002167 " | | +--rw name uint32\n"
2168 " | +--rw tcont/\n"
2169 " | | +--rw tleaf? uint32\n"
aPiecek03cb4872022-10-24 10:31:51 +02002170 " | +--rw lf@? string\n"
aPiecek85cda2a2022-10-13 13:47:05 +02002171 " +--rw lf? string\n";
2172 data = EXT_DATA("a34", "",
2173 SCHEMA_REF_SHARED(
2174 "<parent-reference>/"SM_PREF ":lf</parent-reference>\n"));
2175 ly_ctx_set_ext_data_clb(UTEST_LYCTX, getter, data);
2176 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
2177 TEST_LOCAL_PRINT(mod, 72);
2178 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
2179 assert_string_equal(printed, expect);
2180 ly_out_reset(UTEST_OUT);
2181
2182 /*
2183 * mounting module which is only parsed
2184 */
2185 orig = SM_MOD_MAIN("a35",
2186 "import sm-mod {\n"
2187 " prefix smm;\n"
2188 "}\n"
2189 "container pr {\n"
2190 " leaf prlf {\n"
2191 " type uint32;\n"
2192 " }\n"
2193 "}\n"
2194 "list lt {\n"
2195 " key \"name\";\n"
2196 " yangmnt:mount-point \"mnt-root\";\n"
2197 " leaf name {\n"
2198 " type string;\n"
2199 " }\n"
2200 "}\n");
2201 expect =
2202 "module: a35\n"
2203 " +--rw pr\n"
2204 " | +--rw prlf? uint32\n"
2205 " +--mp lt* [name]\n"
aPiecek03cb4872022-10-24 10:31:51 +02002206 " +--rw tlist/* [name]\n"
aPiecek85cda2a2022-10-13 13:47:05 +02002207 " | +--rw name uint32\n"
2208 " +--rw tcont/\n"
2209 " | +--rw tleaf? uint32\n"
2210 " +--mp ncmp/\n"
2211 " +--rw not-compiled/\n"
2212 " | +--rw first? string\n"
2213 " | +--rw second? string\n"
2214 " +--rw pr@\n"
2215 " | +--rw prlf? uint32\n"
2216 " +--rw name string\n";
2217 data = EXT_DATA("a35",
2218 "<module>\n"
2219 " <name>sm-mod</name>\n"
2220 " <namespace>urn:sm-mod</namespace>\n"
2221 "</module>\n",
2222 SCHEMA_REF_SHARED(
2223 "<parent-reference>/"SM_PREF ":pr/"SM_PREF ":prlf</parent-reference>\n"));
2224 ly_ctx_set_ext_data_clb(UTEST_LYCTX, getter, data);
2225 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
2226 TEST_LOCAL_PRINT(mod, 72);
2227 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
2228 assert_string_equal(printed, expect);
2229 ly_out_reset(UTEST_OUT);
2230
aPiecek36f82232022-10-14 10:08:38 +02002231 /*
2232 * notifications and rpcs in mounted module
2233 */
2234 orig = SM_MOD_MAIN("a36",
2235 "container cont {\n"
2236 " yangmnt:mount-point \"mnt-root\";\n"
2237 "}\n");
2238 expect =
2239 "module: a36\n"
2240 " +--mp cont\n"
aPiecek03cb4872022-10-24 10:31:51 +02002241 " +--rw tlist/* [name]\n"
aPiecek36f82232022-10-14 10:08:38 +02002242 " | +--rw name uint32\n"
2243 " +--rw tcont/\n"
2244 " | +--rw tleaf? uint32\n"
2245 " +--rw cont/\n"
2246 " | +---x cr\n"
2247 " | | +---w input\n"
2248 " | | | +---w in? string\n"
2249 " | | +--ro output\n"
2250 " | | +--ro out? string\n"
2251 " | +---n cn\n"
2252 " +---x r1/\n"
2253 " +---x r2/\n"
2254 " +---n n1/\n"
2255 " +---n n2/\n";
2256 data = EXT_DATA("a36",
2257 "<module>\n"
2258 " <name>sm-rpcnotif</name>\n"
2259 " <namespace>urn:rpcnotif</namespace>\n"
2260 "</module>\n",
2261 SCHEMA_REF_INLINE);
2262 ly_ctx_set_ext_data_clb(UTEST_LYCTX, getter, data);
2263 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
2264 TEST_LOCAL_PRINT(mod, 72);
2265 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
2266 assert_string_equal(printed, expect);
2267 ly_out_reset(UTEST_OUT);
2268
aPiecek8f1073c2022-10-17 16:44:49 +02002269 /*
2270 * parent-ref composes the '@' subtree
2271 */
2272 orig = SM_MOD_MAIN("a37",
2273 "container pr {\n"
2274 " leaf ignored_node {\n"
2275 " type string;\n"
2276 " }\n"
2277 " container cont {\n"
2278 " leaf ignored_lf {\n"
2279 " type uint32;\n"
2280 " }\n"
2281 " }\n"
2282 " container ignored_subtree {\n"
2283 " leaf ignored_lf {\n"
2284 " type uint32;\n"
2285 " }\n"
2286 " }\n"
2287 " container cont_sibl {\n"
2288 " leaf slf {\n"
2289 " type string;\n"
2290 " }\n"
2291 " }\n"
2292 " leaf lf {\n"
2293 " type uint32;\n"
2294 " }\n"
2295 "}\n"
2296 "container cont_mount {\n"
2297 " yangmnt:mount-point \"mnt-root\";\n"
2298 "}\n");
2299 expect =
2300 "module: a37\n"
2301 " +--rw pr\n"
2302 " | +--rw ignored_node? string\n"
2303 " | +--rw cont\n"
2304 " | | +--rw ignored_lf? uint32\n"
2305 " | +--rw ignored_subtree\n"
2306 " | | +--rw ignored_lf? uint32\n"
2307 " | +--rw cont_sibl\n"
2308 " | | +--rw slf? string\n"
2309 " | +--rw lf? uint32\n"
2310 " +--mp cont_mount\n"
aPiecek03cb4872022-10-24 10:31:51 +02002311 " +--rw tlist/* [name]\n"
aPiecek8f1073c2022-10-17 16:44:49 +02002312 " | +--rw name uint32\n"
2313 " +--rw tcont/\n"
2314 " | +--rw tleaf? uint32\n"
2315 " +--rw pr@\n"
2316 " +--rw cont\n"
2317 " +--rw cont_sibl\n"
2318 " | +--rw slf? string\n"
2319 " +--rw lf? uint32\n";
2320 data = EXT_DATA("a37", "", SCHEMA_REF_SHARED(
2321 "<parent-reference>/"SM_PREF ":pr/"SM_PREF ":cont_sibl/slf</parent-reference>\n"
2322 "<parent-reference>/"SM_PREF ":pr/"SM_PREF ":cont</parent-reference>\n"
2323 "<parent-reference>/"SM_PREF ":pr/"SM_PREF ":lf</parent-reference>\n"));
2324 ly_ctx_set_ext_data_clb(UTEST_LYCTX, getter, data);
2325 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
2326 TEST_LOCAL_PRINT(mod, 72);
2327 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
2328 assert_string_equal(printed, expect);
2329 ly_out_reset(UTEST_OUT);
2330
aPiecek85cda2a2022-10-13 13:47:05 +02002331 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
ekinzie0ab8b302022-10-10 03:03:57 -04002332 TEST_LOCAL_TEARDOWN;
2333}
2334
aPiecek03cb4872022-10-24 10:31:51 +02002335static void
2336structure(void **state)
2337{
2338 TEST_LOCAL_SETUP;
2339
2340 orig =
2341 "module example-module {\n"
2342 " yang-version 1.1;\n"
2343 " namespace \"urn:example:example-module\";\n"
2344 " prefix exm;\n"
2345 "\n"
2346 " import ietf-yang-structure-ext {\n"
2347 " prefix sx;\n"
2348 " }\n"
2349 "\n"
2350 " sx:structure address-book {\n"
2351 " list address {\n"
2352 " key \"last first\";\n"
2353 " leaf last {\n"
2354 " type string;\n"
2355 " }\n"
2356 " leaf first {\n"
2357 " type string;\n"
2358 " }\n"
2359 " leaf street {\n"
2360 " type string;\n"
2361 " }\n"
2362 " leaf city {\n"
2363 " type string;\n"
2364 " }\n"
2365 " leaf state {\n"
2366 " type string;\n"
2367 " }\n"
2368 " }\n"
2369 " }\n"
2370 "}\n";
2371
2372 /* from RFC 8791, Appendix A.1 */
2373 expect =
2374 "module: example-module\n"
2375 "\n"
2376 " structure address-book:\n"
2377 " +-- address* [last first]\n"
2378 " +-- last string\n"
2379 " +-- first string\n"
2380 " +-- street? string\n"
2381 " +-- city? string\n"
2382 " +-- state? string\n";
2383
2384 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
2385 TEST_LOCAL_PRINT(mod, 72);
2386 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
2387 assert_string_equal(printed, expect);
2388 ly_out_reset(UTEST_OUT);
2389
2390 /* using lysc tree */
2391 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
2392 TEST_LOCAL_PRINT(mod, 72);
2393 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
2394 assert_string_equal(printed, expect);
2395 ly_out_reset(UTEST_OUT);
2396 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
2397
2398 orig =
2399 "module example-module-aug {\n"
2400 " yang-version 1.1;\n"
2401 " namespace \"urn:example:example-module-aug\";\n"
2402 " prefix exma;\n"
2403 "\n"
2404 " import ietf-yang-structure-ext {\n"
2405 " prefix sx;\n"
2406 " }\n"
2407 " import example-module {\n"
2408 " prefix exm;\n"
2409 " }\n"
2410 "\n"
2411 " sx:augment-structure \"/exm:address-book/exm:address\" {\n"
2412 " leaf county {\n"
2413 " type string;\n"
2414 " }\n"
2415 " leaf zipcode {\n"
2416 " type string;\n"
2417 " }\n"
2418 " }\n"
2419 "}\n";
2420
2421 /* from RFC 8791, Appendix A.2 */
2422 expect =
2423 "module: example-module-aug\n"
2424 "\n"
2425 " augment-structure /exm:address-book/exm:address:\n"
2426 " +-- county? string\n"
2427 " +-- zipcode? string\n";
2428
2429 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
2430 TEST_LOCAL_PRINT(mod, 72);
2431 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
2432 assert_string_equal(printed, expect);
2433 ly_out_reset(UTEST_OUT);
2434
2435 /* using lysc tree */
2436 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
2437 TEST_LOCAL_PRINT(mod, 72);
2438 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
2439 assert_string_equal(printed, expect);
2440 ly_out_reset(UTEST_OUT);
2441 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
2442
2443 TEST_LOCAL_TEARDOWN;
2444}
2445
aPiecekba674622023-03-28 10:57:00 +02002446static void
2447annotation(void **state)
2448{
2449 TEST_LOCAL_SETUP;
2450
2451 orig =
2452 "module ann {\n"
2453 " yang-version 1.1;\n"
2454 " namespace \"urn:example:ann\";\n"
2455 " prefix an;\n"
2456 "\n"
2457 " import ietf-yang-metadata {\n"
2458 " prefix md;\n"
2459 " }\n"
2460 "\n"
2461 " leaf lf1 {\n"
2462 " type string;\n"
2463 " }\n"
2464 " md:annotation avalue {\n"
2465 " type string;\n"
2466 " }\n"
2467 "}\n";
2468
2469 expect =
2470 "module: ann\n"
2471 " +--rw lf1? string\n";
2472
2473 /* annotation is ignored without error message */
2474 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
2475 TEST_LOCAL_PRINT(mod, 72);
2476 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
2477 assert_string_equal(printed, expect);
2478 ly_out_reset(UTEST_OUT);
2479
2480 TEST_LOCAL_TEARDOWN;
2481}
2482
aPiecek20ddf8b2021-01-08 11:50:37 +01002483int
2484main(void)
2485{
2486 const struct CMUnitTest tests[] = {
2487 UTEST(base_sections),
2488 UTEST(node_status),
2489 UTEST(node_config_flags),
2490 UTEST(node_rpcs_flags),
2491 UTEST(node_grouping_flags),
2492 UTEST(notif_inside_container),
2493 UTEST(node_choice),
2494 UTEST(node_case),
2495 UTEST(optional_opts),
2496 UTEST(presence_container),
2497 UTEST(node_keys),
2498 UTEST(node_type_target),
2499 UTEST(node_type_leafref),
2500 UTEST(node_iffeatures),
2501 UTEST(indent_wrapper),
2502 UTEST(line_length_twiddling),
2503 UTEST(break_before_leafref),
2504 UTEST(break_before_leafref_and_iffeature),
2505 UTEST(basic_unified_indent_before_type),
2506 UTEST(twiddling_unified_indent_before_type),
2507 UTEST(inheritance_of_config_flag),
2508 UTEST(inheritance_of_status_flag),
2509 UTEST(key_leaf_is_always_mandatory_true),
2510 UTEST(transition_between_rpc_and_notif),
aPiecek9e505922021-04-19 13:45:08 +02002511 UTEST(local_augment),
aPiecek7745b282021-04-20 13:54:52 +02002512 UTEST(print_compiled_node),
aPieceke4362cc2023-03-29 12:07:41 +02002513 UTEST(print_compiled_node_augment),
aPiecekaecf7512021-04-21 08:34:35 +02002514 UTEST(print_parsed_submodule),
aPiecekb8e43f12021-04-23 12:52:22 +02002515 UTEST(yang_data),
aPiecek03cb4872022-10-24 10:31:51 +02002516 UTEST(mount_point),
2517 UTEST(structure),
aPiecekba674622023-03-28 10:57:00 +02002518 UTEST(annotation),
aPiecek20ddf8b2021-01-08 11:50:37 +01002519 };
2520
2521 return cmocka_run_group_tests(tests, NULL, NULL);
2522}