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