testthat icon indicating copy to clipboard operation
testthat copied to clipboard

expect_not_contains

Open aronatkins opened this issue 10 months ago • 0 comments

It would be helpful to have a collection of negated checkers, or utilities to help negate existing checks. For example, we have expect_contains, but not a straightforward way to check when values should no be in the haystack.

expect_not_contains(c("a","b"), c("a","c"))
Error: c("a", "b") (`actual`) contains some of the values in c("a", "c") (`unexpected`).
* Missing from `actual`: "c"
* Present in `actual`:   "a", "b"

I could write this as the following, but would not receive any assistance in reporting the unexpected values:

expect_false(any(c("a","c") %in% c("a","b")))

Getting feedback is cumbersome:

haystack <- c("a","b")
values <- c("a","c")
testthat::expect_false(any(values %in% haystack), paste("unexpected: ", paste0(values[values %in% haystack], collapse = ", ")))

aronatkins avatar Aug 31 '23 20:08 aronatkins