Skip to content

Commit

Permalink
Better way to ignore attributes when comparing TRUE/FALSE (#2011)
Browse files Browse the repository at this point in the history
Fixes #1996
  • Loading branch information
hadley authored Nov 5, 2024
1 parent 3f70084 commit 477f43c
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 16 deletions.
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_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).
Expand Down
21 changes: 12 additions & 9 deletions R/expect-constant.R
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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`?
Expand All @@ -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),
Expand Down
3 changes: 1 addition & 2 deletions man/logical-expectations.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions tests/testthat/_snaps/expect-constant.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/_snaps/reporter-debug.md
Original file line number Diff line number Diff line change
@@ -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")

Expand Down
6 changes: 5 additions & 1 deletion tests/testthat/test-expect-constant.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -25,4 +30,3 @@ test_that("expect_null works", {
expect_snapshot_failure(expect_null(1L))
expect_snapshot_failure(expect_null(environment()))
})

0 comments on commit 477f43c

Please sign in to comment.