pattern-expectations {testdat} | R Documentation |
Expectations: patterns
Description
Test whether variables in a data frame conform to a given pattern.
Usage
expect_regex(vars, pattern, flt = TRUE, data = get_testdata())
expect_max_length(vars, len, flt = TRUE, data = get_testdata())
Arguments
vars |
< |
pattern |
A str_detect() pattern to match. |
flt |
< |
data |
A data frame to test. The global test data is used by default. |
len |
Maximum string length. |
Value
expect_*()
functions are mainly called for their side effects. The
expectation signals its result (e.g. "success", "failure"), which is logged
by the current test reporter. In a non-testing
context the expectation will raise an error with class
expectation_failure
if it fails.
See Also
Other data expectations:
conditional-expectations
,
datacomp-expectations
,
date-expectations
,
exclusivity-expectations
,
expect_depends()
,
generic-expectations
,
label-expectations
,
proportion-expectations
,
text-expectations
,
uniqueness-expectations
,
value-expectations
Examples
sales <- data.frame(
sale_id = 1:5,
item_code = c("a_1", "b_2", "c_2", NA, "NULL")
)
try(expect_regex(item_code, "[a-z]_[0-9]", data = sales)) # Codes match regex
try(expect_max_length(item_code, 3, data = sales)) # Code width <= 3