Skip to content

Commit

Permalink
Add xml_serialize()/xml_unserialize() tests for HTML-based documents …
Browse files Browse the repository at this point in the history
…+ NEWS entry [#407]
  • Loading branch information
HenrikBengtsson committed Nov 8, 2023
1 parent 7ae7ad0 commit ffb1426
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# xml2 (development version)

* `xml_serialize()` now includes also the document type so that `xml_unserialize()` works also for HTML documents (#407).


# xml2 1.3.5

* Small speedup for `xml_find_all()` (@mgirlich, #393).
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/test-xml_serialize.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ x <- read_xml("<a>
<b><c>123</c></b>
<b><c>456</c></b>
</a>")

test_that("xml_serialize and xml_unserialize work with xml_document input", {
out <- xml_unserialize(xml_serialize(x, NULL))
expect_identical(as.character(x), as.character(out))
Expand Down Expand Up @@ -37,6 +38,20 @@ test_that("xml_serialize and xml_unserialize work with xml_nodeset input", {
expect_identical(as.character(xml_unserialize(f)), as.character(b))
})

test_that("xml_serialize and xml_unserialize work with HTML-based xml_document input", {
file <- system.file("extdata", "r-project.html", package = "xml2")
x <- read_html(file)

out <- xml_unserialize(xml_serialize(x, NULL))
expect_identical(as.character(x), as.character(out))

f <- tempfile()
on.exit(unlink(f))

xml_serialize(x, f)
expect_identical(as.character(xml_unserialize(f)), as.character(x))
})

test_that("xml_unserialize throws an error if given a invalid object", {
expect_error(xml_unserialize(serialize(1, NULL)), "Not a serialized xml2 object")
})

0 comments on commit ffb1426

Please sign in to comment.