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
1136expect_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
2962expect_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-
74115expect_snapshot_reporter <- function (reporter , paths = test_path(" reporters/tests.R" )) {
75116 local_options(rlang_trace_format_srcrefs = FALSE )
76117 local_rng_version(" 3.3" )
0 commit comments