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