diff options
Diffstat (limited to 'tests/utests/basic/test_xpath.c')
-rw-r--r-- | tests/utests/basic/test_xpath.c | 204 |
1 files changed, 198 insertions, 6 deletions
diff --git a/tests/utests/basic/test_xpath.c b/tests/utests/basic/test_xpath.c index b388dc3..754abf2 100644 --- a/tests/utests/basic/test_xpath.c +++ b/tests/utests/basic/test_xpath.c @@ -88,6 +88,19 @@ const char *schema_a = " type string;\n" " }\n" " }\n" + "\n" + " rpc r {\n" + " input {\n" + " leaf l {\n" + " type string;\n" + " }\n" + " }\n" + " output {\n" + " leaf l {\n" + " type string;\n" + " }\n" + " }\n" + " }\n" "}"; static int @@ -254,9 +267,11 @@ test_invalid(void **state) assert_int_equal(LY_EVALID, lyd_find_xpath(tree, "/a:foo2[.=]", &set)); assert_null(set); + CHECK_LOG_CTX("Unexpected XPath token \"]\" (\"]\").", NULL); assert_int_equal(LY_EVALID, lyd_find_xpath(tree, "/a:", &set)); assert_null(set); + CHECK_LOG_CTX("Invalid character 'a'[2] of expression '/a:'.", NULL); lyd_free_all(tree); } @@ -381,6 +396,31 @@ test_hash(void **state) } static void +test_rpc(void **state) +{ + const char *data = + "<r xmlns=\"urn:tests:a\">\n" + " <l>val</l>\n" + "</r>"; + struct ly_in *in; + struct lyd_node *tree; + struct ly_set *set; + + assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in)); + assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_REPLY_YANG, &tree, NULL)); + ly_in_free(in, 0); + assert_non_null(tree); + + /* name collision input/output, hashes are not used */ + assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "/a:r/l", &set)); + assert_int_equal(1, set->count); + + ly_set_free(set, NULL); + + lyd_free_all(tree); +} + +static void test_toplevel(void **state) { const char *schema_b = @@ -479,7 +519,7 @@ test_atomize(void **state) /* some random paths just making sure the API function works */ assert_int_equal(LY_SUCCESS, lys_find_xpath_atoms(UTEST_LYCTX, NULL, "/a:*", 0, &set)); - assert_int_equal(6, set->count); + assert_int_equal(7, set->count); ly_set_free(set, NULL); /* all nodes from all modules (including internal, which can change easily, so check just the test modules) */ @@ -496,7 +536,7 @@ test_atomize(void **state) ly_set_free(set, NULL); assert_int_equal(LY_SUCCESS, lys_find_xpath_atoms(UTEST_LYCTX, NULL, "/*", 0, &set)); - assert_int_equal(13, set->count); + assert_int_equal(14, set->count); ly_set_free(set, NULL); /* @@ -530,7 +570,7 @@ test_atomize(void **state) /* descendant-or-self */ assert_int_equal(LY_SUCCESS, lys_find_xpath_atoms(UTEST_LYCTX, NULL, "/a:*/descendant-or-self::c", 0, &set)); - assert_int_equal(7, set->count); + assert_int_equal(8, set->count); ly_set_free(set, NULL); /* following */ @@ -545,11 +585,11 @@ test_atomize(void **state) /* parent */ assert_int_equal(LY_SUCCESS, lys_find_xpath_atoms(UTEST_LYCTX, NULL, "/child::a:*/c/parent::l1", 0, &set)); - assert_int_equal(7, set->count); + assert_int_equal(8, set->count); ly_set_free(set, NULL); assert_int_equal(LY_SUCCESS, lys_find_xpath_atoms(UTEST_LYCTX, NULL, "/child::a:c//..", 0, &set)); - assert_int_equal(8, set->count); + assert_int_equal(11, set->count); ly_set_free(set, NULL); /* preceding */ @@ -898,6 +938,7 @@ test_variables(void **state) LOCAL_SETUP(data, tree); assert_int_equal(LY_SUCCESS, lyxp_vars_set(&vars, "var1", "\"mstr\"")); assert_int_equal(LY_ENOTFOUND, lyd_find_xpath2(tree, "/foo[text() = $var55]", vars, &set)); + CHECK_LOG_CTX("Variable \"var55\" not defined.", NULL); LOCAL_TEARDOWN(set, tree, vars); /* Syntax error in value. */ @@ -906,6 +947,7 @@ test_variables(void **state) LOCAL_SETUP(data, tree); assert_int_equal(LY_SUCCESS, lyxp_vars_set(&vars, "var", "\"")); assert_int_equal(LY_EVALID, lyd_find_xpath2(tree, "/foo[$var]", vars, &set)); + CHECK_LOG_CTX("Unterminated string delimited with \" (\").", "Data location \"/a:foo\"."); LOCAL_TEARDOWN(set, tree, vars); /* Prefix is not supported. */ @@ -914,7 +956,7 @@ test_variables(void **state) LOCAL_SETUP(data, tree); assert_int_equal(LY_SUCCESS, lyxp_vars_set(&vars, "var", "\"")); assert_int_equal(LY_EVALID, lyd_find_xpath2(tree, "/foo[$pref:var]", vars, &set)); - assert_string_equal("Variable with prefix is not supported.", _UC->err_msg); + CHECK_LOG_CTX("Variable with prefix is not supported.", NULL); LOCAL_TEARDOWN(set, tree, vars); #undef LOCAL_SETUP @@ -1050,6 +1092,154 @@ test_axes(void **state) lyd_free_all(tree); } +static void +test_trim(void **state) +{ + const char *data; + char *str1; + struct lyd_node *tree; + + data = + "<l1 xmlns=\"urn:tests:a\">" + " <a>a1</a>" + " <b>b1</b>" + " <c>c1</c>" + "</l1>" + "<l1 xmlns=\"urn:tests:a\">" + " <a>a2</a>" + " <b>b2</b>" + "</l1>" + "<l1 xmlns=\"urn:tests:a\">" + " <a>a3</a>" + " <b>b3</b>" + "</l1>" + "<l1 xmlns=\"urn:tests:a\">" + " <a>a4</a>" + " <b>b4</b>" + " <c>c4</c>" + "</l1>" + "<l1 xmlns=\"urn:tests:a\">" + " <a>a5</a>" + " <b>b5</b>" + " <c>c5</c>" + "</l1>" + "<foo2 xmlns=\"urn:tests:a\">50</foo2>" + "<c xmlns=\"urn:tests:a\">" + " <x>key2</x>" + " <ll>" + " <a>key1</a>" + " <ll>" + " <a>key11</a>" + " <b>val11</b>" + " </ll>" + " <ll>" + " <a>key12</a>" + " <b>val12</b>" + " </ll>" + " <ll>" + " <a>key13</a>" + " <b>val13</b>" + " </ll>" + " </ll>" + " <ll>" + " <a>key2</a>" + " <ll>" + " <a>key21</a>" + " <b>val21</b>" + " </ll>" + " <ll>" + " <a>key22</a>" + " <b>val22</b>" + " </ll>" + " </ll>" + " <ll>" + " <a>key3</a>" + " <ll>" + " <a>key31</a>" + " <b>val31</b>" + " </ll>" + " <ll>" + " <a>key32</a>" + " <b>val32</b>" + " </ll>" + " </ll>" + "</c>"; + + /* trim #1 */ + assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, &tree)); + assert_non_null(tree); + + assert_int_equal(LY_SUCCESS, lyd_trim_xpath(&tree, "/a:c/ll/ll[a='key11']", NULL)); + lyd_print_mem(&str1, tree, LYD_XML, LYD_PRINT_WITHSIBLINGS); + assert_string_equal(str1, + "<c xmlns=\"urn:tests:a\">\n" + " <ll>\n" + " <a>key1</a>\n" + " <ll>\n" + " <a>key11</a>\n" + " <b>val11</b>\n" + " </ll>\n" + " </ll>\n" + "</c>\n"); + + free(str1); + lyd_free_all(tree); + + /* trim #2 */ + assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, &tree)); + assert_non_null(tree); + + assert_int_equal(LY_SUCCESS, lyd_trim_xpath(&tree, "/a:c/ll/ll[contains(.,'2')]", NULL)); + lyd_print_mem(&str1, tree, LYD_XML, LYD_PRINT_WITHSIBLINGS); + assert_string_equal(str1, + "<c xmlns=\"urn:tests:a\">\n" + " <ll>\n" + " <a>key1</a>\n" + " <ll>\n" + " <a>key12</a>\n" + " <b>val12</b>\n" + " </ll>\n" + " </ll>\n" + " <ll>\n" + " <a>key2</a>\n" + " <ll>\n" + " <a>key21</a>\n" + " <b>val21</b>\n" + " </ll>\n" + " <ll>\n" + " <a>key22</a>\n" + " <b>val22</b>\n" + " </ll>\n" + " </ll>\n" + " <ll>\n" + " <a>key3</a>\n" + " <ll>\n" + " <a>key32</a>\n" + " <b>val32</b>\n" + " </ll>\n" + " </ll>\n" + "</c>\n"); + + free(str1); + lyd_free_all(tree); + + /* trim #3 */ + assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, &tree)); + assert_non_null(tree); + + assert_int_equal(LY_SUCCESS, lyd_trim_xpath(&tree, "/l1[4]//.", NULL)); + lyd_print_mem(&str1, tree, LYD_XML, LYD_PRINT_WITHSIBLINGS); + assert_string_equal(str1, + "<l1 xmlns=\"urn:tests:a\">\n" + " <a>a4</a>\n" + " <b>b4</b>\n" + " <c>c4</c>\n" + "</l1>\n"); + + free(str1); + lyd_free_all(tree); +} + int main(void) { @@ -1058,6 +1248,7 @@ main(void) UTEST(test_union, setup), UTEST(test_invalid, setup), UTEST(test_hash, setup), + UTEST(test_rpc, setup), UTEST(test_toplevel, setup), UTEST(test_atomize, setup), UTEST(test_canonize, setup), @@ -1065,6 +1256,7 @@ main(void) UTEST(test_augment, setup), UTEST(test_variables, setup), UTEST(test_axes, setup), + UTEST(test_trim, setup), }; return cmocka_run_group_tests(tests, NULL, NULL); |