Skip to content

Commit ceb276e

Browse files
committed
Merged origin/main into custom-expectations
2 parents cb0f77e + 2896db2 commit ceb276e

28 files changed

+229
-226
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# testthat (development version)
22

3+
* `expect_matches()` failures should be a little easier to read (#2135).
34
* New `local_on_cran(TRUE)` allows you to simulate how your tests will run on CRAN (#2112).
45
* `expect_no_*()` now executes the entire code block, rather than stopping at the first message or warning (#1991).
56
* `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 (#)

R/expect-constant.R

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@ NULL
3030
#' @rdname logical-expectations
3131
expect_true <- function(object, info = NULL, label = NULL) {
3232
act <- quasi_label(enquo(object), label, arg = "object")
33-
expect_waldo_constant_(act, TRUE, info = info, ignore_attr = TRUE)
33+
exp <- labelled_value(TRUE, "TRUE")
34+
expect_waldo_equal_("equal", act, exp, info = info, ignore_attr = TRUE)
3435
}
3536

3637
#' @export
3738
#' @rdname logical-expectations
3839
expect_false <- function(object, info = NULL, label = NULL) {
3940
act <- quasi_label(enquo(object), label, arg = "object")
40-
expect_waldo_constant_(act, FALSE, info = info, ignore_attr = TRUE)
41+
exp <- labelled_value(FALSE, "FALSE")
42+
expect_waldo_equal_("equal", act, exp, info = info, ignore_attr = TRUE)
4143
}
4244

4345
#' Does code return `NULL`?
@@ -56,35 +58,6 @@ expect_false <- function(object, info = NULL, label = NULL) {
5658
#' show_failure(expect_null(y))
5759
expect_null <- function(object, info = NULL, label = NULL) {
5860
act <- quasi_label(enquo(object), label, arg = "object")
59-
expect_waldo_constant_(act, NULL, info = info)
60-
}
61-
62-
# helpers -----------------------------------------------------------------
63-
64-
expect_waldo_constant_ <- function(
65-
act,
66-
constant,
67-
info,
68-
...,
69-
trace_env = caller_env()
70-
) {
71-
comp <- waldo_compare(
72-
act$val,
73-
constant,
74-
x_arg = "actual",
75-
y_arg = "expected",
76-
...
77-
)
78-
79-
if (length(comp) != 0) {
80-
msg <- sprintf(
81-
"%s is not %s\n\n%s",
82-
act$lab,
83-
deparse(constant),
84-
paste0(comp, collapse = "\n\n")
85-
)
86-
return(fail(msg, info = info, trace_env = trace_env))
87-
}
88-
89-
pass(act$val)
61+
exp <- labelled_value(NULL, "FALSE")
62+
expect_waldo_equal_("equal", act, exp, info = info)
9063
}

R/expect-equality.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ expect_waldo_equal_ <- function(
136136
)
137137
if (length(comp) != 0) {
138138
msg <- sprintf(
139-
"%s (%s) not %s to %s (%s).\n\n%s",
139+
"%s (%s) is not %s to %s (%s).\n\n%s",
140140
act$lab,
141141
"`actual`",
142142
type,

R/expect-match.R

Lines changed: 20 additions & 24 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
}
@@ -75,11 +78,7 @@ expect_no_match <- function(
7578
# Capture here to avoid environment-related messiness
7679
act <- quasi_label(enquo(object), label, arg = "object")
7780
stopifnot(is.character(regexp), length(regexp) == 1)
78-
7981
stopifnot(is.character(act$val))
80-
if (length(object) == 0) {
81-
return(fail(sprintf("%s is empty.", act$lab), info = info))
82-
}
8382

8483
expect_match_(
8584
act = act,
@@ -114,20 +113,17 @@ expect_match_ <- function(
114113
return(pass(act$val))
115114
}
116115

117-
escape <- if (fixed) identity else escape_regex
118-
116+
text <- encodeString(act$val)
119117
if (length(act$val) == 1) {
120-
values <- paste0("Actual value: \"", escape(encodeString(act$val)), "\"")
118+
values <- paste0('Text: "', text, '"')
121119
} else {
122-
values <- paste0(
123-
"Actual values:\n",
124-
paste0("* ", escape(encodeString(act$val)), collapse = "\n")
125-
)
120+
values <- paste0("Text:\n", paste0("* ", text, collapse = "\n"))
126121
}
127122

128123
msg <- sprintf(
129-
if (negate) "%s does match %s.\n%s" else "%s does not match %s.\n%s",
130-
escape(act$lab),
124+
if (negate) "%s matches %s %s.\n%s" else "%s does not match %s %s.\n%s",
125+
act$lab,
126+
if (fixed) "string" else "regexp",
131127
encodeString(regexp, quote = '"'),
132128
values
133129
)

R/expect-output.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ expect_output <- function(
4545
}
4646
pass(act$val)
4747
} else {
48-
act <- new_actual(act$cap, act$lab)
48+
act <- labelled_value(act$cap, act$lab)
4949
expect_match_(act, enc2native(regexp), ...)
5050
}
5151
}

R/expect-self-test.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ expect_failure <- function(expr, message = NULL, ...) {
8585
}
8686

8787
if (!is.null(message)) {
88-
act <- new_actual(status$last_failure$message, "Failure message")
88+
act <- labelled_value(status$last_failure$message, "Failure message")
8989
return(expect_match_(act, message, ...))
9090
}
9191
pass(NULL)

R/local.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,9 @@ waldo_compare <- function(x, y, ..., x_arg = "x", y_arg = "y") {
153153
# Need to very carefully isolate this change to this function - can not set
154154
# in expectation functions because part of expectation handling bubbles
155155
# up through calling handlers, which are run before on.exit()
156-
local_reporter_output()
157-
156+
if (!is_snapshot()) {
157+
local_reporter_output()
158+
}
158159
waldo::compare(x, y, ..., x_arg = x_arg, y_arg = y_arg)
159160
}
160161

R/quasi-label.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ quasi_label <- function(quo, label = NULL, arg = "quo") {
4242

4343
expr <- quo_get_expr(quo)
4444

45-
new_actual(
45+
labelled_value(
4646
eval_bare(expr, quo_get_env(quo)),
4747
label %||% expr_label(expr)
4848
)
4949
}
5050

51-
new_actual <- function(value, label) {
51+
labelled_value <- function(value, label) {
5252
list(
5353
val = value,
5454
lab = label

R/snapshot.R

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -268,21 +268,16 @@ expect_snapshot_condition_ <- function(
268268
)
269269
if (is.null(val)) {
270270
if (base_class == class) {
271-
return(fail(
272-
sprintf("%s did not generate %s", lab, base_class),
273-
trace_env = trace_env
274-
))
271+
msg <- sprintf("%s did not generate %s", lab, base_class)
275272
} else {
276-
return(fail(
277-
sprintf(
278-
"%s did not generate %s with class '%s'",
279-
lab,
280-
base_class,
281-
class
282-
),
283-
trace_env = trace_env
284-
))
273+
msg <- sprintf(
274+
"%s did not generate %s with class '%s'",
275+
lab,
276+
base_class,
277+
class
278+
)
285279
}
280+
return(fail(msg, trace_env = trace_env))
286281
}
287282

288283
expect_snapshot_helper(

R/utils.R

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,6 @@
22
#' @export
33
magrittr::`%>%`
44

5-
escape_regex <- function(x) {
6-
chars <- c(
7-
"*",
8-
".",
9-
"?",
10-
"^",
11-
"+",
12-
"$",
13-
"|",
14-
"(",
15-
")",
16-
"[",
17-
"]",
18-
"{",
19-
"}",
20-
"\\"
21-
)
22-
gsub(
23-
paste0("([\\", paste0(collapse = "\\", chars), "])"),
24-
"\\\\\\1",
25-
x,
26-
perl = TRUE
27-
)
28-
}
29-
305
can_entrace <- function(cnd) {
316
!inherits(cnd, "Throwable")
327
}

0 commit comments

Comments
 (0)