diff --git a/NEWS.md b/NEWS.md index 3fa3f7e5d..78fd1ed9b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # testthat (development version) +* `local_edition()` now gives a useful error for bad values (#1547). * testthat now requires R 4.1. * `expect_s4_class()` now supports unquoting (@stibu81, #2064). * `it()` now finds the correct evaluation environment in more cases (@averissimo, #2085). diff --git a/R/edition.R b/R/edition.R index 333f53322..c2bf9c9dd 100644 --- a/R/edition.R +++ b/R/edition.R @@ -61,8 +61,7 @@ edition_name <- function(x) { #' @param x Edition Should be a single integer. #' @param .env Environment that controls scope of changes. For expert use only. local_edition <- function(x, .env = parent.frame()) { - stopifnot(is_zap(x) || (is.numeric(x) && length(x) == 1)) - + check_number_whole(x, min = 2, max = 3) local_bindings(edition = x, .env = the, .frame = .env) } diff --git a/tests/testthat/_snaps/edition.md b/tests/testthat/_snaps/edition.md index da4619866..82cb92c69 100644 --- a/tests/testthat/_snaps/edition.md +++ b/tests/testthat/_snaps/edition.md @@ -1,3 +1,16 @@ +# checks its inputs + + Code + local_edition("x") + Condition + Error in `local_edition()`: + ! `x` must be a whole number, not the string "x". + Code + local_edition(5) + Condition + Error in `local_edition()`: + ! `x` must be a whole number between 2 and 3, not the number 5. + # deprecation only fired for newer edition Code diff --git a/tests/testthat/test-edition.R b/tests/testthat/test-edition.R index c01c28b50..9e3810fc2 100644 --- a/tests/testthat/test-edition.R +++ b/tests/testthat/test-edition.R @@ -6,6 +6,13 @@ test_that("can locally override edition", { expect_equal(edition_get(), 2) }) +test_that("checks its inputs", { + expect_snapshot(error = TRUE, { + local_edition("x") + local_edition(5) + }) +}) + test_that("deprecation only fired for newer edition", { local_edition(2) expect_warning(edition_deprecate(3, "old stuff"), NA) @@ -34,7 +41,8 @@ test_that("edition for non-package dir is 2", { }) test_that("can set the edition via an environment variable", { - local_edition(zap()) + local_bindings(edition = zap(), .env = the) + withr::local_envvar(TESTTHAT_EDITION = 2) expect_equal(edition_get(), 2)