Request: an assert_that function that runs in its own environment

Create issue
Issue #23 resolved
Former user created an issue

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)

  1. Richard Cotton 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
    
  2. Kendon Bell

    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?

  3. Richard Cotton repo owner

    @kendonB Excellent question. You can use assert_engine(). Try something like this

    library(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")
    
  4. Richard Cotton 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.

  5. Kendon Bell

    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.

  6. Log in to comment