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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
test_that("modifying nodes works", {
x <- read_xml("<x><y/></x>")
node <- xml_find_first(x, "//x")
expect_equal(xml_name(node), "x")
.Call(node_set_name, node$node, "y")
expect_equal(xml_name(node), "y")
expect_equal(xml_text(node), "")
.Call(node_set_content, node$node, "test")
expect_equal(xml_text(node), "test")
})
test_that("xml_text<- only modifies text content", {
x <- read_xml("<node>Text1<subnode/>text2</node>")
expect_equal(xml_text(x), "Text1text2")
# will only change the first text by default
xml_text(x) <- "new_text1"
expect_equal(xml_text(x), "new_text1text2")
# You can change the second by explicitly selecting it
text_node <- xml_find_first(x, "//text()[2]")
xml_text(text_node) <- "new_text2"
expect_equal(xml_text(x), "new_text1new_text2")
})
test_that("xml_text<- creates new text nodes if needed", {
x <- read_xml("<node><subnode/></node>")
xml_text(x) <- "test"
expect_equal(xml_text(x), "test")
})
test_that("xml_remove removes nodes", {
x <- read_xml("<parent><child>1</child><child>2<child>3</child></child></parent>")
children <- xml_children(x)
t1 <- children[[1]]
xml_remove(children, free = TRUE)
expect_equal(xml_text(x), "")
})
test_that("xml_replace replaces nodes", {
x <- read_xml("<parent><child>1</child><child>2<child>3</child></child></parent>")
children <- xml_children(x)
t1 <- children[[1]]
t2 <- children[[2]]
t3 <- xml_children(children[[2]])[[1]]
expect_equal(xml_text(x), "123")
xml_replace(t1, t3)
expect_equal(xml_text(x), "323")
first_child <- xml_children(x)[[1]]
xml_replace(first_child, t1, .copy = FALSE)
expect_equal(xml_text(x), "123")
xml_remove(first_child, free = TRUE)
first_child <- xml_children(x)[[1]]
xml_replace(first_child, t3, .copy = FALSE)
expect_equal(xml_text(x), "32")
xml_remove(first_child, free = TRUE)
})
test_that("xml_replace works with nodesets", {
x <- read_xml("<parent><child>1</child><child>2<child>3</child></child></parent>")
children <- xml_children(x)
t1 <- children[[1]]
xml_replace(children, t1)
expect_equal(xml_text(x), "11")
})
test_that("xml_sibling adds a sibling node", {
x <- read_xml("<parent><child>1</child><child>2<child>3</child></child></parent>")
children <- xml_children(x)
t1 <- children[[1]]
t2 <- children[[2]]
t3 <- xml_children(children[[2]])[[1]]
xml_add_sibling(t1, t3)
expect_length(xml_siblings(t1), 2)
expect_equal(xml_text(x), "1323")
xml_add_sibling(t1, t3, .where = "before")
expect_length(xml_siblings(t1), 3)
expect_equal(xml_text(x), "31323")
children <- xml_children(x)
xml_add_sibling(children, t1)
expect_equal(xml_text(x), "311131231")
})
test_that("xml_add_child adds a child node", {
x <- read_xml("<parent><child>1</child><child>2<child>3</child></child></parent>")
children <- xml_children(x)
t1 <- children[[1]]
t2 <- children[[2]]
t3 <- xml_children(children[[2]])[[1]]
expect_length(xml_children(t1), 0)
xml_add_child(t1, t3, .copy = TRUE)
expect_length(xml_children(t1), 1)
expect_equal(xml_text(x), "1323")
children <- xml_children(x)
xml_add_child(children, t1)
expect_equal(xml_text(x), "1313231313")
})
test_that("xml_add_child can create a new default namespace", {
x <- xml_root(xml_add_child(xml_new_document(), "foo", xmlns = "bar"))
expect_equal(unclass(xml_ns(x)), c(d1 = "bar"))
})
test_that("xml_add_child can create a new prefixed namespace", {
x <- xml_root(xml_add_child(xml_new_document(), "foo", "xmlns:bar" = "baz"))
expect_equal(unclass(xml_ns(x)), c(bar = "baz"))
})
test_that("xml_add_child can create a new attribute", {
x <- xml_add_child(xml_new_document(), "foo", "bar" = "baz")
expect_equal(xml_attr(x, "bar"), "baz")
})
test_that("xml_add_child can create new text", {
x <- xml_add_child(xml_new_document(), "foo", "bar")
expect_equal(xml_text(x), "bar")
})
test_that("xml_add_child can create a new node with the specified prefix", {
x <- xml_root(xml_add_child(xml_new_document(), "foo", "xmlns:bar" = "baz"))
t1 <- xml_add_child(x, "bar:qux")
expect_equal(xml_name(t1), "qux")
expect_equal(xml_name(t1, xml_ns(x)), "bar:qux")
})
test_that("xml_add_child can create a new node with the specified prefix", {
x <- xml_root(xml_add_child(xml_new_document(), "foo", "xmlns:bar" = "baz"))
expect_error(xml_add_child(x, "bar2:qux"), "No namespace with prefix `bar2` found")
})
# https://github.com/r-lib/xml2/issues/284
test_that("xml_add_child can create a new node with attribute named 'par'", {
x <- xml_new_root("foo")
t1 <- xml_add_child(x, "a", "par" = "b")
expect_true(xml_has_attr(t1, "par"))
})
test_that("xml_add_parent works with xml_node input", {
x <- read_xml("<x><y/></x>")
y <- xml_find_first(x, ".//y")
xml_add_parent(y, "z")
expect_equal(xml_name(xml_parent(y)), "z")
expect_equal(xml_name(xml_child(x)), "z")
})
test_that("xml_add_parent works with xml_nodeset input", {
x <- read_xml("<x><y/><y/></x>")
y <- xml_find_all(x, ".//y")
xml_add_parent(y, "z")
expect_equal(xml_name(xml_parent(y)), c("z", "z"))
expect_equal(xml_name(xml_child(x)), "z")
})
test_that("xml_add_parent works with xml_missing input", {
x <- read_xml("<body>
<p>Some <b>text</b>.</p>
<p>Some <b>other</b>.</p>
<p>No bold text</p>
</body>")
y <- xml_find_all(x, ".//p")
z <- xml_find_first(y, ".//b")
xml_add_parent(z, "em")
expect_equal(xml_name(xml_parent(z)), c("em", "em"))
expect_equal(xml_name(xml_children(y)), c("em", "em"))
})
test_that("xml_new_document adds a default character encoding", {
x <- read_xml("<root>\u00E1\u00FC\u00EE</root>")
expect_equal(as.character(x), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root>\u00E1\u00FC\u00EE</root>\n")
x2 <- xml_new_document()
xml_add_child(x2, "root", "\u00E1\u00FC\u00EE")
expect_equal(as.character(x2), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root>\u00E1\u00FC\u00EE</root>\n")
})
test_that("xml_new_root is equivalent to using xml_new_document xml_add_child", {
x1 <- xml_add_child(xml_new_document(), "foo", "bar")
x2 <- xml_new_root("foo", "bar")
expect_identical(as.character(x1), as.character(x2))
})
test_that("xml_add_child can insert anywhere in the child list", {
x <- read_xml("<a/>")
xml_add_child(x, "z")
expect_equal(c("z"), xml_name(xml_children(x)))
xml_add_child(x, "w", .where = 0)
expect_equal(c("w", "z"), xml_name(xml_children(x)))
xml_add_child(x, "y", .where = 1)
expect_equal(c("w", "y", "z"), xml_name(xml_children(x)))
xml_add_child(x, "x", .where = 1)
expect_equal(c("w", "x", "y", "z"), xml_name(xml_children(x)))
})
test_that("xml_add_child can insert anywhere in a nodeset", {
x <- read_xml("<body>
<p>Some <b>text</b>.</p>
<p>Some <b>other</b>.</p>
<p>No bold text</p>
</body>")
y <- xml_find_all(x, ".//p")
z <- xml_find_first(y, ".//b")
xml_add_child(z, "bar")
xml_add_child(z, "foo", .where = 0)
expect_equal(c("foo", "bar", "foo", "bar"), xml_name(xml_children(z)))
})
test_that("Can write root nodes with namespaces", {
x <- xml_new_root("foo:bar", "xmlns:foo" = "http://foo/bar")
expect_equal(unclass(xml_ns(x)), c(foo = "http://foo/bar"))
expect_equal(as.character(xml_find_first(x, "/*")), "<foo:bar xmlns:foo=\"http://foo/bar\"/>")
})
|