Skip to content

Commit dbaf1cf

Browse files
committed
Add an example of argument checking
1 parent fde643d commit dbaf1cf

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

vignettes/custom-expectation.Rmd

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ Or imagine if you're checking to see if an object inherits from an S3 class. In
122122

123123
```{r}
124124
expect_s3_class <- function(object, class) {
125+
if (!rlang::is_string(class)) {
126+
rlang::abort("`class` must be a string.")
127+
}
128+
125129
act <- quasi_label(rlang::enquo(object), arg = "object")
126130
127131
if (!is.object(act$val)) {
@@ -158,6 +162,13 @@ expect_s3_class(x2, "integer")
158162
expect_s3_class(x3, "integer")
159163
```
160164

165+
Note that I also check that the `class` argument must be a string. This is an error, not a failure, because it suggests you're using the function incorrectly.
166+
167+
```{r}
168+
#| error: true
169+
expect_s3_class(x1, 1)
170+
```
171+
161172
## Repeated code
162173

163174
As you write more expectations, you might discover repeated code that you want to extract out in to a helper. Unfortunately creating helper functions is not straightforward in testthat because every `fail()` captures the calling environment in order to give maximally useful tracebacks. Because getting this right is not critical (you'll just get a slightly suboptimal traceback in the case of failure), we don't recommend bothering. However, we document it here because it's important to get it right in testthat itself.

0 commit comments

Comments
 (0)