I am writing some unit tests for an R package using testthat. I would like to compare two objects where not all the details need to match, but they must maintain equivalence with respect to a set of functions of interest.
For a simple example, I want to use something like
library(testthat)
x <- 1:4
y <- matrix(4:1, nrow=2)
test_that("objects behave similarly", {
expect_equal_applied(x, y, .fn=list(sum, prod))
## which would be shorthand for:
## expect_equal(sum(x), sum(y))
## expect_equal(prod(x), prod(y))
})
In practice, x and y might be S3 objects, not simply base data structures.
Obviously, this is simple to implement, but I'd prefer something idiomatic if already existing. So, the question is, does testthat implement an expect function like this?
Searching through the API, nothing struck me as fitting this description, but it seems like a natural pattern. Or maybe there is a reason why such a pattern is objectionable that I'm overlooking.