Skip to content

Commit 28fbc67

Browse files
committed
More tests for xml_find.
Return empty nodeset if no matches
1 parent 407bfc7 commit 28fbc67

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

src/xml2_xpath.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,14 @@ Rcpp::List node_find(XPtrNode node, XPtrDoc doc, std::string xpath, CharacterVec
3232
xmlXPathEval((xmlChar*) xpath.c_str(), context.get()),
3333
xmlXPathFreeObject
3434
);
35+
// Return an empty list if there are no matches
3536
if (result.get() == NULL)
36-
Rcpp::stop("Xpath failed to execute");
37+
return Rcpp::List::create();
3738

3839
if (result->type != XPATH_NODESET)
3940
Rcpp::stop("Currently only nodeset results are supported");
4041

41-
// Return an empty list if there are no matches
4242
xmlNodeSetPtr nodes = result->nodesetval;
43-
if (xmlXPathNodeSetIsEmpty(nodes)) {
44-
return Rcpp::List::create();
45-
}
46-
4743
int n = nodes->nodeNr;
4844
List out(n);
4945
for (int i = 0; i < nodes->nodeNr; i++) {

tests/testthat/test-xml_find.R

+10-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,14 @@ test_that("qualified names matches to namespace", {
1212

1313
expect_equal(length(xml_find(x, "//d1:bar", ns)), 1)
1414
expect_equal(length(xml_find(x, "//d2:bar", ns)), 1)
15-
expect_error(length(xml_find(x, "//g:bar", ns)), "Xpath failed")
15+
})
16+
17+
test_that("warning if unknown namespace", {
18+
x <- xml("<foo><bar /></foo>")
19+
expect_warning(xml_find(x, "//g:bar"), "Undefined namespace prefix")
20+
})
21+
22+
test_that("no matches returns empty nodeset", {
23+
x <- xml("<foo><bar /></foo>")
24+
expect_equal(length(xml_find(x, "//baz")), 0)
1625
})

0 commit comments

Comments
 (0)