55# '
66# ' @param ok `TRUE` or `FALSE` indicating if the expectation was successful.
77# ' @param failure_message Message to show if the expectation failed.
8- # ' @param info Character vector continuing additional information. Included
9- # ' for backward compatibility only and new expectations should not use it.
10- # ' @param srcref Location of the failure. Should only needed to be explicitly
11- # ' supplied when you need to forward a srcref captured elsewhere.
12- # ' @param trace An optional backtrace created by [rlang::trace_back()].
13- # ' When supplied, the expectation is displayed with the backtrace.
14- # ' @param trace_env If `is.null(trace)`, this is used to automatically
15- # ' generate a traceback running from `test_code()`/`test_file()` to
16- # ' `trace_env`. You'll generally only need to set this if you're wrapping
17- # ' an expectation inside another function.
18- # ' @return An expectation object. Signals the expectation condition
8+ # ' @inheritParams fail
9+ # ' @return An expectation object from either `succeed()` or `fail()`.
1910# ' with a `continue_test` restart.
20- # '
21- # ' @details
22- # '
23- # ' While `expect()` creates and signals an expectation in one go,
24- # ' `exp_signal()` separately signals an expectation that you
25- # ' have manually created with [new_expectation()]. Expectations are
26- # ' signalled with the following protocol:
27- # '
28- # ' * If the expectation is a failure or an error, it is signalled with
29- # ' [base::stop()]. Otherwise, it is signalled with
30- # ' [base::signalCondition()].
31- # '
32- # ' * The `continue_test` restart is registered. When invoked, failing
33- # ' expectations are ignored and normal control flow is resumed to
34- # ' run the other tests.
35- # '
3611# ' @seealso [exp_signal()]
3712# ' @export
3813expect <- function (
@@ -43,58 +18,39 @@ expect <- function(
4318 trace = NULL ,
4419 trace_env = caller_env()
4520) {
46- type <- if (ok ) " success" else " failure"
47-
48- # Preserve existing API which appear to be used in package test code
49- # Can remove in next major release
50- if (missing(failure_message )) {
51- warn(" `failure_message` is missing, with no default." )
52- message <- " unknown failure"
21+ if (ok ) {
22+ succeed(failure_message )
5323 } else {
54- # A few packages include code in info that errors on evaluation
55- if (ok ) {
56- message <- paste(failure_message , collapse = " \n " )
57- } else {
58- message <- paste(c(failure_message , info ), collapse = " \n " )
59- }
60- }
61-
62- if (! ok ) {
63- if (is.null(trace )) {
64- trace <- trace_back(
65- top = getOption(" testthat_topenv" ),
66- bottom = trace_env
67- )
68- }
69-
70- # Only show if there's at least one function apart from the expectation
71- if (trace_length(trace ) < = 1 ) {
72- trace <- NULL
73- }
24+ fail(
25+ failure_message ,
26+ info ,
27+ srcref = srcref ,
28+ trace = trace ,
29+ trace_env = trace_env
30+ )
7431 }
75-
76- exp <- expectation(type , message , srcref = srcref , trace = trace )
77- exp_signal(exp )
7832}
7933
80-
8134# ' Construct an expectation object
8235# '
36+ # ' @description
8337# ' For advanced use only. If you are creating your own expectation, you should
84- # ' call [expect ()] instead . See `vignette("custom-expectation")` for more
38+ # ' call [pass ()] or [fail()] . See `vignette("custom-expectation")` for more
8539# ' details.
8640# '
87- # ' Create an expectation with `expectation()` or `new_expectation ()`
88- # ' and signal it with `exp_signal ()`.
41+ # ' `new_expectation()` creates an expectation object and `exp_signal ()` signals
42+ # ' it. `expectation ()` does both .
8943# '
9044# ' @param type Expectation type. Must be one of "success", "failure", "error",
9145# ' "skip", "warning".
9246# ' @param message Message describing test failure
9347# ' @param srcref Optional `srcref` giving location of test.
48+ # ' @keywords internal
9449# ' @inheritParams expect
9550# ' @export
9651expectation <- function (type , message , srcref = NULL , trace = NULL ) {
97- new_expectation(type , message , srcref = srcref , trace = trace )
52+ exp <- new_expectation(type , message , srcref = srcref , trace = trace )
53+ exp_signal(exp )
9854}
9955# ' @rdname expectation
10056# ' @param ... Additional attributes for the expectation object.
@@ -207,7 +163,7 @@ as.expectation.error <- function(x, srcref = NULL) {
207163 cnd_message(x )
208164 )
209165
210- expectation (" error" , msg , srcref , trace = x [[" trace" ]])
166+ new_expectation (" error" , msg , srcref = srcref , trace = x [[" trace" ]])
211167}
212168
213169
@@ -217,12 +173,17 @@ is_simple_error <- function(x) {
217173
218174# ' @export
219175as.expectation.warning <- function (x , srcref = NULL ) {
220- expectation(" warning" , cnd_message(x ), srcref , trace = x [[" trace" ]])
176+ new_expectation(
177+ " warning" ,
178+ cnd_message(x ),
179+ srcref = srcref ,
180+ trace = x [[" trace" ]]
181+ )
221182}
222183
223184# ' @export
224185as.expectation.skip <- function (x , ... , srcref = NULL ) {
225- expectation (" skip" , cnd_message(x ), srcref , trace = x [[" trace" ]])
186+ new_expectation (" skip" , cnd_message(x ), srcref = srcref , trace = x [[" trace" ]])
226187}
227188
228189# ' @export
0 commit comments