Skip to content

Commit eb51b50

Browse files
authored
Improve show_text() (#2257)
* Fix `lengths()` type * Handle case where we remove first line of new item * Add some tests Fixes #2249
1 parent e8b06e1 commit eb51b50

File tree

3 files changed

+79
-10
lines changed

3 files changed

+79
-10
lines changed

R/expect-match.R

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,18 +144,15 @@ expect_match_ <- function(
144144
}
145145

146146
# Adapted from print.ellmer_prompt
147-
show_text <- function(
148-
x,
149-
condition,
150-
...,
151-
max_items = 20,
152-
max_lines = max_items * 25
153-
) {
147+
show_text <- function(x, matches = NULL, max_items = 20, max_lines = NULL) {
148+
matches <- matches %||% rep(TRUE, length(x))
149+
max_lines <- max_lines %||% (max_items * 25)
150+
154151
n <- length(x)
155152
n_extra <- length(x) - max_items
156153
if (n_extra > 0) {
157154
x <- x[seq_len(max_items)]
158-
condition <- condition[seq_len(max_items)]
155+
matches <- matches[seq_len(max_items)]
159156
}
160157

161158
if (length(x) == 0) {
@@ -165,7 +162,7 @@ show_text <- function(
165162
bar <- if (cli::is_utf8_output()) "\u2502" else "|"
166163

167164
id <- ifelse(
168-
condition,
165+
matches,
169166
cli::col_green(cli::symbol$tick),
170167
cli::col_red(cli::symbol$cross)
171168
)
@@ -178,10 +175,15 @@ show_text <- function(
178175
x <- gsub("\n", paste0("\n", exdent), x)
179176

180177
lines <- strsplit(x, "\n")
181-
ids <- rep(seq_along(x), length(lines))
178+
ids <- rep(seq_along(x), lengths(lines))
179+
first <- c(TRUE, ids[-length(ids)] != ids[-1])
182180
lines <- unlist(lines)
183181

184182
if (length(lines) > max_lines) {
183+
if (first[max_lines + 1]) {
184+
max_lines <- max_lines - 1
185+
}
186+
185187
lines <- lines[seq_len(max_lines)]
186188
lines <- c(lines, paste0(exdent, "..."))
187189
n_extra <- n - ids[max_lines - 1]

tests/testthat/_snaps/expect-match.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,46 @@
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+
160+
# show_text() truncates values and lines
161+
162+
Code
163+
base::writeLines(show_text(lines, max_lines = 3))
164+
Output
165+
✔ │ a
166+
│ b
167+
│ ...
168+
... and 8 more.
169+
170+
Code
171+
base::writeLines(show_text(lines, max_items = 3))
172+
Output
173+
✔ │ a
174+
│ b
175+
│ c
176+
✔ │ d
177+
│ e
178+
│ f
179+
✔ │ g
180+
│ h
181+
│ i
182+
... and 6 more.
183+
184+
Code
185+
base::writeLines(show_text(lines, max_items = 2, max_lines = 4))
186+
Output
187+
✔ │ a
188+
│ b
189+
│ c
190+
✔ │ d
191+
│ ...
192+
... and 8 more.
193+
194+

tests/testthat/test-expect-match.R

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,27 @@ test_that("expect_no_match works", {
6666
test_that("empty string is never a match", {
6767
expect_success(expect_no_match(character(), "x"))
6868
})
69+
70+
# show_text() ------------------------------------------------------------------
71+
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+
79+
test_that("show_text() truncates values and lines", {
80+
local_reproducible_output(unicode = TRUE)
81+
lines <- map_chr(
82+
split(letters, (seq_along(letters) - 1) %/% 3),
83+
paste,
84+
collapse = "\n"
85+
)
86+
87+
expect_snapshot({
88+
base::writeLines(show_text(lines, max_lines = 3))
89+
base::writeLines(show_text(lines, max_items = 3))
90+
base::writeLines(show_text(lines, max_items = 2, max_lines = 4))
91+
})
92+
})

0 commit comments

Comments
 (0)