File: test-xml_write.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 (142 lines) | stat: -rw-r--r-- 3,903 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
test_that("write_xml errors for incorrect directory and with invalid inputs", {
  x <- read_xml("<x/>")
  filename <- "does_not_exist/test.xml"
  expect_error(write_xml(x, filename), "'does_not_exist' does not exist in current working directory")

  expect_snapshot(error = TRUE, {
    write_xml(x, c("test.xml", "foo"))
  })
})

test_that("write_xml works with relative file paths", {
  x <- read_xml("<x/>")

  filename <- "../test.xml"
  on.exit(unlink(filename))
  write_xml(x, filename, options = "no_declaration")
  expect_identical(readChar(filename, 1000L), "<x/>\n")
})

test_that("write_xml works with no options", {
  x <- read_xml("<x/>")

  filename <- "../test.xml"
  on.exit(unlink(filename))
  write_xml(x, filename, options = NULL)
  expect_identical(readChar(filename, 1000L), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<x/>\n")
})

test_that("write_xml works with an explicit connections", {
  x <- read_xml("<x/>")

  filename <- "../test.xml"
  file <- file(filename, "wb")
  on.exit(unlink(filename))
  write_xml(x, file, options = "no_declaration")
  close(file)
  expect_identical(readChar(filename, 1000L), "<x/>\n")
})

test_that("write_xml works with an implicit connections", {
  x <- read_xml("<x/>")

  filename <- "../test.xml.gz"
  write_xml(x, filename, options = "no_declaration")
  file <- gzfile(filename, "rb")
  on.exit({
    unlink(filename)
    close(file)
  })
  expect_identical(readChar(file, 1000L), "<x/>\n")
})

test_that("write_xml works with nodeset input and files", {
  x <- read_xml("<x><y/><y><z/></y></x>")
  y <- xml_find_all(x, "//y")

  filename <- "../test.xml"
  on.exit(unlink(filename))
  expect_error(
    write_xml(y, filename, options = "no_declaration"),
    "Can only save length 1 node sets"
  )

  write_xml(y[1], filename, options = "no_declaration")
  expect_identical(readChar(filename, 1000L), "<y/>")
})

test_that("write_xml works with nodeset input and connections", {
  x <- read_xml("<x><y/><y/></x>")
  y <- xml_find_all(x, "//y")

  filename <- "../test.xml.gz"
  expect_error(
    write_xml(y, filename, options = "no_declaration"),
    "Can only save length 1 node sets"
  )

  expect_snapshot(error = TRUE, {
    write_xml(y[1], c(filename, "foo"))
  })

  write_xml(y[1], filename, options = "no_declaration")
  file <- gzfile(filename, "rb")
  on.exit({
    unlink(filename)
    close(file)
  })
  expect_identical(readChar(file, 1000L), "<y/>")
})

test_that("write_xml works with node input and files", {
  x <- read_xml("<x><y/><y/></x>")
  y <- xml_find_first(x, "//y")

  filename <- "../test.xml"
  expect_snapshot(error = TRUE, write_xml(y, c(filename, "foo")))

  write_xml(y, filename, options = "no_declaration")
  on.exit(unlink(filename))
  expect_identical(readChar(filename, 1000L), "<y/>")
})

test_that("write_xml works with node input and connections", {
  x <- read_xml("<x><y/><y/></x>")
  y <- xml_find_first(x, "//y")

  filename <- "../test.xml.gz"
  write_xml(y, filename, options = "no_declaration")
  file <- gzfile(filename, "rb")
  on.exit({
    unlink(filename)
    close(file)
  })
  expect_identical(readChar(file, 1000L), "<y/>")
})

test_that("write_html work with html input", {
  x <- read_html("<html><title>Foo</title></html>")

  filename <- "../test.html.gz"
  write_html(x, filename)
  file <- gzfile(filename, "rb")
  on.exit({
    unlink(filename)
    close(file)
  })
  expect_identical(
    readChar(file, 1000L),
    "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n<html><head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n<title>Foo</title>\n</head></html>\n"
  )
})

test_that("write_xml returns invisibly", {
  x <- read_xml("<x>foo</x>")
  tf <- tempfile()
  on.exit(unlink(tf))

  res <- withVisible(write_xml(x, tf))

  expect_null(res$value)
  expect_false(res$visible)
})