How to compose expectations in testthat?

Viewed 79

A naive approach to writing custom expectations from existing expectations is:

expect_between <- function (x, lo, hi) {
    expect_lte(lo, x)
    expect_lte(x, hi)
}

but this doesn't work with expect_failure, since expect_failure captures the first expectation.

expect_between(0.5, 0, 1)                  # PASSES
expect_between(-99, 0, 1)                  # FAILS
expect_between(99, 0, 1)                   # FAILS
expect_failure(expect_between(-99, 0, 1))  # PASSES
expect_failure(expect_between(99, 0, 1))   # FAILS <--- the problem

What is the proper way to compose expectations when writing user-defined expectations, so that they still play well with expect_failure?

0 Answers
Related