Skip to content

Commit 5ec21fe

Browse files
committed
Polish docs and add argument checking
1 parent ff6a673 commit 5ec21fe

File tree

4 files changed

+55
-15
lines changed

4 files changed

+55
-15
lines changed

R/expect-match.R

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#' Does a string match a regular expression?
22
#'
33
#' @details
4-
#' `expect_match()` is a wrapper around [grepl()]. See its documentation for
5-
#' more detail about the individual arguments. `expect_no_match()` provides
6-
#' the complementary case, checking that a string *does not* match a regular
7-
#' expression.
4+
#' `expect_match()` checks if a character vector matches a regular expression,
5+
#' powered by [grepl()].
6+
#'
7+
#' `expect_no_match()` provides the complementary case, checking that a
8+
#' character vector *does not* match a regular expression.
89
#'
910
#' @inheritParams expect_that
1011
#' @inheritParams base::grepl
@@ -21,12 +22,11 @@
2122
#' expect_match("Testing is fun", "f.n")
2223
#' expect_no_match("Testing is fun", "horrible")
2324
#'
24-
#' \dontrun{
25-
#' expect_match("Testing is fun", "horrible")
25+
#' show_failure(expect_match("Testing is fun", "horrible"))
26+
#' show_failure(expect_match("Testing is fun", "horrible", fixed = TRUE))
2627
#'
2728
#' # Zero-length inputs always fail
28-
#' expect_match(character(), ".")
29-
#' }
29+
#' show_failure(expect_match(character(), "."))
3030
expect_match <- function(
3131
object,
3232
regexp,
@@ -37,11 +37,14 @@ expect_match <- function(
3737
info = NULL,
3838
label = NULL
3939
) {
40-
# Capture here to avoid environment-related messiness
4140
act <- quasi_label(enquo(object), label, arg = "object")
42-
stopifnot(is.character(regexp), length(regexp) == 1)
4341

44-
stopifnot(is.character(act$val))
42+
check_character(object)
43+
check_string(regexp)
44+
check_bool(perl)
45+
check_bool(fixed)
46+
check_bool(all)
47+
4548
if (length(object) == 0) {
4649
return(fail(sprintf("%s is empty.", act$lab), info = info))
4750
}

man/expect_match.Rd

Lines changed: 3 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/expect-match.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,34 @@
1717
* d
1818
* e
1919

20+
# checks its inputs
21+
22+
Code
23+
expect_match(1)
24+
Condition
25+
Error in `expect_match()`:
26+
! `object` must be a character vector, not the number 1.
27+
Code
28+
expect_match("x", 1)
29+
Condition
30+
Error in `expect_match()`:
31+
! `regexp` must be a single string, not the number 1.
32+
Code
33+
expect_match("x", "x", fixed = 1)
34+
Condition
35+
Error in `expect_match()`:
36+
! `fixed` must be `TRUE` or `FALSE`, not the number 1.
37+
Code
38+
expect_match("x", "x", perl = 1)
39+
Condition
40+
Error in `expect_match()`:
41+
! `perl` must be `TRUE` or `FALSE`, not the number 1.
42+
Code
43+
expect_match("x", "x", all = 1)
44+
Condition
45+
Error in `expect_match()`:
46+
! `all` must be `TRUE` or `FALSE`, not the number 1.
47+
2048
# expect_no_match works
2149

2250
`x` does match string "e*".

tests/testthat/test-expect-match.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ test_that("generates useful failure messages", {
99
expect_snapshot_failure(expect_match(many, 'asdf'))
1010
})
1111

12+
test_that("checks its inputs", {
13+
expect_snapshot(error = TRUE, {
14+
expect_match(1)
15+
expect_match("x", 1)
16+
expect_match("x", "x", fixed = 1)
17+
expect_match("x", "x", perl = 1)
18+
expect_match("x", "x", all = 1)
19+
})
20+
})
21+
1222
test_that("extra arguments passed onto grepl", {
1323
expect_failure(expect_match("\\s", "\\s"))
1424
expect_success(expect_match("\\s", "\\s", fixed = TRUE))

0 commit comments

Comments
 (0)