diff --git a/NEWS.md b/NEWS.md index 5b09a7c9..3f1607f8 100644 --- a/NEWS.md +++ b/NEWS.md @@ -8,6 +8,8 @@ * `parameters()` and the `grid_*()` functions give more information in the error message when non-parameter objects are passed in (#437, #438). +* `encode_unit()` now provides a helpful error when `x` is not a parameter object (#430). + * `value_validate()`, `encode_unit()`, `get_p()`, `get_log_p()`, `get_n()`, `get_n_frac()`, `get_n_frac_range()`, and `get_batch_sizes()` now enforce empty dots (#439). diff --git a/R/encode_unit.R b/R/encode_unit.R index d4533fb2..58986ade 100644 --- a/R/encode_unit.R +++ b/R/encode_unit.R @@ -14,7 +14,6 @@ #' @keywords internal #' @export encode_unit <- function(x, value, direction, ...) { - arg_match0(direction, values = c("forward", "backward")) UseMethod("encode_unit") } @@ -29,6 +28,7 @@ encode_unit.default <- function(x, value, direction, ...) { #' @rdname encode_unit #' @export encode_unit.quant_param <- function(x, value, direction, original = TRUE, ...) { + arg_match0(direction, values = c("forward", "backward")) check_bool(original) if (has_unknowns(x)) { cli::cli_abort("The parameter object contains unknowns.") @@ -70,6 +70,7 @@ encode_unit.quant_param <- function(x, value, direction, original = TRUE, ...) { #' @rdname encode_unit #' @export encode_unit.qual_param <- function(x, value, direction, ...) { + arg_match0(direction, values = c("forward", "backward")) check_dots_empty() if (has_unknowns(x)) { cli::cli_abort("The parameter object contains unknowns.") diff --git a/tests/testthat/_snaps/encode_unit.md b/tests/testthat/_snaps/encode_unit.md index 71689298..22bfa7ee 100644 --- a/tests/testthat/_snaps/encode_unit.md +++ b/tests/testthat/_snaps/encode_unit.md @@ -32,6 +32,14 @@ # bad args + Code + encode_unit("foo") + Condition + Error in `encode_unit()`: + ! `x` should be a dials parameter object, not a string. + +--- + Code encode_unit(2, prune_method()$values, direction = "forward") Condition diff --git a/tests/testthat/test-encode_unit.R b/tests/testthat/test-encode_unit.R index 0777a385..e6bfeb94 100644 --- a/tests/testthat/test-encode_unit.R +++ b/tests/testthat/test-encode_unit.R @@ -71,6 +71,10 @@ test_that("bad args", { z_0 <- encode_unit(z, prune_method()$values, direction = "forward") x_0 <- encode_unit(x, 2:7, direction = "forward") + expect_snapshot( + error = TRUE, + encode_unit("foo") + ) expect_snapshot( error = TRUE, encode_unit(2, prune_method()$values, direction = "forward")