blob: ded22324ba2598608325eccd6d51140e16f710c8 [file] [log] [blame]
David Sedlák3b4db242018-10-19 16:11:01 +02001#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ák872c7b42018-10-26 13:15:20 +020013
14static struct lysp_module *
15mod_renew(struct ly_parser_ctx *ctx, struct lysp_module *mod, uint8_t submodule)
David Sedlák3b4db242018-10-19 16:11:01 +020016{
David Sedlák872c7b42018-10-26 13:15:20 +020017 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
25static void
26test_parse(void **state)
27{
28 (void)state; /* unused */
29
David Sedlák3b4db242018-10-19 16:11:01 +020030 struct ly_ctx *ctx;
31 struct lysp_module *mod;
David Sedlák872c7b42018-10-26 13:15:20 +020032
David Sedlák3b4db242018-10-19 16:11:01 +020033 ly_ctx_new(NULL, 0, &ctx);
34 mod = malloc(sizeof(*mod));
David Sedlák872c7b42018-10-26 13:15:20 +020035 mod->ctx = ctx;
David Sedlák3b4db242018-10-19 16:11:01 +020036
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ák872c7b42018-10-26 13:15:20 +020039
40 assert_string_equal(mod->name, "example-foo");
41
David Sedlák3b4db242018-10-19 16:11:01 +020042 ly_ctx_destroy(ctx, NULL);
David Sedlák872c7b42018-10-26 13:15:20 +020043
44 free(mod);
David Sedlák3b4db242018-10-19 16:11:01 +020045}
46
47static int
48test_setup(void **state) {
49 (void)state; /* unused */
50 return 0;
51}
52
53static void
54test_match_keyword(void **state)
55{
56 (void)state; /* unused */
57
David Sedlák872c7b42018-10-26 13:15:20 +020058 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ák3b4db242018-10-19 16:11:01 +0200129
David Sedlák872c7b42018-10-26 13:15:20 +0200130static void
131test_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ák3b4db242018-10-19 16:11:01 +0200147}
148
149int
150main(void)
151{
152
153 const struct CMUnitTest tests[] = {
David Sedlák872c7b42018-10-26 13:15:20 +0200154 cmocka_unit_test_setup(test_parse, test_setup),
David Sedlák3b4db242018-10-19 16:11:01 +0200155 cmocka_unit_test(test_match_keyword),
David Sedlák872c7b42018-10-26 13:15:20 +0200156 cmocka_unit_test(test_match_argument),
David Sedlák3b4db242018-10-19 16:11:01 +0200157 };
158
159 return cmocka_run_group_tests(tests, NULL, NULL);
160}