You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: vignettes/custom-expectation.Rmd
+9-2Lines changed: 9 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -95,7 +95,9 @@ To do this we'll add an extra check that the input is either an atomic vector or
95
95
expect_vector_length <- function(object, n) {
96
96
act <- quasi_label(rlang::enquo(object))
97
97
98
-
if (!is.atomic(act$val) && !is.list(act$val)) {
98
+
# It's non-trivial to check if an object is a vector in base R so we
99
+
# use an rlang helper
100
+
if (!rlang::is_vector(act$val)) {
99
101
msg <- sprintf("%s is a %s, not a vector", act$lab, typeof(act$val))
100
102
return(fail(msg))
101
103
}
@@ -160,6 +162,7 @@ x3 <- factor()
160
162
expect_s3_class(x1, "integer")
161
163
expect_s3_class(x2, "integer")
162
164
expect_s3_class(x3, "integer")
165
+
expect_s3_class(x3, "factor")
163
166
```
164
167
165
168
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.
Note that the helper probably shouldn't be user facing, and we give it a `_` suffix to make that clear. It's also typically easiest for a helper to take the labelled value produced by `quasi_label()` rather than having to do that repeatedly.
198
+
A few recommendations:
199
+
200
+
* The helper shouldn't be user facing, so we give it a `_` suffix to make that clear.
201
+
* It's typically easiest for a helper to take the labelled value produced by `quasi_label()`.
202
+
* Your helper should usually call both `fail()` and `pass()` and be returned from the wrapping expectation.
0 commit comments