File: test-as_list.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 (41 lines) | stat: -rw-r--r-- 1,489 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
list_xml <- function(x) as_list(read_xml(x))

test_that("empty elements become empty lists", {
  expect_equal(list_xml("<x></x>"), list(x = list()))
  expect_equal(list_xml("<x><y/></x>"), list(x = list(y = list())))
  expect_equal(list_xml("<x><y><z/></y></x>"), list(x = list(y = list(z = list()))))
})

test_that("text nodes become character vectors", {
  expect_equal(list_xml("<x>a</x>"), list(x = list("a")))
  expect_equal(list_xml("<x><y>a</y></x>"), list(x = list(y = list("a"))))
})

test_that("cdata nodes become character vectors", {
  expect_equal(list_xml("<x><![CDATA[<y/>]]></x>"), list(x = list("<y/>")))
})

test_that("xml attributes become R attibutes", {
  expect_equal(list_xml("<x a='1' b='2'></x>"), list(x = structure(list(), a = "1", b = "2")))
})

test_that("xml names are preserved when attributes exist", {
  expect_equal(
    list_xml("<x a='1' b='2'><y>3</y><z>4</z></x>"),
    list(x = structure(list(y = list("3"), z = list("4")), a = "1", b = "2"))
  )
})

test_that("special attributes are escaped", {
  expect_equal(
    list_xml("<x a='1' b='2' names='esc'><y>3</y><z>4</z></x>"),
    list(x = structure(list(y = list("3"), z = list("4")), a = "1", b = "2", .names = "esc"))
  )
})

test_that("attributes in child nodes", {
  expect_equal(
    list_xml("<w aa = '0'><x a='1' b='2' names='esc'><y>3</y><z>4</z></x></w>"),
    list(w = structure(list(x = structure(list(y = list("3"), z = list("4")), a = "1", b = "2", .names = "esc")), aa = "0"))
  )
})