Skip to content

Commit

Permalink
Use expect_snapshot(error = TRUE) over expect_snapshot_error() (#429
Browse files Browse the repository at this point in the history
)

Also add error to snapshots.
  • Loading branch information
olivroy authored Oct 21, 2024
1 parent d440d45 commit 9900058
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 23 deletions.
6 changes: 5 additions & 1 deletion tests/testthat/_snaps/xml_attr.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# xml_attrs<- modifies all attributes

`test` must be a list of named character vectors.
Code
xml_attrs(docs) <- "test"
Condition
Error in `xml_attrs<-`:
! `test` must be a list of named character vectors.

6 changes: 5 additions & 1 deletion tests/testthat/_snaps/xml_children.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# xml_child() errors if more than one search is given

`1` and `2` must be of length 1.
Code
xml_child(x, 1:2)
Condition
Error in `xml_child()`:
! `1` and `2` must be of length 1.

63 changes: 60 additions & 3 deletions tests/testthat/_snaps/xml_find.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
# xml_find_num errors with non numeric results

Element at path `//z` must be a number, not a <xml_missing> object.
Code
xml_find_num(x, "//z")
Condition
Error in `xml_find_num()`:
! Element at path `//z` must be a number, not a <xml_missing> object.
Code
xml_find_num(x, "//y")
Condition
Error in `xml_find_num()`:
! Element at path `//y` must be a number, not a list.
Code
xml_find_num(x, "1=1")
Condition
Error in `xml_find_num()`:
! Element at path `1=1` must be a number, not `TRUE`.
Code
xml_find_num(x, "string(5)")
Condition
Error in `xml_find_num()`:
! Element at path `string(5)` must be a number, not the string "5".

# xml_find_int errors with non integer results

Expand All @@ -22,9 +41,47 @@

# xml_find_chr errors with non character results

Element at path `//z` must be a single string, not a <xml_missing> object.
Code
xml_find_chr(x, "//z")
Condition
Error in `xml_find_chr()`:
! Element at path `//z` must be a single string, not a <xml_missing> object.
Code
xml_find_chr(x, "//y")
Condition
Error in `xml_find_chr()`:
! Element at path `//y` must be a single string, not a list.
Code
xml_find_chr(x, "1=1")
Condition
Error in `xml_find_chr()`:
! Element at path `1=1` must be a single string, not `TRUE`.
Code
xml_find_chr(x, "1+1")
Condition
Error in `xml_find_chr()`:
! Element at path `1+1` must be a single string, not the number 2.

# xml_find_lgl errors with non logical results

Element at path `//z` must be `TRUE` or `FALSE`, not a <xml_missing> object.
Code
xml_find_lgl(x, "//z")
Condition
Error in `xml_find_lgl()`:
! Element at path `//z` must be `TRUE` or `FALSE`, not a <xml_missing> object.
Code
xml_find_lgl(x, "//y")
Condition
Error in `xml_find_lgl()`:
! Element at path `//y` must be `TRUE` or `FALSE`, not a list.
Code
xml_find_lgl(x, "string(5)")
Condition
Error in `xml_find_lgl()`:
! Element at path `string(5)` must be `TRUE` or `FALSE`, not the string "5".
Code
xml_find_lgl(x, "1+1")
Condition
Error in `xml_find_lgl()`:
! Element at path `1+1` must be `TRUE` or `FALSE`, not the number 2.

6 changes: 5 additions & 1 deletion tests/testthat/_snaps/xml_name.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# error if missing ns spec

Couldn't find prefix for url http://bar.com
Code
xml_name(bars, ns)
Condition
Error in `xml_name()`:
! Couldn't find prefix for url http://bar.com

17 changes: 15 additions & 2 deletions tests/testthat/_snaps/xml_parse.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# read_xml errors with an empty document

`x` must be a single string, not an empty character vector.
Code
read_xml(character())
Condition
Error in `read_xml()`:
! `x` must be a single string, not an empty character vector.

# parse_options errors when given an invalid option

Expand All @@ -14,5 +18,14 @@

# read_xml and read_html fail with > 1 input

`x` must be a single string, not a character vector.
Code
read_xml(c("foo", "bar"))
Condition
Error in `read_xml()`:
! `x` must be a single string, not a character vector.
Code
read_html(c("foo", "bar"))
Condition
Error in `read_xml()`:
! `x` must be a single string, not a character vector.

18 changes: 15 additions & 3 deletions tests/testthat/_snaps/xml_write.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
# write_xml errors for incorrect directory and with invalid inputs

`file` must be a single string, not a character vector.
Code
write_xml(x, c("test.xml", "foo"))
Condition
Error in `write_xml()`:
! `file` must be a single string, not a character vector.

# write_xml works with nodeset input and connections

`file` must be a single string, not a character vector.
Code
write_xml(y[1], c(filename, "foo"))
Condition
Error in `write_xml()`:
! `file` must be a single string, not a character vector.

# write_xml works with node input and files

`file` must be a single string, not a character vector.
Code
write_xml(y, c(filename, "foo"))
Condition
Error in `write_xml()`:
! `file` must be a single string, not a character vector.

2 changes: 1 addition & 1 deletion tests/testthat/test-xml_attr.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ test_that("xml_attrs<- modifies all attributes", {
xml_attrs(doc, ns) <- c("b:id" = "b", "id" = "test")
expect_equal(xml_attrs(doc, ns), c("b:id" = "b", "id" = "test"))

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

xml_attrs(docs, ns) <- c("b:id" = "b", "id" = "test")
expect_equal(
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-xml_children.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test_that("xml_child() returns child by name", {
})

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

test_that("xml_child() errors if search is not numeric or character", {
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-xml_find.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ test_that("xml_find_all returns nodeset or list of nodesets based on flatten", {
# Find num ---------------------------------------------------------------------
test_that("xml_find_num errors with non numeric results", {
x <- read_xml("<x><y/><y/></x>")
expect_snapshot_error({
expect_snapshot(error = TRUE, {
xml_find_num(x, "//z")
xml_find_num(x, "//y")
xml_find_num(x, "1=1")
Expand Down Expand Up @@ -106,7 +106,7 @@ test_that("xml_find_int returns a integer result", {
# Find chr ---------------------------------------------------------------------
test_that("xml_find_chr errors with non character results", {
x <- read_xml("<x><y/><y/></x>")
expect_snapshot_error({
expect_snapshot(error = TRUE, {
xml_find_chr(x, "//z")
xml_find_chr(x, "//y")
xml_find_chr(x, "1=1")
Expand Down Expand Up @@ -134,7 +134,7 @@ test_that("xml_find_chr returns a character result", {
# Find lgl ---------------------------------------------------------------------
test_that("xml_find_lgl errors with non logical results", {
x <- read_xml("<x><y/><y/></x>")
expect_snapshot_error({
expect_snapshot(error = TRUE, {
xml_find_lgl(x, "//z")
xml_find_lgl(x, "//y")
xml_find_lgl(x, "string(5)")
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-xml_name.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ test_that("error if missing ns spec", {
ns <- xml_ns(x)[1]

bars <- xml_children(xml_children(x))
expect_snapshot_error(xml_name(bars, ns))
expect_snapshot(error = TRUE, xml_name(bars, ns))
})

test_that("xml_name<- modifies the name", {
Expand Down
6 changes: 4 additions & 2 deletions tests/testthat/test-xml_parse.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ test_that("download_xml fails if curl is not installed", {
})

test_that("read_xml errors with an empty document", {
expect_snapshot_error({read_xml(character())})
expect_snapshot(error = TRUE, {
read_xml(character())
})

tf <- tempfile()
file.create(tf)
Expand Down Expand Up @@ -104,7 +106,7 @@ test_that("read_html works with non-ASCII encodings", {
})

test_that("read_xml and read_html fail with > 1 input", {
expect_snapshot_error({
expect_snapshot(error = TRUE, {
read_xml(c("foo", "bar"))
read_html(c("foo", "bar"))
})
Expand Down
11 changes: 7 additions & 4 deletions tests/testthat/test-xml_write.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ test_that("write_xml errors for incorrect directory and with invalid inputs", {
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(write_xml(x, c("test.xml", "foo")))
expect_snapshot(error = TRUE, {
write_xml(x, c("test.xml", "foo"))
})
})

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

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

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

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

write_xml(y, filename, options = "no_declaration")
on.exit(unlink(filename))
Expand Down

0 comments on commit 9900058

Please sign in to comment.