diff --git a/NEWS.md b/NEWS.md index 64ae36713..1f595bb41 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # testthat (development version) +* `expect_true()` and `expect_false()` give better errors if `actual` isn't a vector (#1996). * `expect_no_*()` expectations no longer incorrectly emit a passing test result if they in fact fail (#1997). * Require the latest version of waldo (0.6.0) in order to get the latest goodies (#1955). * `expect_visible()` and `expect_invisible()` have improved failure messages (#1966). diff --git a/R/expect-constant.R b/R/expect-constant.R index 72e267e96..d391ed00c 100644 --- a/R/expect-constant.R +++ b/R/expect-constant.R @@ -1,5 +1,6 @@ #' Does code return `TRUE` or `FALSE`? #' +#' @description #' These are fall-back expectations that you can use when none of the other #' more specific expectations apply. The disadvantage is that you may get #' a less informative error message. @@ -30,18 +31,14 @@ NULL #' @rdname logical-expectations expect_true <- function(object, info = NULL, label = NULL) { act <- quasi_label(enquo(object), label, arg = "object") - act$val <- as.vector(act$val) - - expect_waldo_constant(act, TRUE, info = info) + expect_waldo_constant(act, TRUE, info = info, ignore_attr = TRUE) } #' @export #' @rdname logical-expectations expect_false <- function(object, info = NULL, label = NULL) { act <- quasi_label(enquo(object), label, arg = "object") - act$val <- as.vector(act$val) - - expect_waldo_constant(act, FALSE, info = info) + expect_waldo_constant(act, FALSE, info = info, ignore_attr = TRUE) } #' Does code return `NULL`? @@ -66,11 +63,17 @@ expect_null <- function(object, info = NULL, label = NULL) { # helpers ----------------------------------------------------------------- -expect_waldo_constant <- function(act, constant, info) { - comp <- waldo_compare(act$val, constant, x_arg = "actual", y_arg = "expected") +expect_waldo_constant <- function(act, constant, info, ...) { + comp <- waldo_compare( + act$val, + constant, + x_arg = "actual", + y_arg = "expected", + ... + ) expect( - identical(act$val, constant), + length(comp) == 0, sprintf( "%s is not %s\n\n%s", act$lab, deparse(constant), diff --git a/man/logical-expectations.Rd b/man/logical-expectations.Rd index da38c3bde..7d436b5ec 100644 --- a/man/logical-expectations.Rd +++ b/man/logical-expectations.Rd @@ -26,8 +26,7 @@ alternatives in \link{quasi_label}.} These are fall-back expectations that you can use when none of the other more specific expectations apply. The disadvantage is that you may get a less informative error message. -} -\details{ + Attributes are ignored. } \examples{ diff --git a/tests/testthat/_snaps/expect-constant.md b/tests/testthat/_snaps/expect-constant.md index 1b59b90d7..6d9ae9d4c 100644 --- a/tests/testthat/_snaps/expect-constant.md +++ b/tests/testthat/_snaps/expect-constant.md @@ -12,6 +12,13 @@ `actual`: TRUE `expected`: FALSE +# can compare non-vectors + + quote(x) is not TRUE + + `actual` is a symbol + `expected` is a logical vector (TRUE) + # expect_null works 1L is not NULL diff --git a/tests/testthat/_snaps/reporter-debug.md b/tests/testthat/_snaps/reporter-debug.md index d57076d42..532957d8e 100644 --- a/tests/testthat/_snaps/reporter-debug.md +++ b/tests/testthat/_snaps/reporter-debug.md @@ -1,13 +1,13 @@ # produces consistent output 1: expect_true(FALSE) - 2: expect_waldo_constant(act, TRUE, info = info) - 3: expect(identical(act$val, constant), sprintf("%s is not %s\n\n%s", act$lab, + 2: expect_waldo_constant(act, TRUE, info = info, ignore_attr = TRUE) + 3: expect(length(comp) == 0, sprintf("%s is not %s\n\n%s", act$lab, deparse(co 1: f() 2: expect_true(FALSE) - 3: expect_waldo_constant(act, TRUE, info = info) - 4: expect(identical(act$val, constant), sprintf("%s is not %s\n\n%s", act$lab, + 3: expect_waldo_constant(act, TRUE, info = info, ignore_attr = TRUE) + 4: expect(length(comp) == 0, sprintf("%s is not %s\n\n%s", act$lab, deparse(co 1: stop("stop") diff --git a/tests/testthat/test-expect-constant.R b/tests/testthat/test-expect-constant.R index b5cadb766..1c7cada82 100644 --- a/tests/testthat/test-expect-constant.R +++ b/tests/testthat/test-expect-constant.R @@ -13,6 +13,11 @@ test_that("logical tests ignore attributes", { expect_success(expect_false(c(a = FALSE))) }) +test_that("can compare non-vectors", { + local_output_override() + expect_snapshot_failure(expect_true(quote(x))) +}) + test_that("additional info returned in message", { expect_failure(expect_true(FALSE, "NOPE"), "\nNOPE") expect_failure(expect_false(TRUE, "YUP"), "\nYUP") @@ -25,4 +30,3 @@ test_that("expect_null works", { expect_snapshot_failure(expect_null(1L)) expect_snapshot_failure(expect_null(environment())) }) -