File: test-as_xml_document.R

package info (click to toggle)
r-cran-xml2 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 976 kB
  • sloc: cpp: 1,826; xml: 333; javascript: 238; ansic: 178; sh: 71; makefile: 6
file content (43 lines) | stat: -rw-r--r-- 1,217 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
roundtrip_xml <- function(x) {
  xml <- read_xml(x)
  lst <- as_list(xml)
  xml2 <- as_xml_document(lst)
  expect_equal(as.character(xml), as.character(xml2))
}

test_that("roundtrips with single children", {
  roundtrip_xml("<a><b/></a>")

  roundtrip_xml("<a><b><c/></b></a>")

  roundtrip_xml("<a><b>foo<c/></b></a>")

  roundtrip_xml("<a><b>foo<c>bar</c></b></a>")

  roundtrip_xml("<a x = '1'><b y = '2'>foo<c z = '3'>bar</c></b></a>")
})

test_that("roundtrips with multi children", {
  roundtrip_xml("<a><b1/><b2/></a>")

  roundtrip_xml("<a><b><c1/><c2/></b></a>")

  roundtrip_xml("<a><b1>foo<c/></b1><b2>bar<c/></b2></a>")

  roundtrip_xml("<a><b>foo<c>bar</c><c>baz</c></b></a>")

  roundtrip_xml("<a x = '1'><b y = '2'>foo<c z = '3'>bar</c></b></a>")
  roundtrip_xml("<a x = '1'><b y = '2'>foo<c z = '3'>bar</c></b><c zz = '4'>baz</c></a>")
})

test_that("rountrips with special attributes", {
  roundtrip_xml("<a names = 'test'><b/></a>")
})

test_that("more than one root node is an error", {
  expect_error(as_xml_document(list(a = list(), b = list())), "Root nodes must be of length 1")
})

test_that("Can convert nodes with leading and trailing text", {
  roundtrip_xml("<a>foo<b>bar</b>baz</a>")
})