Skip to content

Commit ae4c8aa

Browse files
committed
WIP on expect_all_equal()
Fixes #1836. Fixes #2235
1 parent e80fdbb commit ae4c8aa

File tree

6 files changed

+118
-32
lines changed

6 files changed

+118
-32
lines changed

R/expect-all.R

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
expect_all_equal <- function(object, expected) {
2+
act <- quasi_label(enquo(object))
3+
exp <- quasi_label(enquo(expected))
4+
5+
check_vector(act$val, error_arg = "object")
6+
if (length(act$val) == 0) {
7+
cli::cli_abort("{.arg object} must not be empty.")
8+
}
9+
10+
check_vector(exp$val, error_arg = "expected")
11+
if (length(exp$val) != 1) {
12+
cli::cli_abort("{.arg expected} must be length 1.")
13+
}
14+
15+
exp$val <- rep(exp$val, length(act$val))
16+
names(exp$val) <- names(act$val)
17+
expect_waldo_equal_("Expected every element of %s to equal %s.", act, exp)
18+
19+
invisible(act$val)
20+
}

R/expect-match.R

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,12 @@ expect_match_ <- function(
120120
ok <- if (all) all(condition) else any(condition)
121121

122122
if (!ok) {
123-
values <- show_text(act$val, condition)
123+
labels <- ifelse(
124+
condition,
125+
cli::col_green(cli::symbol$tick),
126+
cli::col_red(cli::symbol$cross)
127+
)
128+
values <- show_text(act$val, labels)
124129
if (length(act$val) == 1) {
125130
which <- ""
126131
} else {
@@ -144,15 +149,15 @@ expect_match_ <- function(
144149
}
145150

146151
# Adapted from print.ellmer_prompt
147-
show_text <- function(x, matches = NULL, max_items = 20, max_lines = NULL) {
148-
matches <- matches %||% rep(TRUE, length(x))
152+
show_text <- function(x, labels = NULL, max_items = 20, max_lines = NULL) {
153+
labels <- labels %||% seq_along(x)
149154
max_lines <- max_lines %||% (max_items * 25)
150155

151156
n <- length(x)
152157
n_extra <- length(x) - max_items
153158
if (n_extra > 0) {
154159
x <- x[seq_len(max_items)]
155-
matches <- matches[seq_len(max_items)]
160+
labels <- labels[seq_len(max_items)]
156161
}
157162

158163
if (length(x) == 0) {
@@ -161,13 +166,7 @@ show_text <- function(x, matches = NULL, max_items = 20, max_lines = NULL) {
161166

162167
bar <- if (cli::is_utf8_output()) "\u2502" else "|"
163168

164-
id <- ifelse(
165-
matches,
166-
cli::col_green(cli::symbol$tick),
167-
cli::col_red(cli::symbol$cross)
168-
)
169-
170-
indent <- paste0(id, " ", bar, " ")
169+
indent <- paste0(labels, " ", bar, " ")
171170
exdent <- paste0(" ", cli::col_grey(bar), " ")
172171

173172
x[is.na(x)] <- cli::col_red("<NA>")
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# validates its inputs
2+
3+
Code
4+
expect_all_equal(mean, 1)
5+
Condition
6+
Error in `expect_all_equal()`:
7+
! `object` must be a vector, not a function.
8+
Code
9+
expect_all_equal(logical(), 1)
10+
Condition
11+
Error in `expect_all_equal()`:
12+
! `object` must not be empty.
13+
Code
14+
expect_all_equal(1:10, mean)
15+
Condition
16+
Error in `expect_all_equal()`:
17+
! `expected` must be a vector, not a function.
18+
Code
19+
expect_all_equal(1:10, 1:2)
20+
Condition
21+
Error in `expect_all_equal()`:
22+
! `expected` must be length 1.
23+
24+
# can compare atomic vectors
25+
26+
Code
27+
expect_all_equal(x, TRUE)
28+
Condition
29+
Error:
30+
! Expected every element of `x` to equal TRUE.
31+
Differences:
32+
`actual[2:8]`: TRUE TRUE TRUE FALSE TRUE TRUE TRUE
33+
`expected[2:8]`: TRUE TRUE TRUE TRUE TRUE TRUE TRUE
34+
35+
# can compare lists
36+
37+
Code
38+
expect_all_equal(x, list(1))
39+
Condition
40+
Error:
41+
! Expected every element of `x` to equal `list(1)`.
42+
Differences:
43+
`actual$c`: 2.0
44+
`expected$c`: 1.0
45+
46+
# truncates very long differences
47+
48+
Code
49+
expect_all_equal(x, FALSE)
50+
Condition
51+
Error:
52+
! Expected every element of `x` to equal FALSE.
53+
Differences:
54+
`actual`: TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
55+
`expected`: FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
56+

tests/testthat/_snaps/expect-match.md

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -149,45 +149,37 @@
149149
Actual text:
150150
x | test
151151

152-
# show_text() shows success and failure
153-
154-
Code
155-
base::writeLines(show_text(c("a", "b"), c(TRUE, FALSE)))
156-
Output
157-
✔ │ a
158-
✖ │ b
159-
160152
# show_text() truncates values and lines
161153

162154
Code
163155
base::writeLines(show_text(lines, max_lines = 3))
164156
Output
165-
│ a
157+
1 │ a
166158
│ b
167159
│ ...
168160
... and 8 more.
169161
170162
Code
171163
base::writeLines(show_text(lines, max_items = 3))
172164
Output
173-
│ a
165+
1 │ a
174166
│ b
175167
│ c
176-
│ d
168+
2 │ d
177169
│ e
178170
│ f
179-
│ g
171+
3 │ g
180172
│ h
181173
│ i
182174
... and 6 more.
183175
184176
Code
185177
base::writeLines(show_text(lines, max_items = 2, max_lines = 4))
186178
Output
187-
│ a
179+
1 │ a
188180
│ b
189181
│ c
190-
│ d
182+
2 │ d
191183
│ ...
192184
... and 8 more.
193185

tests/testthat/test-expect-all.R

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
test_that("validates its inputs", {
2+
expect_snapshot(error = TRUE, {
3+
expect_all_equal(mean, 1)
4+
expect_all_equal(logical(), 1)
5+
expect_all_equal(1:10, mean)
6+
expect_all_equal(1:10, 1:2)
7+
})
8+
})
9+
10+
test_that("can compare atomic vectors", {
11+
x <- rep(TRUE, 10)
12+
expect_success(expect_all_equal(x, TRUE))
13+
14+
x[5] <- FALSE
15+
expect_snapshot_failure(expect_all_equal(x, TRUE))
16+
})
17+
18+
test_that("can compare lists", {
19+
x <- list(a = 1, b = 1, c = 2, d = 1, e = 1)
20+
expect_snapshot_failure(expect_all_equal(x, list(1)))
21+
})
22+
23+
test_that("truncates very long differences", {
24+
x <- rep(TRUE, 10)
25+
expect_snapshot_failure(expect_all_equal(x, FALSE))
26+
})

tests/testthat/test-expect-match.R

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,6 @@ test_that("empty string is never a match", {
6969

7070
# show_text() ------------------------------------------------------------------
7171

72-
test_that("show_text() shows success and failure", {
73-
local_reproducible_output(unicode = TRUE)
74-
expect_snapshot({
75-
base::writeLines(show_text(c("a", "b"), c(TRUE, FALSE)))
76-
})
77-
})
78-
7972
test_that("show_text() truncates values and lines", {
8073
local_reproducible_output(unicode = TRUE)
8174
lines <- map_chr(

0 commit comments

Comments
 (0)