Skip to content

Commit 89b63dd

Browse files
authored
Standardise expectation helpers (#2136)
Always finish with `_` and include a `trace_env` argument. Part of #2132
1 parent e4ab555 commit 89b63dd

18 files changed

+163
-95
lines changed

R/expect-comparison.R

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@
1919
#' @name comparison-expectations
2020
NULL
2121

22-
expect_compare <- function(operator = c("<", "<=", ">", ">="), act, exp) {
22+
expect_compare_ <- function(
23+
operator = c("<", "<=", ">", ">="),
24+
act,
25+
exp,
26+
trace_env = caller_env()
27+
) {
2328
operator <- match.arg(operator)
2429
op <- match.fun(operator)
2530

@@ -42,7 +47,7 @@ expect_compare <- function(operator = c("<", "<=", ">", ">="), act, exp) {
4247
exp$lab,
4348
act$val - exp$val
4449
)
45-
return(fail(msg, trace_env = caller_env()))
50+
return(fail(msg, trace_env = trace_env))
4651
}
4752
pass(act$val)
4853
}
@@ -52,7 +57,7 @@ expect_lt <- function(object, expected, label = NULL, expected.label = NULL) {
5257
act <- quasi_label(enquo(object), label, arg = "object")
5358
exp <- quasi_label(enquo(expected), expected.label, arg = "expected")
5459

55-
expect_compare("<", act, exp)
60+
expect_compare_("<", act, exp)
5661
}
5762

5863
#' @export
@@ -61,7 +66,7 @@ expect_lte <- function(object, expected, label = NULL, expected.label = NULL) {
6166
act <- quasi_label(enquo(object), label, arg = "object")
6267
exp <- quasi_label(enquo(expected), expected.label, arg = "expected")
6368

64-
expect_compare("<=", act, exp)
69+
expect_compare_("<=", act, exp)
6570
}
6671

6772
#' @export
@@ -70,7 +75,7 @@ expect_gt <- function(object, expected, label = NULL, expected.label = NULL) {
7075
act <- quasi_label(enquo(object), label, arg = "object")
7176
exp <- quasi_label(enquo(expected), expected.label, arg = "expected")
7277

73-
expect_compare(">", act, exp)
78+
expect_compare_(">", act, exp)
7479
}
7580

7681
#' @export
@@ -79,7 +84,7 @@ expect_gte <- function(object, expected, label = NULL, expected.label = NULL) {
7984
act <- quasi_label(enquo(object), label, arg = "object")
8085
exp <- quasi_label(enquo(expected), expected.label, arg = "expected")
8186

82-
expect_compare(">=", act, exp)
87+
expect_compare_(">=", act, exp)
8388
}
8489

8590

R/expect-condition.R

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ expect_error <- function(
115115
label = NULL
116116
) {
117117
if (edition_get() >= 3) {
118-
expect_condition_matching(
118+
expect_condition_matching_(
119119
"error",
120120
{{ object }},
121121
regexp = regexp,
@@ -163,7 +163,7 @@ expect_warning <- function(
163163
warn("The `all` argument is deprecated")
164164
}
165165

166-
expect_condition_matching(
166+
expect_condition_matching_(
167167
"warning",
168168
{{ object }},
169169
regexp = regexp,
@@ -208,7 +208,7 @@ expect_message <- function(
208208
label = NULL
209209
) {
210210
if (edition_get() >= 3) {
211-
expect_condition_matching(
211+
expect_condition_matching_(
212212
"message",
213213
{{ object }},
214214
regexp = regexp,
@@ -240,7 +240,7 @@ expect_condition <- function(
240240
label = NULL
241241
) {
242242
if (edition_get() >= 3) {
243-
expect_condition_matching(
243+
expect_condition_matching_(
244244
"condition",
245245
{{ object }},
246246
regexp = regexp,
@@ -273,7 +273,7 @@ expect_condition <- function(
273273
}
274274
}
275275

276-
expect_condition_matching <- function(
276+
expect_condition_matching_ <- function(
277277
base_class,
278278
object,
279279
regexp = NULL,
@@ -309,7 +309,12 @@ expect_condition_matching <- function(
309309
# Access error fields with `[[` rather than `$` because the
310310
# `$.Throwable` from the rJava package throws with unknown fields
311311
if (!is.null(msg)) {
312-
return(fail(msg, info = info, trace = act$cap[["trace"]], trace_env = trace_env))
312+
return(fail(
313+
msg,
314+
info = info,
315+
trace = act$cap[["trace"]],
316+
trace_env = trace_env
317+
))
313318
}
314319
# If a condition was expected, return it. Otherwise return the value
315320
# of the expression.

R/expect-constant.R

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ 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+
expect_waldo_constant_(act, TRUE, info = info, ignore_attr = TRUE)
3434
}
3535

3636
#' @export
3737
#' @rdname logical-expectations
3838
expect_false <- function(object, info = NULL, label = NULL) {
3939
act <- quasi_label(enquo(object), label, arg = "object")
40-
expect_waldo_constant(act, FALSE, info = info, ignore_attr = TRUE)
40+
expect_waldo_constant_(act, FALSE, info = info, ignore_attr = TRUE)
4141
}
4242

4343
#' Does code return `NULL`?
@@ -56,12 +56,18 @@ expect_false <- function(object, info = NULL, label = NULL) {
5656
#' show_failure(expect_null(y))
5757
expect_null <- function(object, info = NULL, label = NULL) {
5858
act <- quasi_label(enquo(object), label, arg = "object")
59-
expect_waldo_constant(act, NULL, info = info)
59+
expect_waldo_constant_(act, NULL, info = info)
6060
}
6161

6262
# helpers -----------------------------------------------------------------
6363

64-
expect_waldo_constant <- function(act, constant, info, ...) {
64+
expect_waldo_constant_ <- function(
65+
act,
66+
constant,
67+
info,
68+
...,
69+
trace_env = caller_env()
70+
) {
6571
comp <- waldo_compare(
6672
act$val,
6773
constant,
@@ -77,7 +83,7 @@ expect_waldo_constant <- function(act, constant, info, ...) {
7783
deparse(constant),
7884
paste0(comp, collapse = "\n\n")
7985
)
80-
return(fail(msg, info = info, trace_env = caller_env()))
86+
return(fail(msg, info = info, trace_env = trace_env))
8187
}
8288

8389
pass(act$val)

R/expect-equality.R

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ expect_equal <- function(
6767
exp <- quasi_label(enquo(expected), expected.label, arg = "expected")
6868

6969
if (edition_get() >= 3) {
70-
expect_waldo_equal("equal", act, exp, info, ..., tolerance = tolerance)
70+
expect_waldo_equal_("equal", act, exp, info, ..., tolerance = tolerance)
7171
} else {
7272
if (!is.null(tolerance)) {
7373
comp <- compare(act$val, exp$val, ..., tolerance = tolerance)
@@ -97,7 +97,7 @@ expect_identical <- function(
9797
exp <- quasi_label(enquo(expected), expected.label, arg = "expected")
9898

9999
if (edition_get() >= 3) {
100-
expect_waldo_equal("identical", act, exp, info, ...)
100+
expect_waldo_equal_("identical", act, exp, info, ...)
101101
} else {
102102
ident <- identical(act$val, exp$val, ...)
103103
if (ident) {
@@ -119,7 +119,14 @@ expect_identical <- function(
119119
}
120120
}
121121

122-
expect_waldo_equal <- function(type, act, exp, info, ...) {
122+
expect_waldo_equal_ <- function(
123+
type,
124+
act,
125+
exp,
126+
info,
127+
...,
128+
trace_env = caller_env()
129+
) {
123130
comp <- waldo_compare(
124131
act$val,
125132
exp$val,
@@ -137,7 +144,7 @@ expect_waldo_equal <- function(type, act, exp, info, ...) {
137144
"`expected`",
138145
paste0(comp, collapse = "\n\n")
139146
)
140-
return(fail(msg, info = info, trace_env = caller_env()))
147+
return(fail(msg, info = info, trace_env = trace_env))
141148
}
142149
pass(act$val)
143150
}

R/expect-length.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ expect_length <- function(object, n) {
2121

2222
if (act$n != n) {
2323
msg <- sprintf("%s has length %i, not length %i.", act$lab, act$n, n)
24-
return(fail(msg, trace_env = parent.frame()))
24+
return(fail(msg))
2525
}
2626
pass(act$val)
2727
}

R/expect-output.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ expect_output <- function(
4545
}
4646
pass(act$val)
4747
} else {
48-
expect_match(act$cap, enc2native(regexp), ..., info = info, label = act$lab)
48+
act <- new_actual(act$cap, act$lab)
49+
expect_match_(act, enc2native(regexp), ...)
4950
}
5051
}

R/expect-self-test.R

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,16 @@ expect_failure <- function(expr, message = NULL, ...) {
8585
}
8686

8787
if (!is.null(message)) {
88-
return(expect_match(status$last_failure$message, message, ...))
88+
act <- new_actual(status$last_failure$message, "Failure message")
89+
return(expect_match_(act, message, ...))
8990
}
9091
pass(NULL)
9192
}
9293

9394
#' @export
9495
#' @rdname expect_success
9596
expect_snapshot_failure <- function(expr) {
96-
expect_snapshot_error(expr, "expectation_failure")
97+
expect_snapshot_condition_("expectation_failure", expr)
9798
}
9899

99100
#' Test for absence of success or failure
@@ -134,13 +135,13 @@ expect_no_failure <- function(expr) {
134135
}
135136

136137
expect_snapshot_skip <- function(x, cran = FALSE) {
137-
expect_snapshot_error(x, class = "skip", cran = cran)
138+
expect_snapshot_condition_("skip", x)
138139
}
139140
expect_skip <- function(code) {
140-
expect_condition(code, class = "skip")
141+
expect_condition_matching_("skip", code)
141142
}
142143
expect_no_skip <- function(code) {
143-
expect_no_condition(code, class = "skip")
144+
expect_no_("skip", code)
144145
}
145146

146147

R/expectations-matches.R

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,17 @@ expect_match_ <- function(
103103
all = TRUE,
104104
info = NULL,
105105
label = NULL,
106-
negate = FALSE
106+
negate = FALSE,
107+
trace_env = caller_env()
107108
) {
108109
matches <- grepl(regexp, act$val, perl = perl, fixed = fixed, ...)
109110
condition <- if (negate) !matches else matches
111+
ok <- if (all) all(condition) else any(condition)
112+
113+
if (ok) {
114+
return(pass(act$val))
115+
}
116+
110117
escape <- if (fixed) identity else escape_regex
111118

112119
if (length(act$val) == 1) {
@@ -117,14 +124,12 @@ expect_match_ <- function(
117124
paste0("* ", escape(encodeString(act$val)), collapse = "\n")
118125
)
119126
}
120-
if (if (all) !all(condition) else !any(condition)) {
121-
msg <- sprintf(
122-
if (negate) "%s does match %s.\n%s" else "%s does not match %s.\n%s",
123-
escape(act$lab),
124-
encodeString(regexp, quote = '"'),
125-
values
126-
)
127-
return(fail(msg, info = info, trace_env = caller_env()))
128-
}
129-
pass(act$val)
127+
128+
msg <- sprintf(
129+
if (negate) "%s does match %s.\n%s" else "%s does not match %s.\n%s",
130+
escape(act$lab),
131+
encodeString(regexp, quote = '"'),
132+
values
133+
)
134+
return(fail(msg, info = info, trace_env = trace_env))
130135
}

R/quasi-label.R

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

4343
expr <- quo_get_expr(quo)
4444

45+
new_actual(
46+
eval_bare(expr, quo_get_env(quo)),
47+
label %||% expr_label(expr)
48+
)
49+
}
50+
51+
new_actual <- function(value, label) {
4552
list(
46-
val = eval_bare(expr, quo_get_env(quo)),
47-
lab = label %||% expr_label(expr)
53+
val = value,
54+
lab = label
4855
)
4956
}
5057

R/snapshot-file.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ snapshot_file_equal <- function(
191191
path,
192192
file_equal = compare_file_binary,
193193
fail_on_new = FALSE,
194-
trace_env = NULL
194+
trace_env = caller_env()
195195
) {
196196
if (!file.exists(path)) {
197197
abort(paste0("`", path, "` not found"))

0 commit comments

Comments
 (0)