1
1
# ' The building block of all `expect_` functions
2
2
# '
3
- # ' Use this if you are writing your own expectation . See
3
+ # ' Call this function when writing your own expectations . See
4
4
# ' `vignette("custom-expectation")` for details.
5
5
# '
6
- # ' @param ok Was the expectation successful?
7
- # ' @param failure_message What message should be shown if the expectation was
8
- # ' not successful?
9
- # ' @param info Additional information. Included for backward compatibility
10
- # ' only and new expectations should not use it.
11
- # ' @param srcref Only needed in very rare circumstances where you need to
12
- # ' forward a srcref captured elsewhere.
6
+ # ' @param ok `TRUE` or `FALSE` indicating if the expectation was successful.
7
+ # ' @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
+ # ' @return An expectation object. Signals the expectation condition
13
+ # ' with a `continue_test` restart.
13
14
# ' @export
14
15
expect <- function (ok , failure_message , info = NULL , srcref = NULL ) {
15
16
type <- if (ok ) " success" else " failure"
16
17
message <- paste(c(failure_message , info ), collapse = " \n " )
17
18
exp <- expectation(type , message , srcref = srcref )
18
19
19
20
withRestarts(
20
- if (expectation_broken(exp )) {
21
- stop(exp )
22
- } else {
23
- signalCondition(exp )
24
- },
21
+ if (ok ) signalCondition(exp ) else stop(exp ),
25
22
continue_test = function (e ) NULL
26
23
)
27
24
@@ -31,7 +28,7 @@ expect <- function(ok, failure_message, info = NULL, srcref = NULL) {
31
28
# ' Construct an expectation object
32
29
# '
33
30
# ' For advanced use only. If you are creating your own expectation, you should
34
- # ' call ` expect()` instead. See `vignette("custom-expectation")` for more
31
+ # ' call [ expect()] instead. See `vignette("custom-expectation")` for more
35
32
# ' details.
36
33
# '
37
34
# ' @param type Expectation type. Must be one of "success", "failure", "error",
@@ -63,39 +60,27 @@ expectation <- function(type, message, srcref = NULL) {
63
60
# ' @param x object to test for class membership
64
61
is.expectation <- function (x ) inherits(x , " expectation" )
65
62
66
- expectation_type <- function (exp ) {
67
- stopifnot(is.expectation(exp ))
68
- gsub(" ^expectation_" , " " , class(exp )[[1 ]])
69
- }
70
-
71
- expectation_success <- function (exp ) {
72
- expectation_type(exp ) == " success"
73
- }
74
-
75
- expectation_failure <- function (exp ) {
76
- expectation_type(exp ) == " failure"
77
- }
78
-
79
- expectation_error <- function (exp ) {
80
- expectation_type(exp ) == " error"
63
+ # ' @export
64
+ print.expectation <- function (x , ... ) {
65
+ cat(format(x ), " \n " )
81
66
}
82
67
83
- expectation_skip <- function (exp ) {
84
- expectation_type(exp ) == " skip"
68
+ # ' @export
69
+ format.expectation_success <- function (x , ... ) {
70
+ " As expected"
85
71
}
86
72
87
- expectation_warning <- function (exp ) {
88
- expectation_type(exp ) == " warning"
73
+ # ' @export
74
+ format.expectation_error <- function (x , ... ) {
75
+ paste(c(x $ message , create_traceback(x $ call )), collapse = " \n " )
89
76
}
90
77
91
- expectation_broken <- function (exp ) {
92
- expectation_failure(exp ) || expectation_error(exp )
93
- }
94
- expectation_ok <- function (exp ) {
95
- expectation_type(exp ) %in% c(" success" , " warning" )
78
+ # ' @export
79
+ format.expectation <- function (x , ... ) {
80
+ x $ message
96
81
}
97
82
98
-
83
+ # as.expectation ----------------------------------------------------------
99
84
100
85
as.expectation <- function (x , ... ) UseMethod(" as.expectation" , x )
101
86
@@ -109,9 +94,7 @@ as.expectation.default <- function(x, ..., srcref = NULL) {
109
94
110
95
# ' @export
111
96
as.expectation.expectation <- function (x , ... , srcref = NULL ) {
112
- if (is.null(x $ srcref )) {
113
- x $ srcref <- srcref
114
- }
97
+ x $ srcref <- x $ srcref %|| % srcref
115
98
x
116
99
}
117
100
@@ -120,51 +103,36 @@ as.expectation.error <- function(x, ..., srcref = NULL) {
120
103
error <- x $ message
121
104
122
105
msg <- gsub(" Error.*?: " , " " , as.character(error ))
123
-
124
- # Need to remove trailing newline from error message to be consistent
125
- # with other messages
106
+ # Remove trailing newline to be consistent with other conditons
126
107
msg <- gsub(" \n $" , " " , msg )
127
108
128
109
expectation(" error" , msg , srcref )
129
110
}
130
111
131
112
# ' @export
132
113
as.expectation.warning <- function (x , ... , srcref = NULL ) {
133
- msg <- x $ message
134
-
135
- # msg <- gsub("Error.*?: ", "", as.character(error))
136
- # msg <- gsub("\n$", "", msg)
137
-
138
- expectation(" warning" , msg , srcref )
114
+ expectation(" warning" , x $ message , srcref )
139
115
}
140
116
141
117
# ' @export
142
118
as.expectation.skip <- function (x , ... , srcref = NULL ) {
143
- error <- x $ message
144
- msg <- gsub(" Error.*?: " , " " , as.character(error ))
145
-
146
- expectation(" skip" , msg , srcref )
147
- }
148
-
149
- # ' @export
150
- print.expectation <- function (x , ... ) {
151
- cat(format(x ), " \n " )
119
+ expectation(" skip" , x $ message , srcref )
152
120
}
153
121
154
- # ' @export
155
- format.expectation_success <- function (x , ... ) {
156
- " As expected"
157
- }
122
+ # expectation_type --------------------------------------------------------
158
123
159
- # ' @export
160
- format.expectation_error <- function ( x , ... ) {
161
- paste(c( x $ message , create_traceback( x $ call )), collapse = " \n " )
124
+ expectation_type <- function ( exp ) {
125
+ stopifnot(is.expectation( exp ))
126
+ gsub( " ^expectation_ " , " " , class( exp )[[ 1 ]] )
162
127
}
163
128
164
- # ' @export
165
- format.expectation <- function (x , ... ) {
166
- x $ message
167
- }
129
+ expectation_success <- function (exp ) expectation_type(exp ) == " success"
130
+ expectation_failure <- function (exp ) expectation_type(exp ) == " failure"
131
+ expectation_error <- function (exp ) expectation_type(exp ) == " error"
132
+ expectation_skip <- function (exp ) expectation_type(exp ) == " skip"
133
+ expectation_warning <- function (exp ) expectation_type(exp ) == " warning"
134
+ expectation_broken <- function (exp ) expectation_failure(exp ) || expectation_error(exp )
135
+ expectation_ok <- function (exp ) expectation_type(exp ) %in% c(" success" , " warning" )
168
136
169
137
single_letter_summary <- function (x ) {
170
138
switch (expectation_type(x ),
0 commit comments