Skip to content

Commit

Permalink
Simplify tests by using cnd_class = TRUE from expect_snapshot() (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
olivroy authored Oct 25, 2024
1 parent 3d48c13 commit 55453da
Show file tree
Hide file tree
Showing 36 changed files with 387 additions and 496 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Suggests:
pillar,
rmarkdown,
stats,
testthat (>= 3.0.0),
testthat (>= 3.2.0),
tibble,
usethis,
vctrs (>= 0.2.3),
Expand Down
168 changes: 70 additions & 98 deletions tests/testthat/_snaps/arg.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,224 +5,199 @@
# gives an error with more than one arg

Code
(expect_error(arg_match0_wrapper(c("bar", "fun"), c("bar", "baz"))))
Output
<error/rlang_error>
arg_match0_wrapper(c("bar", "fun"), c("bar", "baz"))
Condition <rlang_error>
Error in `arg_match0_wrapper()`:
! `arg` must be length 1 or a permutation of `c("bar", "baz")`.

# gives error with different than rearranged arg vs value

`myarg` must be one of "fun" or "bar", not "foo".

---

`arg` must be length 1 or a permutation of `c("foo", "bar")`.
Code
f()
Condition
Error in `f()`:
! `myarg` must be one of "fun" or "bar", not "foo".
Code
arg_match0_wrapper(c("foo", "foo"), c("foo", "bar"), arg_nm = "x")
Condition
Error in `arg_match0_wrapper()`:
! `arg` must be length 1 or a permutation of `c("foo", "bar")`.

# `arg_match()` has informative error messages

Code
(expect_error(arg_match_wrapper("continuuos", c("discrete", "continuous"),
"my_arg")))
Output
<error/rlang_error>
arg_match_wrapper("continuuos", c("discrete", "continuous"), "my_arg")
Condition <rlang_error>
Error in `arg_match0_wrapper()`:
! `my_arg` must be one of "discrete" or "continuous", not "continuuos".
i Did you mean "continuous"?
Code
(expect_error(arg_match_wrapper("fou", c("bar", "foo"), "my_arg")))
Output
<error/rlang_error>
arg_match_wrapper("fou", c("bar", "foo"), "my_arg")
Condition <rlang_error>
Error in `arg_match0_wrapper()`:
! `my_arg` must be one of "bar" or "foo", not "fou".
i Did you mean "foo"?
Code
(expect_error(arg_match_wrapper("fu", c("ba", "fo"), "my_arg")))
Output
<error/rlang_error>
arg_match_wrapper("fu", c("ba", "fo"), "my_arg")
Condition <rlang_error>
Error in `arg_match0_wrapper()`:
! `my_arg` must be one of "ba" or "fo", not "fu".
i Did you mean "fo"?
Code
(expect_error(arg_match_wrapper("baq", c("foo", "baz", "bas"), "my_arg")))
Output
<error/rlang_error>
arg_match_wrapper("baq", c("foo", "baz", "bas"), "my_arg")
Condition <rlang_error>
Error in `arg_match0_wrapper()`:
! `my_arg` must be one of "foo", "baz", or "bas", not "baq".
i Did you mean "baz"?
Code
(expect_error(arg_match_wrapper("", character(), "my_arg")))
Output
<error/rlang_error>
arg_match_wrapper("", character(), "my_arg")
Condition <rlang_error>
Error in `arg_match0()`:
! `values` must have at least one element.
Code
(expect_error(arg_match_wrapper("fo", "foo", quote(f()))))
Output
<error/rlang_error>
arg_match_wrapper("fo", "foo", quote(f()))
Condition <rlang_error>
Error in `arg_match0()`:
! `arg_nm` must be a string or symbol.

# `arg_match()` provides no suggestion when the edit distance is too large

Code
(expect_error(arg_match0_wrapper("foobaz", c("fooquxs", "discrete"), "my_arg")))
Output
<error/rlang_error>
arg_match0_wrapper("foobaz", c("fooquxs", "discrete"), "my_arg")
Condition <rlang_error>
Error in `arg_match0_wrapper()`:
! `my_arg` must be one of "fooquxs" or "discrete", not "foobaz".
Code
(expect_error(arg_match0_wrapper("a", c("b", "c"), "my_arg")))
Output
<error/rlang_error>
arg_match0_wrapper("a", c("b", "c"), "my_arg")
Condition <rlang_error>
Error in `arg_match0_wrapper()`:
! `my_arg` must be one of "b" or "c", not "a".

# `arg_match()` makes case-insensitive match

Code
(expect_error(arg_match0_wrapper("a", c("A", "B"), "my_arg"),
"Did you mean \"A\"?"))
Output
<error/rlang_error>
arg_match0_wrapper("a", c("A", "B"), "my_arg")
Condition <rlang_error>
Error in `arg_match0_wrapper()`:
! `my_arg` must be one of "A" or "B", not "a".
i Did you mean "A"?
Code
(expect_error(arg_match0_wrapper("aa", c("AA", "aA"), "my_arg"),
"Did you mean \"aA\"?"))
Output
<error/rlang_error>
arg_match0_wrapper("aa", c("AA", "aA"), "my_arg")
Condition <rlang_error>
Error in `arg_match0_wrapper()`:
! `my_arg` must be one of "AA" or "aA", not "aa".
i Did you mean "aA"?

# check_required() checks argument is supplied (#1118)

Code
(expect_error(f()))
Output
<error/rlang_error>
f()
Condition <rlang_error>
Error in `f()`:
! `x` is absent but must be supplied.
Code
(expect_error(g()))
Output
<error/rlang_error>
g()
Condition <rlang_error>
Error in `f()`:
! `x` is absent but must be supplied.

# arg_match() supports symbols and scalar strings

Code
(expect_error(arg_match0_wrapper(chr_get("fo", 0L), c("bar", "foo"), "my_arg")))
Output
<error/rlang_error>
arg_match0_wrapper(chr_get("fo", 0L), c("bar", "foo"), "my_arg")
Condition <rlang_error>
Error in `arg_match0_wrapper()`:
! `my_arg` must be one of "bar" or "foo", not "fo".
i Did you mean "foo"?

# arg_match() requires an argument symbol

Code
(expect_error(wrapper()))
Output
<error/rlang_error>
wrapper()
Condition <rlang_error>
Error in `arg_match()`:
! `arg` must be a symbol, not the string "foo".

# can match multiple arguments

Code
(expect_error(my_wrapper("ba")))
Output
<error/rlang_error>
my_wrapper("ba")
Condition <rlang_error>
Error in `my_wrapper()`:
! `my_arg` must be one of "foo", "bar", or "baz", not "ba".
i Did you mean "bar"?
Code
(expect_error(my_wrapper(c("foo", "ba"))))
Output
<error/rlang_error>
my_wrapper(c("foo", "ba"))
Condition <rlang_error>
Error in `my_wrapper()`:
! `my_arg` must be one of "foo", "bar", or "baz", not "ba".
i Did you mean "bar"?

# arg_match0() defuses argument

Code
(expect_error(fn("foo")))
Output
<error/rlang_error>
fn("foo")
Condition <rlang_error>
Error in `fn()`:
! `arg` must be one of "bar" or "baz", not "foo".
Code
(expect_error(arg_match0("foo", c("bar", "baz"))))
Output
<error/rlang_error>
arg_match0("foo", c("bar", "baz"))
Condition <rlang_error>
Error:
! `"foo"` must be one of "bar" or "baz", not "foo".

# check_exclusive works

Code
(expect_error(f()))
Output
<error/rlang_error>
f()
Condition <rlang_error>
Error in `check_exclusive()`:
! Must supply at least two arguments.
Code
(expect_error(g()))
Output
<error/rlang_error>
g()
Condition <rlang_error>
Error in `check_exclusive()`:
! Must supply at least two arguments.
Code
(expect_error(h()))
Output
<error/rlang_error>
h()
Condition <rlang_error>
Error in `check_exclusive()`:
! Must supply at least two arguments.

---

Code
(expect_error(f()))
Output
<error/rlang_error>
f()
Condition <rlang_error>
Error in `f()`:
! One of `foo` or `bar` must be supplied.

---

Code
# All arguments supplied
(expect_error(g(foo, bar, baz)))
Output
<error/rlang_error>
g(foo, bar, baz)
Condition <rlang_error>
Error in `g()`:
! Exactly one of `foo`, `bar`, or `baz` must be supplied.
Code
# Some arguments supplied
(expect_error(g(foo, bar)))
Output
<error/rlang_error>
g(foo, bar)
Condition <rlang_error>
Error in `g()`:
! Exactly one of `foo`, `bar`, or `baz` must be supplied.
x `foo` and `bar` were supplied together.

# arg_match() mentions correct call if wrong type is supplied (#1388)

Code
(expect_error(f(1)))
Output
<error/rlang_error>
f(1)
Condition <rlang_error>
Error in `f()`:
! `my_arg` must be a string or character vector.
Code
(expect_error(g(1)))
Output
<error/rlang_error>
g(1)
Condition <rlang_error>
Error in `g()`:
! `my_arg` must be a character vector, not the number 1.

Expand Down Expand Up @@ -253,21 +228,18 @@
# arg_match() supports `NA` (#1519)

Code
(expect_error(f(NA)))
Output
<error/rlang_error>
f(NA)
Condition <rlang_error>
Error in `f()`:
! `x` must be a character vector, not `NA`.
Code
(expect_error(f(na_chr)))
Output
<error/rlang_error>
f(na_chr)
Condition <rlang_error>
Error in `f()`:
! `x` must be a single string, not a character `NA`.
Code
(expect_error(f(chr())))
Output
<error/rlang_error>
f(chr())
Condition <rlang_error>
Error in `f()`:
! `x` must be length 1, not length 0

10 changes: 4 additions & 6 deletions tests/testthat/_snaps/attr.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
# inputs must be valid

Code
(expect_error(set_names(environment())))
Output
<error/rlang_error>
set_names(environment())
Condition <rlang_error>
Error in `set_names()`:
! `x` must be a vector
Code
(expect_error(set_names(1:10, letters[1:4])))
Output
<error/rlang_error>
set_names(1:10, letters[1:4])
Condition <rlang_error>
Error in `set_names()`:
! The size of `nm` (4) must be compatible with the size of `x` (10).

7 changes: 4 additions & 3 deletions tests/testthat/_snaps/c-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@
# re-encoding fails purposefully with any bytes

Code
(expect_error(r_obj_encode_utf8(bytes)))
Output
<simpleError in r_obj_encode_utf8(bytes): translating strings with "bytes" encoding is not allowed>
r_obj_encode_utf8(bytes)
Condition <simpleError>
Error in `r_obj_encode_utf8()`:
! translating strings with "bytes" encoding is not allowed

---

Expand Down
5 changes: 2 additions & 3 deletions tests/testthat/_snaps/cnd-handlers.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# try_fetch() checks inputs

Code
(expect_error(try_fetch(NULL, function(...) NULL)))
Output
<error/rlang_error>
try_fetch(NULL, function(...) NULL)
Condition <rlang_error>
Error in `try_fetch()`:
! `...` must be named with condition classes.

Expand Down
10 changes: 4 additions & 6 deletions tests/testthat/_snaps/cnd-message.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,18 +209,16 @@
# parent errors prints with bullets in all cases

Code
(expect_error(f(TRUE)))
Output
<error/rlang_error>
f(TRUE)
Condition <rlang_error>
Error in `f()`:
! Wrapper
Caused by error in `f()`:
! Header
i Bullet
Code
(expect_error(f(FALSE)))
Output
<error/rlang_error>
f(FALSE)
Condition <rlang_error>
Error in `f()`:
! Wrapper
Caused by error in `f()`:
Expand Down
Loading

0 comments on commit 55453da

Please sign in to comment.