Test whether variables in a data frame conform to a given pattern.

expect_regex(vars, pattern, flt = TRUE, data = get_testdata())

expect_max_length(vars, len, flt = TRUE, data = get_testdata())

Arguments

vars

<tidy-select> A set of columns to test.

pattern

A str_detect() pattern to match.

flt

<data-masking> A filter specifying a subset of the data frame to test.

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.

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
#> Error : `sales` has 1 records failing pattern check on variable `item_code`.
#> Variable set: `item_code`
#> Filter: None
#> Arguments: `pattern = "[a-z]_[0-9]"`
try(expect_max_length(item_code,  3, data = sales)) # Code width <= 3
#> Error : `sales` has 1 records failing length check on variable `item_code`.
#> Variable set: `item_code`
#> Filter: None
#> Arguments: `len = 3`