These functions allow for testing of multiple columns (vars) of a data frame (data), with an optional filter (flt), using an arbitrary function (func).

expect_allany(
  vars,
  func,
  flt = TRUE,
  data = get_testdata(),
  args = list(),
  allany = c(chk_filter_all, chk_filter_any),
  func_desc = NULL
)

expect_all(...)

expect_any(...)

Arguments

vars

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

func

A function to use for testing that takes a vector as the first argument and returns a logical vector of the same length showing whether an element passed or failed.

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.

args

A named list of arguments to pass to func.

allany

The function to combine the func results for each row.

func_desc

A human friendly description of func to use in the expectation failure message.

...

Arguments to pass to expect_allany().

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.

Details

  • expect_allany() tests the columns in vars to see whether func returns TRUE for each of them, and combines the results for each row using the function in allany. Both expect_all() and expect_any() are wrappers around expect_allany().

  • expect_all() tests the vars to see whether func returns TRUE for all of them (i.e. whether the conjunction of results of applying func to each of the vars is TRUE).

  • expect_any() tests the vars to see whether func returns TRUE for any of them (i.e. whether the disjunction of the results of applying func to each of the vars is TRUE).

Examples

# Check that every 4-cylinder car has an engine displacement of < 100 cubic
# inches *AND* < 100 horsepower
try(
expect_all(
  vars = c(disp, hp),
  func = chk_range,
  flt = (cyl == 4),
  args = list(min = 0, max = 100),
  data = mtcars
)
)
#> Error : `mtcars` has 7 records failing `chk_range` on variable `disp, hp`.
#> Filter: `(cyl == 4)`
#> Arguments: `min = 0, max = 100`

# Check that every 4-cylinder car has an engine displacement of < 100 cubic
# inches *OR* < 100 horsepower
try(
expect_any(
  vars = c(disp, hp),
  func = chk_range,
  flt = (cyl == 4),
  args = list(min = 0, max = 100),
  data = mtcars
)
)
#> Error : `mtcars` has 1 records failing `chk_range` on variable `disp, hp`.
#> Filter: `(cyl == 4)`
#> Arguments: `min = 0, max = 100`

# Check petal dimensions are positive
expect_allany(
  vars = where(is.numeric),
  func = chk_range,
  args = list(min = 0, max = Inf),
  allany = chk_filter_all,
  data = iris
)