Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# rlang (development version)

* `arg_match()` now throws better error messages if `multiple = TRUE` or if a single value is allowed (@olivroy, #1635, #1682).

# rlang 1.1.6

* Fixes for CRAN checks.
Expand Down
25 changes: 20 additions & 5 deletions R/arg.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ arg_match <- function(
abort(msg, call = error_call, arg = error_arg)
}
if (length(arg) > 1 && !setequal(arg, values)) {
msg <- arg_match_invalid_msg(arg, values, error_arg)
msg <- arg_match_invalid_msg(arg, values, multiple, error_arg)
abort(msg, call = error_call, arg = error_arg)
}

Expand All @@ -76,6 +76,8 @@ arg_match <- function(
}

arg_match_multi <- function(arg, values, error_arg, error_call) {
# Set an option temporarily to make sure the multiple message shows.
local_options("rlang_multiple_error" = TRUE)
map_chr(arg, ~ arg_match0(.x, values, error_arg, error_call = error_call))
}

Expand Down Expand Up @@ -132,7 +134,8 @@ stop_arg_match <- function(arg, values, error_arg, error_call) {
check_string(arg, arg = error_arg, call = error_call)
}

msg <- arg_match_invalid_msg(arg, values, error_arg)
multiple <- getOption("rlang_multiple_error", FALSE)
msg <- arg_match_invalid_msg(arg, values, multiple, error_arg)

# Try suggest the most probable and helpful candidate value
candidate <- NULL
Expand Down Expand Up @@ -164,14 +167,26 @@ stop_arg_match <- function(arg, values, error_arg, error_call) {
abort(msg, call = error_call, arg = error_arg)
}

arg_match_invalid_msg <- function(val, values, error_arg) {
msg <- paste0(format_arg(error_arg), " must be one of ")
arg_match_invalid_msg <- function(val, values, multiple, error_arg) {
if (length(values) == 1) {
#1635
word <- " must be "
} else if (multiple) {
#1682
word <- " must be in "
} else {
word <- " must be one of "
}
msg <- paste0(format_arg(error_arg), word)
msg <- paste0(msg, oxford_comma(chr_quoted(values, "\"")))

if (is_null(val)) {
msg <- paste0(msg, ".")
} else {
msg <- paste0(msg, sprintf(', not "%s\".', val[[1]]))
# only show incorrect values.
incorrect_val <- setdiff(val, values)
incorrect_val <- oxford_comma(incorrect_val)
msg <- paste0(msg, sprintf(', not "%s\".', incorrect_val))
}

msg
Expand Down
17 changes: 15 additions & 2 deletions tests/testthat/_snaps/arg.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@
Error in `arg_match0_wrapper()`:
! `arg` must be length 1 or a permutation of `c("foo", "bar")`.

# arg_match() works well when multiple = TRUE (#1682)

Code
f("x", values = c("y", "z"))
Condition
Error in `f()`:
! `x` must be in "y" or "z", not "x".
Code
f(c("x", "y"), values = c("y", "z"))
Condition
Error in `f()`:
! `x` must be in "y" or "z", not "x".

# `arg_match()` has informative error messages

Code
Expand Down Expand Up @@ -124,13 +137,13 @@
my_wrapper("ba")
Condition <rlang_error>
Error in `my_wrapper()`:
! `my_arg` must be one of "foo", "bar", or "baz", not "ba".
! `my_arg` must be in "foo", "bar", or "baz", not "ba".
i Did you mean "bar"?
Code
my_wrapper(c("foo", "ba"))
Condition <rlang_error>
Error in `my_wrapper()`:
! `my_arg` must be one of "foo", "bar", or "baz", not "ba".
! `my_arg` must be in "foo", "bar", or "baz", not "ba".
i Did you mean "bar"?

# arg_match0() defuses argument
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/_snaps/cnd-abort.md
Original file line number Diff line number Diff line change
Expand Up @@ -1397,21 +1397,21 @@
Output
<error/rlang_error>
Error:
! `"f"` must be one of "foo", not "f".
! `"f"` must be "foo", not "f".
i Did you mean "foo"?
Code
(expect_error(eval_bare(quote(arg_match0("f", "foo")))))
Output
<error/rlang_error>
Error:
! `"f"` must be one of "foo", not "f".
! `"f"` must be "foo", not "f".
i Did you mean "foo"?
Code
(expect_error(eval_bare(quote(arg_match0("f", "foo")), env())))
Output
<error/rlang_error>
Error:
! `"f"` must be one of "foo", not "f".
! `"f"` must be "foo", not "f".
i Did you mean "foo"?

# error_call() and format_error_call() preserve special syntax ops
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-arg.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ test_that("informative error message on partial match", {
)
})

test_that("arg_match() works well when multiple = TRUE (#1682)", {
f <- function(x, ...) {
arg_match(x, ..., multiple = TRUE)
}
expect_snapshot(error = TRUE, {
f("x", values = c("y", "z"))
f(c("x", "y"), values = c("y", "z"))

})
})

test_that("`arg_match()` has informative error messages", {
arg_match_wrapper <- function(...) {
arg_match0_wrapper(...)
Expand Down