Skip to content

Commit 2af856e

Browse files
plietarhadley
andauthored
Improve errors when a non-vector is passed to expect_vector. (#2229)
The `expect_vector` function delegates most of its logic to `vctrs::vec_assert`, which can throw one of two errors, `vctrs_error_assert` and `vctrs_error_scalar_type`. Only the former one was being handled, not the latter. This doesn't affect the behaviour of tests much, but it produces sub-par error messages and causes failing tests to terminate immediately rather than recording the error and continuing like other assertions do. --------- Co-authored-by: Hadley Wickham <[email protected]>
1 parent fc2d48d commit 2af856e

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
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+
* `expect_vector()` fails, instead of erroring, if `object` is not a vector (@plietar, #2224).
34
* New `vignette("mocking")` explains mocking in detail (#1265).
45
* New `vignette("challenging-functions")` provides an index to other documentation organised by testing challenges (#1265).
56
* When running a test interactively, testthat now reports the number of succeses. The results should also be more useful if you are using nested tests.

R/expect-vector.R

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,20 @@
1717
expect_vector <- function(object, ptype = NULL, size = NULL) {
1818
check_installed("vctrs")
1919
check_number_whole(size, min = 0, allow_null = TRUE)
20-
2120
act <- quasi_label(enquo(object))
21+
# vec_assert() automatically adds backticks so we hack out the ones
22+
# added by as_label()
23+
act$lab <- gsub("^`|`$", "", act$lab)
2224

23-
message <- NULL
24-
tryCatch(
25+
withCallingHandlers(
2526
vctrs::vec_assert(act$val, ptype = ptype, size = size, arg = act$lab),
27+
vctrs_error_scalar_type = function(e) {
28+
fail(e$message)
29+
},
2630
vctrs_error_assert = function(e) {
27-
message <<- e$message
31+
fail(e$message)
2832
}
2933
)
3034

31-
if (!is.null(message)) {
32-
return(fail(message))
33-
}
3435
pass(act$val)
3536
}

tests/testthat/_snaps/expect-vector.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
# basic properties upheld
2+
3+
Code
4+
expect_vector(x, size = 5)
5+
Condition
6+
Error:
7+
! `x` must have size 5, not size 10.
8+
9+
---
10+
11+
Code
12+
expect_vector(y)
13+
Condition
14+
Error:
15+
! `y` must be a vector, not `NULL`.
16+
117
# expect_vector validates its inputs
218

319
Code

tests/testthat/test-expect-vector.R

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ test_that("basic properties upheld", {
22
skip_if_not_installed("vctrs", "0.1.0.9002")
33

44
expect_success(expect_vector(1:10, size = 10))
5-
expect_failure(expect_vector(1:10, size = 5))
5+
6+
x <- 1:10
7+
expect_snapshot_failure(expect_vector(x, size = 5))
8+
9+
y <- NULL
10+
expect_snapshot_failure(expect_vector(y))
611
})
712

813
test_that("expect_vector validates its inputs", {

0 commit comments

Comments
 (0)