9
9
# ' for backward compatibility only and new expectations should not use it.
10
10
# ' @param srcref Location of the failure. Should only needed to be explicitly
11
11
# ' supplied when you need to forward a srcref captured elsewhere.
12
+ # ' @param env The environment of the `expect_` function. You should
13
+ # ' only pass this when you're calling `expect()` from an internal
14
+ # ' helper. This environment is passed to [rlang::trace_back()] as
15
+ # ' `bottom` argument, in order to remove the uninformative testthat
16
+ # ' context from backtraces.
12
17
# ' @return An expectation object. Signals the expectation condition
13
18
# ' with a `continue_test` restart.
14
19
# ' @export
15
- expect <- function (ok , failure_message , info = NULL , srcref = NULL ) {
20
+ expect <- function (ok , failure_message , info = NULL , srcref = NULL ,
21
+ env = NULL ) {
22
+ env <- env %|| % caller_env()
23
+
16
24
type <- if (ok ) " success" else " failure"
17
25
18
26
# Preserve existing API which appear to be used in package test code
@@ -29,7 +37,7 @@ expect <- function(ok, failure_message, info = NULL, srcref = NULL) {
29
37
}
30
38
}
31
39
32
- exp <- expectation(type , message , srcref = srcref )
40
+ exp <- expectation(type , message , srcref = srcref , env = env )
33
41
34
42
withRestarts(
35
43
if (ok ) signalCondition(exp ) else stop(exp ),
@@ -49,15 +57,23 @@ expect <- function(ok, failure_message, info = NULL, srcref = NULL) {
49
57
# ' "skip", "warning".
50
58
# ' @param message Message describing test failure
51
59
# ' @param srcref Optional `srcref` giving location of test.
60
+ # ' @inheritParams expect
52
61
# ' @keywords internal
53
62
# ' @export
54
- expectation <- function (type , message , srcref = NULL ) {
63
+ expectation <- function (type , message , srcref = NULL , env = NULL ) {
55
64
type <- match.arg(type , c(" success" , " failure" , " error" , " skip" , " warning" ))
56
65
66
+ if (type %in% c(" failure" , " error" )) {
67
+ trace <- trace_back(test_data $ trace_top , env )
68
+ } else {
69
+ trace <- NULL
70
+ }
71
+
57
72
structure(
58
73
list (
59
74
message = message ,
60
- srcref = srcref
75
+ srcref = srcref ,
76
+ trace = trace
61
77
),
62
78
class = c(
63
79
paste0(" expectation_" , type ),
@@ -86,14 +102,22 @@ format.expectation_success <- function(x, ...) {
86
102
87
103
# ' @export
88
104
format.expectation_error <- function (x , ... ) {
89
- paste(c( x $ message , create_traceback( x $ call )), collapse = " \n " )
105
+ format_with_trace( x )
90
106
}
91
107
92
108
# ' @export
93
109
format.expectation <- function (x , ... ) {
94
110
x $ message
95
111
}
96
112
113
+ format_with_trace <- function (exp ) {
114
+ paste_line(
115
+ paste0(" Message: " , crayon :: italic(exp $ message )),
116
+ " Backtrace:" ,
117
+ !!! format(exp $ trace , simplify = " branch" )
118
+ )
119
+ }
120
+
97
121
# as.expectation ----------------------------------------------------------
98
122
99
123
as.expectation <- function (x , ... ) UseMethod(" as.expectation" , x )
@@ -120,7 +144,7 @@ as.expectation.error <- function(x, ..., srcref = NULL) {
120
144
# Remove trailing newline to be consistent with other conditons
121
145
msg <- gsub(" \n $" , " " , msg )
122
146
123
- expectation(" error" , msg , srcref )
147
+ expectation(" error" , msg , srcref , env = x $ trace_bottom )
124
148
}
125
149
126
150
# ' @export
0 commit comments