Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions R/expect-match.R
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,15 @@ expect_match_ <- function(
}

# Adapted from print.ellmer_prompt
show_text <- function(
x,
condition,
...,
max_items = 20,
max_lines = max_items * 25
) {
show_text <- function(x, matches = NULL, max_items = 20, max_lines = NULL) {
matches <- matches %||% rep(TRUE, length(x))
max_lines <- max_lines %||% (max_items * 25)

n <- length(x)
n_extra <- length(x) - max_items
if (n_extra > 0) {
x <- x[seq_len(max_items)]
condition <- condition[seq_len(max_items)]
matches <- matches[seq_len(max_items)]
}

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

id <- ifelse(
condition,
matches,
cli::col_green(cli::symbol$tick),
cli::col_red(cli::symbol$cross)
)
Expand All @@ -178,10 +175,15 @@ show_text <- function(
x <- gsub("\n", paste0("\n", exdent), x)

lines <- strsplit(x, "\n")
ids <- rep(seq_along(x), length(lines))
ids <- rep(seq_along(x), lengths(lines))
first <- c(TRUE, ids[-length(ids)] != ids[-1])
lines <- unlist(lines)

if (length(lines) > max_lines) {
if (first[max_lines + 1]) {
max_lines <- max_lines - 1
}
Comment on lines +183 to +185

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be misreading this, but I would have expected

Suggested change
if (first[max_lines + 1]) {
max_lines <- max_lines - 1
}
if (first[max_lines]) {
max_lines <- max_lines - 1
}

to avoid output like this (assuming max_lines = 2)

[1] | x
    | y
[2] | ...
... and 1 more.

i.e. I was thinking this we'd prefer to show an item only if you see at least one line, e.g.

[1] | x
    | y
... and 2 more.

whereas using first[max_lines + 1] gives

[1] | x
    | ...
... and 2 more

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohhh, I worked it out. + 1 is right

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😅


lines <- lines[seq_len(max_lines)]
lines <- c(lines, paste0(exdent, "..."))
n_extra <- n - ids[max_lines - 1]
Expand Down
43 changes: 43 additions & 0 deletions tests/testthat/_snaps/expect-match.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,46 @@
Actual text:
x | test

# show_text() shows success and failure

Code
base::writeLines(show_text(c("a", "b"), c(TRUE, FALSE)))
Output
✔ │ a
✖ │ b

# show_text() truncates values and lines

Code
base::writeLines(show_text(lines, max_lines = 3))
Output
✔ │ a
│ b
│ ...
... and 8 more.

Code
base::writeLines(show_text(lines, max_items = 3))
Output
✔ │ a
│ b
│ c
✔ │ d
│ e
│ f
✔ │ g
│ h
│ i
... and 6 more.

Code
base::writeLines(show_text(lines, max_items = 2, max_lines = 4))
Output
✔ │ a
│ b
│ c
✔ │ d
│ ...
... and 8 more.


24 changes: 24 additions & 0 deletions tests/testthat/test-expect-match.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,27 @@ test_that("expect_no_match works", {
test_that("empty string is never a match", {
expect_success(expect_no_match(character(), "x"))
})

# show_text() ------------------------------------------------------------------

test_that("show_text() shows success and failure", {
local_reproducible_output(unicode = TRUE)
expect_snapshot({
base::writeLines(show_text(c("a", "b"), c(TRUE, FALSE)))
})
})

test_that("show_text() truncates values and lines", {
local_reproducible_output(unicode = TRUE)
lines <- map_chr(
split(letters, (seq_along(letters) - 1) %/% 3),
paste,
collapse = "\n"
)

expect_snapshot({
base::writeLines(show_text(lines, max_lines = 3))
base::writeLines(show_text(lines, max_items = 3))
base::writeLines(show_text(lines, max_items = 2, max_lines = 4))
})
})