+enum NodeType {
+ missing = 1,
+ node = 2,
+ nodeset = 3,
+};
+
+inline const NodeType getNodeType(SEXP x) {
+ if (Rf_inherits(x, "xml_node")) {
+ return(NodeType::node);
+ } else if (Rf_inherits(x, "xml_nodeset")) {
+ return(NodeType::nodeset);
+ } else if (Rf_inherits(x, "xml_missing")) {
+ return(NodeType::missing);
+ } else {
+ Rf_error("Unexpected node type");
+ }
+}
+
inline const xmlChar* asXmlChar(std::string const& x) {
return (const xmlChar*) x.c_str();
}
diff --git a/tests/testthat/_snaps/xml_name.md b/tests/testthat/_snaps/xml_name.md
new file mode 100644
index 0000000..38ee479
--- /dev/null
+++ b/tests/testthat/_snaps/xml_name.md
@@ -0,0 +1,4 @@
+# error if missing ns spec
+
+ Couldn't find prefix for url http://bar.com
+
diff --git a/tests/testthat/test-xml_missing.R b/tests/testthat/test-xml_missing.R
index 66124d7..78a3a14 100644
--- a/tests/testthat/test-xml_missing.R
+++ b/tests/testthat/test-xml_missing.R
@@ -21,7 +21,7 @@ test_that("xml_missing methods return properly for all S3 methods", {
expect_equal(tree_structure(mss), NA_character_)
expect_error(write_xml(mss), "Missing data cannot be written")
expect_error(write_html(mss), "Missing data cannot be written")
- expect_equal(xml_attr(mss), NA_character_)
+ expect_equal(xml_attr(mss, "dummy_attr"), NA_character_)
expect_equal(xml_attrs(mss), NA_character_)
expect_equal(xml_find_all(mss), xml_nodeset())
expect_equal(xml_find_chr(mss), character())
@@ -33,7 +33,6 @@ test_that("xml_missing methods return properly for all S3 methods", {
expect_equal(xml_parent(mss), xml_missing())
expect_equal(xml_path(mss), NA_character_)
expect_equal(xml_text(mss), NA_character_)
- expect_equal(xml_type(mss), NA_character_)
expect_equal(xml_url(mss), NA_character_)
})
diff --git a/tests/testthat/test-xml_name.R b/tests/testthat/test-xml_name.R
index 94b3d28..869db26 100644
--- a/tests/testthat/test-xml_name.R
+++ b/tests/testthat/test-xml_name.R
@@ -1,3 +1,20 @@
+test_that("xml_name() returns the name", {
+ x <- read_xml("
+ Some text.
+ Some other.
+ No bold text
+ ")
+
+ children <- xml_children(x)
+ x <- xml_find_first(children, ".//b|.//i")
+
+ expect_equal(xml_name(x[[1]]), "b")
+ expect_equal(xml_name(x[[2]]), "i")
+ expect_equal(xml_name(x[[3]]), NA_character_)
+
+ expect_equal(xml_name(x), c("b", "i", NA_character_))
+})
+
test_that("qualified names returned when ns given", {
x <- read_xml(test_path("ns-multiple-default.xml"))
ns <- xml_ns(x)
@@ -12,7 +29,7 @@ test_that("error if missing ns spec", {
ns <- xml_ns(x)[1]
bars <- xml_children(xml_children(x))
- expect_error(xml_name(bars, ns), "Couldn't find prefix")
+ expect_snapshot_error(xml_name(bars, ns))
})
test_that("xml_name<- modifies the name", {
diff --git a/tests/testthat/test-xml_nodeset.R b/tests/testthat/test-xml_nodeset.R
index 81382fb..3a92c0b 100644
--- a/tests/testthat/test-xml_nodeset.R
+++ b/tests/testthat/test-xml_nodeset.R
@@ -67,7 +67,6 @@ test_that("methods work on empty nodesets", {
expect_output(xml_structure(empty), NA)
expect_identical(xml_text(empty), character())
- expect_identical(xml_type(empty), character())
expect_identical(xml_url(empty), character())
})
diff --git a/tests/testthat/test-xml_text.R b/tests/testthat/test-xml_text.R
index 56e8986..6b7af3b 100644
--- a/tests/testthat/test-xml_text.R
+++ b/tests/testthat/test-xml_text.R
@@ -6,22 +6,19 @@ test_that("xml_text returns only text without markup", {
expect_identical(xml_text(xml_children(x)), "bold!")
})
-test_that("xml_text returns only text without markup", {
- x <- read_xml("This is some text. This is bold!
")
-
- expect_identical(xml_text(x), "This is some text. This is bold!")
-
- expect_identical(xml_text(xml_children(x)), "bold!")
-})
-
test_that("xml_text works properly with xml_nodeset objects", {
- x <- read_xml("This is some text. This is some nested text.")
+ x <- read_xml("
+ Some text.
+ Some other.
+ No bold text
+ ")
- expect_identical(xml_text(x), "This is some text. This is some nested text.")
+ children <- xml_children(x)
+ x <- xml_find_first(children, ".//b|.//i")
expect_identical(
- xml_text(xml_find_all(x, "//x")),
- c("This is some text. This is some nested text.", "This is some nested text.")
+ xml_text(x),
+ c("text", "other", NA)
)
})
@@ -47,6 +44,12 @@ test_that("xml_text trims whitespace if requested, including non-breaking spaces
xml_text(x, trim = TRUE),
"Some text \u20ac"
)
+
+ x2 <- read_html(" Some text €
and more € text ")
+ expect_identical(
+ xml_text(xml_find_all(x2, ".//p"), trim = TRUE),
+ c("Some text \u20ac", "and more \u20ac text")
+ )
})
test_that("xml_integer() returns an integer vector", {
diff --git a/tests/testthat/test-xml_type.R b/tests/testthat/test-xml_type.R
new file mode 100644
index 0000000..eda8230
--- /dev/null
+++ b/tests/testthat/test-xml_type.R
@@ -0,0 +1,18 @@
+test_that("xml_type() works", {
+ x <- read_xml("
+ Some text.
+ Some other.
+ No bold text
+ ")
+
+ children <- xml_children(x)
+ x <- xml_find_first(children, ".//b|.//i")
+
+ expect_equal(xml_type(x[[1]]), "element")
+ expect_equal(xml_type(x[[3]]), NA_character_)
+
+ expect_equal(xml_type(x), c("element", "element", NA))
+
+ empty <- xml_children(x)
+ expect_identical(xml_type(empty), character())
+})