-
Notifications
You must be signed in to change notification settings - Fork 340
Overhaul expect_matches()
#2138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ff6a673
Overhaul `expect_matches()`
hadley 5ec21fe
Polish docs and add argument checking
hadley b344867
Remove no longer used function
hadley aa5d3b0
Merge commit '9c93b8f0be372cfd495d87fa18e5854b04c45ea8'
hadley c06d92f
Apply suggestions from code review
hadley d24128a
Tweak message
hadley File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| # generates useful failure messages | ||
|
|
||
| `zero` is empty. | ||
|
|
||
| --- | ||
|
|
||
| `one` does not match regexp "asdf". | ||
| Text: "bcde" | ||
|
|
||
| --- | ||
|
|
||
| `many` does not match regexp "asdf". | ||
| Text: | ||
| * a | ||
| * b | ||
| * c | ||
| * d | ||
| * e | ||
|
|
||
| # checks its inputs | ||
|
|
||
| Code | ||
| expect_match(1) | ||
| Condition | ||
| Error in `expect_match()`: | ||
| ! `object` must be a character vector, not the number 1. | ||
| Code | ||
| expect_match("x", 1) | ||
| Condition | ||
| Error in `expect_match()`: | ||
| ! `regexp` must be a single string, not the number 1. | ||
| Code | ||
| expect_match("x", "x", fixed = 1) | ||
| Condition | ||
| Error in `expect_match()`: | ||
| ! `fixed` must be `TRUE` or `FALSE`, not the number 1. | ||
| Code | ||
| expect_match("x", "x", perl = 1) | ||
| Condition | ||
| Error in `expect_match()`: | ||
| ! `perl` must be `TRUE` or `FALSE`, not the number 1. | ||
| Code | ||
| expect_match("x", "x", all = 1) | ||
| Condition | ||
| Error in `expect_match()`: | ||
| ! `all` must be `TRUE` or `FALSE`, not the number 1. | ||
|
|
||
| # expect_no_match works | ||
|
|
||
| `x` does match string "e*". | ||
| Text: "te*st" | ||
|
|
||
| --- | ||
|
|
||
| `x` does match regexp "TEST". | ||
| Text: "test" | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,47 +1,42 @@ | ||
| test_that("extra arguments to matches passed onto grepl", { | ||
| expect_success(expect_match("te*st", "e*", fixed = TRUE)) | ||
| expect_success(expect_match("test", "TEST", ignore.case = TRUE)) | ||
| }) | ||
| test_that("generates useful failure messages", { | ||
| zero <- character(0) | ||
| expect_snapshot_failure(expect_match(zero, 'asdf')) | ||
|
|
||
| test_that("special regex characters are escaped in output", { | ||
| error <- tryCatch( | ||
| expect_match("f() test", "f() test"), | ||
| expectation = function(e) e$message | ||
| ) | ||
| expect_equal( | ||
| error, | ||
| "\"f\\(\\) test\" does not match \"f() test\".\nActual value: \"f\\(\\) test\"" | ||
| ) | ||
| }) | ||
| one <- "bcde" | ||
| expect_snapshot_failure(expect_match(one, 'asdf')) | ||
|
|
||
| test_that("correct reporting of expected label", { | ||
| expect_failure(expect_match("[a]", "[b]"), escape_regex("[a]"), fixed = TRUE) | ||
| expect_failure(expect_match("[a]", "[b]", fixed = TRUE), "[a]", fixed = TRUE) | ||
| many <- letters[1:5] | ||
| expect_snapshot_failure(expect_match(many, 'asdf')) | ||
| }) | ||
|
|
||
| test_that("errors if obj is empty str", { | ||
| x <- character(0) | ||
| err <- expect_error( | ||
| expect_match(x, 'asdf'), | ||
| class = "expectation_failure" | ||
| ) | ||
| expect_match(err$message, 'is empty') | ||
| test_that("checks its inputs", { | ||
| expect_snapshot(error = TRUE, { | ||
| expect_match(1) | ||
| expect_match("x", 1) | ||
| expect_match("x", "x", fixed = 1) | ||
| expect_match("x", "x", perl = 1) | ||
| expect_match("x", "x", all = 1) | ||
| }) | ||
| }) | ||
|
|
||
| test_that("prints multiple unmatched values", { | ||
| err <- expect_error( | ||
| expect_match(letters[1:10], 'asdf'), | ||
| class = "expectation_failure" | ||
| ) | ||
| expect_match(err$message, "does not match") | ||
| test_that("extra arguments passed onto grepl", { | ||
| expect_failure(expect_match("\\s", "\\s")) | ||
| expect_success(expect_match("\\s", "\\s", fixed = TRUE)) | ||
|
|
||
| expect_failure(expect_match("test", "TEST")) | ||
| expect_success(expect_match("test", "TEST", ignore.case = TRUE)) | ||
| }) | ||
|
|
||
| test_that("expect_no_match works", { | ||
| expect_success(expect_no_match("[a]", "[b]")) | ||
| expect_success(expect_no_match("[a]", "[b]", fixed = TRUE)) | ||
| expect_failure( | ||
| expect_no_match("te*st", "e*", fixed = TRUE), | ||
| escape_regex("te*st") | ||
| ) | ||
| expect_failure(expect_no_match("test", "TEST", ignore.case = TRUE), "test") | ||
|
|
||
| x <- "te*st" | ||
| expect_snapshot_failure(expect_no_match(x, "e*", fixed = TRUE)) | ||
| x <- "test" | ||
| expect_snapshot_failure(expect_no_match(x, "TEST", ignore.case = TRUE)) | ||
| }) | ||
|
|
||
| test_that("empty string is never a match", { | ||
| expect_success(expect_no_match(character(), "x")) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
labelis likely checked above byquasi_label(), do we want to checkinfo? It's still used in the empty case below.grepl_args = list()(or= NULL) argument and soft-deprecate...(with a lifecycle warning)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't care that much about info since we're phasing it out in new expectations.
I don't want to do any bigger refactorings because if I did I'd really want to stop it from being vectorising and instead make it work with a single string; but that's going to break a bunch of tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we use the
allargument for enforcing a scalar?all = NAorall = "scalar"would mean to expect a scalar, with a lifecycle warning to addall = TRUEif a non-scalar is provided without settingall?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it's important enough to do all that work.