diff --git a/R/edition.R b/R/edition.R index 9201e268a..b50b58f34 100644 --- a/R/edition.R +++ b/R/edition.R @@ -51,6 +51,14 @@ edition_name <- function(x) { } } +is_valid_edition <- function(x) { + if (is_zap(x)) { + TRUE + } else { + is.numeric(x) && length(x) == 1 && x %in% c(2, 3) + } +} + #' Temporarily change the active testthat edition #' #' `local_edition()` allows you to temporarily (within a single test or @@ -62,7 +70,9 @@ edition_name <- function(x) { #' @param .env Environment that controls scope of changes. For expert use only. #' @keywords internal local_edition <- function(x, .env = parent.frame()) { - stopifnot(is_zap(x) || (is.numeric(x) && length(x) == 1)) + if (!is_valid_edition(x)) { + stop("Available editions are 2 and 3", call. = FALSE) + } old <- edition_set(x) withr::defer(edition_set(old), envir = .env) } diff --git a/tests/testthat/test-edition.R b/tests/testthat/test-edition.R index 4fc4d19b1..5213ad22f 100644 --- a/tests/testthat/test-edition.R +++ b/tests/testthat/test-edition.R @@ -6,6 +6,17 @@ test_that("can locally override edition", { expect_equal(edition_get(), 2) }) +test_that("only existing editions can be set", { + expect_error( + local_edition(5), + regexp = "Available editions are 2 and 3" + ) + expect_error( + local_edition(-1), + regexp = "Available editions are 2 and 3" + ) +}) + test_that("deprecation only fired for newer edition", { local_edition(2) expect_warning(edition_deprecate(3, "old stuff"), NA)