Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert expect_error() to expect_snapshot() #570

Merged
merged 2 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When there's a single expression to test could you please fit on one line like this?

expect_snapshot(str_conv("A", c("ISO-8859-1", "ISO-8859-2")), 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
Loading