David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 1 | #include <stdarg.h> |
| 2 | #include <stddef.h> |
| 3 | #include <setjmp.h> |
| 4 | #include <cmocka.h> |
| 5 | |
| 6 | #include <stdio.h> |
| 7 | #include <string.h> |
| 8 | |
| 9 | #include "libyang.h" |
| 10 | #include "tree_schema_internal.h" |
| 11 | #include "../../src/parser_yin.c" |
| 12 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame^] | 13 | |
| 14 | static struct lysp_module * |
| 15 | mod_renew(struct ly_parser_ctx *ctx, struct lysp_module *mod, uint8_t submodule) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 16 | { |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame^] | 17 | lysp_module_free(mod); |
| 18 | mod = calloc(1, sizeof *mod); |
| 19 | mod->ctx = ctx->ctx; |
| 20 | mod->submodule = submodule; |
| 21 | assert_non_null(mod); |
| 22 | return mod; |
| 23 | } |
| 24 | |
| 25 | static void |
| 26 | test_parse(void **state) |
| 27 | { |
| 28 | (void)state; /* unused */ |
| 29 | |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 30 | struct ly_ctx *ctx; |
| 31 | struct lysp_module *mod; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame^] | 32 | |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 33 | ly_ctx_new(NULL, 0, &ctx); |
| 34 | mod = malloc(sizeof(*mod)); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame^] | 35 | mod->ctx = ctx; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 36 | |
| 37 | yin_parse(ctx, "<module name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" xmlns:foo=\"urn:example:foo\" xmlns:myext=\"urn:example:extensions\">", |
| 38 | &mod); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame^] | 39 | |
| 40 | assert_string_equal(mod->name, "example-foo"); |
| 41 | |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 42 | ly_ctx_destroy(ctx, NULL); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame^] | 43 | |
| 44 | free(mod); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static int |
| 48 | test_setup(void **state) { |
| 49 | (void)state; /* unused */ |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | static void |
| 54 | test_match_keyword(void **state) |
| 55 | { |
| 56 | (void)state; /* unused */ |
| 57 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame^] | 58 | assert_int_equal(match_keyword("anydata"), YANG_ANYDATA); |
| 59 | assert_int_equal(match_keyword("asdasd"), YANG_NONE); |
| 60 | assert_int_equal(match_keyword(""), YANG_NONE); |
| 61 | assert_int_equal(match_keyword("anydata"), YANG_ANYDATA); |
| 62 | assert_int_equal(match_keyword("anyxml"), YANG_ANYXML); |
| 63 | assert_int_equal(match_keyword("argument"), YANG_ARGUMENT); |
| 64 | assert_int_equal(match_keyword("augment"), YANG_AUGMENT); |
| 65 | assert_int_equal(match_keyword("base"), YANG_BASE); |
| 66 | assert_int_equal(match_keyword("belongs-to"), YANG_BELONGS_TO); |
| 67 | assert_int_equal(match_keyword("bit"), YANG_BIT); |
| 68 | assert_int_equal(match_keyword("case"), YANG_CASE); |
| 69 | assert_int_equal(match_keyword("choice"), YANG_CHOICE); |
| 70 | assert_int_equal(match_keyword("config"), YANG_CONFIG); |
| 71 | assert_int_equal(match_keyword("contact"), YANG_CONTACT); |
| 72 | assert_int_equal(match_keyword("container"), YANG_CONTAINER); |
| 73 | assert_int_equal(match_keyword("default"), YANG_DEFAULT); |
| 74 | assert_int_equal(match_keyword("description"), YANG_DESCRIPTION); |
| 75 | assert_int_equal(match_keyword("deviate"), YANG_DEVIATE); |
| 76 | assert_int_equal(match_keyword("deviation"), YANG_DEVIATION); |
| 77 | assert_int_equal(match_keyword("enum"), YANG_ENUM); |
| 78 | assert_int_equal(match_keyword("error-app-tag"), YANG_ERROR_APP_TAG); |
| 79 | assert_int_equal(match_keyword("error-message"), YANG_ERROR_MESSAGE); |
| 80 | assert_int_equal(match_keyword("extension"), YANG_EXTENSION); |
| 81 | assert_int_equal(match_keyword("feature"), YANG_FEATURE); |
| 82 | assert_int_equal(match_keyword("fraction-digits"), YANG_FRACTION_DIGITS); |
| 83 | assert_int_equal(match_keyword("grouping"), YANG_GROUPING); |
| 84 | assert_int_equal(match_keyword("identity"), YANG_IDENTITY); |
| 85 | assert_int_equal(match_keyword("if-feature"), YANG_IF_FEATURE); |
| 86 | assert_int_equal(match_keyword("import"), YANG_IMPORT); |
| 87 | assert_int_equal(match_keyword("include"), YANG_INCLUDE); |
| 88 | assert_int_equal(match_keyword("input"), YANG_INPUT); |
| 89 | assert_int_equal(match_keyword("key"), YANG_KEY); |
| 90 | assert_int_equal(match_keyword("leaf"), YANG_LEAF); |
| 91 | assert_int_equal(match_keyword("leaf-list"), YANG_LEAF_LIST); |
| 92 | assert_int_equal(match_keyword("length"), YANG_LENGTH); |
| 93 | assert_int_equal(match_keyword("list"), YANG_LIST); |
| 94 | assert_int_equal(match_keyword("mandatory"), YANG_MANDATORY); |
| 95 | assert_int_equal(match_keyword("max-elements"), YANG_MAX_ELEMENTS); |
| 96 | assert_int_equal(match_keyword("min-elements"), YANG_MIN_ELEMENTS); |
| 97 | assert_int_equal(match_keyword("modifier"), YANG_MODIFIER); |
| 98 | assert_int_equal(match_keyword("module"), YANG_MODULE); |
| 99 | assert_int_equal(match_keyword("must"), YANG_MUST); |
| 100 | assert_int_equal(match_keyword("namespace"), YANG_NAMESPACE); |
| 101 | assert_int_equal(match_keyword("notification"), YANG_NOTIFICATION); |
| 102 | assert_int_equal(match_keyword("ordered-by"), YANG_ORDERED_BY); |
| 103 | assert_int_equal(match_keyword("organization"), YANG_ORGANIZATION); |
| 104 | assert_int_equal(match_keyword("output"), YANG_OUTPUT); |
| 105 | assert_int_equal(match_keyword("path"), YANG_PATH); |
| 106 | assert_int_equal(match_keyword("pattern"), YANG_PATTERN); |
| 107 | assert_int_equal(match_keyword("position"), YANG_POSITION); |
| 108 | assert_int_equal(match_keyword("prefix"), YANG_PREFIX); |
| 109 | assert_int_equal(match_keyword("presence"), YANG_PRESENCE); |
| 110 | assert_int_equal(match_keyword("range"), YANG_RANGE); |
| 111 | assert_int_equal(match_keyword("reference"), YANG_REFERENCE); |
| 112 | assert_int_equal(match_keyword("refine"), YANG_REFINE); |
| 113 | assert_int_equal(match_keyword("require-instance"), YANG_REQUIRE_INSTANCE); |
| 114 | assert_int_equal(match_keyword("revision"), YANG_REVISION); |
| 115 | assert_int_equal(match_keyword("revision-date"), YANG_REVISION_DATE); |
| 116 | assert_int_equal(match_keyword("rpc"), YANG_RPC); |
| 117 | assert_int_equal(match_keyword("status"), YANG_STATUS); |
| 118 | assert_int_equal(match_keyword("submodule"), YANG_SUBMODULE); |
| 119 | assert_int_equal(match_keyword("type"), YANG_TYPE); |
| 120 | assert_int_equal(match_keyword("typedef"), YANG_TYPEDEF); |
| 121 | assert_int_equal(match_keyword("unique"), YANG_UNIQUE); |
| 122 | assert_int_equal(match_keyword("units"), YANG_UNITS); |
| 123 | assert_int_equal(match_keyword("uses"), YANG_USES); |
| 124 | assert_int_equal(match_keyword("value"), YANG_VALUE); |
| 125 | assert_int_equal(match_keyword("when"), YANG_WHEN); |
| 126 | assert_int_equal(match_keyword("yang-version"), YANG_YANG_VERSION); |
| 127 | assert_int_equal(match_keyword("yin-element"), YANG_YIN_ELEMENT); |
| 128 | } |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 129 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame^] | 130 | static void |
| 131 | test_match_argument(void **state) |
| 132 | { |
| 133 | (void)state; |
| 134 | |
| 135 | assert_int_equal(match_argument_name("", 5), YIN_ARG_NONE); |
| 136 | assert_int_equal(match_argument_name("qwertyasd", 5), YIN_ARG_NONE); |
| 137 | assert_int_equal(match_argument_name("conditionasd", 8), YIN_ARG_NONE); |
| 138 | assert_int_equal(match_argument_name("condition", 9), YIN_ARG_CONDITION); |
| 139 | assert_int_equal(match_argument_name("date", 4), YIN_ARG_DATE); |
| 140 | assert_int_equal(match_argument_name("module", 6), YIN_ARG_MODULE); |
| 141 | assert_int_equal(match_argument_name("name", 4), YIN_ARG_NAME); |
| 142 | assert_int_equal(match_argument_name("tag", 3), YIN_ARG_TAG); |
| 143 | assert_int_equal(match_argument_name("target-node", 11), YIN_ARG_TARGET_NODE); |
| 144 | assert_int_equal(match_argument_name("text", 4), YIN_ARG_TEXT); |
| 145 | assert_int_equal(match_argument_name("uri", 3), YIN_ARG_URI); |
| 146 | assert_int_equal(match_argument_name("value", 5), YIN_ARG_VALUE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | int |
| 150 | main(void) |
| 151 | { |
| 152 | |
| 153 | const struct CMUnitTest tests[] = { |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame^] | 154 | cmocka_unit_test_setup(test_parse, test_setup), |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 155 | cmocka_unit_test(test_match_keyword), |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame^] | 156 | cmocka_unit_test(test_match_argument), |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 160 | } |