Skip to content

Commit 953ea67

Browse files
committed
Merge commit '9c93b8f0be372cfd495d87fa18e5854b04c45ea8'
2 parents fb724f9 + 9c93b8f commit 953ea67

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+387
-288
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ export(it)
156156
export(local_edition)
157157
export(local_mock)
158158
export(local_mocked_bindings)
159+
export(local_on_cran)
159160
export(local_reproducible_output)
160161
export(local_snapshotter)
161162
export(local_test_context)

NEWS.md

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

3+
* New `local_on_cran(TRUE)` allows you to simulate how your tests will run on CRAN (#2112).
4+
* `expect_no_*()` now executes the entire code block, rather than stopping at the first message or warning (#1991).
35
* `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 (#)
46
* New `pass()` function to use in place of `succeed()` (#2113).
57
* `expectation()` is now a combination of `new_expectation()` and `exp_signal()` (#2125).

R/deprec-condition.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ capture_messages <- function(code) {
101101
code,
102102
message = function(condition) {
103103
out$push(condition)
104-
maybe_restart("muffleMessage")
104+
tryInvokeRestart("muffleMessage")
105105
}
106106
)
107107

@@ -121,7 +121,7 @@ capture_warnings <- function(code, ignore_deprecation = FALSE) {
121121
}
122122

123123
out$push(condition)
124-
maybe_restart("muffleWarning")
124+
tryInvokeRestart("muffleWarning")
125125
}
126126
)
127127

R/evaluate-promise.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ evaluate_promise <- function(code, print = FALSE) {
2020
handle_warning <- function(condition) {
2121
if (!is_deprecation(condition)) {
2222
warnings$push(condition)
23-
maybe_restart("muffleWarning")
23+
tryInvokeRestart("muffleWarning")
2424
}
2525
}
2626

2727
messages <- Stack$new()
2828
handle_message <- function(condition) {
2929
messages$push(condition)
30-
maybe_restart("muffleMessage")
30+
tryInvokeRestart("muffleMessage")
3131
}
3232

3333
path <- withr::local_tempfile()
File renamed without changes.

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: 12 additions & 7 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.
@@ -346,7 +351,7 @@ cnd_matcher <- function(
346351
if (!is.null(class) && !inherits(x, class)) {
347352
return(FALSE)
348353
}
349-
if (!is.null(regexp) && !isNA(regexp)) {
354+
if (!is.null(regexp) && !identical(regexp, NA)) {
350355
withCallingHandlers(
351356
grepl(regexp, conditionMessage(x), ...),
352357
error = function(e) {

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 & 43 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
}
@@ -194,42 +201,3 @@ expect_equivalent <- function(
194201
}
195202
pass(act$val)
196203
}
197-
198-
199-
#' Does code return a reference to the expected object?
200-
#'
201-
#' `expect_reference()` compares the underlying memory addresses of
202-
#' two symbols. It is for expert use only.
203-
#'
204-
#' @section 3rd edition:
205-
#' `r lifecycle::badge("deprecated")`
206-
#'
207-
#' `expect_reference()` is deprecated in the third edition. If you know what
208-
#' you're doing, and you really need this behaviour, just use `is_reference()`
209-
#' directly: `expect_true(rlang::is_reference(x, y))`.
210-
#'
211-
#' @inheritParams expect_equal
212-
#' @family expectations
213-
#' @keywords internal
214-
#' @export
215-
expect_reference <- function(
216-
object,
217-
expected,
218-
info = NULL,
219-
label = NULL,
220-
expected.label = NULL
221-
) {
222-
edition_deprecate(3, "expect_reference()")
223-
224-
act <- quasi_label(enquo(object), label, arg = "object")
225-
exp <- quasi_label(enquo(expected), expected.label, arg = "expected")
226-
227-
if (!is_reference(act$val, exp$val)) {
228-
msg <- sprintf("%s not a reference to %s.", act$lab, exp$lab)
229-
return(fail(msg, info = info))
230-
}
231-
pass(act$val)
232-
}
233-
234-
# expect_reference() needs dev version of rlang
235-
utils::globalVariables("is_reference")

R/expectations-matches.R renamed to R/expect-match.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
}

0 commit comments

Comments
 (0)