Skip to content

Commit

Permalink
Avoid nested expect_error() in snapshot tests (#361)
Browse files Browse the repository at this point in the history
  • Loading branch information
olivroy authored Oct 24, 2024
1 parent 070810f commit 7c7e309
Show file tree
Hide file tree
Showing 13 changed files with 151 additions and 205 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Suggests:
magrittr,
rmarkdown,
stringr,
testthat (>= 3.1.1),
testthat (>= 3.2.1),
tibble (>= 2.1.3)
VignetteBuilder:
knitr
Expand Down
75 changes: 30 additions & 45 deletions tests/testthat/_snaps/eval-relocate.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,85 +18,74 @@
# can't relocate with out-of-bounds variables by default

Code
(expect_error(relocate_loc(x, c)))
Output
<error/vctrs_error_subscript_oob>
relocate_loc(x, c)
Condition <vctrs_error_subscript_oob>
Error in `relocate_loc()`:
! Can't relocate columns that don't exist.
x Column `c` doesn't exist.
Code
(expect_error(relocate_loc(x, c(1, 3))))
Output
<error/vctrs_error_subscript_oob>
relocate_loc(x, c(1, 3))
Condition <vctrs_error_subscript_oob>
Error in `relocate_loc()`:
! Can't relocate columns that don't exist.
i Location 3 doesn't exist.
i There are only 2 columns.
Code
(expect_error(relocate_loc(x, a, before = c)))
Output
<error/vctrs_error_subscript_oob>
relocate_loc(x, a, before = c)
Condition <vctrs_error_subscript_oob>
Error in `relocate_loc()`:
! Can't select columns that don't exist.
x Column `c` doesn't exist.
Code
(expect_error(relocate_loc(x, a, after = c)))
Output
<error/vctrs_error_subscript_oob>
relocate_loc(x, a, after = c)
Condition <vctrs_error_subscript_oob>
Error in `relocate_loc()`:
! Can't select columns that don't exist.
x Column `c` doesn't exist.

# can relocate with out-of-bounds variables in `expr` if `strict = FALSE`

Code
(expect_error(relocate_loc(x, a, before = c, strict = FALSE)))
Output
<error/vctrs_error_subscript_oob>
relocate_loc(x, a, before = c, strict = FALSE)
Condition <vctrs_error_subscript_oob>
Error in `relocate_loc()`:
! Can't select columns that don't exist.
x Column `c` doesn't exist.
Code
(expect_error(relocate_loc(x, a, after = c, strict = FALSE)))
Output
<error/vctrs_error_subscript_oob>
relocate_loc(x, a, after = c, strict = FALSE)
Condition <vctrs_error_subscript_oob>
Error in `relocate_loc()`:
! Can't select columns that don't exist.
x Column `c` doesn't exist.

# can forbid rename syntax

Code
(expect_error(relocate_loc(x, c(foo = b), allow_rename = FALSE)))
Output
<error/tidyselect:::error_disallowed_rename>
relocate_loc(x, c(foo = b), allow_rename = FALSE)
Condition <tidyselect:::error_disallowed_rename>
Error in `relocate_loc()`:
! Can't rename variables in this context.
Code
(expect_error(relocate_loc(x, c(b, foo = b), allow_rename = FALSE)))
Output
<error/tidyselect:::error_disallowed_rename>
relocate_loc(x, c(b, foo = b), allow_rename = FALSE)
Condition <tidyselect:::error_disallowed_rename>
Error in `relocate_loc()`:
! Can't rename variables in this context.

# can forbid empty selections

Code
(expect_error(relocate_loc(x, allow_empty = FALSE, error_arg = "...")))
Output
<error/tidyselect_error_empty_selection>
relocate_loc(x, allow_empty = FALSE, error_arg = "...")
Condition
Error in `relocate_loc()`:
! `...` must select at least one column.
Code
(expect_error(relocate_loc(mtcars, integer(), allow_empty = FALSE)))
Output
<error/tidyselect_error_empty_selection>
relocate_loc(mtcars, integer(), allow_empty = FALSE)
Condition
Error in `relocate_loc()`:
! Must select at least one item.
Code
(expect_error(relocate_loc(mtcars, starts_with("z"), allow_empty = FALSE)))
Output
<error/tidyselect_error_empty_selection>
relocate_loc(mtcars, starts_with("z"), allow_empty = FALSE)
Condition
Error in `relocate_loc()`:
! Must select at least one item.

Expand All @@ -116,27 +105,23 @@
# `before` and `after` forbid renaming

Code
(expect_error(relocate_loc(x, b, before = c(new = c))))
Output
<error/rlang_error>
relocate_loc(x, b, before = c(new = c))
Condition <rlang_error>
Error in `relocate_loc()`:
! Can't rename variables when `before` is supplied.
Code
(expect_error(relocate_loc(x, b, before = c(new = c), before_arg = ".before")))
Output
<error/rlang_error>
relocate_loc(x, b, before = c(new = c), before_arg = ".before")
Condition <rlang_error>
Error in `relocate_loc()`:
! Can't rename variables when `.before` is supplied.
Code
(expect_error(relocate_loc(x, b, after = c(new = c))))
Output
<error/rlang_error>
relocate_loc(x, b, after = c(new = c))
Condition <rlang_error>
Error in `relocate_loc()`:
! Can't rename variables when `after` is supplied.
Code
(expect_error(relocate_loc(x, b, after = c(new = c), after_arg = ".after")))
Output
<error/rlang_error>
relocate_loc(x, b, after = c(new = c), after_arg = ".after")
Condition <rlang_error>
Error in `relocate_loc()`:
! Can't rename variables when `.after` is supplied.

55 changes: 22 additions & 33 deletions tests/testthat/_snaps/eval-select.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,42 @@

Code
x <- list(a = 1, b = 2, c = 3)
(expect_error(select_loc(x, "a", include = 1)))
Output
<error/rlang_error>
select_loc(x, "a", include = 1)
Condition <rlang_error>
Error in `select_loc()`:
! `include` must be a character vector.
Code
(expect_error(select_loc(x, "a", include = "d")))
Output
<error/rlang_error>
select_loc(x, "a", include = "d")
Condition <rlang_error>
Error in `select_loc()`:
! `include` must only include variables found in `data`.
i Unknown variables: d
Code
(expect_error(select_loc(x, "a", exclude = 1)))
Output
<error/rlang_error>
select_loc(x, "a", exclude = 1)
Condition <rlang_error>
Error in `select_loc()`:
! `include` must be a character vector.

# can forbid rename syntax (#178)

Code
(expect_error(select_loc(mtcars, c(foo = cyl), allow_rename = FALSE)))
Output
<error/tidyselect:::error_disallowed_rename>
select_loc(mtcars, c(foo = cyl), allow_rename = FALSE)
Condition <tidyselect:::error_disallowed_rename>
Error in `select_loc()`:
! Can't rename variables in this context.
Code
(expect_error(select_loc(mtcars, c(cyl, foo = cyl), allow_rename = FALSE)))
Output
<error/tidyselect:::error_disallowed_rename>
select_loc(mtcars, c(cyl, foo = cyl), allow_rename = FALSE)
Condition <tidyselect:::error_disallowed_rename>
Error in `select_loc()`:
! Can't rename variables in this context.
Code
(expect_error(select_loc(mtcars, c(cyl, foo = mpg), allow_rename = FALSE)))
Output
<error/tidyselect:::error_disallowed_rename>
select_loc(mtcars, c(cyl, foo = mpg), allow_rename = FALSE)
Condition <tidyselect:::error_disallowed_rename>
Error in `select_loc()`:
! Can't rename variables in this context.
Code
(expect_error(select_loc(mtcars, c(foo = mpg, cyl), allow_rename = FALSE)))
Output
<error/tidyselect:::error_disallowed_rename>
select_loc(mtcars, c(foo = mpg, cyl), allow_rename = FALSE)
Condition <tidyselect:::error_disallowed_rename>
Error in `select_loc()`:
! Can't rename variables in this context.

Expand Down Expand Up @@ -87,19 +80,17 @@
# eval_select() errors mention correct calls

Code
(expect_error(select_loc(mtcars, f())))
Output
<error/rlang_error>
select_loc(mtcars, f())
Condition <rlang_error>
Error in `select_loc()`:
Caused by error in `f()`:
! foo

# predicate outputs are type-checked

Code
(expect_error(select_loc(mtcars, function(x) "")))
Output
<error/rlang_error>
select_loc(mtcars, function(x) "")
Condition <rlang_error>
Error in `select_loc()`:
! Predicate must return `TRUE` or `FALSE`, not `""`.

Expand Down Expand Up @@ -174,18 +165,16 @@
# eval_select() produces correct chained errors

Code
(expect_error(select_loc(mtcars, 1 + "")))
Output
<error/rlang_error>
select_loc(mtcars, 1 + "")
Condition <rlang_error>
Error in `select_loc()`:
i In argument: `1 + ""`.
Caused by error in `1 + ""`:
! non-numeric argument to binary operator
Code
f <- (function() 1 + "")
(expect_error(select_loc(mtcars, f())))
Output
<error/rlang_error>
select_loc(mtcars, f())
Condition <rlang_error>
Error in `select_loc()`:
i In argument: `f()`.
Caused by error in `1 + ""`:
Expand Down
30 changes: 13 additions & 17 deletions tests/testthat/_snaps/eval-walk.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,9 @@
Use of .data in tidyselect expressions was deprecated in tidyselect 1.2.0.
i Please use `all_of(var)` (or `any_of(var)`) instead of `.data[[var]]`

# eval_walk() has informative messages
# eval_walk() warns when using a predicate without where()

Code
# Using a predicate without where() warns
invisible(select_loc(iris, is_integer))
Condition
Warning:
Expand Down Expand Up @@ -131,11 +130,12 @@
# Now:
data %>% select(where(is_integer))

# eval_walk() errors when formula shorthand are not wrapped

Code
# formula shorthand must be wrapped
(expect_error(select_loc(mtcars, ~ is.numeric(.x))))
Output
<error/rlang_error>
select_loc(mtcars, ~ is.numeric(.x))
Condition <rlang_error>
Error in `select_loc()`:
! Formula shorthand must be wrapped in `where()`.
Expand All @@ -145,10 +145,8 @@
# Good
data %>% select(where(~is.numeric(.x)))
Code
(expect_error(select_loc(mtcars, ~ is.numeric(.x) || is.factor(.x) ||
is.character(.x))))
Output
<error/rlang_error>
select_loc(mtcars, ~ is.numeric(.x) || is.factor(.x) || is.character(.x))
Condition <rlang_error>
Error in `select_loc()`:
! Formula shorthand must be wrapped in `where()`.
Expand All @@ -158,10 +156,9 @@
# Good
data %>% select(where(~is.numeric(.x) || is.factor(.x) || is.character(.x)))
Code
(expect_error(select_loc(mtcars, ~ is.numeric(.x) || is.factor(.x) ||
is.character(.x) || is.numeric(.x) || is.factor(.x) || is.character(.x))))
Output
<error/rlang_error>
select_loc(mtcars, ~ is.numeric(.x) || is.factor(.x) || is.character(.x) ||
is.numeric(.x) || is.factor(.x) || is.character(.x))
Condition <rlang_error>
Error in `select_loc()`:
! Formula shorthand must be wrapped in `where()`.
Expand All @@ -171,9 +168,8 @@
# Good
data %>% select(where(~...))
Code
(expect_error(select_loc(mtcars, .data$"foo")))
Output
<error/rlang_error>
select_loc(mtcars, .data$"foo")
Condition <rlang_error>
Error in `select_loc()`:
! The RHS of `.data$rhs` must be a symbol.

Expand Down
Loading

0 comments on commit 7c7e309

Please sign in to comment.