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
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* `vignette("custom-expectations)` has been overhauled to make it much clearer how to create high-quality expectations (#2113, #2132, #2072).
* `expect_snapshot()` and friends will now fail when creating a new snapshot on CI. This is usually a signal that you've forgotten to run it locally before committing (#1461).
* `expect_snapshot_value()` can now handle expressions that generate `-` (#1678) or zero length atomic vectors (#2042).
* `expect_matches()` failures should be a little easier to read (#2135).
* `expect_matches()` failures should be a little easier to read (#2135, #2181).
* New `local_on_cran(TRUE)` allows you to simulate how your tests will run on CRAN (#2112).
* `expect_no_*()` now executes the entire code block, rather than stopping at the first message or warning (#1991).
* `expect_no_failures()` and `expect_no_successes()` are now deprecated as `expect_success()` now test for no failures and `expect_failure()` tests for no successes (#)
Expand Down
17 changes: 14 additions & 3 deletions R/expect-match.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ expect_match_ <- function(
info = NULL,
label = NULL,
negate = FALSE,
title = "Text",
trace_env = caller_env()
) {
matches <- grepl(regexp, act$val, perl = perl, fixed = fixed, ...)
Expand All @@ -118,14 +119,24 @@ expect_match_ <- function(

text <- encodeString(act$val)
if (length(act$val) == 1) {
values <- paste0('Text: "', text, '"')
values <- paste0(title, ': "', text, '"')
which <- ""
} else {
values <- paste0("Text:\n", paste0("* ", text, collapse = "\n"))
bullet <- ifelse(
condition,
cli::col_green(cli::symbol$tick),
cli::col_red(cli::symbol$cross)
)
values <- paste0(title, ":\n", paste0(bullet, " ", text, collapse = "\n"))
which <- if (all) "Every element of " else "Some element of "
}
match <- if (negate) "matches" else "does not match"

msg <- sprintf(
if (negate) "%s matches %s %s.\n%s" else "%s does not match %s %s.\n%s",
"%s%s %s %s %s.\n%s",
which,
act$lab,
match,
if (fixed) "string" else "regexp",
encodeString(regexp, quote = '"'),
values
Expand Down
2 changes: 1 addition & 1 deletion R/expect-output.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ expect_output <- function(
pass(act$val)
} else {
act <- labelled_value(act$cap, act$lab)
expect_match_(act, enc2native(regexp), ...)
expect_match_(act, enc2native(regexp), ..., title = "Output")
}
}
18 changes: 12 additions & 6 deletions tests/testthat/_snaps/expect-match.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@

---

`many` does not match regexp "asdf".
Every element of `many` does not match regexp "a".
Text:
* a
* b
* c
* d
* e
✔ a
✔ a
✖ b

---

Some element of `many` does not match regexp "c".
Text:
✖ a
✖ a
✖ b

# expect_match validates its inputs

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/expect-output.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# expect = string checks for match

`g()` does not match regexp "x".
Text: "!"
Output: "!"

---

Expand Down
7 changes: 5 additions & 2 deletions tests/testthat/test-expect-match.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
test_that("generates useful failure messages", {
local_reproducible_output(unicode = TRUE)

zero <- character(0)
expect_snapshot_failure(expect_match(zero, 'asdf'))

one <- "bcde"
expect_snapshot_failure(expect_match(one, 'asdf'))

many <- letters[1:5]
expect_snapshot_failure(expect_match(many, 'asdf'))
many <- c("a", "a", "b")
expect_snapshot_failure(expect_match(many, "a"))
expect_snapshot_failure(expect_match(many, "c", all = FALSE))
})

test_that("expect_match validates its inputs", {
Expand Down
Loading