Skip to content

Commit e25d328

Browse files
committed
Merge commit '6ba4397f440014d3ee9a24441e2a913c2d06e8fe'
2 parents 8eb147b + 6ba4397 commit e25d328

13 files changed

+147
-63
lines changed

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,10 @@ export(expect_more_than)
106106
export(expect_named)
107107
export(expect_no_condition)
108108
export(expect_no_error)
109+
export(expect_no_failure)
109110
export(expect_no_match)
110111
export(expect_no_message)
112+
export(expect_no_success)
111113
export(expect_no_warning)
112114
export(expect_null)
113115
export(expect_output)
@@ -119,6 +121,7 @@ export(expect_setequal)
119121
export(expect_silent)
120122
export(expect_snapshot)
121123
export(expect_snapshot_error)
124+
export(expect_snapshot_failure)
122125
export(expect_snapshot_file)
123126
export(expect_snapshot_output)
124127
export(expect_snapshot_value)

NEWS.md

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

33
* `expect_error()` and friends now error if you supply `...` but not `pattern` (#1932).
4+
* New `expect_no_failure()`, `expect_no_success()` and `expect_snapshot_failure()` provide more options for testing expectations.
5+
* `expect_error()` and friends no longer give an uninformative error if they fail inside a magrittr pipe (#1994).
6+
* `expect_setequal()` correctly identifies what is missing where (#1962).
47
* `expect_true()` and `expect_false()` give better errors if `actual` isn't a vector (#1996).
58
* `expect_no_*()` expectations no longer incorrectly emit a passing test result if they in fact fail (#1997).
69
* Require the latest version of waldo (0.6.0) in order to get the latest goodies (#1955).

R/expect-condition.R

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,7 @@ expect_warning <- function(object,
163163
...,
164164
inherit = inherit,
165165
info = info,
166-
label = label,
167-
trace_env = caller_env()
166+
label = label
168167
)
169168
} else {
170169
act <- quasi_capture(enquo(object), label, capture_warnings, ignore_deprecation = identical(regexp, NA))
@@ -196,8 +195,7 @@ expect_message <- function(object,
196195
...,
197196
inherit = inherit,
198197
info = info,
199-
label = label,
200-
trace_env = caller_env()
198+
label = label
201199
)
202200
} else {
203201
act <- quasi_capture(enquo(object), label, capture_messages)
@@ -225,8 +223,7 @@ expect_condition <- function(object,
225223
...,
226224
inherit = inherit,
227225
info = info,
228-
label = label,
229-
trace_env = caller_env()
226+
label = label
230227
)
231228
} else {
232229

@@ -263,7 +260,7 @@ expect_condition_matching <- function(base_class,
263260
...,
264261
inherit = inherit,
265262
ignore_deprecation = base_class == "warning" && identical(regexp, NA),
266-
error_call = trace_env
263+
error_call = error_call
267264
)
268265

269266
act <- quasi_capture(

R/expect-no-condition.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ expect_no_condition <- function(object,
8282

8383

8484
expect_no_ <- function(base_class,
85-
object,
86-
regexp = NULL,
87-
class = NULL,
88-
error_call = caller_env()) {
85+
object,
86+
regexp = NULL,
87+
class = NULL,
88+
trace_env = caller_env()) {
8989

9090
matcher <- cnd_matcher(
9191
base_class,
@@ -116,7 +116,7 @@ expect_no_ <- function(base_class,
116116
indent_lines(rlang::cnd_message(cnd))
117117
)
118118
message <- format_error_bullets(c(expected, i = actual))
119-
fail(message, trace_env = error_call)
119+
fail(message, trace_env = trace_env)
120120
}
121121
)
122122
}

R/expect-self-test.R

Lines changed: 62 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,56 @@
1+
capture_failure <- new_capture("expectation_failure")
2+
capture_success <- function(expr) {
3+
cnd <- NULL
4+
5+
withCallingHandlers(
6+
expr,
7+
expectation_failure = function(cnd) {
8+
invokeRestart("continue_test")
9+
},
10+
expectation_success = function(cnd) {
11+
cnd <<- cnd
12+
}
13+
)
14+
cnd
15+
}
16+
17+
new_capture("expectation_success")
18+
119
#' Tools for testing expectations
220
#'
3-
#' Use these expectations to test other expectations.
21+
#' @description
22+
#' * `expect_sucess()` and `expect_failure()` check that there's at least
23+
#' one success or failure respectively.
24+
#' * `expect_snapshot_failure()` records the failure message so that you can
25+
#' manually check that it is informative.
26+
#' * `expect_no_success()` and `expect_no_failure()` check that are no
27+
#' successes or failures.
28+
#'
429
#' Use `show_failure()` in examples to print the failure message without
530
#' throwing an error.
631
#'
7-
#' @param expr Expression that evaluates a single expectation.
32+
#' @param expr Code to evalute
833
#' @param message Check that the failure message matches this regexp.
934
#' @param ... Other arguments passed on to [expect_match()].
1035
#' @export
1136
expect_success <- function(expr) {
12-
exp <- capture_expectation(expr)
37+
exp <- capture_success(expr)
1338

1439
if (is.null(exp)) {
15-
fail("no expectation used.")
16-
} else if (!expectation_success(exp)) {
17-
fail(paste0(
18-
"Expectation did not succeed:\n",
19-
exp$message
20-
))
40+
fail("Expectation did not succeed")
41+
} else {
42+
succeed()
43+
}
44+
invisible(NULL)
45+
}
46+
47+
#' @export
48+
#' @rdname expect_success
49+
expect_no_success <- function(expr) {
50+
exp <- capture_success(expr)
51+
52+
if (!is.null(exp)) {
53+
fail("Expectation succeeded")
2154
} else {
2255
succeed()
2356
}
@@ -27,19 +60,31 @@ expect_success <- function(expr) {
2760
#' @export
2861
#' @rdname expect_success
2962
expect_failure <- function(expr, message = NULL, ...) {
30-
exp <- capture_expectation(expr)
63+
exp <- capture_failure(expr)
3164

3265
if (is.null(exp)) {
33-
fail("No expectation used")
34-
return()
35-
}
36-
if (!expectation_failure(exp)) {
3766
fail("Expectation did not fail")
38-
return()
67+
} else if (!is.null(message)) {
68+
expect_match(exp$message, message, ...)
69+
} else {
70+
succeed()
3971
}
72+
invisible(NULL)
73+
}
4074

41-
if (!is.null(message)) {
42-
expect_match(exp$message, message, ...)
75+
#' @export
76+
#' @rdname expect_success
77+
expect_snapshot_failure <- function(expr) {
78+
expect_snapshot_error(expr, "expectation_failure")
79+
}
80+
81+
#' @export
82+
#' @rdname expect_success
83+
expect_no_failure <- function(expr) {
84+
exp <- capture_failure(expr)
85+
86+
if (!is.null(exp)) {
87+
fail("Expectation failed")
4388
} else {
4489
succeed()
4590
}
@@ -67,10 +112,6 @@ show_failure <- function(expr) {
67112
invisible()
68113
}
69114

70-
expect_snapshot_failure <- function(x) {
71-
expect_snapshot_error(x, "expectation_failure")
72-
}
73-
74115
expect_snapshot_reporter <- function(reporter, paths = test_path("reporters/tests.R")) {
75116
local_options(rlang_trace_format_srcrefs = FALSE)
76117
local_rng_version("3.3")

R/expect-setequal.R

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,16 @@ expect_setequal <- function(object, expected) {
3535
warn("expect_setequal() ignores names")
3636
}
3737

38-
act_miss <- !act$val %in% exp$val
39-
exp_miss <- !exp$val %in% act$val
38+
act_miss <- setdiff(act$val, exp$val)
39+
exp_miss <- setdiff(exp$val, act$val)
4040

41-
if (any(exp_miss) || any(act_miss)) {
41+
if (length(exp_miss) || length(act_miss)) {
4242
fail(paste0(
4343
act$lab, " (`actual`) and ", exp$lab, " (`expected`) don't have the same values.\n",
44-
if (any(act_miss))
45-
paste0("* Only in `expected`: ", values(act$val[act_miss]), "\n"),
46-
if (any(exp_miss))
47-
paste0("* Only in `actual`: ", values(exp$val[exp_miss]), "\n")
44+
if (length(act_miss))
45+
paste0("* Only in `actual`: ", values(act_miss), "\n"),
46+
if (length(exp_miss))
47+
paste0("* Only in `expected`: ", values(exp_miss), "\n")
4848
))
4949
} else {
5050
succeed()

man/expect_success.Rd

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

tests/testthat/_snaps/expect-condition.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@
3434
Code
3535
expect_condition(stop("Hi!"), foo = "bar")
3636
Condition
37-
Error:
37+
Error in `expect_condition()`:
3838
! `...` ignored when `pattern` is not set.
3939
Code
4040
expect_condition(stop("Hi!"), "x", foo = "bar")
4141
Condition
42-
Error:
42+
Error in `expect_condition()`:
4343
! Failed to compare message to `pattern`.
4444
Caused by error in `grepl()`:
4545
! unused argument (foo = "bar")

tests/testthat/_snaps/expect-setequal.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
11
# useful message on failure
22

3+
"actual" (`actual`) and "expected" (`expected`) don't have the same values.
4+
* Only in `actual`: "actual"
5+
* Only in `expected`: "expected"
6+
7+
8+
---
9+
310
1:2 (`actual`) and 2 (`expected`) don't have the same values.
4-
* Only in `expected`: 1
11+
* Only in `actual`: 1
512

613

714
---
815

916
2 (`actual`) and 2:3 (`expected`) don't have the same values.
10-
* Only in `actual`: 3
17+
* Only in `expected`: 3
1118

1219

1320
---
1421

1522
1:2 (`actual`) and 2:3 (`expected`) don't have the same values.
16-
* Only in `expected`: 1
17-
* Only in `actual`: 3
23+
* Only in `actual`: 1
24+
* Only in `expected`: 3
1825

1926

2027
# truncates long vectors
2128

2229
1:2 (`actual`) and 1:50 (`expected`) don't have the same values.
23-
* Only in `actual`: 3, 4, 5, 6, 7, 8, 9, 10, 11, ...
30+
* Only in `expected`: 3, 4, 5, 6, 7, 8, 9, 10, 11, ...
2431

2532

2633
# expect_contains() gives useful message on failure

tests/testthat/test-expect-condition.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ test_that("can capture Throwable conditions from rJava", {
7676
expect_error(throw("foo"), "foo", class = "Throwable")
7777
})
7878

79+
test_that("capture correct trace_env (#1994)", {
80+
# This should fail, not error
81+
expect_failure(expect_error(stop("oops")) %>% expect_warning())
82+
expect_failure(expect_warning(expect_error(stop("oops"))))
83+
})
84+
7985
# expect_warning() ----------------------------------------------------------
8086

8187
test_that("warnings are converted to errors when options('warn') >= 2", {

0 commit comments

Comments
 (0)