Request: an assert_that function that runs in its own environment
This would work similarly to the test_that
function in package testthat
. With more complicated assertions where there are intermediate steps, I'd like to not pollute the function environment I'm in. It would work something like this:
assertive::assert_that("This complicated assertion holds", {
tmp <- "a new variable that the rest of the function can't see"
assertive::is_character(tmp)
})
Comments (9)
-
-
repo owner @kendonB
Isn't that what base R's
local()
function is for?local({ tmp <- "a new variable that the rest of the function can't see" assertive::is_character(tmp) }) ls() # no tmp
-
Did not know about
local
. That works. -
The other attractive behavior of the
testthat::test_that
function is that it allows you to give a custom message for a larger group of assertions.if
+is_*
+stop()
only really works well for a single assertion.Is this something in
assertive
that provides this sort of behavior? -
repo owner @kendonB Excellent question. You can use
assert_engine()
. Try something like thislibrary(assertive) letters %>% assert_engine(predicate = is_character, msg = "x is not a character vector") %>% assert_engine(predicate = is_scalar, msg = "x is not of length 1")
-
repo owner @kendonB If there are several assertions that you often group together, tell me what they are and perhaps they can be added as a single
assert_
function. -
Most of situations I run into that involve several assertions that don't already exist are single use. Usually, I'm wanting to test something very specific about a dataset.
-
repo owner @kendonB If you want more advice on how to make your own custom assertions, it's discussed in Chapter 7 of Testing R Code.
-
repo owner - changed status to resolved
- Log in to comment
Sorry for the anon post - the above is me