Skip to content

expect_vector with NULL terminates the test prematurely #2224

@plietar

Description

@plietar

With all testthat expect_* functions, the tests keep running even after a failed check.

However I've noticed that this is not the case if the first argument of expect_vector is NULL. Instead a vctrs_error_scalar_type condition is thrown and the test terminates immediately.

For example, the following prints the two failed assertions:

test_that("expect_true", {
  expect_true(FALSE)
  expect_true(FALSE)
})

Whereas the test below aborts after the first line:

test_that("expect_vector", {
  expect_vector(NULL)
  expect_vector(NULL)
})
<vctrs_error_scalar_type/vctrs_error/rlang_error/error/condition>
Error in `expect_vector(NULL)`: `NULL` must be a vector, not `NULL`.
Backtrace:
    ▆
 1. └─testthat::expect_vector(NULL) at ../../foo.R:7:3
 2.   ├─base::tryCatch(...)
 3.   │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
 4.   │   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
 5.   │     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
 6.   └─vctrs::vec_assert(act$val, ptype = ptype, size = size, arg = act$lab)
 7.     └─vctrs:::stop_scalar_type(x, arg, call = call)
 8.       └─vctrs:::stop_vctrs(...)
 9.         └─rlang::abort(message, class = c(class, "vctrs_error"), ..., call = call)

Error:
! Test failed
Run `rlang::last_trace()` to see where the error occurred.

vec_assert is documented to throw either a vctrs_error_scalar_type or a vctrs_error_assert, but expect_vector only catches the latter. Catching both kinds should be enough to fix this behaviour I imagine.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behaviorexpectation 🙀

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions