Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/xpath.c
Original file line number Diff line number Diff line change
Expand Up @@ -8091,9 +8091,6 @@ moveto_op_comp_item(const struct lyxp_set *set1, uint32_t idx1, struct lyxp_set
case LYXP_SET_NUMBER:
rc = set_comp_cast(&tmp1, set1, LYXP_SET_NUMBER, idx1);
break;
case LYXP_SET_BOOLEAN:
rc = set_comp_cast(&tmp1, set1, LYXP_SET_BOOLEAN, idx1);
break;
default:
rc = set_comp_cast(&tmp1, set1, LYXP_SET_STRING, idx1);
break;
Expand Down Expand Up @@ -8235,6 +8232,13 @@ moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, ly_
int cmp;
LY_ERR rc;

/* a node-set compared with a boolean is converted to a boolean as a whole (XPath 1.0 section 3.4) */
if ((set1->type == LYXP_SET_NODE_SET) && (set2->type == LYXP_SET_BOOLEAN)) {
LY_CHECK_RET(lyxp_set_cast(set1, LYXP_SET_BOOLEAN));
} else if ((set1->type == LYXP_SET_BOOLEAN) && (set2->type == LYXP_SET_NODE_SET)) {
LY_CHECK_RET(lyxp_set_cast(set2, LYXP_SET_BOOLEAN));
}

/* iterative evaluation with node-sets */
if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
if (set1->type == LYXP_SET_NODE_SET) {
Expand Down
40 changes: 40 additions & 0 deletions tests/utests/basic/test_xpath.c
Original file line number Diff line number Diff line change
Expand Up @@ -1377,6 +1377,45 @@ test_no_value(void **state)
lyd_free_all(tree);
}

static void
test_nodeset_boolean(void **state)
{
const char *data = "<foo xmlns=\"urn:tests:a\">false</foo>";
struct lyd_node *tree;
ly_bool r;

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);

/* an empty node-set is false */
assert_int_equal(LY_SUCCESS, lyd_eval_xpath(tree, "/a:foo2 = false()", &r));
assert_int_equal(r, 1);

assert_int_equal(LY_SUCCESS, lyd_eval_xpath(tree, "/a:foo2 = true()", &r));
assert_int_equal(r, 0);

assert_int_equal(LY_SUCCESS, lyd_eval_xpath(tree, "/a:foo2 != false()", &r));
assert_int_equal(r, 0);

assert_int_equal(LY_SUCCESS, lyd_eval_xpath(tree, "/a:foo2 != true()", &r));
assert_int_equal(r, 1);

assert_int_equal(LY_SUCCESS, lyd_eval_xpath(tree, "false() = /a:foo2", &r));
assert_int_equal(r, 1);

assert_int_equal(LY_SUCCESS, lyd_eval_xpath(tree, "/a:foo2 < true()", &r));
assert_int_equal(r, 1);

/* a non-empty node-set is true, whatever its value */
assert_int_equal(LY_SUCCESS, lyd_eval_xpath(tree, "/a:foo = false()", &r));
assert_int_equal(r, 0);

assert_int_equal(LY_SUCCESS, lyd_eval_xpath(tree, "/a:foo = true()", &r));
assert_int_equal(r, 1);

lyd_free_all(tree);
}

int
main(void)
{
Expand All @@ -1398,6 +1437,7 @@ main(void)
UTEST(test_anydata, setup),
UTEST(test_number, setup),
UTEST(test_no_value, setup),
UTEST(test_nodeset_boolean, setup),
};

return cmocka_run_group_tests(tests, NULL, NULL);
Expand Down
Loading