Skip to content

Commit be0ce42

Browse files
authored
Use expect_waldo_equal() for constant tests (#2144)
And rename `new_actual()` to `labelled_value()` which is MUCH better name
1 parent 9c93b8f commit be0ce42

20 files changed

+106
-121
lines changed

R/expect-constant.R

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@ NULL
3030
#' @rdname logical-expectations
3131
expect_true <- function(object, info = NULL, label = NULL) {
3232
act <- quasi_label(enquo(object), label, arg = "object")
33-
expect_waldo_constant_(act, TRUE, info = info, ignore_attr = TRUE)
33+
exp <- labelled_value(TRUE, "TRUE")
34+
expect_waldo_equal_("equal", act, exp, info = info, ignore_attr = TRUE)
3435
}
3536

3637
#' @export
3738
#' @rdname logical-expectations
3839
expect_false <- function(object, info = NULL, label = NULL) {
3940
act <- quasi_label(enquo(object), label, arg = "object")
40-
expect_waldo_constant_(act, FALSE, info = info, ignore_attr = TRUE)
41+
exp <- labelled_value(FALSE, "FALSE")
42+
expect_waldo_equal_("equal", act, exp, info = info, ignore_attr = TRUE)
4143
}
4244

4345
#' Does code return `NULL`?
@@ -56,35 +58,6 @@ expect_false <- function(object, info = NULL, label = NULL) {
5658
#' show_failure(expect_null(y))
5759
expect_null <- function(object, info = NULL, label = NULL) {
5860
act <- quasi_label(enquo(object), label, arg = "object")
59-
expect_waldo_constant_(act, NULL, info = info)
60-
}
61-
62-
# helpers -----------------------------------------------------------------
63-
64-
expect_waldo_constant_ <- function(
65-
act,
66-
constant,
67-
info,
68-
...,
69-
trace_env = caller_env()
70-
) {
71-
comp <- waldo_compare(
72-
act$val,
73-
constant,
74-
x_arg = "actual",
75-
y_arg = "expected",
76-
...
77-
)
78-
79-
if (length(comp) != 0) {
80-
msg <- sprintf(
81-
"%s is not %s\n\n%s",
82-
act$lab,
83-
deparse(constant),
84-
paste0(comp, collapse = "\n\n")
85-
)
86-
return(fail(msg, info = info, trace_env = trace_env))
87-
}
88-
89-
pass(act$val)
61+
exp <- labelled_value(NULL, "FALSE")
62+
expect_waldo_equal_("equal", act, exp, info = info)
9063
}

R/expect-equality.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ expect_waldo_equal_ <- function(
136136
)
137137
if (length(comp) != 0) {
138138
msg <- sprintf(
139-
"%s (%s) not %s to %s (%s).\n\n%s",
139+
"%s (%s) is not %s to %s (%s).\n\n%s",
140140
act$lab,
141141
"`actual`",
142142
type,

R/expect-output.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ expect_output <- function(
4545
}
4646
pass(act$val)
4747
} else {
48-
act <- new_actual(act$cap, act$lab)
48+
act <- labelled_value(act$cap, act$lab)
4949
expect_match_(act, enc2native(regexp), ...)
5050
}
5151
}

R/expect-self-test.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ expect_failure <- function(expr, message = NULL, ...) {
8585
}
8686

8787
if (!is.null(message)) {
88-
act <- new_actual(status$last_failure$message, "Failure message")
88+
act <- labelled_value(status$last_failure$message, "Failure message")
8989
return(expect_match_(act, message, ...))
9090
}
9191
pass(NULL)

R/local.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,9 @@ waldo_compare <- function(x, y, ..., x_arg = "x", y_arg = "y") {
153153
# Need to very carefully isolate this change to this function - can not set
154154
# in expectation functions because part of expectation handling bubbles
155155
# up through calling handlers, which are run before on.exit()
156-
local_reporter_output()
157-
156+
if (!is_snapshot()) {
157+
local_reporter_output()
158+
}
158159
waldo::compare(x, y, ..., x_arg = x_arg, y_arg = y_arg)
159160
}
160161

R/quasi-label.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ quasi_label <- function(quo, label = NULL, arg = "quo") {
4242

4343
expr <- quo_get_expr(quo)
4444

45-
new_actual(
45+
labelled_value(
4646
eval_bare(expr, quo_get_env(quo)),
4747
label %||% expr_label(expr)
4848
)
4949
}
5050

51-
new_actual <- function(value, label) {
51+
labelled_value <- function(value, label) {
5252
list(
5353
val = value,
5454
lab = label
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
# logical tests act as expected
22

3-
FALSE is not TRUE
3+
FALSE (`actual`) is not equal to TRUE (`expected`).
44

5-
`actual`: [32mFALSE[39m
6-
`expected`: [32mTRUE[39m
5+
`actual`: FALSE
6+
`expected`: TRUE
77

88
---
99

10-
TRUE is not FALSE
10+
TRUE (`actual`) is not equal to FALSE (`expected`).
1111

12-
`actual`: [32mTRUE[39m
13-
`expected`: [32mFALSE[39m
12+
`actual`: TRUE
13+
`expected`: FALSE
1414

1515
# can compare non-vectors
1616

17-
quote(x) is not TRUE
17+
quote(x) (`actual`) is not equal to TRUE (`expected`).
1818

19-
`actual` is [32ma symbol[39m
20-
`expected` is [32ma logical vector[39m (TRUE)
19+
`actual` is a symbol
20+
`expected` is a logical vector (TRUE)
2121

2222
# expect_null works
2323

24-
1L is not NULL
24+
1L (`actual`) is not equal to FALSE (`expected`).
2525

26-
`actual` is [32man integer vector[39m (1)
27-
`expected` is [32mNULL[39m
26+
`actual` is an integer vector (1)
27+
`expected` is NULL
2828

2929
---
3030

31-
environment() is not NULL
31+
environment() (`actual`) is not equal to FALSE (`expected`).
3232

33-
`actual` is [32man environment[39m
34-
`expected` is [32mNULL[39m
33+
`actual` is an environment
34+
`expected` is NULL
3535

tests/testthat/_snaps/expect-equality.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
# provide useful feedback on failure
22

3-
1 (`actual`) not identical to "a" (`expected`).
3+
1 (`actual`) is not identical to "a" (`expected`).
44

5-
`actual` is [32ma double vector[39m (1)
6-
`expected` is [32ma character vector[39m ('a')
5+
`actual` is a double vector (1)
6+
`expected` is a character vector ('a')
77

88
---
99

10-
1 (`actual`) not equal to "a" (`expected`).
10+
1 (`actual`) is not equal to "a" (`expected`).
1111

12-
`actual` is [32ma double vector[39m (1)
13-
`expected` is [32ma character vector[39m ('a')
12+
`actual` is a double vector (1)
13+
`expected` is a character vector ('a')
1414

1515
---
1616

tests/testthat/_snaps/expect-self-test.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@
1111
Error:
1212
! error
1313

14+
# show_failure
15+
16+
Code
17+
show_failure(expect_true(FALSE))
18+
Output
19+
Failed expectation:
20+
FALSE (`actual`) is not equal to TRUE (`expected`).
21+
22+
`actual`: FALSE
23+
`expected`: TRUE
24+
1425
# expect_no are deprecated
1526

1627
Code

tests/testthat/_snaps/reporter-check.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515

1616
== Failed tests ================================================================
1717
-- Failure ('reporters/tests.R:12:3'): Failure:1 -------------------------------
18-
FALSE is not TRUE
18+
FALSE (`actual`) is not equal to TRUE (`expected`).
1919

2020
`actual`: FALSE
2121
`expected`: TRUE
2222
-- Failure ('reporters/tests.R:16:8'): Failure:2a ------------------------------
23-
FALSE is not TRUE
23+
FALSE (`actual`) is not equal to TRUE (`expected`).
2424

2525
`actual`: FALSE
2626
`expected`: TRUE
@@ -71,12 +71,12 @@
7171

7272
== Failed tests ================================================================
7373
-- Failure ('reporters/tests.R:12:3'): Failure:1 -------------------------------
74-
FALSE is not TRUE
74+
FALSE (`actual`) is not equal to TRUE (`expected`).
7575

7676
`actual`: FALSE
7777
`expected`: TRUE
7878
-- Failure ('reporters/tests.R:16:8'): Failure:2a ------------------------------
79-
FALSE is not TRUE
79+
FALSE (`actual`) is not equal to TRUE (`expected`).
8080

8181
`actual`: FALSE
8282
`expected`: TRUE

0 commit comments

Comments
 (0)