| /* |
| * @file test_printer_tree.c |
| * @author: Adam Piecek <piecek@cesnet.cz> |
| * @brief unit tests for functions from printer_tree.c |
| * |
| * Copyright (c) 2019-2021 CESNET, z.s.p.o. |
| * |
| * This source code is licensed under BSD 3-Clause License (the "License"). |
| * You may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * https://opensource.org/licenses/BSD-3-Clause |
| */ |
| #define _UTEST_MAIN_ |
| #include "utests.h" |
| |
| #include "common.h" |
| #include "context.h" |
| #include "out.h" |
| #include "printer_schema.h" |
| #include "tree_schema.h" |
| |
| #define TEST_LOCAL_SETUP \ |
| char *printed; \ |
| struct ly_out *out; \ |
| const struct lys_module *mod; \ |
| const char *orig; \ |
| const char *expect; |
| |
| #define TEST_LOCAL_CALL_PRINT(LINE_LENGTH) \ |
| assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_TREE, LINE_LENGTH, 0)); |
| |
| #define TEST_LOCAL_PRECHECK(LINE_LENGTH) \ |
| assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out)); \ |
| UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod); \ |
| TEST_LOCAL_CALL_PRINT(LINE_LENGTH) |
| |
| #define TEST_LOCAL_TEARDOWN \ |
| ly_out_free(out, NULL, 1); |
| |
| static void |
| base_sections(void **state) |
| { |
| TEST_LOCAL_SETUP; |
| orig = |
| "module a01 {\n" |
| " yang-version 1.1;\n" |
| " namespace \"x:y\";\n" |
| " prefix x;\n" |
| "\n" |
| " grouping g1;\n" |
| "\n" |
| " grouping g2;\n" |
| " container c;\n" |
| " container d;\n" |
| " augment \"/x:c\" {\n" |
| " }\n" |
| " augment \"/x:d\" {\n" |
| " }\n" |
| " rpc rpc1;\n" |
| " rpc rpc2;\n" |
| " notification n1;\n" |
| " notification n2;\n" |
| "}\n"; |
| |
| /* from yanglint 1 */ |
| expect = |
| "module: a01\n" |
| " +--rw c\n" |
| " +--rw d\n" |
| "\n" |
| " rpcs:\n" |
| " +---x rpc1\n" |
| " +---x rpc2\n" |
| "\n" |
| " notifications:\n" |
| " +---n n1\n" |
| " +---n n2\n"; |
| |
| TEST_LOCAL_PRECHECK(72); |
| assert_int_equal(strlen(expect), ly_out_printed(out)); |
| assert_string_equal(printed, expect); |
| TEST_LOCAL_TEARDOWN; |
| } |
| |
| static void |
| node_status(void **state) |
| { |
| TEST_LOCAL_SETUP; |
| orig = |
| "module a02 {\n" |
| " yang-version 1.1;\n" |
| " namespace \"x:y\";\n" |
| " prefix x;\n" |
| " container l {\n" |
| " status current;\n" |
| " }\n" |
| " container m {\n" |
| " status deprecated;\n" |
| " }\n" |
| " container n {\n" |
| " status obsolete;\n" |
| " }\n" |
| "}\n"; |
| |
| /* from pyang */ |
| expect = |
| "module: a02\n" |
| " +--rw l\n" |
| " x--rw m\n" |
| " o--rw n\n"; |
| |
| TEST_LOCAL_PRECHECK(72); |
| assert_int_equal(strlen(expect), ly_out_printed(out)); |
| assert_string_equal(printed, expect); |
| TEST_LOCAL_TEARDOWN; |
| } |
| |
| static void |
| node_config_flags(void **state) |
| { |
| TEST_LOCAL_SETUP; |
| orig = |
| "module a03 {\n" |
| " yang-version 1.1;\n" |
| " namespace \"x:y\";\n" |
| " prefix x;\n" |
| " container l {\n" |
| " config true;\n" |
| " }\n" |
| " container m {\n" |
| " config false;\n" |
| " }\n" |
| "}\n"; |
| |
| /* from pyang */ |
| expect = |
| "module: a03\n" |
| " +--rw l\n" |
| " +--ro m\n"; |
| |
| TEST_LOCAL_PRECHECK(72); |
| assert_int_equal(strlen(expect), ly_out_printed(out)); |
| assert_string_equal(printed, expect); |
| TEST_LOCAL_TEARDOWN; |
| } |
| |
| static void |
| node_rpcs_flags(void **state) |
| { |
| TEST_LOCAL_SETUP; |
| orig = |
| "module a04 {\n" |
| " yang-version 1.1;\n" |
| " namespace \"x:y\";\n" |
| " prefix x;\n" |
| " container cont {\n" |
| " action rpc1 {\n" |
| "\n" |
| " input {\n" |
| " leaf in {\n" |
| " type string;\n" |
| " }\n" |
| " }\n" |
| " }\n" |
| " }\n" |
| "}\n"; |
| |
| /* from pyang */ |
| expect = |
| "module: a04\n" |
| " +--rw cont\n" |
| " +---x rpc1\n" |
| " +---w input\n" |
| " +---w in? string\n"; |
| |
| TEST_LOCAL_PRECHECK(72); |
| assert_int_equal(strlen(expect), ly_out_printed(out)); |
| assert_string_equal(printed, expect); |
| TEST_LOCAL_TEARDOWN; |
| } |
| |
| static void |
| node_grouping_flags(void **state) |
| { |
| TEST_LOCAL_SETUP; |
| orig = |
| "module a05 {\n" |
| " yang-version 1.1;\n" |
| " namespace \"x:y\";\n" |
| " prefix x;\n" |
| "\n" |
| " grouping g {\n" |
| " container c;\n" |
| " }\n" |
| " container d {\n" |
| " uses g;\n" |
| " }\n" |
| "}\n"; |
| |
| /* handwritten. Neither Pyang nor Yanglint1 provided the required output. */ |
| expect = |
| "module: a05\n" |
| " +--rw d\n" |
| " +---u g\n" |
| "\n" |
| " grouping g:\n" |
| " +--rw c\n"; |
| |
| TEST_LOCAL_PRECHECK(72); |
| assert_int_equal(strlen(expect), ly_out_printed(out)); |
| assert_string_equal(printed, expect); |
| TEST_LOCAL_TEARDOWN; |
| } |
| |
| static void |
| notif_inside_container(void **state) |
| { |
| TEST_LOCAL_SETUP; |
| orig = |
| "module a06 {\n" |
| " yang-version 1.1;\n" |
| " namespace \"x:y\";\n" |
| " prefix x;\n" |
| " container c {\n" |
| " notification notif;\n" |
| " }\n" |
| "}\n"; |
| |
| /* from pyang */ |
| expect = |
| "module: a06\n" |
| " +--rw c\n" |
| " +---n notif\n"; |
| |
| TEST_LOCAL_PRECHECK(72); |
| assert_int_equal(strlen(expect), ly_out_printed(out)); |
| assert_string_equal(printed, expect); |
| TEST_LOCAL_TEARDOWN; |
| } |
| |
| static void |
| node_choice(void **state) |
| { |
| TEST_LOCAL_SETUP; |
| orig = |
| "module a07 {\n" |
| " yang-version 1.1;\n" |
| " namespace \"x:y\";\n" |
| " prefix x;\n" |
| " choice my_choice;\n" |
| "}\n"; |
| |
| /* from pyang */ |
| expect = |
| "module: a07\n" |
| " +--rw (my_choice)?\n"; |
| |
| TEST_LOCAL_PRECHECK(72); |
| assert_int_equal(strlen(expect), ly_out_printed(out)); |
| assert_string_equal(printed, expect); |
| TEST_LOCAL_TEARDOWN; |
| } |
| |
| static void |
| node_case(void **state) |
| { |
| TEST_LOCAL_SETUP; |
| orig = |
| "module a08 {\n" |
| " yang-version 1.1;\n" |
| " namespace \"x:y\";\n" |
| " prefix x;\n" |
| " choice my_choice {\n" |
| " case my_case;\n" |
| " }\n" |
| "}\n"; |
| |
| /* from pyang */ |
| expect = |
| "module: a08\n" |
| " +--rw (my_choice)?\n" |
| " +--:(my_case)\n"; |
| |
| TEST_LOCAL_PRECHECK(72); |
| assert_int_equal(strlen(expect), ly_out_printed(out)); |
| assert_string_equal(printed, expect); |
| TEST_LOCAL_TEARDOWN; |
| } |
| |
| static void |
| optional_opts(void **state) |
| { |
| TEST_LOCAL_SETUP; |
| /* throws libyang warn: Use of anydata to define configuration data is not recommended... */ |
| orig = |
| "module a09 {\n" |
| " yang-version 1.1;\n" |
| " namespace \"x:y\";\n" |
| " prefix x;\n" |
| " leaf l1 {\n" |
| " type string;\n" |
| " mandatory true;\n" |
| " }\n" |
| " leaf l2 {\n" |
| " type string;\n" |
| " mandatory false;\n" |
| " }\n" |
| " choice c1 {\n" |
| " mandatory true;\n" |
| " }\n" |
| " choice c2 {\n" |
| " mandatory false;\n" |
| " }\n" |
| " anydata a1 {\n" |
| " mandatory true;\n" |
| " }\n" |
| " anydata a2 {\n" |
| " mandatory false;\n" |
| " }\n" |
| " anyxml x1 {\n" |
| " mandatory true;\n" |
| " }\n" |
| " anyxml x2 {\n" |
| " mandatory false;\n" |
| " }\n" |
| "}\n"; |
| |
| /* from yanglint 1 */ |
| expect = |
| "module: a09\n" |
| " +--rw l1 string\n" |
| " +--rw l2? string\n" |
| " +--rw (c1)\n" |
| " +--rw (c2)?\n" |
| " +--rw a1 anydata\n" |
| " +--rw a2? anydata\n" |
| " +--rw x1 anyxml\n" |
| " +--rw x2? anyxml\n"; |
| |
| TEST_LOCAL_PRECHECK(72); |
| assert_int_equal(strlen(expect), ly_out_printed(out)); |
| assert_string_equal(printed, expect); |
| TEST_LOCAL_TEARDOWN; |
| } |
| |
| static void |
| presence_container(void **state) |
| { |
| TEST_LOCAL_SETUP; |
| orig = |
| "module a10 {\n" |
| " yang-version 1.1;\n" |
| " namespace \"x:y\";\n" |
| " prefix x;\n" |
| " container c;\n" |
| " container d {\n" |
| " presence \"str1\";\n" |
| " }\n" |
| "}\n"; |
| |
| /* from pyang */ |
| expect = |
| "module: a10\n" |
| " +--rw c\n" |
| " +--rw d!\n"; |
| |
| TEST_LOCAL_PRECHECK(72); |
| assert_int_equal(strlen(expect), ly_out_printed(out)); |
| assert_string_equal(printed, expect); |
| TEST_LOCAL_TEARDOWN; |
| } |
| |
| static void |
| node_keys(void **state) |
| { |
| TEST_LOCAL_SETUP; |
| orig = |
| "module a11 {\n" |
| " yang-version 1.1;\n" |
| " namespace \"x:y\";\n" |
| " prefix x;\n" |
| " list l1 {\n" |
| " key \"a\";\n" |
| " leaf a {\n" |
| " type string;\n" |
| " }\n" |
| " }\n" |
| " list l2 {\n" |
| " key \"a b\";\n" |
| " leaf a {\n" |
| " type string;\n" |
| " }\n" |
| " leaf b {\n" |
| " type string;\n" |
| " }\n" |
| " }\n" |
| " leaf-list ll {\n" |
| " type string;\n" |
| " }\n" |
| "}\n"; |
| |
| /* from pyang */ |
| expect = |
| "module: a11\n" |
| " +--rw l1* [a]\n" |
| " | +--rw a string\n" |
| " +--rw l2* [a b]\n" |
| " | +--rw a string\n" |
| " | +--rw b string\n" |
| " +--rw ll* string\n"; |
| |
| TEST_LOCAL_PRECHECK(72); |
| assert_int_equal(strlen(expect), ly_out_printed(out)); |
| assert_string_equal(printed, expect); |
| TEST_LOCAL_TEARDOWN; |
| } |
| |
| static void |
| node_type_target(void **state) |
| { |
| TEST_LOCAL_SETUP; |
| orig = |
| "module a12 {\n" |
| " yang-version 1.1;\n" |
| " namespace \"x:y\";\n" |
| " prefix x;\n" |
| " leaf a {\n" |
| " type leafref {\n" |
| " path \"/x:b\";\n" |
| " }\n" |
| " }\n" |
| " leaf b {\n" |
| " type string;\n" |
| " }\n" |
| "}\n"; |
| |
| /* from yanglint 1 */ |
| expect = |
| "module: a12\n" |
| " +--rw a? -> /x:b\n" |
| " +--rw b? string\n"; |
| |
| TEST_LOCAL_PRECHECK(72); |
| assert_int_equal(strlen(expect), ly_out_printed(out)); |
| assert_string_equal(printed, expect); |
| TEST_LOCAL_TEARDOWN; |
| } |
| |
| static void |
| node_type_leafref(void **state) |
| { |
| TEST_LOCAL_SETUP; |
| orig = |
| "module a13 {\n" |
| " yang-version 1.1;\n" |
| " namespace \"x:y\";\n" |
| " prefix x;\n" |
| " leaf pretty-long-identifier-name-which-should-exceed-the-limit-of-72-characters {\n" |
| " type string;\n" |
| " }\n" |
| " leaf a {\n" |
| " type leafref {\n" |
| " path \"/x:pretty-long-identifier-name-which-should-exceed-the-limit-of-72-characters\";\n" |
| " }\n" |
| " }\n" |
| "}\n"; |
| |
| /* yanglint --tree-no-leafref-target --tree-line-length=72 */ |
| expect = |
| "module: a13\n" |
| " +--rw pretty-long-identifier-name-which-should-exceed-the-limit-of-72-characters?\n" |
| " | string\n" |
| " +--rw a?\n" |
| " leafref\n"; |
| |
| TEST_LOCAL_PRECHECK(72); |
| assert_int_equal(strlen(expect), ly_out_printed(out)); |
| assert_string_equal(printed, expect); |
| TEST_LOCAL_TEARDOWN; |
| } |
| |
| static void |
| node_iffeatures(void **state) |
| { |
| TEST_LOCAL_SETUP; |
| orig = |
| "module a14 {\n" |
| " yang-version 1.1;\n" |
| " namespace \"x:y\";\n" |
| " prefix x;\n" |
| "\n" |
| " feature foo;\n" |
| "\n" |
| " feature bar;\n" |
| " container c {\n" |
| " if-feature \"foo or bar\";\n" |
| " }\n" |
| "}\n"; |
| |
| /* from pyang */ |
| expect = |
| "module: a14\n" |
| " +--rw c {foo or bar}?\n"; |
| |
| TEST_LOCAL_PRECHECK(72); |
| assert_int_equal(strlen(expect), ly_out_printed(out)); |
| assert_string_equal(printed, expect); |
| TEST_LOCAL_TEARDOWN; |
| } |
| |
| static void |
| indent_wrapper(void **state) |
| { |
| TEST_LOCAL_SETUP; |
| orig = |
| "module a15 {\n" |
| " yang-version 1.1;\n" |
| " namespace \"x:y\";\n" |
| " prefix x;\n" |
| " container a {\n" |
| " container b;\n" |
| " }\n" |
| " container c {\n" |
| " container d {\n" |
| " container e;\n" |
| " }\n" |
| " container f {\n" |
| " container g;\n" |
| " }\n" |
| " }\n" |
| " container h;\n" |
| " container i {\n" |
| " container j;\n" |
| " container k;\n" |
| " }\n" |
| "}\n"; |
| |
| /* from pyang */ |
| expect = |
| "module: a15\n" |
| " +--rw a\n" |
| " | +--rw b\n" |
| " +--rw c\n" |
| " | +--rw d\n" |
| " | | +--rw e\n" |
| " | +--rw f\n" |
| " | +--rw g\n" |
| " +--rw h\n" |
| " +--rw i\n" |
| " +--rw j\n" |
| " +--rw k\n"; |
| |
| TEST_LOCAL_PRECHECK(72); |
| assert_int_equal(strlen(expect), ly_out_printed(out)); |
| assert_string_equal(printed, expect); |
| TEST_LOCAL_TEARDOWN; |
| } |
| |
| static void |
| line_length_twiddling(void **state) |
| { |
| TEST_LOCAL_SETUP; |
| /* node_fits_tight */ |
| |
| orig = |
| "module a16 {\n" |
| " yang-version 1.1;\n" |
| " namespace \"x:y\";\n" |
| " prefix x;\n" |
| "\n" |
| " feature f;\n" |
| "\n" |
| " typedef some-long-type {\n" |
| " type string;\n" |
| " }\n" |
| " list my-list-name {\n" |
| " key \"key\";\n" |
| " leaf key {\n" |
| " type string;\n" |
| " }\n" |
| " leaf nod-leaf {\n" |
| " if-feature \"f\";\n" |
| " type some-long-type;\n" |
| " }\n" |
| " leaf nos-leaf {\n" |
| " if-feature \"f\";\n" |
| " type int32;\n" |
| " }\n" |
| " }\n" |
| "}\n"; |
| |
| /* pyang --tree-line-length 42 */ |
| expect = |
| "module: a16\n" |
| " +--rw my-list-name* [key]\n" |
| " +--rw key string\n" |
| " +--rw nod-leaf? some-long-type {f}?\n" |
| " +--rw nos-leaf? int32 {f}?\n"; |
| |
| TEST_LOCAL_PRECHECK(42); |
| assert_int_equal(strlen(expect), ly_out_printed(out)); |
| assert_string_equal(printed, expect); |
| |
| ly_out_reset(out); |
| /* break_before_iffeature */ |
| |
| /* pyang --tree-line-length 41 */ |
| expect = |
| "module: a16\n" |
| " +--rw my-list-name* [key]\n" |
| " +--rw key string\n" |
| " +--rw nod-leaf? some-long-type\n" |
| " | {f}?\n" |
| " +--rw nos-leaf? int32 {f}?\n"; |
| |
| TEST_LOCAL_CALL_PRINT(41); |
| assert_int_equal(strlen(expect), ly_out_printed(out)); |
| assert_string_equal(printed, expect); |
| |
| ly_out_reset(out); |
| /* break_before_type */ |
| |
| /* pyang --tree-line-length 29 */ |
| expect = |
| "module: a16\n" |
| " +--rw my-list-name* [key]\n" |
| " +--rw key string\n" |
| " +--rw nod-leaf?\n" |
| " | some-long-type\n" |
| " | {f}?\n" |
| " +--rw nos-leaf? int32\n" |
| " {f}?\n"; |
| |
| TEST_LOCAL_CALL_PRINT(29); |
| assert_int_equal(strlen(expect), ly_out_printed(out)); |
| assert_string_equal(printed, expect); |
| |
| ly_out_reset(out); |
| /* break_before_keys */ |
| |
| /* pyang --tree-line-length 23 */ |
| expect = |
| "module: a16\n" |
| " +--rw my-list-name*\n" |
| " [key]\n" |
| " +--rw key\n" |
| " | string\n" |
| " +--rw nod-leaf?\n" |
| " | some-long-type\n" |
| " | {f}?\n" |
| " +--rw nos-leaf?\n" |
| " int32 {f}?\n"; |
| |
| TEST_LOCAL_CALL_PRINT(23); |
| assert_int_equal(strlen(expect), ly_out_printed(out)); |
| assert_string_equal(printed, expect); |
| |
| ly_out_reset(out); |
| /* every_node_name_is_too_long */ |
| |
| /* pyang --tree-line-length 14 */ |
| expect = |
| "module: a16\n" |
| " +--rw my-list-name*\n" |
| " [key]\n" |
| " +--rw key\n" |
| " | string\n" |
| " +--rw nod-leaf?\n" |
| " | some-long-type\n" |
| " | {f}?\n" |
| " +--rw nos-leaf?\n" |
| " int32\n" |
| " {f}?\n"; |
| |
| TEST_LOCAL_CALL_PRINT(14); |
| assert_int_equal(strlen(expect), ly_out_printed(out)); |
| assert_string_equal(printed, expect); |
| |
| TEST_LOCAL_TEARDOWN; |
| } |
| |
| static void |
| break_before_leafref(void **state) |
| { |
| TEST_LOCAL_SETUP; |
| orig = |
| "module a17 {\n" |
| " yang-version 1.1;\n" |
| " namespace \"x:y\";\n" |
| " prefix x;\n" |
| " leaf e {\n" |
| " type string;\n" |
| " }\n" |
| " leaf abcd {\n" |
| " type leafref {\n" |
| " path \"/x:e\";\n" |
| " }\n" |
| " }\n" |
| "}\n"; |
| |
| /* yanglint --tree-line-length 14 */ |
| expect = |
| "module: a17\n" |
| " +--rw e?\n" |
| " | string\n" |
| " +--rw abcd?\n" |
| " -> /x:e\n"; |
| |
| TEST_LOCAL_PRECHECK(14); |
| assert_int_equal(strlen(expect), ly_out_printed(out)); |
| assert_string_equal(printed, expect); |
| TEST_LOCAL_TEARDOWN; |
| } |
| |
| static void |
| break_before_leafref_and_iffeature(void **state) |
| { |
| TEST_LOCAL_SETUP; |
| orig = |
| "module a18 {\n" |
| " yang-version 1.1;\n" |
| " namespace \"x:y\";\n" |
| " prefix x;\n" |
| "\n" |
| " feature f;\n" |
| " leaf some-long-id {\n" |
| " type string;\n" |
| " }\n" |
| " leaf a {\n" |
| " if-feature \"f\";\n" |
| " type leafref {\n" |
| " path \"/x:some-long-id\";\n" |
| " }\n" |
| " }\n" |
| "}\n"; |
| |
| /* yanglint --tree-no-leafref-target --tree-line-length=20 */ |
| expect = |
| "module: a18\n" |
| " +--rw some-long-id?\n" |
| " | string\n" |
| " +--rw a?\n" |
| " leafref\n" |
| " {f}?\n"; |
| |
| TEST_LOCAL_PRECHECK(20); |
| assert_int_equal(strlen(expect), ly_out_printed(out)); |
| assert_string_equal(printed, expect); |
| TEST_LOCAL_TEARDOWN; |
| } |
| |
| static void |
| basic_unified_indent_before_type(void **state) |
| { |
| TEST_LOCAL_SETUP; |
| orig = |
| "module a19 {\n" |
| " yang-version 1.1;\n" |
| " namespace \"x:y\";\n" |
| " prefix x;\n" |
| "\n" |
| " typedef longType {\n" |
| " type string;\n" |
| " }\n" |
| " container A {\n" |
| " leaf Bnode {\n" |
| " type int8;\n" |
| " }\n" |
| " leaf Cnode {\n" |
| " type int8;\n" |
| " mandatory true;\n" |
| " }\n" |
| " leaf Dnode {\n" |
| " type int8;\n" |
| " mandatory true;\n" |
| " }\n" |
| " leaf E {\n" |
| " type longType;\n" |
| " mandatory true;\n" |
| " }\n" |
| " leaf G {\n" |
| " type int8;\n" |
| " }\n" |
| " }\n" |
| "}\n"; |
| |
| /* from pyang */ |
| expect = |
| "module: a19\n" |
| " +--rw A\n" |
| " +--rw Bnode? int8\n" |
| " +--rw Cnode int8\n" |
| " +--rw Dnode int8\n" |
| " +--rw E longType\n" |
| " +--rw G? int8\n"; |
| |
| TEST_LOCAL_PRECHECK(72); |
| assert_int_equal(strlen(expect), ly_out_printed(out)); |
| assert_string_equal(printed, expect); |
| TEST_LOCAL_TEARDOWN; |
| } |
| |
| static void |
| twiddling_unified_indent_before_type(void **state) |
| { |
| TEST_LOCAL_SETUP; |
| /* basic_functionality */ |
| |
| orig = |
| "module a20 {\n" |
| " yang-version 1.1;\n" |
| " namespace \"x:y\";\n" |
| " prefix x;\n" |
| "\n" |
| " typedef longType {\n" |
| " type string;\n" |
| " }\n" |
| " container A {\n" |
| " leaf Bnode {\n" |
| " type int8;\n" |
| " }\n" |
| " leaf CnodeIsBigger {\n" |
| " type int8;\n" |
| " mandatory true;\n" |
| " }\n" |
| " leaf Dnode {\n" |
| " type int8;\n" |
| " mandatory true;\n" |
| " }\n" |
| " leaf E {\n" |
| " type longType;\n" |
| " mandatory true;\n" |
| " }\n" |
| " leaf G {\n" |
| " type int8;\n" |
| " }\n" |
| " }\n" |
| "}\n"; |
| |
| /* pyang --tree-line-length 36 */ |
| expect = |
| "module: a20\n" |
| " +--rw A\n" |
| " +--rw Bnode? int8\n" |
| " +--rw CnodeIsBigger int8\n" |
| " +--rw Dnode int8\n" |
| " +--rw E longType\n" |
| " +--rw G? int8\n"; |
| |
| TEST_LOCAL_PRECHECK(36); |
| assert_int_equal(strlen(expect), ly_out_printed(out)); |
| assert_string_equal(printed, expect); |
| |
| ly_out_reset(out); |
| /* unified_indent_before_type_long_node_name */ |
| |
| /* pyang --tree-line-length 32 */ |
| expect = |
| "module: a20\n" |
| " +--rw A\n" |
| " +--rw Bnode? int8\n" |
| " +--rw CnodeIsBigger int8\n" |
| " +--rw Dnode int8\n" |
| " +--rw E\n" |
| " | longType\n" |
| " +--rw G? int8\n"; |
| |
| TEST_LOCAL_CALL_PRINT(32); |
| assert_int_equal(strlen(expect), ly_out_printed(out)); |
| assert_string_equal(printed, expect); |
| |
| ly_out_reset(out); |
| /* unified_indent_before_type_long_node_type */ |
| |
| /* pyang --tree-line-length 31 */ |
| expect = |
| "module: a20\n" |
| " +--rw A\n" |
| " +--rw Bnode?\n" |
| " | int8\n" |
| " +--rw CnodeIsBigger\n" |
| " | int8\n" |
| " +--rw Dnode\n" |
| " | int8\n" |
| " +--rw E\n" |
| " | longType\n" |
| " +--rw G?\n" |
| " int8\n"; |
| |
| TEST_LOCAL_CALL_PRINT(31); |
| assert_int_equal(strlen(expect), ly_out_printed(out)); |
| assert_string_equal(printed, expect); |
| |
| TEST_LOCAL_TEARDOWN; |
| } |
| |
| static void |
| inheritance_of_config_flag(void **state) |
| { |
| TEST_LOCAL_SETUP; |
| orig = |
| "module a21 {\n" |
| " yang-version 1.1;\n" |
| " namespace \"x:y\";\n" |
| " prefix x;\n" |
| " container a {\n" |
| " config false;\n" |
| " leaf b {\n" |
| " type string;\n" |
| " }\n" |
| " }\n" |
| "}\n"; |
| |
| /* from pyang */ |
| expect = |
| "module: a21\n" |
| " +--ro a\n" |
| " +--ro b? string\n"; |
| |
| TEST_LOCAL_PRECHECK(72); |
| assert_int_equal(strlen(expect), ly_out_printed(out)); |
| assert_string_equal(printed, expect); |
| TEST_LOCAL_TEARDOWN; |
| } |
| |
| static void |
| inheritance_of_status_flag(void **state) |
| { |
| TEST_LOCAL_SETUP; |
| /* throws libyang warn: Missing explicit "..." status that was already specified in parent, inheriting. */ |
| orig = |
| "module a22 {\n" |
| " yang-version 1.1;\n" |
| " namespace \"x:y\";\n" |
| " prefix x;\n" |
| " container a {\n" |
| " status current;\n" |
| " container b {\n" |
| " status deprecated;\n" |
| " leaf f {\n" |
| " type string;\n" |
| " }\n" |
| " }\n" |
| " container c {\n" |
| " status obsolete;\n" |
| " leaf g {\n" |
| " type string;\n" |
| " }\n" |
| " }\n" |
| " }\n" |
| " container d {\n" |
| " status deprecated;\n" |
| " container h {\n" |
| " status obsolete;\n" |
| " leaf e {\n" |
| " type string;\n" |
| " }\n" |
| " }\n" |
| " }\n" |
| "}\n"; |
| |
| /* from yanglint 1 */ |
| expect = |
| "module: a22\n" |
| " +--rw a\n" |
| " | x--rw b\n" |
| " | | x--rw f? string\n" |
| " | o--rw c\n" |
| " | o--rw g? string\n" |
| " x--rw d\n" |
| " o--rw h\n" |
| " o--rw e? string\n"; |
| |
| TEST_LOCAL_PRECHECK(72); |
| assert_int_equal(strlen(expect), ly_out_printed(out)); |
| assert_string_equal(printed, expect); |
| TEST_LOCAL_TEARDOWN; |
| } |
| |
| static void |
| key_leaf_is_always_mandatory_true(void **state) |
| { |
| TEST_LOCAL_SETUP; |
| orig = |
| "module a23 {\n" |
| " yang-version 1.1;\n" |
| " namespace \"x:y\";\n" |
| " prefix x;\n" |
| " list a {\n" |
| " key \"k1\";\n" |
| " list b {\n" |
| " key \"k2\";\n" |
| " leaf k1 {\n" |
| " type string;\n" |
| " }\n" |
| " leaf k2 {\n" |
| " type string;\n" |
| " }\n" |
| " }\n" |
| " leaf k1 {\n" |
| " type string;\n" |
| " }\n" |
| " }\n" |
| "}\n"; |
| |
| /* from pyang */ |
| expect = |
| "module: a23\n" |
| " +--rw a* [k1]\n" |
| " +--rw b* [k2]\n" |
| " | +--rw k1? string\n" |
| " | +--rw k2 string\n" |
| " +--rw k1 string\n"; |
| |
| TEST_LOCAL_PRECHECK(72); |
| assert_int_equal(strlen(expect), ly_out_printed(out)); |
| assert_string_equal(printed, expect); |
| TEST_LOCAL_TEARDOWN; |
| } |
| |
| static void |
| transition_between_rpc_and_notif(void **state) |
| { |
| TEST_LOCAL_SETUP; |
| orig = |
| "module a24 {\n" |
| " yang-version 1.1;\n" |
| " namespace \"x:y\";\n" |
| " prefix x;\n" |
| " container top {\n" |
| " action rpc1 {\n" |
| "\n" |
| " input {\n" |
| " leaf in {\n" |
| " type string;\n" |
| " }\n" |
| " }\n" |
| " }\n" |
| " action rpc2 {\n" |
| "\n" |
| " input {\n" |
| " leaf in {\n" |
| " type string;\n" |
| " }\n" |
| " }\n" |
| "\n" |
| " output {\n" |
| " leaf out {\n" |
| " type string;\n" |
| " }\n" |
| " }\n" |
| " }\n" |
| " notification n1;\n" |
| " notification n2;\n" |
| " }\n" |
| "}\n"; |
| |
| /* from pyang but edited -> n1 node is moved to n2 */ |
| expect = |
| "module: a24\n" |
| " +--rw top\n" |
| " +---x rpc1\n" |
| " | +---w input\n" |
| " | +---w in? string\n" |
| " +---x rpc2\n" |
| " | +---w input\n" |
| " | | +---w in? string\n" |
| " | +--ro output\n" |
| " | +--ro out? string\n" |
| " +---n n1\n" |
| " +---n n2\n"; |
| |
| TEST_LOCAL_PRECHECK(72); |
| assert_int_equal(strlen(expect), ly_out_printed(out)); |
| assert_string_equal(printed, expect); |
| TEST_LOCAL_TEARDOWN; |
| } |
| |
| int |
| main(void) |
| { |
| const struct CMUnitTest tests[] = { |
| UTEST(base_sections), |
| UTEST(node_status), |
| UTEST(node_config_flags), |
| UTEST(node_rpcs_flags), |
| UTEST(node_grouping_flags), |
| UTEST(notif_inside_container), |
| UTEST(node_choice), |
| UTEST(node_case), |
| UTEST(optional_opts), |
| UTEST(presence_container), |
| UTEST(node_keys), |
| UTEST(node_type_target), |
| UTEST(node_type_leafref), |
| UTEST(node_iffeatures), |
| UTEST(indent_wrapper), |
| UTEST(line_length_twiddling), |
| UTEST(break_before_leafref), |
| UTEST(break_before_leafref_and_iffeature), |
| UTEST(basic_unified_indent_before_type), |
| UTEST(twiddling_unified_indent_before_type), |
| UTEST(inheritance_of_config_flag), |
| UTEST(inheritance_of_status_flag), |
| UTEST(key_leaf_is_always_mandatory_true), |
| UTEST(transition_between_rpc_and_notif), |
| }; |
| |
| return cmocka_run_group_tests(tests, NULL, NULL); |
| } |