blob: 00a8eef8c3ccd3b08b2f4ce1a1902734c9ee23c7 [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"
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
aPiecekaecf7512021-04-21 08:34:35 +02001671static LY_ERR
1672local_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
1673 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
1674 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
1675{
1676 *module_data = user_data;
1677 *format = LYS_IN_YANG;
1678 *free_module_data = NULL;
1679 return LY_SUCCESS;
1680}
1681
1682static void
1683print_parsed_submodule(void **state)
1684{
1685 TEST_LOCAL_SETUP;
1686
1687 orig = "module a27 {\n"
1688 " yang-version 1.1;\n"
1689 " namespace \"x:y\";\n"
1690 " prefix x;\n"
1691 "\n"
1692 " include \"a27-sub\";\n"
1693 "}\n";
1694
1695 char *submodule =
1696 "submodule a27-sub {\n"
1697 " yang-version 1.1;\n"
1698 " belongs-to a27 {\n"
1699 " prefix x;\n"
1700 " }\n"
1701 "\n"
1702 " container h {\n"
1703 " leaf b {\n"
1704 " type string;\n"
1705 " }\n"
1706 " }\n"
1707 "}\n";
1708
1709 /* edited pyang output */
1710 expect =
1711 "submodule: a27-sub\n"
1712 " +--rw h\n"
1713 " +--rw b? string\n";
1714
1715 ly_ctx_set_module_imp_clb(UTEST_LYCTX, local_imp_clb, submodule);
1716 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1717 assert_int_equal(LY_SUCCESS, lys_print_submodule(UTEST_OUT, mod->parsed->includes[0].submodule, LYS_OUT_TREE, 72, 0));
1718 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1719 assert_string_equal(printed, expect);
1720
1721 TEST_LOCAL_TEARDOWN;
1722}
1723
aPiecekb8e43f12021-04-23 12:52:22 +02001724static void
1725yang_data(void **state)
1726{
1727 TEST_LOCAL_SETUP;
1728
1729 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
1730 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-restconf", "2017-01-26", NULL));
1731
1732 orig =
1733 "module a28 {\n"
1734 " yang-version 1.1;\n"
1735 " namespace \"x:y\";\n"
1736 " prefix x;\n"
1737 "\n"
1738 " import ietf-restconf {\n"
1739 " prefix rc;\n"
1740 " revision-date 2017-01-26;\n"
1741 " }\n"
1742 "\n"
1743 " rc:yang-data \"tmp1\" {\n"
1744 " container cont1 {\n"
1745 " leaf lf {\n"
1746 " type string;\n"
1747 " }\n"
1748 " list l2 {\n"
1749 " key\n"
1750 " \"a b\";\n"
1751 " leaf a {\n"
1752 " type string;\n"
1753 " }\n"
1754 " leaf b {\n"
1755 " type string;\n"
1756 " }\n"
1757 " }\n"
1758 " }\n"
1759 " }\n"
1760 " rc:yang-data \"tmp2\" {\n"
1761 " container con2 {\n"
1762 " leaf lf {\n"
1763 " type string;\n"
1764 " }\n"
1765 " }\n"
1766 " }\n"
1767 " rc:yang-data \"tmp3\" {\n"
1768 " uses g1;\n"
1769 " uses g2;\n"
1770 " }\n"
1771 " rc:yang-data \"tmp4\" {\n"
1772 " choice x {\n"
1773 " case a {\n"
1774 " container z;\n"
1775 " }\n"
1776 " case b {\n"
1777 " container y;\n"
1778 " }\n"
1779 " }\n"
1780 " }\n"
1781 "\n"
1782 " grouping g1 {\n"
1783 " description\n"
1784 " \"Some Text\";\n"
1785 " }\n"
1786 "\n"
1787 " grouping g2 {\n"
1788 " container cont3;\n"
1789 " }\n"
1790 " container mont;\n"
1791 "}\n";
1792
aPiecekb352de72021-05-21 09:04:04 +02001793 /* from pyang (--tree-print-yang-data --tree-print-groupings -p "...")
1794 * but with these adjustments:
aPiecekb8e43f12021-04-23 12:52:22 +02001795 * - <flags> is always '--' for yang-data nodes
1796 * - yang-data tmp3 has two 'uses' nodes
1797 * - grouping g2 has ':' character at the end
1798 */
1799 expect =
1800 "module: a28\n"
1801 " +--rw mont\n"
1802 "\n"
1803 " grouping g1\n"
1804 " grouping g2:\n"
1805 " +---- cont3\n"
1806 "\n"
1807 " yang-data tmp1:\n"
1808 " +---- cont1\n"
1809 " +---- lf? string\n"
1810 " +---- l2* [a b]\n"
1811 " +---- a string\n"
1812 " +---- b string\n"
1813 " yang-data tmp2:\n"
1814 " +---- con2\n"
1815 " +---- lf? string\n"
1816 " yang-data tmp3:\n"
1817 " +---u g1\n"
1818 " +---u g2\n"
1819 " yang-data tmp4:\n"
1820 " +---- (x)?\n"
1821 " +--:(a)\n"
1822 " | +---- z\n"
1823 " +--:(b)\n"
1824 " +---- y\n";
1825
1826 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1827 TEST_LOCAL_PRINT(mod, 72);
1828 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1829 assert_string_equal(printed, expect);
1830
1831 ly_out_reset(UTEST_OUT);
1832
aPiecekb352de72021-05-21 09:04:04 +02001833 /* from pyang (--tree-print-yang-data -p "...")
1834 * but with these adjustments:
aPiecekb8e43f12021-04-23 12:52:22 +02001835 * <flags> is always '--' for yang-data nodes
1836 */
1837 expect =
1838 "module: a28\n"
1839 " +--rw mont\n"
1840 "\n"
1841 " yang-data tmp1:\n"
1842 " +---- cont1\n"
1843 " +---- lf? string\n"
1844 " +---- l2* [a b]\n"
1845 " +---- a string\n"
1846 " +---- b string\n"
1847 " yang-data tmp2:\n"
1848 " +---- con2\n"
1849 " +---- lf? string\n"
1850 " yang-data tmp3:\n"
1851 " +---- cont3\n"
1852 " yang-data tmp4:\n"
1853 " +---- (x)?\n"
1854 " +--:(a)\n"
1855 " | +---- z\n"
1856 " +--:(b)\n"
1857 " +---- y\n";
1858
1859 /* using lysc tree */
1860 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1861 TEST_LOCAL_PRINT(mod, 72);
1862 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1863 assert_string_equal(printed, expect);
1864 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
1865
1866 TEST_LOCAL_TEARDOWN;
1867}
1868
aPiecek85cda2a2022-10-13 13:47:05 +02001869static LY_ERR
1870getter(const struct lysc_ext_instance *ext, void *user_data, void **ext_data, ly_bool *ext_data_free)
1871{
1872 struct ly_ctx *ctx;
1873 struct lyd_node *data = NULL;
1874
1875 ctx = ext->module->ctx;
1876 if (user_data) {
1877 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, user_data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &data));
1878 }
1879
1880 *ext_data = data;
1881 *ext_data_free = 1;
1882 return LY_SUCCESS;
1883}
1884
1885#define SM_MODNAME_EXT "sm-extension"
1886#define SM_MOD_EXT_NAMESPACE "urn:sm-ext"
1887#define SM_PREF "sm"
1888#define SCHEMA_REF_INLINE "<inline></inline>"
1889#define SCHEMA_REF_SHARED(REF) "<shared-schema>"REF"</shared-schema>"
1890
1891#define EXT_DATA(MPMOD_NAME, MODULES, SCHEMA_REF) \
1892 "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\"\n" \
1893 " xmlns:ds=\"urn:ietf:params:xml:ns:yang:ietf-datastores\">\n" \
1894 "<module-set>\n" \
1895 " <name>test-set</name>\n" \
1896 " <module>\n" \
1897 " <name>"SM_MODNAME_EXT"</name>\n" \
1898 " <namespace>"SM_MOD_EXT_NAMESPACE"</namespace>\n" \
1899 " </module>\n" \
1900 MODULES \
1901 "</module-set>\n" \
1902 "<content-id>1</content-id>\n" \
1903 "</yang-library>\n" \
1904 "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n" \
1905 "<module-set-id>1</module-set-id>\n" \
1906 "</modules-state>\n" \
1907 "<schema-mounts xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-schema-mount\">\n" \
1908 "<namespace>\n" \
1909 " <prefix>"SM_PREF"</prefix>\n" \
1910 " <uri>x:"MPMOD_NAME"</uri>\n" \
1911 "</namespace>\n" \
1912 "<mount-point>\n" \
1913 " <module>"MPMOD_NAME"</module>\n" \
1914 " <label>mnt-root</label>\n" \
1915 SCHEMA_REF \
1916 "</mount-point>\n" \
1917 "</schema-mounts>"
1918
1919#define SM_MOD_MAIN(NAME, BODY) \
1920 "module "NAME" {\n" \
1921 " yang-version 1.1;\n" \
1922 " namespace \"x:"NAME"\";\n" \
1923 " prefix \"x\";\n" \
1924 " import ietf-yang-schema-mount {\n" \
1925 " prefix yangmnt;\n" \
1926 " }\n" \
1927 BODY \
1928 "}"
1929
ekinzie0ab8b302022-10-10 03:03:57 -04001930static void
1931mount_point(void **state)
1932{
aPiecek85cda2a2022-10-13 13:47:05 +02001933 char *data;
1934
ekinzie0ab8b302022-10-10 03:03:57 -04001935 TEST_LOCAL_SETUP;
aPiecek85cda2a2022-10-13 13:47:05 +02001936 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
ekinzie0ab8b302022-10-10 03:03:57 -04001937
aPiecek85cda2a2022-10-13 13:47:05 +02001938 /* interested in sm-extension.yang and sm-mod.yang */
1939 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
ekinzie0ab8b302022-10-10 03:03:57 -04001940
aPiecek85cda2a2022-10-13 13:47:05 +02001941 /*
1942 * 'mp' flag for list and container
1943 */
1944 orig = SM_MOD_MAIN("a29",
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 "container cont {\n"
1953 " yangmnt:mount-point \"mnt-root\";\n"
1954 "}\n");
ekinzie0ab8b302022-10-10 03:03:57 -04001955 expect =
Michal Vasko8bb6e7e2022-10-10 09:05:14 +02001956 "module: a29\n"
aPiecek85cda2a2022-10-13 13:47:05 +02001957 " +--mp lt* [name]\n"
Michal Vasko8bb6e7e2022-10-10 09:05:14 +02001958 " | +--rw name string\n"
aPiecek85cda2a2022-10-13 13:47:05 +02001959 " +--mp cont\n";
ekinzie0ab8b302022-10-10 03:03:57 -04001960 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1961 TEST_LOCAL_PRINT(mod, 72);
1962 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1963 assert_string_equal(printed, expect);
ekinzie0ab8b302022-10-10 03:03:57 -04001964 ly_out_reset(UTEST_OUT);
1965
aPiecek85cda2a2022-10-13 13:47:05 +02001966 /*
1967 * mount schema by 'inline' schema-ref
1968 */
1969 orig = SM_MOD_MAIN("a30",
1970 "list lt {\n"
1971 " key \"name\";\n"
1972 " leaf name {\n"
1973 " type string;\n"
1974 " }\n"
1975 " yangmnt:mount-point \"mnt-root\";\n"
1976 "}\n");
1977 expect =
1978 "module: a30\n"
1979 " +--mp lt* [name]\n"
aPiecek03cb4872022-10-24 10:31:51 +02001980 " +--rw tlist/* [name]\n"
aPiecek85cda2a2022-10-13 13:47:05 +02001981 " | +--rw name uint32\n"
1982 " +--rw tcont/\n"
1983 " | +--rw tleaf? uint32\n"
1984 " +--rw name string\n";
1985 data = EXT_DATA("a30", "", SCHEMA_REF_INLINE);
1986 ly_ctx_set_ext_data_clb(UTEST_LYCTX, getter, data);
1987 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
ekinzie0ab8b302022-10-10 03:03:57 -04001988 TEST_LOCAL_PRINT(mod, 72);
1989 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
1990 assert_string_equal(printed, expect);
aPiecek85cda2a2022-10-13 13:47:05 +02001991 ly_out_reset(UTEST_OUT);
ekinzie0ab8b302022-10-10 03:03:57 -04001992
aPiecek85cda2a2022-10-13 13:47:05 +02001993 /*
1994 * mount schema into empty container
1995 */
1996 orig = SM_MOD_MAIN("a31",
1997 "container cont {\n"
1998 " yangmnt:mount-point \"mnt-root\";\n"
1999 "}\n"
2000 "leaf lf {\n"
2001 " type string;\n"
2002 "}\n");
2003 expect =
2004 "module: a31\n"
2005 " +--mp cont\n"
aPiecek03cb4872022-10-24 10:31:51 +02002006 " | +--rw tlist/* [name]\n"
aPiecek85cda2a2022-10-13 13:47:05 +02002007 " | | +--rw name uint32\n"
2008 " | +--rw tcont/\n"
2009 " | +--rw tleaf? uint32\n"
2010 " +--rw lf? string\n";
2011 data = EXT_DATA("a31", "", SCHEMA_REF_INLINE);
2012 ly_ctx_set_ext_data_clb(UTEST_LYCTX, getter, data);
2013 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);
2017 ly_out_reset(UTEST_OUT);
2018
2019 /*
2020 * mount schema into non-empty container
2021 */
2022 orig = SM_MOD_MAIN("a32",
2023 "container cont {\n"
2024 " leaf lf1 {\n"
2025 " type string;\n"
2026 " }\n"
2027 " yangmnt:mount-point \"mnt-root\";\n"
2028 " leaf lf2 {\n"
2029 " type string;\n"
2030 " }\n"
2031 "}\n");
2032 expect =
2033 "module: a32\n"
2034 " +--mp cont\n"
aPiecek03cb4872022-10-24 10:31:51 +02002035 " +--rw tlist/* [name]\n"
aPiecek85cda2a2022-10-13 13:47:05 +02002036 " | +--rw name uint32\n"
2037 " +--rw tcont/\n"
2038 " | +--rw tleaf? uint32\n"
2039 " +--rw lf1? string\n"
2040 " +--rw lf2? string\n";
2041 data = EXT_DATA("a32", "", SCHEMA_REF_INLINE);
2042 ly_ctx_set_ext_data_clb(UTEST_LYCTX, getter, data);
2043 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
2044 TEST_LOCAL_PRINT(mod, 72);
2045 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
2046 assert_string_equal(printed, expect);
2047 ly_out_reset(UTEST_OUT);
2048
2049 /*
2050 * mounting with parent-reference
2051 */
2052 orig = SM_MOD_MAIN("a33",
2053 "list pr {\n"
2054 " key \"name\";\n"
2055 " leaf name {\n"
2056 " type string;\n"
2057 " }\n"
2058 " leaf prlf {\n"
2059 " type string;\n"
2060 " }\n"
2061 "}\n"
2062 "leaf lf {\n"
2063 " type string;\n"
2064 "}\n"
2065 "container cont {\n"
2066 " yangmnt:mount-point \"mnt-root\";\n"
2067 " list lt {\n"
2068 " key \"name\";\n"
2069 " leaf name {\n"
2070 " type string;\n"
2071 " }\n"
2072 " }\n"
2073 "}\n");
2074 expect =
2075 "module: a33\n"
2076 " +--rw pr* [name]\n"
2077 " | +--rw name string\n"
2078 " | +--rw prlf? string\n"
2079 " +--rw lf? string\n"
2080 " +--mp cont\n"
aPiecek03cb4872022-10-24 10:31:51 +02002081 " +--rw tlist/* [name]\n"
aPiecek85cda2a2022-10-13 13:47:05 +02002082 " | +--rw name uint32\n"
2083 " +--rw tcont/\n"
2084 " | +--rw tleaf? uint32\n"
aPiecek03cb4872022-10-24 10:31:51 +02002085 " +--rw pr@* [name]\n"
aPiecek85cda2a2022-10-13 13:47:05 +02002086 " | +--rw prlf? string\n"
aPiecek03cb4872022-10-24 10:31:51 +02002087 " +--rw lf@? string\n"
aPiecek85cda2a2022-10-13 13:47:05 +02002088 " +--rw lt* [name]\n"
2089 " +--rw name string\n";
2090 data = EXT_DATA("a33", "", SCHEMA_REF_SHARED(
2091 "<parent-reference>/"SM_PREF ":pr/"SM_PREF ":prlf</parent-reference>\n"
2092 "<parent-reference>/"SM_PREF ":lf</parent-reference>\n"));
2093 ly_ctx_set_ext_data_clb(UTEST_LYCTX, getter, data);
2094 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
2095 TEST_LOCAL_PRINT(mod, 72);
2096 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
2097 assert_string_equal(printed, expect);
2098 ly_out_reset(UTEST_OUT);
2099
2100 /*
2101 * mounting with parent-reference into empty container
2102 */
2103 orig = SM_MOD_MAIN("a34",
2104 "container cont {\n"
2105 " yangmnt:mount-point \"mnt-root\";\n"
2106 "}\n"
2107 "leaf lf {\n"
2108 " type string;\n"
2109 "}\n");
2110 expect =
2111 "module: a34\n"
2112 " +--mp cont\n"
aPiecek03cb4872022-10-24 10:31:51 +02002113 " | +--rw tlist/* [name]\n"
aPiecek85cda2a2022-10-13 13:47:05 +02002114 " | | +--rw name uint32\n"
2115 " | +--rw tcont/\n"
2116 " | | +--rw tleaf? uint32\n"
aPiecek03cb4872022-10-24 10:31:51 +02002117 " | +--rw lf@? string\n"
aPiecek85cda2a2022-10-13 13:47:05 +02002118 " +--rw lf? string\n";
2119 data = EXT_DATA("a34", "",
2120 SCHEMA_REF_SHARED(
2121 "<parent-reference>/"SM_PREF ":lf</parent-reference>\n"));
2122 ly_ctx_set_ext_data_clb(UTEST_LYCTX, getter, data);
2123 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
2124 TEST_LOCAL_PRINT(mod, 72);
2125 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
2126 assert_string_equal(printed, expect);
2127 ly_out_reset(UTEST_OUT);
2128
2129 /*
2130 * mounting module which is only parsed
2131 */
2132 orig = SM_MOD_MAIN("a35",
2133 "import sm-mod {\n"
2134 " prefix smm;\n"
2135 "}\n"
2136 "container pr {\n"
2137 " leaf prlf {\n"
2138 " type uint32;\n"
2139 " }\n"
2140 "}\n"
2141 "list lt {\n"
2142 " key \"name\";\n"
2143 " yangmnt:mount-point \"mnt-root\";\n"
2144 " leaf name {\n"
2145 " type string;\n"
2146 " }\n"
2147 "}\n");
2148 expect =
2149 "module: a35\n"
2150 " +--rw pr\n"
2151 " | +--rw prlf? uint32\n"
2152 " +--mp lt* [name]\n"
aPiecek03cb4872022-10-24 10:31:51 +02002153 " +--rw tlist/* [name]\n"
aPiecek85cda2a2022-10-13 13:47:05 +02002154 " | +--rw name uint32\n"
2155 " +--rw tcont/\n"
2156 " | +--rw tleaf? uint32\n"
2157 " +--mp ncmp/\n"
2158 " +--rw not-compiled/\n"
2159 " | +--rw first? string\n"
2160 " | +--rw second? string\n"
2161 " +--rw pr@\n"
2162 " | +--rw prlf? uint32\n"
2163 " +--rw name string\n";
2164 data = EXT_DATA("a35",
2165 "<module>\n"
2166 " <name>sm-mod</name>\n"
2167 " <namespace>urn:sm-mod</namespace>\n"
2168 "</module>\n",
2169 SCHEMA_REF_SHARED(
2170 "<parent-reference>/"SM_PREF ":pr/"SM_PREF ":prlf</parent-reference>\n"));
2171 ly_ctx_set_ext_data_clb(UTEST_LYCTX, getter, data);
2172 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
2173 TEST_LOCAL_PRINT(mod, 72);
2174 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
2175 assert_string_equal(printed, expect);
2176 ly_out_reset(UTEST_OUT);
2177
aPiecek36f82232022-10-14 10:08:38 +02002178 /*
2179 * notifications and rpcs in mounted module
2180 */
2181 orig = SM_MOD_MAIN("a36",
2182 "container cont {\n"
2183 " yangmnt:mount-point \"mnt-root\";\n"
2184 "}\n");
2185 expect =
2186 "module: a36\n"
2187 " +--mp cont\n"
aPiecek03cb4872022-10-24 10:31:51 +02002188 " +--rw tlist/* [name]\n"
aPiecek36f82232022-10-14 10:08:38 +02002189 " | +--rw name uint32\n"
2190 " +--rw tcont/\n"
2191 " | +--rw tleaf? uint32\n"
2192 " +--rw cont/\n"
2193 " | +---x cr\n"
2194 " | | +---w input\n"
2195 " | | | +---w in? string\n"
2196 " | | +--ro output\n"
2197 " | | +--ro out? string\n"
2198 " | +---n cn\n"
2199 " +---x r1/\n"
2200 " +---x r2/\n"
2201 " +---n n1/\n"
2202 " +---n n2/\n";
2203 data = EXT_DATA("a36",
2204 "<module>\n"
2205 " <name>sm-rpcnotif</name>\n"
2206 " <namespace>urn:rpcnotif</namespace>\n"
2207 "</module>\n",
2208 SCHEMA_REF_INLINE);
2209 ly_ctx_set_ext_data_clb(UTEST_LYCTX, getter, data);
2210 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
2211 TEST_LOCAL_PRINT(mod, 72);
2212 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
2213 assert_string_equal(printed, expect);
2214 ly_out_reset(UTEST_OUT);
2215
aPiecek8f1073c2022-10-17 16:44:49 +02002216 /*
2217 * parent-ref composes the '@' subtree
2218 */
2219 orig = SM_MOD_MAIN("a37",
2220 "container pr {\n"
2221 " leaf ignored_node {\n"
2222 " type string;\n"
2223 " }\n"
2224 " container cont {\n"
2225 " leaf ignored_lf {\n"
2226 " type uint32;\n"
2227 " }\n"
2228 " }\n"
2229 " container ignored_subtree {\n"
2230 " leaf ignored_lf {\n"
2231 " type uint32;\n"
2232 " }\n"
2233 " }\n"
2234 " container cont_sibl {\n"
2235 " leaf slf {\n"
2236 " type string;\n"
2237 " }\n"
2238 " }\n"
2239 " leaf lf {\n"
2240 " type uint32;\n"
2241 " }\n"
2242 "}\n"
2243 "container cont_mount {\n"
2244 " yangmnt:mount-point \"mnt-root\";\n"
2245 "}\n");
2246 expect =
2247 "module: a37\n"
2248 " +--rw pr\n"
2249 " | +--rw ignored_node? string\n"
2250 " | +--rw cont\n"
2251 " | | +--rw ignored_lf? uint32\n"
2252 " | +--rw ignored_subtree\n"
2253 " | | +--rw ignored_lf? uint32\n"
2254 " | +--rw cont_sibl\n"
2255 " | | +--rw slf? string\n"
2256 " | +--rw lf? uint32\n"
2257 " +--mp cont_mount\n"
aPiecek03cb4872022-10-24 10:31:51 +02002258 " +--rw tlist/* [name]\n"
aPiecek8f1073c2022-10-17 16:44:49 +02002259 " | +--rw name uint32\n"
2260 " +--rw tcont/\n"
2261 " | +--rw tleaf? uint32\n"
2262 " +--rw pr@\n"
2263 " +--rw cont\n"
2264 " +--rw cont_sibl\n"
2265 " | +--rw slf? string\n"
2266 " +--rw lf? uint32\n";
2267 data = EXT_DATA("a37", "", SCHEMA_REF_SHARED(
2268 "<parent-reference>/"SM_PREF ":pr/"SM_PREF ":cont_sibl/slf</parent-reference>\n"
2269 "<parent-reference>/"SM_PREF ":pr/"SM_PREF ":cont</parent-reference>\n"
2270 "<parent-reference>/"SM_PREF ":pr/"SM_PREF ":lf</parent-reference>\n"));
2271 ly_ctx_set_ext_data_clb(UTEST_LYCTX, getter, data);
2272 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
2273 TEST_LOCAL_PRINT(mod, 72);
2274 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
2275 assert_string_equal(printed, expect);
2276 ly_out_reset(UTEST_OUT);
2277
aPiecek85cda2a2022-10-13 13:47:05 +02002278 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
ekinzie0ab8b302022-10-10 03:03:57 -04002279 TEST_LOCAL_TEARDOWN;
2280}
2281
aPiecek03cb4872022-10-24 10:31:51 +02002282static void
2283structure(void **state)
2284{
2285 TEST_LOCAL_SETUP;
2286
2287 orig =
2288 "module example-module {\n"
2289 " yang-version 1.1;\n"
2290 " namespace \"urn:example:example-module\";\n"
2291 " prefix exm;\n"
2292 "\n"
2293 " import ietf-yang-structure-ext {\n"
2294 " prefix sx;\n"
2295 " }\n"
2296 "\n"
2297 " sx:structure address-book {\n"
2298 " list address {\n"
2299 " key \"last first\";\n"
2300 " leaf last {\n"
2301 " type string;\n"
2302 " }\n"
2303 " leaf first {\n"
2304 " type string;\n"
2305 " }\n"
2306 " leaf street {\n"
2307 " type string;\n"
2308 " }\n"
2309 " leaf city {\n"
2310 " type string;\n"
2311 " }\n"
2312 " leaf state {\n"
2313 " type string;\n"
2314 " }\n"
2315 " }\n"
2316 " }\n"
2317 "}\n";
2318
2319 /* from RFC 8791, Appendix A.1 */
2320 expect =
2321 "module: example-module\n"
2322 "\n"
2323 " structure address-book:\n"
2324 " +-- address* [last first]\n"
2325 " +-- last string\n"
2326 " +-- first string\n"
2327 " +-- street? string\n"
2328 " +-- city? string\n"
2329 " +-- state? string\n";
2330
2331 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
2332 TEST_LOCAL_PRINT(mod, 72);
2333 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
2334 assert_string_equal(printed, expect);
2335 ly_out_reset(UTEST_OUT);
2336
2337 /* using lysc tree */
2338 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
2339 TEST_LOCAL_PRINT(mod, 72);
2340 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
2341 assert_string_equal(printed, expect);
2342 ly_out_reset(UTEST_OUT);
2343 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
2344
2345 orig =
2346 "module example-module-aug {\n"
2347 " yang-version 1.1;\n"
2348 " namespace \"urn:example:example-module-aug\";\n"
2349 " prefix exma;\n"
2350 "\n"
2351 " import ietf-yang-structure-ext {\n"
2352 " prefix sx;\n"
2353 " }\n"
2354 " import example-module {\n"
2355 " prefix exm;\n"
2356 " }\n"
2357 "\n"
2358 " sx:augment-structure \"/exm:address-book/exm:address\" {\n"
2359 " leaf county {\n"
2360 " type string;\n"
2361 " }\n"
2362 " leaf zipcode {\n"
2363 " type string;\n"
2364 " }\n"
2365 " }\n"
2366 "}\n";
2367
2368 /* from RFC 8791, Appendix A.2 */
2369 expect =
2370 "module: example-module-aug\n"
2371 "\n"
2372 " augment-structure /exm:address-book/exm:address:\n"
2373 " +-- county? string\n"
2374 " +-- zipcode? string\n";
2375
2376 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
2377 TEST_LOCAL_PRINT(mod, 72);
2378 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
2379 assert_string_equal(printed, expect);
2380 ly_out_reset(UTEST_OUT);
2381
2382 /* using lysc tree */
2383 ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
2384 TEST_LOCAL_PRINT(mod, 72);
2385 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
2386 assert_string_equal(printed, expect);
2387 ly_out_reset(UTEST_OUT);
2388 ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED);
2389
2390 TEST_LOCAL_TEARDOWN;
2391}
2392
aPiecekba674622023-03-28 10:57:00 +02002393static void
2394annotation(void **state)
2395{
2396 TEST_LOCAL_SETUP;
2397
2398 orig =
2399 "module ann {\n"
2400 " yang-version 1.1;\n"
2401 " namespace \"urn:example:ann\";\n"
2402 " prefix an;\n"
2403 "\n"
2404 " import ietf-yang-metadata {\n"
2405 " prefix md;\n"
2406 " }\n"
2407 "\n"
2408 " leaf lf1 {\n"
2409 " type string;\n"
2410 " }\n"
2411 " md:annotation avalue {\n"
2412 " type string;\n"
2413 " }\n"
2414 "}\n";
2415
2416 expect =
2417 "module: ann\n"
2418 " +--rw lf1? string\n";
2419
2420 /* annotation is ignored without error message */
2421 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
2422 TEST_LOCAL_PRINT(mod, 72);
2423 assert_int_equal(strlen(expect), ly_out_printed(UTEST_OUT));
2424 assert_string_equal(printed, expect);
2425 ly_out_reset(UTEST_OUT);
2426
2427 TEST_LOCAL_TEARDOWN;
2428}
2429
aPiecek20ddf8b2021-01-08 11:50:37 +01002430int
2431main(void)
2432{
2433 const struct CMUnitTest tests[] = {
2434 UTEST(base_sections),
2435 UTEST(node_status),
2436 UTEST(node_config_flags),
2437 UTEST(node_rpcs_flags),
2438 UTEST(node_grouping_flags),
2439 UTEST(notif_inside_container),
2440 UTEST(node_choice),
2441 UTEST(node_case),
2442 UTEST(optional_opts),
2443 UTEST(presence_container),
2444 UTEST(node_keys),
2445 UTEST(node_type_target),
2446 UTEST(node_type_leafref),
2447 UTEST(node_iffeatures),
2448 UTEST(indent_wrapper),
2449 UTEST(line_length_twiddling),
2450 UTEST(break_before_leafref),
2451 UTEST(break_before_leafref_and_iffeature),
2452 UTEST(basic_unified_indent_before_type),
2453 UTEST(twiddling_unified_indent_before_type),
2454 UTEST(inheritance_of_config_flag),
2455 UTEST(inheritance_of_status_flag),
2456 UTEST(key_leaf_is_always_mandatory_true),
2457 UTEST(transition_between_rpc_and_notif),
aPiecek9e505922021-04-19 13:45:08 +02002458 UTEST(local_augment),
aPiecek7745b282021-04-20 13:54:52 +02002459 UTEST(print_compiled_node),
aPiecekaecf7512021-04-21 08:34:35 +02002460 UTEST(print_parsed_submodule),
aPiecekb8e43f12021-04-23 12:52:22 +02002461 UTEST(yang_data),
aPiecek03cb4872022-10-24 10:31:51 +02002462 UTEST(mount_point),
2463 UTEST(structure),
aPiecekba674622023-03-28 10:57:00 +02002464 UTEST(annotation),
aPiecek20ddf8b2021-01-08 11:50:37 +01002465 };
2466
2467 return cmocka_run_group_tests(tests, NULL, NULL);
2468}