Skip to content

Commit

Permalink
test find_tune_id()
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpcouch committed Oct 16, 2024
1 parent 1117bce commit 1e21f19
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
4 changes: 2 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ no_param <-
component_id = character(0)
)

find_tune_id <- function(x, call = caller_env()) {
find_tune_id <- function(x, arg = caller_arg(x), call = caller_env()) {
if (length(x) == 0L) {
return(NA_character_)
}
Expand Down Expand Up @@ -121,7 +121,7 @@ find_tune_id <- function(x, call = caller_env()) {
cli::cli_abort(
c(
"Only one tunable value is currently allowed per argument.",
"The current argument has {.val {offenders}}."
"{.arg {arg}} has {.code {offenders}}."
),
call = call
)
Expand Down
31 changes: 31 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,34 @@ test_that("fit.tailor() errors informatively with incompatible outcome", {
)
)
})

test_that("find_tune_id() works", {
# empty input
expect_equal(find_tune_id(list()), NA_character_)

# handles quosures
x <- rlang::quos(a = 1, b = tune())
expect_equal(find_tune_id(x), "")

# non-tunable atomic values
expect_equal(find_tune_id(1), NA_character_)
expect_equal(find_tune_id("a"), NA_character_)
expect_equal(find_tune_id(TRUE), NA_character_)

# non-tunable names
expect_equal(find_tune_id(quote(x)), NA_character_)

# nested lists
x <- list(a = 1, b = list(c = hardhat::tune(), d = 2))
expect_equal(find_tune_id(x), "")

# tune() without id
expect_equal(find_tune_id(hardhat::tune()), "")

# tune() with id
expect_equal(find_tune_id(hardhat::tune("test_id")), "test_id")

# multiple tunable values
x <- list(a = hardhat::tune(), b = hardhat::tune())
expect_snapshot(error = TRUE, find_tune_id(x))
})

0 comments on commit 1e21f19

Please sign in to comment.