Skip to content

Commit 8c8964b

Browse files
committed
Use existing check functions
1 parent f68880c commit 8c8964b

File tree

4 files changed

+22
-22
lines changed

4 files changed

+22
-22
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# testthat (development version)
22

3+
* `local_edition()` now gives a useful error for bad values (#1547).
34
* testthat now requires R 4.1.
45
* `expect_s4_class()` now supports unquoting (@stibu81, #2064).
56
* `it()` now finds the correct evaluation environment in more cases (@averissimo, #2085).

R/edition.R

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,6 @@ edition_name <- function(x) {
5151
}
5252
}
5353

54-
is_valid_edition <- function(x) {
55-
if (is_zap(x)) {
56-
TRUE
57-
} else {
58-
is.numeric(x) && length(x) == 1 && x %in% c(2, 3)
59-
}
60-
}
61-
6254
#' Temporarily change the active testthat edition
6355
#'
6456
#' `local_edition()` allows you to temporarily (within a single test or
@@ -69,10 +61,7 @@ is_valid_edition <- function(x) {
6961
#' @param x Edition Should be a single integer.
7062
#' @param .env Environment that controls scope of changes. For expert use only.
7163
local_edition <- function(x, .env = parent.frame()) {
72-
if (!is_valid_edition(x)) {
73-
stop("Available editions are 2 and 3", call. = FALSE)
74-
}
75-
64+
check_number_whole(x, min = 2, max = 3)
7665
local_bindings(edition = x, .env = the, .frame = .env)
7766
}
7867

tests/testthat/_snaps/edition.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
# checks its inputs
2+
3+
Code
4+
local_edition("x")
5+
Condition
6+
Error in `local_edition()`:
7+
! `x` must be a whole number, not the string "x".
8+
Code
9+
local_edition(5)
10+
Condition
11+
Error in `local_edition()`:
12+
! `x` must be a whole number between 2 and 3, not the number 5.
13+
114
# deprecation only fired for newer edition
215

316
Code

tests/testthat/test-edition.R

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,11 @@ test_that("can locally override edition", {
66
expect_equal(edition_get(), 2)
77
})
88

9-
test_that("only existing editions can be set", {
10-
expect_error(
11-
local_edition(5),
12-
regexp = "Available editions are 2 and 3"
13-
)
14-
expect_error(
15-
local_edition(-1),
16-
regexp = "Available editions are 2 and 3"
17-
)
9+
test_that("checks its inputs", {
10+
expect_snapshot(error = TRUE, {
11+
local_edition("x")
12+
local_edition(5)
13+
})
1814
})
1915

2016
test_that("deprecation only fired for newer edition", {
@@ -45,7 +41,8 @@ test_that("edition for non-package dir is 2", {
4541
})
4642

4743
test_that("can set the edition via an environment variable", {
48-
local_edition(zap())
44+
local_bindings(edition = zap(), .env = the)
45+
4946
withr::local_envvar(TESTTHAT_EDITION = 2)
5047
expect_equal(edition_get(), 2)
5148

0 commit comments

Comments
 (0)