Skip to content

Commit 477f43c

Browse files
authored
Better way to ignore attributes when comparing TRUE/FALSE (#2011)
Fixes #1996
1 parent 3f70084 commit 477f43c

File tree

6 files changed

+30
-16
lines changed

6 files changed

+30
-16
lines changed

NEWS.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# testthat (development version)
22

3+
* `expect_true()` and `expect_false()` give better errors if `actual` isn't a vector (#1996).
34
* `expect_no_*()` expectations no longer incorrectly emit a passing test result if they in fact fail (#1997).
45
* Require the latest version of waldo (0.6.0) in order to get the latest goodies (#1955).
56
* `expect_visible()` and `expect_invisible()` have improved failure messages (#1966).

R/expect-constant.R

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#' Does code return `TRUE` or `FALSE`?
22
#'
3+
#' @description
34
#' These are fall-back expectations that you can use when none of the other
45
#' more specific expectations apply. The disadvantage is that you may get
56
#' a less informative error message.
@@ -30,18 +31,14 @@ NULL
3031
#' @rdname logical-expectations
3132
expect_true <- function(object, info = NULL, label = NULL) {
3233
act <- quasi_label(enquo(object), label, arg = "object")
33-
act$val <- as.vector(act$val)
34-
35-
expect_waldo_constant(act, TRUE, info = info)
34+
expect_waldo_constant(act, TRUE, info = info, ignore_attr = TRUE)
3635
}
3736

3837
#' @export
3938
#' @rdname logical-expectations
4039
expect_false <- function(object, info = NULL, label = NULL) {
4140
act <- quasi_label(enquo(object), label, arg = "object")
42-
act$val <- as.vector(act$val)
43-
44-
expect_waldo_constant(act, FALSE, info = info)
41+
expect_waldo_constant(act, FALSE, info = info, ignore_attr = TRUE)
4542
}
4643

4744
#' Does code return `NULL`?
@@ -66,11 +63,17 @@ expect_null <- function(object, info = NULL, label = NULL) {
6663

6764
# helpers -----------------------------------------------------------------
6865

69-
expect_waldo_constant <- function(act, constant, info) {
70-
comp <- waldo_compare(act$val, constant, x_arg = "actual", y_arg = "expected")
66+
expect_waldo_constant <- function(act, constant, info, ...) {
67+
comp <- waldo_compare(
68+
act$val,
69+
constant,
70+
x_arg = "actual",
71+
y_arg = "expected",
72+
...
73+
)
7174

7275
expect(
73-
identical(act$val, constant),
76+
length(comp) == 0,
7477
sprintf(
7578
"%s is not %s\n\n%s",
7679
act$lab, deparse(constant),

man/logical-expectations.Rd

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/expect-constant.md

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
`actual`: TRUE
1313
`expected`: FALSE
1414

15+
# can compare non-vectors
16+
17+
quote(x) is not TRUE
18+
19+
`actual` is a symbol
20+
`expected` is a logical vector (TRUE)
21+
1522
# expect_null works
1623

1724
1L is not NULL

tests/testthat/_snaps/reporter-debug.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# produces consistent output
22

33
1: expect_true(FALSE)
4-
2: expect_waldo_constant(act, TRUE, info = info)
5-
3: expect(identical(act$val, constant), sprintf("%s is not %s\n\n%s", act$lab,
4+
2: expect_waldo_constant(act, TRUE, info = info, ignore_attr = TRUE)
5+
3: expect(length(comp) == 0, sprintf("%s is not %s\n\n%s", act$lab, deparse(co
66

77
1: f()
88
2: expect_true(FALSE)
9-
3: expect_waldo_constant(act, TRUE, info = info)
10-
4: expect(identical(act$val, constant), sprintf("%s is not %s\n\n%s", act$lab,
9+
3: expect_waldo_constant(act, TRUE, info = info, ignore_attr = TRUE)
10+
4: expect(length(comp) == 0, sprintf("%s is not %s\n\n%s", act$lab, deparse(co
1111

1212
1: stop("stop")
1313

tests/testthat/test-expect-constant.R

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ test_that("logical tests ignore attributes", {
1313
expect_success(expect_false(c(a = FALSE)))
1414
})
1515

16+
test_that("can compare non-vectors", {
17+
local_output_override()
18+
expect_snapshot_failure(expect_true(quote(x)))
19+
})
20+
1621
test_that("additional info returned in message", {
1722
expect_failure(expect_true(FALSE, "NOPE"), "\nNOPE")
1823
expect_failure(expect_false(TRUE, "YUP"), "\nYUP")
@@ -25,4 +30,3 @@ test_that("expect_null works", {
2530
expect_snapshot_failure(expect_null(1L))
2631
expect_snapshot_failure(expect_null(environment()))
2732
})
28-

0 commit comments

Comments
 (0)