Skip to content

Commit 9900058

Browse files
authored
Use expect_snapshot(error = TRUE) over expect_snapshot_error() (#429)
Also add error to snapshots.
1 parent d440d45 commit 9900058

File tree

12 files changed

+122
-23
lines changed

12 files changed

+122
-23
lines changed

tests/testthat/_snaps/xml_attr.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# xml_attrs<- modifies all attributes
22

3-
`test` must be a list of named character vectors.
3+
Code
4+
xml_attrs(docs) <- "test"
5+
Condition
6+
Error in `xml_attrs<-`:
7+
! `test` must be a list of named character vectors.
48

tests/testthat/_snaps/xml_children.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# xml_child() errors if more than one search is given
22

3-
`1` and `2` must be of length 1.
3+
Code
4+
xml_child(x, 1:2)
5+
Condition
6+
Error in `xml_child()`:
7+
! `1` and `2` must be of length 1.
48

tests/testthat/_snaps/xml_find.md

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
# xml_find_num errors with non numeric results
22

3-
Element at path `//z` must be a number, not a <xml_missing> object.
3+
Code
4+
xml_find_num(x, "//z")
5+
Condition
6+
Error in `xml_find_num()`:
7+
! Element at path `//z` must be a number, not a <xml_missing> object.
8+
Code
9+
xml_find_num(x, "//y")
10+
Condition
11+
Error in `xml_find_num()`:
12+
! Element at path `//y` must be a number, not a list.
13+
Code
14+
xml_find_num(x, "1=1")
15+
Condition
16+
Error in `xml_find_num()`:
17+
! Element at path `1=1` must be a number, not `TRUE`.
18+
Code
19+
xml_find_num(x, "string(5)")
20+
Condition
21+
Error in `xml_find_num()`:
22+
! Element at path `string(5)` must be a number, not the string "5".
423

524
# xml_find_int errors with non integer results
625

@@ -22,9 +41,47 @@
2241

2342
# xml_find_chr errors with non character results
2443

25-
Element at path `//z` must be a single string, not a <xml_missing> object.
44+
Code
45+
xml_find_chr(x, "//z")
46+
Condition
47+
Error in `xml_find_chr()`:
48+
! Element at path `//z` must be a single string, not a <xml_missing> object.
49+
Code
50+
xml_find_chr(x, "//y")
51+
Condition
52+
Error in `xml_find_chr()`:
53+
! Element at path `//y` must be a single string, not a list.
54+
Code
55+
xml_find_chr(x, "1=1")
56+
Condition
57+
Error in `xml_find_chr()`:
58+
! Element at path `1=1` must be a single string, not `TRUE`.
59+
Code
60+
xml_find_chr(x, "1+1")
61+
Condition
62+
Error in `xml_find_chr()`:
63+
! Element at path `1+1` must be a single string, not the number 2.
2664

2765
# xml_find_lgl errors with non logical results
2866

29-
Element at path `//z` must be `TRUE` or `FALSE`, not a <xml_missing> object.
67+
Code
68+
xml_find_lgl(x, "//z")
69+
Condition
70+
Error in `xml_find_lgl()`:
71+
! Element at path `//z` must be `TRUE` or `FALSE`, not a <xml_missing> object.
72+
Code
73+
xml_find_lgl(x, "//y")
74+
Condition
75+
Error in `xml_find_lgl()`:
76+
! Element at path `//y` must be `TRUE` or `FALSE`, not a list.
77+
Code
78+
xml_find_lgl(x, "string(5)")
79+
Condition
80+
Error in `xml_find_lgl()`:
81+
! Element at path `string(5)` must be `TRUE` or `FALSE`, not the string "5".
82+
Code
83+
xml_find_lgl(x, "1+1")
84+
Condition
85+
Error in `xml_find_lgl()`:
86+
! Element at path `1+1` must be `TRUE` or `FALSE`, not the number 2.
3087

tests/testthat/_snaps/xml_name.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# error if missing ns spec
22

3-
Couldn't find prefix for url http://bar.com
3+
Code
4+
xml_name(bars, ns)
5+
Condition
6+
Error in `xml_name()`:
7+
! Couldn't find prefix for url http://bar.com
48

tests/testthat/_snaps/xml_parse.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# read_xml errors with an empty document
22

3-
`x` must be a single string, not an empty character vector.
3+
Code
4+
read_xml(character())
5+
Condition
6+
Error in `read_xml()`:
7+
! `x` must be a single string, not an empty character vector.
48

59
# parse_options errors when given an invalid option
610

@@ -14,5 +18,14 @@
1418

1519
# read_xml and read_html fail with > 1 input
1620

17-
`x` must be a single string, not a character vector.
21+
Code
22+
read_xml(c("foo", "bar"))
23+
Condition
24+
Error in `read_xml()`:
25+
! `x` must be a single string, not a character vector.
26+
Code
27+
read_html(c("foo", "bar"))
28+
Condition
29+
Error in `read_xml()`:
30+
! `x` must be a single string, not a character vector.
1831

tests/testthat/_snaps/xml_write.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
# write_xml errors for incorrect directory and with invalid inputs
22

3-
`file` must be a single string, not a character vector.
3+
Code
4+
write_xml(x, c("test.xml", "foo"))
5+
Condition
6+
Error in `write_xml()`:
7+
! `file` must be a single string, not a character vector.
48

59
# write_xml works with nodeset input and connections
610

7-
`file` must be a single string, not a character vector.
11+
Code
12+
write_xml(y[1], c(filename, "foo"))
13+
Condition
14+
Error in `write_xml()`:
15+
! `file` must be a single string, not a character vector.
816

917
# write_xml works with node input and files
1018

11-
`file` must be a single string, not a character vector.
19+
Code
20+
write_xml(y, c(filename, "foo"))
21+
Condition
22+
Error in `write_xml()`:
23+
! `file` must be a single string, not a character vector.
1224

tests/testthat/test-xml_attr.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ test_that("xml_attrs<- modifies all attributes", {
100100
xml_attrs(doc, ns) <- c("b:id" = "b", "id" = "test")
101101
expect_equal(xml_attrs(doc, ns), c("b:id" = "b", "id" = "test"))
102102

103-
expect_snapshot_error(xml_attrs(docs) <- "test")
103+
expect_snapshot(error = TRUE, xml_attrs(docs) <- "test")
104104

105105
xml_attrs(docs, ns) <- c("b:id" = "b", "id" = "test")
106106
expect_equal(

tests/testthat/test-xml_children.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ test_that("xml_child() returns child by name", {
1111
})
1212

1313
test_that("xml_child() errors if more than one search is given", {
14-
expect_snapshot_error(xml_child(x, 1:2))
14+
expect_snapshot(error = TRUE, xml_child(x, 1:2))
1515
})
1616

1717
test_that("xml_child() errors if search is not numeric or character", {

tests/testthat/test-xml_find.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ test_that("xml_find_all returns nodeset or list of nodesets based on flatten", {
6262
# Find num ---------------------------------------------------------------------
6363
test_that("xml_find_num errors with non numeric results", {
6464
x <- read_xml("<x><y/><y/></x>")
65-
expect_snapshot_error({
65+
expect_snapshot(error = TRUE, {
6666
xml_find_num(x, "//z")
6767
xml_find_num(x, "//y")
6868
xml_find_num(x, "1=1")
@@ -106,7 +106,7 @@ test_that("xml_find_int returns a integer result", {
106106
# Find chr ---------------------------------------------------------------------
107107
test_that("xml_find_chr errors with non character results", {
108108
x <- read_xml("<x><y/><y/></x>")
109-
expect_snapshot_error({
109+
expect_snapshot(error = TRUE, {
110110
xml_find_chr(x, "//z")
111111
xml_find_chr(x, "//y")
112112
xml_find_chr(x, "1=1")
@@ -134,7 +134,7 @@ test_that("xml_find_chr returns a character result", {
134134
# Find lgl ---------------------------------------------------------------------
135135
test_that("xml_find_lgl errors with non logical results", {
136136
x <- read_xml("<x><y/><y/></x>")
137-
expect_snapshot_error({
137+
expect_snapshot(error = TRUE, {
138138
xml_find_lgl(x, "//z")
139139
xml_find_lgl(x, "//y")
140140
xml_find_lgl(x, "string(5)")

tests/testthat/test-xml_name.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ test_that("error if missing ns spec", {
2929
ns <- xml_ns(x)[1]
3030

3131
bars <- xml_children(xml_children(x))
32-
expect_snapshot_error(xml_name(bars, ns))
32+
expect_snapshot(error = TRUE, xml_name(bars, ns))
3333
})
3434

3535
test_that("xml_name<- modifies the name", {

tests/testthat/test-xml_parse.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ test_that("download_xml fails if curl is not installed", {
99
})
1010

1111
test_that("read_xml errors with an empty document", {
12-
expect_snapshot_error({read_xml(character())})
12+
expect_snapshot(error = TRUE, {
13+
read_xml(character())
14+
})
1315

1416
tf <- tempfile()
1517
file.create(tf)
@@ -104,7 +106,7 @@ test_that("read_html works with non-ASCII encodings", {
104106
})
105107

106108
test_that("read_xml and read_html fail with > 1 input", {
107-
expect_snapshot_error({
109+
expect_snapshot(error = TRUE, {
108110
read_xml(c("foo", "bar"))
109111
read_html(c("foo", "bar"))
110112
})

tests/testthat/test-xml_write.R

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ test_that("write_xml errors for incorrect directory and with invalid inputs", {
33
filename <- "does_not_exist/test.xml"
44
expect_error(write_xml(x, filename), "'does_not_exist' does not exist in current working directory")
55

6-
7-
expect_snapshot_error(write_xml(x, c("test.xml", "foo")))
6+
expect_snapshot(error = TRUE, {
7+
write_xml(x, c("test.xml", "foo"))
8+
})
89
})
910

1011
test_that("write_xml works with relative file paths", {
@@ -74,7 +75,9 @@ test_that("write_xml works with nodeset input and connections", {
7475
"Can only save length 1 node sets"
7576
)
7677

77-
expect_snapshot_error(write_xml(y[1], c(filename, "foo")))
78+
expect_snapshot(error = TRUE, {
79+
write_xml(y[1], c(filename, "foo"))
80+
})
7881

7982
write_xml(y[1], filename, options = "no_declaration")
8083
file <- gzfile(filename, "rb")
@@ -90,7 +93,7 @@ test_that("write_xml works with node input and files", {
9093
y <- xml_find_first(x, "//y")
9194

9295
filename <- "../test.xml"
93-
expect_snapshot_error(write_xml(y, c(filename, "foo")))
96+
expect_snapshot(error = TRUE, write_xml(y, c(filename, "foo")))
9497

9598
write_xml(y, filename, options = "no_declaration")
9699
on.exit(unlink(filename))

0 commit comments

Comments
 (0)