Skip to content

Commit 0e7ad15

Browse files
committed
Improve errors when a non-vector is passed to expect_vector.
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.
1 parent b684336 commit 0e7ad15

File tree

2 files changed

+4
-0
lines changed

2 files changed

+4
-0
lines changed

R/expect-vector.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ expect_vector <- function(object, ptype = NULL, size = NULL) {
2323
message <- NULL
2424
tryCatch(
2525
vctrs::vec_assert(act$val, ptype = ptype, size = size, arg = act$lab),
26+
vctrs_error_scalar_type = function(e) {
27+
message <<- e$message
28+
},
2629
vctrs_error_assert = function(e) {
2730
message <<- e$message
2831
}

tests/testthat/test-expect-vector.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ test_that("basic properties upheld", {
33

44
expect_success(expect_vector(1:10, size = 10))
55
expect_failure(expect_vector(1:10, size = 5))
6+
expect_failure(expect_vector(NULL))
67
})
78

89
test_that("expect_vector validates its inputs", {

0 commit comments

Comments
 (0)