blob: 06ae4271c2df4434f77e52265483bc2db99f165f [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; \
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"
1577 " container g {\n"
1578 " leaf a {\n"
1579 " type string;\n"
1580 " }\n"
1581 " container h {\n"
1582 " leaf b {\n"
1583 " type string;\n"
1584 " mandatory true;\n"
1585 " }\n"
1586 " leaf c {\n"
1587 " type string;\n"
1588 " }\n"
1589 " }\n"
1590 " }\n"
1591 "}\n";
1592
1593 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1594
1595 /* pyang -f tree --tree-path /g/h/c */
1596 expect =
1597 "module: a26\n"
1598 " +--rw g\n"
1599 " +--rw h\n"
1600 " +--rw c? string\n";
1601
1602 /* using lysc tree */
1603 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1604
1605 node = lys_find_path(UTEST_LYCTX, NULL, "/a26:g/h/c", 0);
1606 CHECK_POINTER(node, 1);
1607 assert_int_equal(LY_SUCCESS, lys_print_node(UTEST_OUT, node, LYS_OUT_TREE, 72, 0));
1608 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1609 assert_string_equal(printed, expect);
1610
1611 ly_out_reset(UTEST_OUT);
1612
1613 /* pyang -f tree --tree-path /g/h */
1614 expect =
1615 "module: a26\n"
1616 " +--rw g\n"
1617 " +--rw h\n"
1618 " +--rw b string\n"
1619 " +--rw c? string\n";
1620
1621 node = lys_find_path(UTEST_LYCTX, NULL, "/a26:g/h", 0);
1622 CHECK_POINTER(node, 1);
1623 assert_int_equal(LY_SUCCESS, lys_print_node(UTEST_OUT, node, LYS_OUT_TREE, 72, 0));
1624 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1625 assert_string_equal(printed, expect);
1626
1627 ly_out_reset(UTEST_OUT);
1628
1629 /* pyang whose output is adjusted manually */
1630 expect =
1631 "module: a26\n"
1632 " +--rw g\n"
1633 " +--rw h\n";
1634
1635 node = lys_find_path(UTEST_LYCTX, NULL, "/a26:g/h", 0);
1636 CHECK_POINTER(node, 1);
1637 assert_int_equal(LY_SUCCESS, lys_print_node(UTEST_OUT, node, LYS_OUT_TREE, 72, LYS_PRINT_NO_SUBSTMT));
1638 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1639 assert_string_equal(printed, expect);
1640
1641 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1642
1643 TEST_LOCAL_TEARDOWN;
1644}
1645
aPiecekaecf7512021-04-21 08:34:35 +02001646static LY_ERR
1647local_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
1648 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
1649 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
1650{
1651 *module_data = user_data;
1652 *format = LYS_IN_YANG;
1653 *free_module_data = NULL;
1654 return LY_SUCCESS;
1655}
1656
1657static void
1658print_parsed_submodule(void **state)
1659{
1660 TEST_LOCAL_SETUP;
1661
1662 orig = "module a27 {\n"
1663 " yang-version 1.1;\n"
1664 " namespace \"x:y\";\n"
1665 " prefix x;\n"
1666 "\n"
1667 " include \"a27-sub\";\n"
1668 "}\n";
1669
1670 char *submodule =
1671 "submodule a27-sub {\n"
1672 " yang-version 1.1;\n"
1673 " belongs-to a27 {\n"
1674 " prefix x;\n"
1675 " }\n"
1676 "\n"
1677 " container h {\n"
1678 " leaf b {\n"
1679 " type string;\n"
1680 " }\n"
1681 " }\n"
1682 "}\n";
1683
1684 /* edited pyang output */
1685 expect =
1686 "submodule: a27-sub\n"
1687 " +--rw h\n"
1688 " +--rw b? string\n";
1689
1690 ly_ctx_set_module_imp_clb(UTEST_LYCTX, local_imp_clb, submodule);
1691 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1692 assert_int_equal(LY_SUCCESS, lys_print_submodule(UTEST_OUT, mod->parsed->includes[0].submodule, LYS_OUT_TREE, 72, 0));
1693 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1694 assert_string_equal(printed, expect);
1695
1696 TEST_LOCAL_TEARDOWN;
1697}
1698
aPiecekb8e43f12021-04-23 12:52:22 +02001699static void
1700yang_data(void **state)
1701{
1702 TEST_LOCAL_SETUP;
1703
1704 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
1705 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-restconf", "2017-01-26", NULL));
1706
1707 orig =
1708 "module a28 {\n"
1709 " yang-version 1.1;\n"
1710 " namespace \"x:y\";\n"
1711 " prefix x;\n"
1712 "\n"
1713 " import ietf-restconf {\n"
1714 " prefix rc;\n"
1715 " revision-date 2017-01-26;\n"
1716 " }\n"
1717 "\n"
1718 " rc:yang-data \"tmp1\" {\n"
1719 " container cont1 {\n"
1720 " leaf lf {\n"
1721 " type string;\n"
1722 " }\n"
1723 " list l2 {\n"
1724 " key\n"
1725 " \"a b\";\n"
1726 " leaf a {\n"
1727 " type string;\n"
1728 " }\n"
1729 " leaf b {\n"
1730 " type string;\n"
1731 " }\n"
1732 " }\n"
1733 " }\n"
1734 " }\n"
1735 " rc:yang-data \"tmp2\" {\n"
1736 " container con2 {\n"
1737 " leaf lf {\n"
1738 " type string;\n"
1739 " }\n"
1740 " }\n"
1741 " }\n"
1742 " rc:yang-data \"tmp3\" {\n"
1743 " uses g1;\n"
1744 " uses g2;\n"
1745 " }\n"
1746 " rc:yang-data \"tmp4\" {\n"
1747 " choice x {\n"
1748 " case a {\n"
1749 " container z;\n"
1750 " }\n"
1751 " case b {\n"
1752 " container y;\n"
1753 " }\n"
1754 " }\n"
1755 " }\n"
1756 "\n"
1757 " grouping g1 {\n"
1758 " description\n"
1759 " \"Some Text\";\n"
1760 " }\n"
1761 "\n"
1762 " grouping g2 {\n"
1763 " container cont3;\n"
1764 " }\n"
1765 " container mont;\n"
1766 "}\n";
1767
aPiecekb352de72021-05-21 09:04:04 +02001768 /* from pyang (--tree-print-yang-data --tree-print-groupings -p "...")
1769 * but with these adjustments:
aPiecekb8e43f12021-04-23 12:52:22 +02001770 * - <flags> is always '--' for yang-data nodes
1771 * - yang-data tmp3 has two 'uses' nodes
1772 * - grouping g2 has ':' character at the end
1773 */
1774 expect =
1775 "module: a28\n"
1776 " +--rw mont\n"
1777 "\n"
1778 " grouping g1\n"
1779 " grouping g2:\n"
1780 " +---- cont3\n"
1781 "\n"
1782 " yang-data tmp1:\n"
1783 " +---- cont1\n"
1784 " +---- lf? string\n"
1785 " +---- l2* [a b]\n"
1786 " +---- a string\n"
1787 " +---- b string\n"
1788 " yang-data tmp2:\n"
1789 " +---- con2\n"
1790 " +---- lf? string\n"
1791 " yang-data tmp3:\n"
1792 " +---u g1\n"
1793 " +---u g2\n"
1794 " yang-data tmp4:\n"
1795 " +---- (x)?\n"
1796 " +--:(a)\n"
1797 " | +---- z\n"
1798 " +--:(b)\n"
1799 " +---- y\n";
1800
1801 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1802 TEST_LOCAL_PRINT(mod, 72);
1803 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1804 assert_string_equal(printed, expect);
1805
1806 ly_out_reset(UTEST_OUT);
1807
aPiecekb352de72021-05-21 09:04:04 +02001808 /* from pyang (--tree-print-yang-data -p "...")
1809 * but with these adjustments:
aPiecekb8e43f12021-04-23 12:52:22 +02001810 * <flags> is always '--' for yang-data nodes
1811 */
1812 expect =
1813 "module: a28\n"
1814 " +--rw mont\n"
1815 "\n"
1816 " yang-data tmp1:\n"
1817 " +---- cont1\n"
1818 " +---- lf? string\n"
1819 " +---- l2* [a b]\n"
1820 " +---- a string\n"
1821 " +---- b string\n"
1822 " yang-data tmp2:\n"
1823 " +---- con2\n"
1824 " +---- lf? string\n"
1825 " yang-data tmp3:\n"
1826 " +---- cont3\n"
1827 " yang-data tmp4:\n"
1828 " +---- (x)?\n"
1829 " +--:(a)\n"
1830 " | +---- z\n"
1831 " +--:(b)\n"
1832 " +---- y\n";
1833
1834 /* using lysc tree */
1835 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1836 TEST_LOCAL_PRINT(mod, 72);
1837 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1838 assert_string_equal(printed, expect);
1839 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1840
1841 TEST_LOCAL_TEARDOWN;
1842}
1843
aPiecek85cda2a2022-10-13 13:47:05 +02001844static LY_ERR
1845getter(const struct lysc_ext_instance *ext, void *user_data, void **ext_data, ly_bool *ext_data_free)
1846{
1847 struct ly_ctx *ctx;
1848 struct lyd_node *data = NULL;
1849
1850 ctx = ext->module->ctx;
1851 if (user_data) {
1852 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, user_data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &data));
1853 }
1854
1855 *ext_data = data;
1856 *ext_data_free = 1;
1857 return LY_SUCCESS;
1858}
1859
1860#define SM_MODNAME_EXT "sm-extension"
1861#define SM_MOD_EXT_NAMESPACE "urn:sm-ext"
1862#define SM_PREF "sm"
1863#define SCHEMA_REF_INLINE "<inline></inline>"
1864#define SCHEMA_REF_SHARED(REF) "<shared-schema>"REF"</shared-schema>"
1865
1866#define EXT_DATA(MPMOD_NAME, MODULES, SCHEMA_REF) \
1867 "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\"\n" \
1868 " xmlns:ds=\"urn:ietf:params:xml:ns:yang:ietf-datastores\">\n" \
1869 "<module-set>\n" \
1870 " <name>test-set</name>\n" \
1871 " <module>\n" \
1872 " <name>"SM_MODNAME_EXT"</name>\n" \
1873 " <namespace>"SM_MOD_EXT_NAMESPACE"</namespace>\n" \
1874 " </module>\n" \
1875 MODULES \
1876 "</module-set>\n" \
1877 "<content-id>1</content-id>\n" \
1878 "</yang-library>\n" \
1879 "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n" \
1880 "<module-set-id>1</module-set-id>\n" \
1881 "</modules-state>\n" \
1882 "<schema-mounts xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-schema-mount\">\n" \
1883 "<namespace>\n" \
1884 " <prefix>"SM_PREF"</prefix>\n" \
1885 " <uri>x:"MPMOD_NAME"</uri>\n" \
1886 "</namespace>\n" \
1887 "<mount-point>\n" \
1888 " <module>"MPMOD_NAME"</module>\n" \
1889 " <label>mnt-root</label>\n" \
1890 SCHEMA_REF \
1891 "</mount-point>\n" \
1892 "</schema-mounts>"
1893
1894#define SM_MOD_MAIN(NAME, BODY) \
1895 "module "NAME" {\n" \
1896 " yang-version 1.1;\n" \
1897 " namespace \"x:"NAME"\";\n" \
1898 " prefix \"x\";\n" \
1899 " import ietf-yang-schema-mount {\n" \
1900 " prefix yangmnt;\n" \
1901 " }\n" \
1902 BODY \
1903 "}"
1904
ekinzie0ab8b302022-10-10 03:03:57 -04001905static void
1906mount_point(void **state)
1907{
aPiecek85cda2a2022-10-13 13:47:05 +02001908 char *data;
1909
ekinzie0ab8b302022-10-10 03:03:57 -04001910 TEST_LOCAL_SETUP;
aPiecek85cda2a2022-10-13 13:47:05 +02001911 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
ekinzie0ab8b302022-10-10 03:03:57 -04001912
aPiecek85cda2a2022-10-13 13:47:05 +02001913 /* interested in sm-extension.yang and sm-mod.yang */
1914 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
ekinzie0ab8b302022-10-10 03:03:57 -04001915
aPiecek85cda2a2022-10-13 13:47:05 +02001916 /*
1917 * 'mp' flag for list and container
1918 */
1919 orig = SM_MOD_MAIN("a29",
1920 "list lt {\n"
1921 " key \"name\";\n"
1922 " leaf name {\n"
1923 " type string;\n"
1924 " }\n"
1925 " yangmnt:mount-point \"mnt-root\";\n"
1926 "}\n"
1927 "container cont {\n"
1928 " yangmnt:mount-point \"mnt-root\";\n"
1929 "}\n");
ekinzie0ab8b302022-10-10 03:03:57 -04001930 expect =
Michal Vasko8bb6e7e2022-10-10 09:05:14 +02001931 "module: a29\n"
aPiecek85cda2a2022-10-13 13:47:05 +02001932 " +--mp lt* [name]\n"
Michal Vasko8bb6e7e2022-10-10 09:05:14 +02001933 " | +--rw name string\n"
aPiecek85cda2a2022-10-13 13:47:05 +02001934 " +--mp cont\n";
ekinzie0ab8b302022-10-10 03:03:57 -04001935 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1936 TEST_LOCAL_PRINT(mod, 72);
1937 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1938 assert_string_equal(printed, expect);
ekinzie0ab8b302022-10-10 03:03:57 -04001939 ly_out_reset(UTEST_OUT);
1940
aPiecek85cda2a2022-10-13 13:47:05 +02001941 /*
1942 * mount schema by 'inline' schema-ref
1943 */
1944 orig = SM_MOD_MAIN("a30",
1945 "list lt {\n"
1946 " key \"name\";\n"
1947 " leaf name {\n"
1948 " type string;\n"
1949 " }\n"
1950 " yangmnt:mount-point \"mnt-root\";\n"
1951 "}\n");
1952 expect =
1953 "module: a30\n"
1954 " +--mp lt* [name]\n"
aPiecek03cb4872022-10-24 10:31:51 +02001955 " +--rw tlist/* [name]\n"
aPiecek85cda2a2022-10-13 13:47:05 +02001956 " | +--rw name uint32\n"
1957 " +--rw tcont/\n"
1958 " | +--rw tleaf? uint32\n"
1959 " +--rw name string\n";
1960 data = EXT_DATA("a30", "", SCHEMA_REF_INLINE);
1961 ly_ctx_set_ext_data_clb(UTEST_LYCTX, getter, data);
1962 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
ekinzie0ab8b302022-10-10 03:03:57 -04001963 TEST_LOCAL_PRINT(mod, 72);
1964 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1965 assert_string_equal(printed, expect);
aPiecek85cda2a2022-10-13 13:47:05 +02001966 ly_out_reset(UTEST_OUT);
ekinzie0ab8b302022-10-10 03:03:57 -04001967
aPiecek85cda2a2022-10-13 13:47:05 +02001968 /*
1969 * mount schema into empty container
1970 */
1971 orig = SM_MOD_MAIN("a31",
1972 "container cont {\n"
1973 " yangmnt:mount-point \"mnt-root\";\n"
1974 "}\n"
1975 "leaf lf {\n"
1976 " type string;\n"
1977 "}\n");
1978 expect =
1979 "module: a31\n"
1980 " +--mp cont\n"
aPiecek03cb4872022-10-24 10:31:51 +02001981 " | +--rw tlist/* [name]\n"
aPiecek85cda2a2022-10-13 13:47:05 +02001982 " | | +--rw name uint32\n"
1983 " | +--rw tcont/\n"
1984 " | +--rw tleaf? uint32\n"
1985 " +--rw lf? string\n";
1986 data = EXT_DATA("a31", "", SCHEMA_REF_INLINE);
1987 ly_ctx_set_ext_data_clb(UTEST_LYCTX, getter, data);
1988 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1989 TEST_LOCAL_PRINT(mod, 72);
1990 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1991 assert_string_equal(printed, expect);
1992 ly_out_reset(UTEST_OUT);
1993
1994 /*
1995 * mount schema into non-empty container
1996 */
1997 orig = SM_MOD_MAIN("a32",
1998 "container cont {\n"
1999 " leaf lf1 {\n"
2000 " type string;\n"
2001 " }\n"
2002 " yangmnt:mount-point \"mnt-root\";\n"
2003 " leaf lf2 {\n"
2004 " type string;\n"
2005 " }\n"
2006 "}\n");
2007 expect =
2008 "module: a32\n"
2009 " +--mp cont\n"
aPiecek03cb4872022-10-24 10:31:51 +02002010 " +--rw tlist/* [name]\n"
aPiecek85cda2a2022-10-13 13:47:05 +02002011 " | +--rw name uint32\n"
2012 " +--rw tcont/\n"
2013 " | +--rw tleaf? uint32\n"
2014 " +--rw lf1? string\n"
2015 " +--rw lf2? string\n";
2016 data = EXT_DATA("a32", "", SCHEMA_REF_INLINE);
2017 ly_ctx_set_ext_data_clb(UTEST_LYCTX, getter, data);
2018 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
2019 TEST_LOCAL_PRINT(mod, 72);
2020 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
2021 assert_string_equal(printed, expect);
2022 ly_out_reset(UTEST_OUT);
2023
2024 /*
2025 * mounting with parent-reference
2026 */
2027 orig = SM_MOD_MAIN("a33",
2028 "list pr {\n"
2029 " key \"name\";\n"
2030 " leaf name {\n"
2031 " type string;\n"
2032 " }\n"
2033 " leaf prlf {\n"
2034 " type string;\n"
2035 " }\n"
2036 "}\n"
2037 "leaf lf {\n"
2038 " type string;\n"
2039 "}\n"
2040 "container cont {\n"
2041 " yangmnt:mount-point \"mnt-root\";\n"
2042 " list lt {\n"
2043 " key \"name\";\n"
2044 " leaf name {\n"
2045 " type string;\n"
2046 " }\n"
2047 " }\n"
2048 "}\n");
2049 expect =
2050 "module: a33\n"
2051 " +--rw pr* [name]\n"
2052 " | +--rw name string\n"
2053 " | +--rw prlf? string\n"
2054 " +--rw lf? string\n"
2055 " +--mp cont\n"
aPiecek03cb4872022-10-24 10:31:51 +02002056 " +--rw tlist/* [name]\n"
aPiecek85cda2a2022-10-13 13:47:05 +02002057 " | +--rw name uint32\n"
2058 " +--rw tcont/\n"
2059 " | +--rw tleaf? uint32\n"
aPiecek03cb4872022-10-24 10:31:51 +02002060 " +--rw pr@* [name]\n"
aPiecek85cda2a2022-10-13 13:47:05 +02002061 " | +--rw prlf? string\n"
aPiecek03cb4872022-10-24 10:31:51 +02002062 " +--rw lf@? string\n"
aPiecek85cda2a2022-10-13 13:47:05 +02002063 " +--rw lt* [name]\n"
2064 " +--rw name string\n";
2065 data = EXT_DATA("a33", "", SCHEMA_REF_SHARED(
2066 "<parent-reference>/"SM_PREF ":pr/"SM_PREF ":prlf</parent-reference>\n"
2067 "<parent-reference>/"SM_PREF ":lf</parent-reference>\n"));
2068 ly_ctx_set_ext_data_clb(UTEST_LYCTX, getter, data);
2069 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
2070 TEST_LOCAL_PRINT(mod, 72);
2071 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
2072 assert_string_equal(printed, expect);
2073 ly_out_reset(UTEST_OUT);
2074
2075 /*
2076 * mounting with parent-reference into empty container
2077 */
2078 orig = SM_MOD_MAIN("a34",
2079 "container cont {\n"
2080 " yangmnt:mount-point \"mnt-root\";\n"
2081 "}\n"
2082 "leaf lf {\n"
2083 " type string;\n"
2084 "}\n");
2085 expect =
2086 "module: a34\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"
aPiecek03cb4872022-10-24 10:31:51 +02002092 " | +--rw lf@? string\n"
aPiecek85cda2a2022-10-13 13:47:05 +02002093 " +--rw lf? string\n";
2094 data = EXT_DATA("a34", "",
2095 SCHEMA_REF_SHARED(
2096 "<parent-reference>/"SM_PREF ":lf</parent-reference>\n"));
2097 ly_ctx_set_ext_data_clb(UTEST_LYCTX, getter, data);
2098 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
2099 TEST_LOCAL_PRINT(mod, 72);
2100 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
2101 assert_string_equal(printed, expect);
2102 ly_out_reset(UTEST_OUT);
2103
2104 /*
2105 * mounting module which is only parsed
2106 */
2107 orig = SM_MOD_MAIN("a35",
2108 "import sm-mod {\n"
2109 " prefix smm;\n"
2110 "}\n"
2111 "container pr {\n"
2112 " leaf prlf {\n"
2113 " type uint32;\n"
2114 " }\n"
2115 "}\n"
2116 "list lt {\n"
2117 " key \"name\";\n"
2118 " yangmnt:mount-point \"mnt-root\";\n"
2119 " leaf name {\n"
2120 " type string;\n"
2121 " }\n"
2122 "}\n");
2123 expect =
2124 "module: a35\n"
2125 " +--rw pr\n"
2126 " | +--rw prlf? uint32\n"
2127 " +--mp lt* [name]\n"
aPiecek03cb4872022-10-24 10:31:51 +02002128 " +--rw tlist/* [name]\n"
aPiecek85cda2a2022-10-13 13:47:05 +02002129 " | +--rw name uint32\n"
2130 " +--rw tcont/\n"
2131 " | +--rw tleaf? uint32\n"
2132 " +--mp ncmp/\n"
2133 " +--rw not-compiled/\n"
2134 " | +--rw first? string\n"
2135 " | +--rw second? string\n"
2136 " +--rw pr@\n"
2137 " | +--rw prlf? uint32\n"
2138 " +--rw name string\n";
2139 data = EXT_DATA("a35",
2140 "<module>\n"
2141 " <name>sm-mod</name>\n"
2142 " <namespace>urn:sm-mod</namespace>\n"
2143 "</module>\n",
2144 SCHEMA_REF_SHARED(
2145 "<parent-reference>/"SM_PREF ":pr/"SM_PREF ":prlf</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
aPiecek36f82232022-10-14 10:08:38 +02002153 /*
2154 * notifications and rpcs in mounted module
2155 */
2156 orig = SM_MOD_MAIN("a36",
2157 "container cont {\n"
2158 " yangmnt:mount-point \"mnt-root\";\n"
2159 "}\n");
2160 expect =
2161 "module: a36\n"
2162 " +--mp cont\n"
aPiecek03cb4872022-10-24 10:31:51 +02002163 " +--rw tlist/* [name]\n"
aPiecek36f82232022-10-14 10:08:38 +02002164 " | +--rw name uint32\n"
2165 " +--rw tcont/\n"
2166 " | +--rw tleaf? uint32\n"
2167 " +--rw cont/\n"
2168 " | +---x cr\n"
2169 " | | +---w input\n"
2170 " | | | +---w in? string\n"
2171 " | | +--ro output\n"
2172 " | | +--ro out? string\n"
2173 " | +---n cn\n"
2174 " +---x r1/\n"
2175 " +---x r2/\n"
2176 " +---n n1/\n"
2177 " +---n n2/\n";
2178 data = EXT_DATA("a36",
2179 "<module>\n"
2180 " <name>sm-rpcnotif</name>\n"
2181 " <namespace>urn:rpcnotif</namespace>\n"
2182 "</module>\n",
2183 SCHEMA_REF_INLINE);
2184 ly_ctx_set_ext_data_clb(UTEST_LYCTX, getter, data);
2185 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
2186 TEST_LOCAL_PRINT(mod, 72);
2187 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
2188 assert_string_equal(printed, expect);
2189 ly_out_reset(UTEST_OUT);
2190
aPiecek8f1073c2022-10-17 16:44:49 +02002191 /*
2192 * parent-ref composes the '@' subtree
2193 */
2194 orig = SM_MOD_MAIN("a37",
2195 "container pr {\n"
2196 " leaf ignored_node {\n"
2197 " type string;\n"
2198 " }\n"
2199 " container cont {\n"
2200 " leaf ignored_lf {\n"
2201 " type uint32;\n"
2202 " }\n"
2203 " }\n"
2204 " container ignored_subtree {\n"
2205 " leaf ignored_lf {\n"
2206 " type uint32;\n"
2207 " }\n"
2208 " }\n"
2209 " container cont_sibl {\n"
2210 " leaf slf {\n"
2211 " type string;\n"
2212 " }\n"
2213 " }\n"
2214 " leaf lf {\n"
2215 " type uint32;\n"
2216 " }\n"
2217 "}\n"
2218 "container cont_mount {\n"
2219 " yangmnt:mount-point \"mnt-root\";\n"
2220 "}\n");
2221 expect =
2222 "module: a37\n"
2223 " +--rw pr\n"
2224 " | +--rw ignored_node? string\n"
2225 " | +--rw cont\n"
2226 " | | +--rw ignored_lf? uint32\n"
2227 " | +--rw ignored_subtree\n"
2228 " | | +--rw ignored_lf? uint32\n"
2229 " | +--rw cont_sibl\n"
2230 " | | +--rw slf? string\n"
2231 " | +--rw lf? uint32\n"
2232 " +--mp cont_mount\n"
aPiecek03cb4872022-10-24 10:31:51 +02002233 " +--rw tlist/* [name]\n"
aPiecek8f1073c2022-10-17 16:44:49 +02002234 " | +--rw name uint32\n"
2235 " +--rw tcont/\n"
2236 " | +--rw tleaf? uint32\n"
2237 " +--rw pr@\n"
2238 " +--rw cont\n"
2239 " +--rw cont_sibl\n"
2240 " | +--rw slf? string\n"
2241 " +--rw lf? uint32\n";
2242 data = EXT_DATA("a37", "", SCHEMA_REF_SHARED(
2243 "<parent-reference>/"SM_PREF ":pr/"SM_PREF ":cont_sibl/slf</parent-reference>\n"
2244 "<parent-reference>/"SM_PREF ":pr/"SM_PREF ":cont</parent-reference>\n"
2245 "<parent-reference>/"SM_PREF ":pr/"SM_PREF ":lf</parent-reference>\n"));
2246 ly_ctx_set_ext_data_clb(UTEST_LYCTX, getter, data);
2247 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
2248 TEST_LOCAL_PRINT(mod, 72);
2249 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
2250 assert_string_equal(printed, expect);
2251 ly_out_reset(UTEST_OUT);
2252
aPiecek85cda2a2022-10-13 13:47:05 +02002253 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
ekinzie0ab8b302022-10-10 03:03:57 -04002254 TEST_LOCAL_TEARDOWN;
2255}
2256
aPiecek03cb4872022-10-24 10:31:51 +02002257static void
2258structure(void **state)
2259{
2260 TEST_LOCAL_SETUP;
2261
2262 orig =
2263 "module example-module {\n"
2264 " yang-version 1.1;\n"
2265 " namespace \"urn:example:example-module\";\n"
2266 " prefix exm;\n"
2267 "\n"
2268 " import ietf-yang-structure-ext {\n"
2269 " prefix sx;\n"
2270 " }\n"
2271 "\n"
2272 " sx:structure address-book {\n"
2273 " list address {\n"
2274 " key \"last first\";\n"
2275 " leaf last {\n"
2276 " type string;\n"
2277 " }\n"
2278 " leaf first {\n"
2279 " type string;\n"
2280 " }\n"
2281 " leaf street {\n"
2282 " type string;\n"
2283 " }\n"
2284 " leaf city {\n"
2285 " type string;\n"
2286 " }\n"
2287 " leaf state {\n"
2288 " type string;\n"
2289 " }\n"
2290 " }\n"
2291 " }\n"
2292 "}\n";
2293
2294 /* from RFC 8791, Appendix A.1 */
2295 expect =
2296 "module: example-module\n"
2297 "\n"
2298 " structure address-book:\n"
2299 " +-- address* [last first]\n"
2300 " +-- last string\n"
2301 " +-- first string\n"
2302 " +-- street? string\n"
2303 " +-- city? string\n"
2304 " +-- state? string\n";
2305
2306 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
2307 TEST_LOCAL_PRINT(mod, 72);
2308 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
2309 assert_string_equal(printed, expect);
2310 ly_out_reset(UTEST_OUT);
2311
2312 /* using lysc tree */
2313 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
2314 TEST_LOCAL_PRINT(mod, 72);
2315 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
2316 assert_string_equal(printed, expect);
2317 ly_out_reset(UTEST_OUT);
2318 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
2319
2320 orig =
2321 "module example-module-aug {\n"
2322 " yang-version 1.1;\n"
2323 " namespace \"urn:example:example-module-aug\";\n"
2324 " prefix exma;\n"
2325 "\n"
2326 " import ietf-yang-structure-ext {\n"
2327 " prefix sx;\n"
2328 " }\n"
2329 " import example-module {\n"
2330 " prefix exm;\n"
2331 " }\n"
2332 "\n"
2333 " sx:augment-structure \"/exm:address-book/exm:address\" {\n"
2334 " leaf county {\n"
2335 " type string;\n"
2336 " }\n"
2337 " leaf zipcode {\n"
2338 " type string;\n"
2339 " }\n"
2340 " }\n"
2341 "}\n";
2342
2343 /* from RFC 8791, Appendix A.2 */
2344 expect =
2345 "module: example-module-aug\n"
2346 "\n"
2347 " augment-structure /exm:address-book/exm:address:\n"
2348 " +-- county? string\n"
2349 " +-- zipcode? string\n";
2350
2351 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
2352 TEST_LOCAL_PRINT(mod, 72);
2353 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
2354 assert_string_equal(printed, expect);
2355 ly_out_reset(UTEST_OUT);
2356
2357 /* using lysc tree */
2358 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
2359 TEST_LOCAL_PRINT(mod, 72);
2360 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
2361 assert_string_equal(printed, expect);
2362 ly_out_reset(UTEST_OUT);
2363 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
2364
2365 TEST_LOCAL_TEARDOWN;
2366}
2367
aPiecekba674622023-03-28 10:57:00 +02002368static void
2369annotation(void **state)
2370{
2371 TEST_LOCAL_SETUP;
2372
2373 orig =
2374 "module ann {\n"
2375 " yang-version 1.1;\n"
2376 " namespace \"urn:example:ann\";\n"
2377 " prefix an;\n"
2378 "\n"
2379 " import ietf-yang-metadata {\n"
2380 " prefix md;\n"
2381 " }\n"
2382 "\n"
2383 " leaf lf1 {\n"
2384 " type string;\n"
2385 " }\n"
2386 " md:annotation avalue {\n"
2387 " type string;\n"
2388 " }\n"
2389 "}\n";
2390
2391 expect =
2392 "module: ann\n"
2393 " +--rw lf1? string\n";
2394
2395 /* annotation is ignored without error message */
2396 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
2397 TEST_LOCAL_PRINT(mod, 72);
2398 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
2399 assert_string_equal(printed, expect);
2400 ly_out_reset(UTEST_OUT);
2401
2402 TEST_LOCAL_TEARDOWN;
2403}
2404
aPiecek20ddf8b2021-01-08 11:50:37 +01002405int
2406main(void)
2407{
2408 const struct CMUnitTest tests[] = {
2409 UTEST(base_sections),
2410 UTEST(node_status),
2411 UTEST(node_config_flags),
2412 UTEST(node_rpcs_flags),
2413 UTEST(node_grouping_flags),
2414 UTEST(notif_inside_container),
2415 UTEST(node_choice),
2416 UTEST(node_case),
2417 UTEST(optional_opts),
2418 UTEST(presence_container),
2419 UTEST(node_keys),
2420 UTEST(node_type_target),
2421 UTEST(node_type_leafref),
2422 UTEST(node_iffeatures),
2423 UTEST(indent_wrapper),
2424 UTEST(line_length_twiddling),
2425 UTEST(break_before_leafref),
2426 UTEST(break_before_leafref_and_iffeature),
2427 UTEST(basic_unified_indent_before_type),
2428 UTEST(twiddling_unified_indent_before_type),
2429 UTEST(inheritance_of_config_flag),
2430 UTEST(inheritance_of_status_flag),
2431 UTEST(key_leaf_is_always_mandatory_true),
2432 UTEST(transition_between_rpc_and_notif),
aPiecek9e505922021-04-19 13:45:08 +02002433 UTEST(local_augment),
aPiecek7745b282021-04-20 13:54:52 +02002434 UTEST(print_compiled_node),
aPiecekaecf7512021-04-21 08:34:35 +02002435 UTEST(print_parsed_submodule),
aPiecekb8e43f12021-04-23 12:52:22 +02002436 UTEST(yang_data),
aPiecek03cb4872022-10-24 10:31:51 +02002437 UTEST(mount_point),
2438 UTEST(structure),
aPiecekba674622023-03-28 10:57:00 +02002439 UTEST(annotation),
aPiecek20ddf8b2021-01-08 11:50:37 +01002440 };
2441
2442 return cmocka_run_group_tests(tests, NULL, NULL);
2443}