File: test-xml_text.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 (69 lines) | stat: -rw-r--r-- 2,060 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
test_that("xml_text returns only text without markup", {
  x <- read_xml("<p>This is some text. This is <b>bold!</b></p>")

  expect_identical(xml_text(x), "This is some text. This is bold!")

  expect_identical(xml_text(xml_children(x)), "bold!")
})

test_that("xml_text works properly with xml_nodeset objects", {
  x <- read_xml("<body>
    <p>Some <b>text</b>.</p>
    <p>Some <i>other</i>.</p>
    <p>No bold text</p>
    </body>")

  children <- xml_children(x)
  x <- xml_find_first(children, ".//b|.//i")

  expect_identical(
    xml_text(x),
    c("text", "other", NA)
  )
})

test_that("xml_text<- and xml_set_text work properly with xml_nodeset objects", {
  x <- read_xml("<x>This is some text. <x>This is some nested text.</x></x>")

  expect_identical(xml_text(x), "This is some text. This is some nested text.")

  xml_text(x) <- "test"
  expect_identical(xml_text(x), "testThis is some nested text.")
  xml_set_text(x, "test2")
  expect_identical(xml_text(x), "test2This is some nested text.")
})

test_that("xml_text trims whitespace if requested, including non-breaking spaces", {
  x <- read_html("<p>   Some text &euro;  &nbsp;</p>")
  expect_identical(
    xml_text(x),
    "   Some text \u20ac  \u00a0"
  )

  expect_identical(
    xml_text(x, trim = TRUE),
    "Some text \u20ac"
  )

  x2 <- read_html("<body><p>   Some text &euro;  &nbsp;</p><p>  and more &euro; text   &nbsp;</body>")
  expect_identical(
    xml_text(xml_find_all(x2, ".//p"), trim = TRUE),
    c("Some text \u20ac", "and more \u20ac text")
  )
})

test_that("xml_integer() returns an integer vector", {
  x <- read_xml("<plot><point x='1' y='2' /><point x='2' y='1' /></plot>")

  expect_identical(
    xml_integer(xml_find_all(x, "//@x")),
    c(1L, 2L)
  )
})


test_that("xml_double() returns a numeric vector", {
  x <- read_xml("<earth><point latitude = '42.3466456' longitude = '-71.0390351' /><point latitude = '-36.8523378' longitude = '174.7691073' /></earth>")

  expect_identical(xml_double(xml_find_all(x, "//@latitude")), c(42.3466456, -36.8523378))
})