schema compile CHANGE partial support for choice/case nodes
- still work in progress - missing choice's default statement support
and most of the unit tests.
diff --git a/tests/src/test_parser_yang.c b/tests/src/test_parser_yang.c
index d4bc352..bc31e12 100644
--- a/tests/src/test_parser_yang.c
+++ b/tests/src/test_parser_yang.c
@@ -1562,6 +1562,59 @@
ly_ctx_destroy(ctx.ctx, NULL);
}
+static void
+test_choice(void **state)
+{
+ *state = test_choice;
+
+ struct lysp_module mod = {0};
+ struct ly_parser_ctx ctx = {0};
+ struct lysp_node_choice *ch = NULL;
+ const char *str;
+
+ assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
+ assert_non_null(ctx.ctx);
+ ctx.line = 1;
+ ctx.mod = &mod;
+ ctx.mod->version = 2; /* simulate YANG 1.1 */
+
+ /* invalid cardinality */
+#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
+ str = "ch {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
+ assert_int_equal(LY_EVALID, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch)); \
+ logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
+ lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
+
+ TEST_DUP("config", "true", "false");
+ TEST_DUP("default", "a", "b");
+ TEST_DUP("description", "text1", "text2");
+ TEST_DUP("mandatory", "true", "false");
+ TEST_DUP("reference", "1", "2");
+ TEST_DUP("status", "current", "obsolete");
+ TEST_DUP("when", "true", "false");
+#undef TEST_DUP
+
+ /* full content */
+ str = "ch {anydata any;anyxml anyxml; case c;choice ch;config false;container c;default c;description test;if-feature f;leaf l {type string;}"
+ "leaf-list ll {type string;} list li;mandatory true;reference test;status current;when true;m:ext;} ...";
+ assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
+ assert_non_null(ch);
+ assert_int_equal(LYS_CHOICE, ch->nodetype);
+ assert_string_equal("ch", ch->name);
+ assert_string_equal("test", ch->dsc);
+ assert_non_null(ch->exts);
+ assert_non_null(ch->iffeatures);
+ assert_string_equal("test", ch->ref);
+ assert_non_null(ch->when);
+ assert_null(ch->parent);
+ assert_null(ch->next);
+ assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, ch->flags);
+ lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
+
+ *state = NULL;
+ ly_ctx_destroy(ctx.ctx, NULL);
+}
+
int main(void)
{
const struct CMUnitTest tests[] = {
@@ -1579,6 +1632,7 @@
cmocka_unit_test_setup_teardown(test_leaf, logger_setup, logger_teardown),
cmocka_unit_test_setup_teardown(test_leaflist, logger_setup, logger_teardown),
cmocka_unit_test_setup_teardown(test_list, logger_setup, logger_teardown),
+ cmocka_unit_test_setup_teardown(test_choice, logger_setup, logger_teardown),
};
return cmocka_run_group_tests(tests, NULL, NULL);
diff --git a/tests/src/test_tree_schema_compile.c b/tests/src/test_tree_schema_compile.c
index fb2e6f8..fe711f6 100644
--- a/tests/src/test_tree_schema_compile.c
+++ b/tests/src/test_tree_schema_compile.c
@@ -640,6 +640,63 @@
ly_ctx_destroy(ctx, NULL);
}
+static void
+test_node_choice(void **state)
+{
+ *state = test_node_choice;
+
+ struct ly_ctx *ctx;
+ struct lys_module *mod;
+ struct lysc_node_choice *ch;
+ struct lysc_node_case *cs;
+
+ assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
+
+ assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;feature f;"
+ "choice ch {case a {leaf a {type string;}}leaf b {type string;}}}", LYS_IN_YANG));
+ assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
+ ch = (struct lysc_node_choice*)mod->compiled->data;
+ assert_non_null(ch);
+ assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR, ch->flags);
+ cs = ch->cases;
+ assert_non_null(cs);
+ assert_string_equal("a", cs->name);
+ assert_ptr_equal(ch, cs->parent);
+ assert_non_null(cs->child);
+ assert_string_equal("a", cs->child->name);
+ assert_ptr_equal(cs, cs->child->parent);
+ cs = (struct lysc_node_case*)cs->next;
+ assert_non_null(cs);
+ assert_string_equal("b", cs->name);
+ assert_ptr_equal(ch, cs->parent);
+ assert_non_null(cs->child);
+ assert_string_equal("b", cs->child->name);
+ assert_ptr_equal(cs, cs->child->parent);
+ assert_ptr_equal(ch->cases->child, cs->child->prev);
+ assert_ptr_equal(ch->cases->child->next, cs->child);
+ assert_ptr_equal(ch->cases->child->prev, cs->child);
+
+ assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
+ "choice ch {case a {leaf x {type string;}}leaf x {type string;}}}", LYS_IN_YANG));
+ assert_int_equal(LY_EVALID, lys_compile(mod, 0));
+ logbuf_assert("Duplicate identifier \"x\" of data definition statement.");
+ assert_non_null(mod = lys_parse_mem(ctx, "module aa2 {namespace urn:aa2;prefix aa;"
+ "choice ch {case a {leaf y {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
+ assert_int_equal(LY_EVALID, lys_compile(mod, 0));
+ logbuf_assert("Duplicate identifier \"y\" of data definition statement.");
+ assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;"
+ "choice ch {case a {leaf x {type string;}}leaf a {type string;}}}", LYS_IN_YANG));
+ assert_int_equal(LY_EVALID, lys_compile(mod, 0));
+ logbuf_assert("Duplicate identifier \"a\" of case statement.");
+ assert_non_null(mod = lys_parse_mem(ctx, "module bb2 {namespace urn:bb2;prefix bb;"
+ "choice ch {case b {leaf x {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
+ assert_int_equal(LY_EVALID, lys_compile(mod, 0));
+ logbuf_assert("Duplicate identifier \"b\" of case statement.");
+
+ *state = NULL;
+ ly_ctx_destroy(ctx, NULL);
+}
+
/**
* actually the same as length restriction (tested in test_type_length()), so just check the correct handling in appropriate types,
* do not test the expression itself
@@ -1666,7 +1723,7 @@
"leaf address {type leafref{ path \"../../interface[ name = current()/../ifname ]/address/ip\";}}}}",
LYS_IN_YANG));
assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
- type = ((struct lysc_node_leaf*)(*lysc_node_children(mod->compiled->data->prev))->prev)->type;
+ type = ((struct lysc_node_leaf*)(*lysc_node_children_p(mod->compiled->data->prev))->prev)->type;
assert_non_null(type);
assert_int_equal(1, type->refcount);
assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
@@ -2108,6 +2165,7 @@
cmocka_unit_test_setup_teardown(test_node_container, logger_setup, logger_teardown),
cmocka_unit_test_setup_teardown(test_node_leaflist, logger_setup, logger_teardown),
cmocka_unit_test_setup_teardown(test_node_list, logger_setup, logger_teardown),
+ cmocka_unit_test_setup_teardown(test_node_choice, logger_setup, logger_teardown),
};
return cmocka_run_group_tests(tests, NULL, NULL);