diff --git a/DESCRIPTION b/DESCRIPTION index 387351da..f4014b54 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -20,13 +20,13 @@ Imports: DiceDesign, dplyr (>= 0.8.5), glue, - hardhat (>= 0.1.6.9000), + hardhat (>= 0.2.0), lifecycle, purrr, - rlang, + rlang (>= 1.0.1), tibble, utils, - vctrs (>= 0.3.1), + vctrs (>= 0.3.8), withr Suggests: covr, diff --git a/NAMESPACE b/NAMESPACE index 0f091f47..0fa08369 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -214,6 +214,8 @@ importFrom(purrr,map_chr) importFrom(purrr,map_dbl) importFrom(purrr,map_dfc) importFrom(purrr,map_lgl) +importFrom(rlang,caller_env) +importFrom(rlang,check_dots_empty) importFrom(rlang,enquo) importFrom(rlang,eval_tidy) importFrom(rlang,is_string) diff --git a/R/0_imports.R b/R/0_imports.R index aa2cca30..abae38b4 100644 --- a/R/0_imports.R +++ b/R/0_imports.R @@ -1,6 +1,7 @@ #' @importFrom purrr map_lgl map2_dfc map_chr map map2 map_dfc map_dbl #' @importFrom DiceDesign dmaxDesign lhsDesign #' @importFrom rlang quos eval_tidy quo_get_expr is_string enquo quo_is_null +#' @importFrom rlang caller_env check_dots_empty #' @importFrom tibble as_tibble is_tibble tibble type_sum #' @importFrom scales log2_trans is.trans log10_trans #' @importFrom utils installed.packages globalVariables diff --git a/R/aaa_unknown.R b/R/aaa_unknown.R index ec8b4187..9579eefc 100644 --- a/R/aaa_unknown.R +++ b/R/aaa_unknown.R @@ -77,13 +77,12 @@ has_unknowns_val <- function(object) { any(rng_check) | any(val_check) } -check_for_unknowns <- function(x, label = "") { - err_txt <- paste0("Unknowns not allowed in `", label, "`.") +check_for_unknowns <- function(x, ..., call = caller_env()) { + check_dots_empty() if (length(x) == 1 && is_unknown(x)) - rlang::abort(err_txt) + rlang::abort("Unknowns not allowed.", call = call) is_ukn <- map_lgl(x, is_unknown) if (any(is_ukn)) - rlang::abort(err_txt) + rlang::abort("Unknowns not allowed.", call = call) invisible(TRUE) } - diff --git a/R/aaa_values.R b/R/aaa_values.R index 956883c2..b2c30de6 100644 --- a/R/aaa_values.R +++ b/R/aaa_values.R @@ -86,9 +86,10 @@ value_validate <- function(object, values) { } -value_validate_quant <- function(object, values) { - check_for_unknowns(object$range, "value_validate") - check_for_unknowns(values, "value_validate") +value_validate_quant <- function(object, values, ..., call = caller_env()) { + check_dots_empty() + check_for_unknowns(object$range, call = call) + check_for_unknowns(values, call = call) is_valid <- rep(TRUE, length(values)) @@ -112,9 +113,10 @@ value_validate_quant <- function(object, values) { is_valid } -value_validate_qual <- function(object, values) { - check_for_unknowns(object$range, "value_validate") - check_for_unknowns(values, "value_validate") +value_validate_qual <- function(object, values, ..., call = caller_env()) { + check_dots_empty() + check_for_unknowns(object$range) + check_for_unknowns(values) res <- values %in% object$values res[is.na(res)] <- FALSE @@ -271,7 +273,7 @@ value_samp_qual <- function(object, n) { #' @export #' @rdname value_validate value_transform <- function(object, values) { - check_for_unknowns(values, "value_transform") + check_for_unknowns(values) if (is.null(object$trans)) return(values) @@ -288,7 +290,7 @@ trans_wrap <- function(x, object) { #' @export #' @rdname value_validate value_inverse <- function(object, values) { - check_for_unknowns(values, "value_inverse") + check_for_unknowns(values) if (is.null(object$trans)) return(values) @@ -306,7 +308,7 @@ inv_wrap <- function(x, object) { #' @export #' @rdname value_validate value_set <- function(object, values) { - check_for_unknowns(values, "value_set") + check_for_unknowns(values) if (length(values) == 0) rlang::abort("`values` should at least one element.") if (!inherits(object, "param")) diff --git a/R/compat-vctrs-helpers-parameters.R b/R/compat-vctrs-helpers-parameters.R index 34b66103..e0c4037e 100644 --- a/R/compat-vctrs-helpers-parameters.R +++ b/R/compat-vctrs-helpers-parameters.R @@ -107,7 +107,7 @@ tib_upcast <- function(x) { df_size <- function(x) { if (!is.list(x)) { - rlang::abort("Cannot get the df size of a non-list.") + rlang::abort("Cannot get the df size of a non-list.", .internal = TRUE) } if (length(x) == 0L) { diff --git a/R/encode_unit.R b/R/encode_unit.R index c9a68d6d..89fe2346 100644 --- a/R/encode_unit.R +++ b/R/encode_unit.R @@ -15,25 +15,26 @@ #' @export encode_unit <- function(x, value, direction, ...) { if (!any(direction %in% c("forward", "backward"))) { - rlang::abort("`direction` should be either 'forward' or 'backward'") + rlang::abort("`direction` should be either 'forward' or 'backward'", + .internal = TRUE) } UseMethod("encode_unit") } #' @export encode_unit.default <- function(x, value, direction, ...) { - rlang::abort("`x` should be a dials parameter object.") + rlang::abort("`x` should be a dials parameter object.", .internal = TRUE) } #' @export encode_unit.quant_param <- function(x, value, direction, original = TRUE, ...) { if (has_unknowns(x)) { - rlang::abort("The parameter object contains unknowns.") + rlang::abort("The parameter object contains unknowns.", .internal = TRUE) } if (!is.numeric(value) || is.matrix(value)) { - rlang::abort("`value` should be a numeric vector.") + rlang::abort("`value` should be a numeric vector.", .internal = TRUE) } param_rng <- x$range$upper - x$range$lower @@ -45,7 +46,7 @@ encode_unit.quant_param <- function(x, value, direction, original = TRUE, ...) { compl <- value[!is.na(value)] if (any(compl < 0) | any(compl > 1)) { - rlang::abort("Values should be on [0, 1].") + rlang::abort("Values should be on [0, 1].", .internal = TRUE) } value <- (value * param_rng) + x$range$lower @@ -68,7 +69,7 @@ encode_unit.quant_param <- function(x, value, direction, original = TRUE, ...) { encode_unit.qual_param <- function(x, value, direction, ...) { if (has_unknowns(x)) { - rlang::abort("The parameter object contains unknowns.") + rlang::abort("The parameter object contains unknowns.", .internal = TRUE) } ref_vals <- x$values @@ -78,7 +79,7 @@ encode_unit.qual_param <- function(x, value, direction, ...) { # convert to [0, 1] if (!is.character(value) || is.matrix(value)) { - rlang::abort("`value` should be a character vector.") + rlang::abort("`value` should be a character vector.", .internal = TRUE) } compl <- value[!is.na(value)] @@ -96,11 +97,11 @@ encode_unit.qual_param <- function(x, value, direction, ...) { compl <- value[!is.na(value)] if (any(compl < 0) | any(compl > 1)) { - rlang::abort("Values should be on [0, 1].") + rlang::abort("Values should be on [0, 1].", .internal = TRUE) } if (!is.numeric(value) || is.matrix(value)) { - rlang::abort("`value` should be a numeric vector.") + rlang::abort("`value` should be a numeric vector.", .internal = TRUE) } ind <- cut(value, breaks = seq(0, 1, length.out = num_lvl + 1), include.lowest = TRUE) diff --git a/R/extract.R b/R/extract.R index c71fdb82..4536d297 100644 --- a/R/extract.R +++ b/R/extract.R @@ -1,8 +1,6 @@ #' @export extract_parameter_dials.parameters <- function(x, parameter, ...) { - dots <- rlang::quos(...) - if (!rlang::is_empty(dots)) - rlang::abort("The `...` are not used with `extract_parameter_dials()`.") + check_dots_empty() if (any(rlang::is_missing(parameter)) || any(!is.character(parameter)) || length(parameter) != 1 || diff --git a/R/finalize.R b/R/finalize.R index 7493e43d..1a1115db 100644 --- a/R/finalize.R +++ b/R/finalize.R @@ -179,6 +179,8 @@ get_log_p <- function(object, x, ...) { #' @export #' @rdname finalize get_n_frac <- function(object, x, log_vals = FALSE, frac = 1/3, ...) { + if (!inherits(object, "param")) + rlang::abort("`object` should be a 'param' object.") rngs <- range_get(object, original = FALSE) if (!is_unknown(rngs$upper)) return(object) @@ -267,4 +269,3 @@ get_batch_sizes <- function(object, x, frac = c(1/10, 1/3), ...) { range_set(object, n_frac) } - diff --git a/R/grids.R b/R/grids.R index 18ef2dd8..cfd7b1ed 100644 --- a/R/grids.R +++ b/R/grids.R @@ -95,8 +95,8 @@ grid_regular.workflow <- function(x, ..., levels = 3, original = TRUE, filter = grid_regular.parameters(parameters(x), ..., levels = levels, original = original, filter = {{filter}}) } -make_regular_grid <- function(..., levels = 3, original = TRUE, filter = NULL) { - validate_params(...) +make_regular_grid <- function(..., levels = 3, original = TRUE, filter = NULL, call = caller_env()) { + validate_params(..., call = call) filter_quo <- enquo(filter) param_quos <- quos(...) params <- map(param_quos, eval_tidy) @@ -107,7 +107,8 @@ make_regular_grid <- function(..., levels = 3, original = TRUE, filter = NULL) { p <- length(levels) if (p > 1 && p != length(param_quos)) rlang::abort( - paste0("`levels` should have length 1 or ", length(param_quos)) + paste0("`levels` should have length 1 or ", length(param_quos)), + call = call ) if (p == 1) { @@ -116,7 +117,10 @@ make_regular_grid <- function(..., levels = 3, original = TRUE, filter = NULL) { if (all(rlang::has_name(levels, names(params)))) { levels <- levels[names(params)] } else if (any(rlang::has_name(levels, names(params)))) { - rlang::abort("Elements of `levels` should either be all named or unnamed, not mixed.") + rlang::abort( + "Elements of `levels` should either be all named or unnamed, not mixed.", + call = call + ) } param_seq <- map2(params, as.list(levels), value_seq, original = original) } @@ -185,8 +189,8 @@ grid_random.workflow <- function(x, ..., size = 5, original = TRUE, filter = NUL } -make_random_grid <- function(..., size = 5, original = TRUE, filter = NULL) { - validate_params(...) +make_random_grid <- function(..., size = 5, original = TRUE, filter = NULL, call = caller_env()) { + validate_params(..., call = call) filter_quo <- enquo(filter) param_quos <- quos(...) params <- map(param_quos, eval_tidy) diff --git a/R/misc.R b/R/misc.R index ad513933..c6858078 100644 --- a/R/misc.R +++ b/R/misc.R @@ -45,23 +45,35 @@ check_installs <- function(x) { # checking functions ----------------------------------------------------------- -check_label <- function(txt) { +check_label <- function(txt, ..., call = caller_env()) { + check_dots_empty() if (is.null(txt)) - rlang::abort("`label` should be a single named character string or NULL.") + rlang::abort( + "`label` should be a single named character string or NULL.", + call = call + ) if (!is.character(txt) || length(txt) > 1) - rlang::abort("`label` should be a single named character string or NULL.") + rlang::abort( + "`label` should be a single named character string or NULL.", + call = call + ) if (length(names(txt)) != 1) - rlang::abort("`label` should be a single named character string or NULL.") + rlang::abort( + "`label` should be a single named character string or NULL.", + call = call + ) invisible(txt) } -check_finalize <- function(x) { +check_finalize <- function(x, ..., call = caller_env()) { + check_dots_empty() if (!is.null(x) & !is.function(x)) - rlang::abort("`finalize` should be NULL or a function.") + rlang::abort("`finalize` should be NULL or a function.", call = call) invisible(x) } -check_range <- function(x, type, trans) { +check_range <- function(x, type, trans, ..., call = caller_env()) { + check_dots_empty() if (!is.null(trans)) { return(invisible(x)) } @@ -85,7 +97,7 @@ check_range <- function(x, type, trans) { "An integer is required for the range and these do not appear to be ", "whole numbers: ", msg ) - rlang::abort(msg) + rlang::abort(msg, call = call) } x0[known] <- as.integer(x0[known]) @@ -93,7 +105,7 @@ check_range <- function(x, type, trans) { msg <- paste0( "Since `type = '", type, "'`, please use that data type for the range." ) - rlang::abort(msg) + rlang::abort(msg, call = call) } } invisible(x0) diff --git a/R/parameters.R b/R/parameters.R index cf63cf5a..ee1fb40f 100644 --- a/R/parameters.R +++ b/R/parameters.R @@ -49,22 +49,26 @@ parameters.list <- function(x, ...) { ) } -chr_check <- function(x) { +chr_check <- function(x, ..., call = caller_env()) { + check_dots_empty() cl <- match.call() if (is.null(x)) { rlang::abort( - glue::glue("Element `{cl$x}` should not be NULL.") + glue::glue("Element `{cl$x}` should not be NULL."), + call = call ) } if (!is.character(x)) { rlang::abort( - glue::glue("Element `{cl$x}` should be a character string.") + glue::glue("Element `{cl$x}` should be a character string."), + call = call ) } invisible(TRUE) } -unique_check <- function(x) { +unique_check <- function(x, ..., call = caller_env()) { + check_dots_empty() x2 <- x[!is.na(x)] is_dup <- duplicated(x2) if (any(is_dup)) { @@ -73,7 +77,7 @@ unique_check <- function(x) { msg <- paste0("Element `", deparse(cl$x), "` should have unique values. Duplicates exist ", "for item(s): ", paste0("'", dup_list, "'", collapse = ", ")) - rlang::abort(msg) + rlang::abort(msg, call = call) } invisible(TRUE) } diff --git a/R/space_filling.R b/R/space_filling.R index 27a05898..31e3ab1f 100644 --- a/R/space_filling.R +++ b/R/space_filling.R @@ -102,8 +102,8 @@ grid_max_entropy.workflow <- function(x, ..., size = 3, original = TRUE, make_max_entropy_grid <- function(..., size = 3, original = TRUE, - variogram_range = 0.5, iter = 1000) { - validate_params(...) + variogram_range = 0.5, iter = 1000, call = caller_env()) { + validate_params(..., call = call) param_quos <- quos(...) params <- map(param_quos, eval_tidy) param_names <- names(param_quos) @@ -188,8 +188,8 @@ grid_latin_hypercube.workflow <- function(x, ..., size = 3, original = TRUE) { -make_latin_hypercube_grid <- function(..., size = 3, original = TRUE) { - validate_params(...) +make_latin_hypercube_grid <- function(..., size = 3, original = TRUE, call = caller_env()) { + validate_params(..., call = call) param_quos <- quos(...) params <- map(param_quos, eval_tidy) param_labs <- map_chr(params, function(x) x$label) diff --git a/R/validation.R b/R/validation.R index 4334b144..41986753 100644 --- a/R/validation.R +++ b/R/validation.R @@ -1,8 +1,8 @@ -validate_params <- function(...) { +validate_params <- function(..., call = caller_env()) { param_quos <- quos(...) param_expr <- purrr::map_chr(param_quos, rlang::quo_text) if (length(param_quos) == 0) { - rlang::abort("At least one parameter object is required.") + rlang::abort("At least one parameter object is required.", call = call) } params <- map(param_quos, eval_tidy) is_param <- map_lgl(params, function(x) inherits(x, "param")) @@ -11,7 +11,8 @@ validate_params <- function(...) { paste0( "These arguments must have class 'param': ", paste0("`", param_expr[!is_param], "`", collapse = ",") - ) + ), + call = call ) } bad_param <- has_unknowns(params) @@ -21,7 +22,8 @@ validate_params <- function(...) { paste0( "These arguments contains unknowns: ", paste0("`", bad_param, "`", collapse = ","), - '. See the `finalize()` function.') + '. See the `finalize()` function.'), + call = call ) } invisible(NULL) diff --git a/tests/testthat/_snaps/aaa_unkown.md b/tests/testthat/_snaps/aaa_unkown.md index 9929d51b..e296621f 100644 --- a/tests/testthat/_snaps/aaa_unkown.md +++ b/tests/testthat/_snaps/aaa_unkown.md @@ -2,111 +2,127 @@ Code grid_regular(p1) - Error - These arguments contains unknowns: `q`. See the `finalize()` function. + Condition + Error in `grid_regular()`: + ! These arguments contains unknowns: `q`. See the `finalize()` function. --- Code grid_regular(p2) - Error - These arguments contains unknowns: `mtry`. See the `finalize()` function. + Condition + Error in `grid_regular()`: + ! These arguments contains unknowns: `mtry`. See the `finalize()` function. --- Code grid_random(p1) - Error - These arguments contains unknowns: `q`. See the `finalize()` function. + Condition + Error in `grid_random()`: + ! These arguments contains unknowns: `q`. See the `finalize()` function. --- Code grid_random(p2) - Error - These arguments contains unknowns: `mtry`. See the `finalize()` function. + Condition + Error in `grid_random()`: + ! These arguments contains unknowns: `mtry`. See the `finalize()` function. --- Code grid_latin_hypercube(p1) - Error - These arguments contains unknowns: `q`. See the `finalize()` function. + Condition + Error in `grid_latin_hypercube()`: + ! These arguments contains unknowns: `q`. See the `finalize()` function. --- Code grid_latin_hypercube(p2) - Error - These arguments contains unknowns: `mtry`. See the `finalize()` function. + Condition + Error in `grid_latin_hypercube()`: + ! These arguments contains unknowns: `mtry`. See the `finalize()` function. --- Code grid_max_entropy(p1) - Error - These arguments contains unknowns: `q`. See the `finalize()` function. + Condition + Error in `grid_max_entropy()`: + ! These arguments contains unknowns: `q`. See the `finalize()` function. --- Code grid_max_entropy(p2) - Error - These arguments contains unknowns: `mtry`. See the `finalize()` function. + Condition + Error in `grid_max_entropy()`: + ! These arguments contains unknowns: `mtry`. See the `finalize()` function. --- Code grid_regular(min_n(), q = mtry()) - Error - These arguments contains unknowns: `q`. See the `finalize()` function. + Condition + Error in `grid_regular()`: + ! These arguments contains unknowns: `q`. See the `finalize()` function. --- Code grid_regular(mtry()) - Error - These arguments contains unknowns: `mtry`. See the `finalize()` function. + Condition + Error in `grid_regular()`: + ! These arguments contains unknowns: `mtry`. See the `finalize()` function. --- Code grid_random(min_n(), q = mtry()) - Error - These arguments contains unknowns: `q`. See the `finalize()` function. + Condition + Error in `grid_random()`: + ! These arguments contains unknowns: `q`. See the `finalize()` function. --- Code grid_random(mtry()) - Error - These arguments contains unknowns: `mtry`. See the `finalize()` function. + Condition + Error in `grid_random()`: + ! These arguments contains unknowns: `mtry`. See the `finalize()` function. --- Code grid_regular(min_n(), q = mtry()) - Error - These arguments contains unknowns: `q`. See the `finalize()` function. + Condition + Error in `grid_regular()`: + ! These arguments contains unknowns: `q`. See the `finalize()` function. --- Code grid_latin_hypercube(mtry()) - Error - These arguments contains unknowns: `mtry`. See the `finalize()` function. + Condition + Error in `grid_latin_hypercube()`: + ! These arguments contains unknowns: `mtry`. See the `finalize()` function. --- Code grid_max_entropy(min_n(), q = mtry()) - Error - These arguments contains unknowns: `q`. See the `finalize()` function. + Condition + Error in `grid_max_entropy()`: + ! These arguments contains unknowns: `q`. See the `finalize()` function. --- Code grid_max_entropy(mtry()) - Error - These arguments contains unknowns: `mtry`. See the `finalize()` function. + Condition + Error in `grid_max_entropy()`: + ! These arguments contains unknowns: `mtry`. See the `finalize()` function. diff --git a/tests/testthat/_snaps/constructors.md b/tests/testthat/_snaps/constructors.md index 9040aacd..c8a6fc69 100644 --- a/tests/testthat/_snaps/constructors.md +++ b/tests/testthat/_snaps/constructors.md @@ -1,3 +1,131 @@ +# qualitative parameter object creation - bad args + + Code + new_qual_param("character", 1:2) + Condition + Error in `new_qual_param()`: + ! `values` must be character + +--- + + Code + new_qual_param("logical", letters[1:2]) + Condition + Error in `new_qual_param()`: + ! `values` must be logical + +# quantitative parameter object creation - bad args + + Code + new_quant_param("mucus", range = 1:2, inclusive = c(TRUE, TRUE)) + Condition + Error in `match.arg()`: + ! 'arg' should be one of "double", "integer" + +--- + + Code + new_quant_param("double", range = 1, inclusive = c(TRUE, TRUE)) + Condition + Error in `new_quant_param()`: + ! `label` should be a single named character string or NULL. + +--- + + Code + new_quant_param("double", range = c(1, NA), inclusive = c(TRUE, TRUE)) + Condition + Error in `new_quant_param()`: + ! `label` should be a single named character string or NULL. + +--- + + Code + new_quant_param("double", range = c(1, NA), inclusive = TRUE) + Condition + Error in `new_quant_param()`: + ! `inclusive` must have upper and lower values. + +--- + + Code + new_quant_param("double", range = c(1, NA), inclusive = c("(", "]")) + Condition + Error in `new_quant_param()`: + ! `inclusive` should be logical + +--- + + Code + new_quant_param("double", range = c(1, NA), inclusive = c(TRUE, TRUE)) + Condition + Error in `new_quant_param()`: + ! `label` should be a single named character string or NULL. + +--- + + Code + new_quant_param("double", range = 1:2, inclusive = c(TRUE, NA)) + Condition + Error in `new_quant_param()`: + ! Since `type = 'double'`, please use that data type for the range. + +--- + + Code + new_quant_param("double", range = 1:2, inclusive = c(TRUE, unknown())) + Condition + Error in `new_quant_param()`: + ! Since `type = 'double'`, please use that data type for the range. + +--- + + Code + new_quant_param("double", range = 1:2, inclusive = c(TRUE, TRUE), trans = log) + Condition + Error in `new_quant_param()`: + ! `trans` must be a 'trans' class object (or NULL). See `?scales::trans_new`. + +--- + + Code + new_quant_param("double", range = 1:2, inclusive = c(TRUE, TRUE), values = 1:4) + Condition + Error in `new_quant_param()`: + ! Since `type = 'double'`, please use that data type for the range. + +# bad args to range_validate + + Code + range_validate(mtry(), range = 1) + Condition + Error in `range_validate()`: + ! `range` must have an upper and lower bound. `Inf` and `unknown()` are acceptable values. + +--- + + Code + range_validate(mtry(), range = c(1, NA)) + Condition + Error in `range_validate()`: + ! Value ranges must be non-missing. + +--- + + Code + range_validate(mtry(), range = c(1, unknown()), FALSE) + Condition + Error in `range_validate()`: + ! Cannot validate ranges when they contains 1+ unknown values. + +--- + + Code + range_validate(mtry(), range = letters[1:2]) + Condition + Error in `range_validate()`: + ! Value ranges must be numeric. + # printing Code @@ -29,48 +157,55 @@ Code mixture(c(1L, 3L)) - Error - Since `type = 'double'`, please use that data type for the range. + Condition + Error in `new_quant_param()`: + ! Since `type = 'double'`, please use that data type for the range. --- Code mixture(c(1L, unknown())) - Error - Since `type = 'double'`, please use that data type for the range. + Condition + Error in `new_quant_param()`: + ! Since `type = 'double'`, please use that data type for the range. --- Code mixture(c(unknown(), 1L)) - Error - Since `type = 'double'`, please use that data type for the range. + Condition + Error in `new_quant_param()`: + ! Since `type = 'double'`, please use that data type for the range. --- Code mixture(letters[1:2]) - Error - Since `type = 'double'`, please use that data type for the range. + Condition + Error in `new_quant_param()`: + ! Since `type = 'double'`, please use that data type for the range. --- Code mtry(c(0.1, 0.5)) - Error - An integer is required for the range and these do not appear to be whole numbers: 0.1, 0.5 + Condition + Error in `new_quant_param()`: + ! An integer is required for the range and these do not appear to be whole numbers: 0.1, 0.5 --- Code mtry(c(0.1, unknown())) - Error - An integer is required for the range and these do not appear to be whole numbers: 0.1 + Condition + Error in `new_quant_param()`: + ! An integer is required for the range and these do not appear to be whole numbers: 0.1 --- Code mtry(c(unknown(), 0.5)) - Error - An integer is required for the range and these do not appear to be whole numbers: 0.5 + Condition + Error in `new_quant_param()`: + ! An integer is required for the range and these do not appear to be whole numbers: 0.5 diff --git a/tests/testthat/_snaps/encode_unit.md b/tests/testthat/_snaps/encode_unit.md new file mode 100644 index 00000000..61f7b553 --- /dev/null +++ b/tests/testthat/_snaps/encode_unit.md @@ -0,0 +1,117 @@ +# bad args + + Code + encode_unit(2, prune_method()$values, direction = "forward") + Condition + Error in `encode_unit()`: + ! `x` should be a dials parameter object. + i This is an internal error, please report it to the package authors. + +--- + + Code + encode_unit(z, prune_method()$values, direction = "forwards") + Condition + Error in `encode_unit()`: + ! `direction` should be either 'forward' or 'backward' + i This is an internal error, please report it to the package authors. + +--- + + Code + encode_unit(x, prune_method()$values, direction = "forward") + Condition + Error in `encode_unit()`: + ! `value` should be a numeric vector. + i This is an internal error, please report it to the package authors. + +--- + + Code + encode_unit(z, 1, direction = "forward") + Condition + Error in `encode_unit()`: + ! `value` should be a character vector. + i This is an internal error, please report it to the package authors. + +--- + + Code + encode_unit(x, matrix(letters[1:4], ncol = 2), direction = "forward") + Condition + Error in `encode_unit()`: + ! `value` should be a numeric vector. + i This is an internal error, please report it to the package authors. + +--- + + Code + encode_unit(x, matrix(1:4, ncol = 2), direction = "forward") + Condition + Error in `encode_unit()`: + ! `value` should be a numeric vector. + i This is an internal error, please report it to the package authors. + +--- + + Code + encode_unit(z, 1, direction = "forward") + Condition + Error in `encode_unit()`: + ! `value` should be a character vector. + i This is an internal error, please report it to the package authors. + +--- + + Code + encode_unit(z, matrix(1:4, ncol = 2), direction = "forward") + Condition + Error in `encode_unit()`: + ! `value` should be a character vector. + i This is an internal error, please report it to the package authors. + +--- + + Code + encode_unit(z, matrix(letters[1:4], ncol = 2), direction = "forward") + Condition + Error in `encode_unit()`: + ! `value` should be a character vector. + i This is an internal error, please report it to the package authors. + +--- + + Code + encode_unit(x, prune_method()$values, direction = "backward") + Condition + Error in `encode_unit()`: + ! `value` should be a numeric vector. + i This is an internal error, please report it to the package authors. + +--- + + Code + encode_unit(z, prune_method()$values, direction = "backward") + Condition + Error in `encode_unit()`: + ! Values should be on [0, 1]. + i This is an internal error, please report it to the package authors. + +--- + + Code + encode_unit(x, 1:2, direction = "backward") + Condition + Error in `encode_unit()`: + ! Values should be on [0, 1]. + i This is an internal error, please report it to the package authors. + +--- + + Code + encode_unit(z, 1:2, direction = "backward") + Condition + Error in `encode_unit()`: + ! Values should be on [0, 1]. + i This is an internal error, please report it to the package authors. + diff --git a/tests/testthat/_snaps/extract_parameter_dials.md b/tests/testthat/_snaps/extract_parameter_dials.md index c59009b7..73c171f3 100644 --- a/tests/testthat/_snaps/extract_parameter_dials.md +++ b/tests/testthat/_snaps/extract_parameter_dials.md @@ -2,48 +2,55 @@ Code extract_parameter_dials(mod_param, "lambdas") - Error - No parameter exists with id 'lambdas'. + Condition + Error in `extract_parameter_dials()`: + ! No parameter exists with id 'lambdas'. --- Code extract_parameter_dials(mod_param) - Error - Please supply a single 'parameter' string. + Condition + Error in `extract_parameter_dials()`: + ! Please supply a single 'parameter' string. --- Code extract_parameter_dials(mod_param, 1) - Error - Please supply a single 'parameter' string. + Condition + Error in `extract_parameter_dials()`: + ! Please supply a single 'parameter' string. --- Code extract_parameter_dials(mod_param, 1:2) - Error - Please supply a single 'parameter' string. + Condition + Error in `extract_parameter_dials()`: + ! Please supply a single 'parameter' string. --- Code extract_parameter_dials(mod_param, letters[1:2]) - Error - Please supply a single 'parameter' string. + Condition + Error in `extract_parameter_dials()`: + ! Please supply a single 'parameter' string. --- Code extract_parameter_dials(mod_param, NA_character_) - Error - Please supply a single 'parameter' string. + Condition + Error in `extract_parameter_dials()`: + ! Please supply a single 'parameter' string. --- Code extract_parameter_dials(mod_param, "") - Error - Please supply a single 'parameter' string. + Condition + Error in `extract_parameter_dials()`: + ! Please supply a single 'parameter' string. diff --git a/tests/testthat/_snaps/finalize.md b/tests/testthat/_snaps/finalize.md new file mode 100644 index 00000000..b33d06e7 --- /dev/null +++ b/tests/testthat/_snaps/finalize.md @@ -0,0 +1,56 @@ +# estimate columns + + Code + get_p(1:10) + Condition + Error in `get_p()`: + ! `object` should be a 'param' object. + +--- + + Code + get_p(1:10, 1:10) + Condition + Error in `get_p()`: + ! `object` should be a 'param' object. + +--- + + Code + get_p(mtry(), 1:10) + Condition + Error in `get_p()`: + ! Cannot determine number of columns. Is `x` a 2D data object? + +# estimate rows + + Code + get_n(1:10) + Condition + Error in `get_n_frac()`: + ! `object` should be a 'param' object. + +--- + + Code + get_n(1:10, 1:10) + Condition + Error in `get_n_frac()`: + ! `object` should be a 'param' object. + +--- + + Code + get_n(mtry(), 1:10) + Condition + Error in `get_n_frac()`: + ! Cannot determine number of columns. Is `x` a 2D data object? + +# estimate sigma + + Code + get_rbf_range(rbf_sigma(), iris) + Condition + Error in `get_rbf_range()`: + ! The matrix version of the initialization data is not numeric. + diff --git a/tests/testthat/_snaps/grids.md b/tests/testthat/_snaps/grids.md index f29864b9..4cc10f37 100644 --- a/tests/testthat/_snaps/grids.md +++ b/tests/testthat/_snaps/grids.md @@ -1,8 +1,17 @@ +# regular grid + + Code + grid_regular(mixture(), trees(), levels = 1:4) + Condition + Error in `grid_regular()`: + ! `levels` should have length 1 or 2 + # wrong argument name Code grid_latin_hypercube(p, levels = 5) - Warning + Condition + Warning: `levels` is not an argument to `grid_latin_hypercube()`. Did you mean `size`? Output # A tibble: 3 x 2 @@ -16,7 +25,8 @@ Code grid_max_entropy(p, levels = 5) - Warning + Condition + Warning: `levels` is not an argument to `grid_max_entropy()`. Did you mean `size`? Output # A tibble: 3 x 2 @@ -30,7 +40,8 @@ Code grid_random(p, levels = 5) - Warning + Condition + Warning: `levels` is not an argument to `grid_random()`. Did you mean `size`? Output # A tibble: 5 x 2 @@ -46,7 +57,8 @@ Code grid_regular(p, size = 5) - Warning + Condition + Warning: `size` is not an argument to `grid_regular()`. Did you mean `levels`? Output # A tibble: 9 x 2 diff --git a/tests/testthat/_snaps/misc.md b/tests/testthat/_snaps/misc.md new file mode 100644 index 00000000..e13eaabf --- /dev/null +++ b/tests/testthat/_snaps/misc.md @@ -0,0 +1,8 @@ +# package install checks + + Code + dials:::check_installs("pistachio") + Condition + Error in `dials:::check_installs()`: + ! Package(s) not installed: 'pistachio' + diff --git a/tests/testthat/_snaps/parameters.md b/tests/testthat/_snaps/parameters.md index b1254252..21cd8dee 100644 --- a/tests/testthat/_snaps/parameters.md +++ b/tests/testthat/_snaps/parameters.md @@ -2,29 +2,33 @@ Code parameters(list(a = mtry(), a = penalty())) - Error - Element `id` should have unique values. Duplicates exist for item(s): 'a' + Condition + Error in `parameters_constr()`: + ! Element `id` should have unique values. Duplicates exist for item(s): 'a' # updating Code update(p_1, new_pen) - Error - All arguments should be named. + Condition + Error in `update()`: + ! All arguments should be named. --- Code update(p_1, penalty = 1:2) - Error - At least one parameter is not a dials parameter object or NA: 'penalty' + Condition + Error in `update()`: + ! At least one parameter is not a dials parameter object or NA: 'penalty' --- Code update(p_1, penalty(), mtry = mtry(3:4)) - Error - All arguments should be named. + Condition + Error in `update()`: + ! All arguments should be named. # printing diff --git a/tests/testthat/_snaps/pull_dials_object.md b/tests/testthat/_snaps/pull_dials_object.md index bf8de236..6fd2ba24 100644 --- a/tests/testthat/_snaps/pull_dials_object.md +++ b/tests/testthat/_snaps/pull_dials_object.md @@ -2,7 +2,8 @@ Code pull_dials_object(mod_param, "mixture") - Warning + Condition + Warning: `pull_dials_object()` was deprecated in dials 0.1.0. Please use `hardhat::extract_parameter_dials()` instead. Output @@ -13,48 +14,55 @@ Code pull_dials_object(mod_param, "lambdas") - Error - No parameter exists with id 'lambdas'. + Condition + Error in `pull_dials_object()`: + ! No parameter exists with id 'lambdas'. --- Code pull_dials_object(mod_param) - Error - Please supply a single 'id' string. + Condition + Error in `pull_dials_object()`: + ! Please supply a single 'id' string. --- Code pull_dials_object(mod_param, 1) - Error - Please supply a single 'id' string. + Condition + Error in `pull_dials_object()`: + ! Please supply a single 'id' string. --- Code pull_dials_object(mod_param, 1:2) - Error - Please supply a single 'id' string. + Condition + Error in `pull_dials_object()`: + ! Please supply a single 'id' string. --- Code pull_dials_object(mod_param, letters[1:2]) - Error - Please supply a single 'id' string. + Condition + Error in `pull_dials_object()`: + ! Please supply a single 'id' string. --- Code pull_dials_object(mod_param, NA_character_) - Error - Please supply a single 'id' string. + Condition + Error in `pull_dials_object()`: + ! Please supply a single 'id' string. --- Code pull_dials_object(mod_param, "") - Error - Please supply a single 'id' string. + Condition + Error in `pull_dials_object()`: + ! Please supply a single 'id' string. diff --git a/tests/testthat/_snaps/space_filling.md b/tests/testthat/_snaps/space_filling.md new file mode 100644 index 00000000..445b2c7f --- /dev/null +++ b/tests/testthat/_snaps/space_filling.md @@ -0,0 +1,16 @@ +# max entropy designs + + Code + grid_max_entropy(mtry(), size = 11, original = FALSE) + Condition + Error in `grid_max_entropy()`: + ! These arguments contains unknowns: `mtry`. See the `finalize()` function. + +# latin square designs + + Code + grid_latin_hypercube(mtry(), size = 11, original = FALSE) + Condition + Error in `grid_latin_hypercube()`: + ! These arguments contains unknowns: `mtry`. See the `finalize()` function. + diff --git a/tests/testthat/_snaps/values.md b/tests/testthat/_snaps/values.md index 8d9543f4..4368091c 100644 --- a/tests/testthat/_snaps/values.md +++ b/tests/testthat/_snaps/values.md @@ -1,9 +1,50 @@ +# transforms with unknowns + + Code + value_transform(penalty(), unknown()) + Condition + Error in `value_transform()`: + ! Unknowns not allowed. + +--- + + Code + value_transform(penalty(), c(unknown(), 1, unknown())) + Condition + Error in `value_transform()`: + ! Unknowns not allowed. + +--- + + Code + value_inverse(penalty(), unknown()) + Condition + Error in `value_inverse()`: + ! Unknowns not allowed. + +--- + + Code + value_inverse(penalty(), c(unknown(), 1, unknown())) + Condition + Error in `value_inverse()`: + ! Unknowns not allowed. + # transforms Code value_object <- value_transform(penalty(), -1:3) - Warning + Condition + Warning in `log()`: NaNs produced Code value_expected <- c(NaN, -Inf, log10(1:3)) +# validate unknowns + + Code + value_validate(mtry(), 17) + Condition + Error in `value_validate()`: + ! Unknowns not allowed. + diff --git a/tests/testthat/test-constructors.R b/tests/testthat/test-constructors.R index 546e31fe..cb94d675 100644 --- a/tests/testthat/test-constructors.R +++ b/tests/testthat/test-constructors.R @@ -1,59 +1,59 @@ test_that('qualitative parameter object creation - bad args', { - expect_error( + expect_snapshot(error = TRUE, new_qual_param("character", 1:2) ) - expect_error( + expect_snapshot(error = TRUE, new_qual_param("logical", letters[1:2]) ) }) test_that('quantitative parameter object creation - bad args', { - expect_error( + expect_snapshot(error = TRUE, new_quant_param("mucus", range = 1:2, inclusive = c(TRUE, TRUE)) ) - expect_error( + expect_snapshot(error = TRUE, new_quant_param("double", range = 1, inclusive = c(TRUE, TRUE)) ) - expect_error( + expect_snapshot(error = TRUE, new_quant_param("double", range = c(1, NA), inclusive = c(TRUE, TRUE)) ) - expect_error( + expect_snapshot(error = TRUE, new_quant_param("double", range = c(1, NA), inclusive = TRUE) ) - expect_error( + expect_snapshot(error = TRUE, new_quant_param("double", range = c(1, NA), inclusive = c("(", "]")) ) - expect_error( + expect_snapshot(error = TRUE, new_quant_param("double", range = c(1, NA), inclusive = c(TRUE, TRUE)) ) - expect_error( + expect_snapshot(error = TRUE, new_quant_param("double", range = 1:2, inclusive = c(TRUE, NA)) ) - expect_error( + expect_snapshot(error = TRUE, new_quant_param("double", range = 1:2, inclusive = c(TRUE, unknown())) ) - expect_error( + expect_snapshot(error = TRUE, new_quant_param("double", range = 1:2, inclusive = c(TRUE, TRUE), trans = log) ) - expect_error( + expect_snapshot(error = TRUE, new_quant_param("double", range = 1:2, inclusive = c(TRUE, TRUE), values = 1:4) ) }) test_that('bad args to range_validate', { - expect_error( + expect_snapshot(error = TRUE, range_validate(mtry(), range = 1) ) - expect_error( + expect_snapshot(error = TRUE, range_validate(mtry(), range = c(1, NA)) ) - expect_error( + expect_snapshot(error = TRUE, range_validate(mtry(), range = c(1, unknown()), FALSE) ) - expect_error( + expect_snapshot(error = TRUE, range_validate(mtry(), range = letters[1:2]) ) diff --git a/tests/testthat/test-encode_unit.R b/tests/testthat/test-encode_unit.R index ec0cb75e..6856a118 100644 --- a/tests/testthat/test-encode_unit.R +++ b/tests/testthat/test-encode_unit.R @@ -48,45 +48,45 @@ test_that('bad args', { z_0 <- encode_unit(z, prune_method()$values, direction = "forward") x_0 <- encode_unit(x, 2:7, direction = "forward") - expect_error( + expect_snapshot(error = TRUE, encode_unit(2, prune_method()$values, direction = "forward") ) - expect_error( + expect_snapshot(error = TRUE, encode_unit(z, prune_method()$values, direction = "forwards") ) - expect_error( + expect_snapshot(error = TRUE, encode_unit(x, prune_method()$values, direction = "forward") ) - expect_error( + expect_snapshot(error = TRUE, encode_unit(z, 1, direction = "forward") ) - expect_error( + expect_snapshot(error = TRUE, encode_unit(x, matrix(letters[1:4], ncol = 2), direction = "forward") ) - expect_error( + expect_snapshot(error = TRUE, encode_unit(x, matrix(1:4, ncol = 2), direction = "forward") ) - expect_error( + expect_snapshot(error = TRUE, encode_unit(z, 1, direction = "forward") ) - expect_error( + expect_snapshot(error = TRUE, encode_unit(z, matrix(1:4, ncol = 2), direction = "forward") ) - expect_error( + expect_snapshot(error = TRUE, encode_unit(z, matrix(letters[1:4], ncol = 2), direction = "forward") ) - expect_error( + expect_snapshot(error = TRUE, encode_unit(x, prune_method()$values, direction = "backward") ) - expect_error( + expect_snapshot(error = TRUE, encode_unit(z, prune_method()$values, direction = "backward") ) - expect_error( + expect_snapshot(error = TRUE, encode_unit(x, 1:2, direction = "backward") ) - expect_error( + expect_snapshot(error = TRUE, encode_unit(z, 1:2, direction = "backward") ) }) diff --git a/tests/testthat/test-finalize.R b/tests/testthat/test-finalize.R index 3be8187d..bf291df0 100644 --- a/tests/testthat/test-finalize.R +++ b/tests/testthat/test-finalize.R @@ -2,9 +2,9 @@ suppressMessages(library(kernlab)) test_that('estimate columns', { - expect_error(get_p(1:10)) - expect_error(get_p(1:10, 1:10)) - expect_error(get_p(mtry(), 1:10)) + expect_snapshot(error = TRUE, get_p(1:10)) + expect_snapshot(error = TRUE, get_p(1:10, 1:10)) + expect_snapshot(error = TRUE, get_p(mtry(), 1:10)) expect_equal( range_get(get_p(mtry(), mtcars)), @@ -18,9 +18,9 @@ test_that('estimate columns', { test_that('estimate rows', { - expect_error(get_n(1:10)) - expect_error(get_n(1:10, 1:10)) - expect_error(get_n(mtry(), 1:10)) + expect_snapshot(error = TRUE, get_n(1:10)) + expect_snapshot(error = TRUE, get_n(1:10, 1:10)) + expect_snapshot(error = TRUE, get_n(mtry(), 1:10)) expect_equal( range_get(get_n_frac(mtry_long(), mtcars, log_vals = TRUE), original = FALSE), @@ -48,7 +48,7 @@ test_that('estimate rows', { test_that('estimate sigma', { - expect_error(get_rbf_range(rbf_sigma(), iris)) + expect_snapshot(error = TRUE, get_rbf_range(rbf_sigma(), iris)) run_1 <- range_get(get_rbf_range(rbf_sigma(), mtcars, seed = 5624)) run_2 <- range_get(get_rbf_range(rbf_sigma(), mtcars, seed = 5624)) diff --git a/tests/testthat/test-grids.R b/tests/testthat/test-grids.R index d65996e7..444d296c 100644 --- a/tests/testthat/test-grids.R +++ b/tests/testthat/test-grids.R @@ -6,7 +6,7 @@ test_that('regular grid', { expect_error( grid_regular() ) - expect_error( + expect_snapshot(error = TRUE, grid_regular(mixture(), trees(), levels = 1:4) ) expect_equal( diff --git a/tests/testthat/test-misc.R b/tests/testthat/test-misc.R index ec732bd8..79a0fb60 100644 --- a/tests/testthat/test-misc.R +++ b/tests/testthat/test-misc.R @@ -1,7 +1,7 @@ test_that('package install checks', { - expect_error(dials:::check_installs("pistachio")) + expect_snapshot(error = TRUE, dials:::check_installs("pistachio")) expect_error( dials:::check_installs("dials"), NA diff --git a/tests/testthat/test-space_filling.R b/tests/testthat/test-space_filling.R index 62fae48f..dfb49b41 100644 --- a/tests/testthat/test-space_filling.R +++ b/tests/testthat/test-space_filling.R @@ -31,7 +31,7 @@ test_that('max entropy designs', { original = FALSE ) ) - expect_error( + expect_snapshot(error = TRUE, grid_max_entropy( mtry(), size = 11, @@ -83,7 +83,7 @@ test_that('latin square designs', { original = FALSE ) ) - expect_error( + expect_snapshot(error = TRUE, grid_latin_hypercube( mtry(), size = 11, diff --git a/tests/testthat/test-values.R b/tests/testthat/test-values.R index 9b47e495..5452c7d4 100644 --- a/tests/testthat/test-values.R +++ b/tests/testthat/test-values.R @@ -1,21 +1,22 @@ test_that('transforms with unknowns', { - expect_error( + expect_snapshot(error = TRUE, value_transform(penalty(), unknown()) ) - expect_error( + expect_snapshot(error = TRUE, value_transform(penalty(), c(unknown(), 1, unknown())) ) - expect_error( + expect_snapshot(error = TRUE, value_inverse(penalty(), unknown()) ) - expect_error( + expect_snapshot(error = TRUE, value_inverse(penalty(), c(unknown(), 1, unknown())) ) }) test_that('transforms', { + skip_if_below_r_version("3.5") expect_equal( value_transform(penalty(), 1:3), log10(1:3) ) @@ -301,3 +302,9 @@ test_that('sampling - character and logical', { sort(unique(value_sample(prune(), 500))), sort(prune()$values) ) }) + +test_that("validate unknowns", { + expect_snapshot(error = TRUE, + value_validate(mtry(), 17) + ) +})