Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# testthat (development version)

* `expect_vector()` fails, instead of erroring, if `object` is not a vector (@plietar, #2224).
* New `vignette("mocking")` explains mocking in detail (#1265).
* New `vignette("challenging-functions")` provides an index to other documentation organised by testing challenges (#1265).
* 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.
Expand Down
15 changes: 8 additions & 7 deletions R/expect-vector.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@
expect_vector <- function(object, ptype = NULL, size = NULL) {
check_installed("vctrs")
check_number_whole(size, min = 0, allow_null = TRUE)

act <- quasi_label(enquo(object))
# vec_assert() automatically adds backticks so we hack out the ones
# added by as_label()
act$lab <- gsub("^`|`$", "", act$lab)

message <- NULL
tryCatch(
withCallingHandlers(
vctrs::vec_assert(act$val, ptype = ptype, size = size, arg = act$lab),
vctrs_error_scalar_type = function(e) {
fail(e$message)
},
vctrs_error_assert = function(e) {
message <<- e$message
fail(e$message)
}
)

if (!is.null(message)) {
return(fail(message))
}
pass(act$val)
}
16 changes: 16 additions & 0 deletions tests/testthat/_snaps/expect-vector.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
# basic properties upheld

Code
expect_vector(x, size = 5)
Condition
Error:
! `x` must have size 5, not size 10.

---

Code
expect_vector(y)
Condition
Error:
! `y` must be a vector, not `NULL`.

# expect_vector validates its inputs

Code
Expand Down
7 changes: 6 additions & 1 deletion tests/testthat/test-expect-vector.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ test_that("basic properties upheld", {
skip_if_not_installed("vctrs", "0.1.0.9002")

expect_success(expect_vector(1:10, size = 10))
expect_failure(expect_vector(1:10, size = 5))

x <- 1:10
expect_snapshot_failure(expect_vector(x, size = 5))

y <- NULL
expect_snapshot_failure(expect_vector(y))
})

test_that("expect_vector validates its inputs", {
Expand Down