Skip to content

Commit

Permalink
Convert expect_error() to expect_snapshot()
Browse files Browse the repository at this point in the history
  • Loading branch information
KimLopezGuell committed Aug 15, 2024
1 parent 83a72a1 commit a7c8630
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 5 deletions.
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/conv.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# check encoding argument

Code
str_conv("A", c("ISO-8859-1", "ISO-8859-2"))
Condition
Error in `str_conv()`:
! `encoding` must be a single string, not a character vector.

8 changes: 8 additions & 0 deletions tests/testthat/_snaps/equal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# vectorised using TRR

Code
str_equal(letters[1:3], c("a", "b"))
Condition
Error in `str_equal()`:
! Can't recycle `x` (size 3) to match `y` (size 2).

8 changes: 8 additions & 0 deletions tests/testthat/_snaps/flatten.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# collapse must be single string

Code
str_flatten("A", c("a", "b"))
Condition
Error in `str_flatten()`:
! `collapse` must be a single string, not a character vector.

13 changes: 13 additions & 0 deletions tests/testthat/_snaps/match.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# match and match_all fail when pattern is not a regex

Code
str_match(phones, fixed("3"))
Condition
Error in `str_match()`:
! `pattern` must be a regular expression.
Code
str_match_all(phones, coll("9"))
Condition
Error in `str_match_all()`:
! `pattern` must be a regular expression.

# match can't use other modifiers

Code
Expand Down
4 changes: 3 additions & 1 deletion tests/testthat/test-conv.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ test_that("encoding conversion works", {
})

test_that("check encoding argument", {
expect_error(str_conv("A", c("ISO-8859-1", "ISO-8859-2")), "single string")
expect_snapshot(error = TRUE,
str_conv("A", c("ISO-8859-1", "ISO-8859-2"))
)
})
4 changes: 3 additions & 1 deletion tests/testthat/test-equal.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ test_that("vectorised using TRR", {
expect_equal(str_equal("a", character()), logical())
expect_equal(str_equal("a", "b"), FALSE)
expect_equal(str_equal("a", c("a", "b")), c(TRUE, FALSE))
expect_error(str_equal(letters[1:3], c("a", "b")), "recycle")
expect_snapshot(error = TRUE,
str_equal(letters[1:3], c("a", "b"))
)
})

test_that("can ignore case", {
Expand Down
4 changes: 3 additions & 1 deletion tests/testthat/test-flatten.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ test_that("equivalent to paste with collapse", {
})

test_that("collapse must be single string", {
expect_error(str_flatten("A", c("a", "b")), "single string")
expect_snapshot(error = TRUE,
str_flatten("A", c("a", "b"))
)
})

test_that("last optionally used instead of final separator", {
Expand Down
6 changes: 4 additions & 2 deletions tests/testthat/test-match.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ test_that("multiple match works", {
})

test_that("match and match_all fail when pattern is not a regex", {
expect_error(str_match(phones, fixed("3")))
expect_error(str_match_all(phones, coll("9")))
expect_snapshot(error = TRUE, {
str_match(phones, fixed("3"))
str_match_all(phones, coll("9"))
})
})

test_that("uses tidyverse recycling rules", {
Expand Down

0 comments on commit a7c8630

Please sign in to comment.