0

I am trying to use assertthat::assert_that() on several conditions to be tested, which are collected in a `pairlist'.

The pairlist that I have a looks like this:

pairlist_1 <- as.pairlist(alist(is.null(list()), is.na(list())))

print(pairlist_1)

[[1]]

is.null(list())

[[2]]

is.na(list())

Now, I check the type of the pairlist and its elements:

print(typeof(pairlist_1))
[1] "pairlist"

print(typeof(pairlist_1[[1]]))
[1] "language"

print(typeof(pairlist_1[[2]]))
[1] "language"

I try to use assert_that and check it is working:

assertthat::assert_that(2 + 2 == 4)
[1] TRUE

assertthat::assert_that(is.null(list()))
Error: list() is not NULL

PROBLEM

However, when I try to use assert_that on the pairlist element directly, I get ...

assertthat::assert_that(pairlist_1[[1]])
Error: assert_that: assertion must return a logical value

... instead of Error: list() is not NULL as above, meaning that the assert_that() function cannot be executed.

I tried to evaluate the expressions in the pairlist and then assert the return value, but if I do assertthat::assert_that(eval(pairlist_1[[1]])) I get Error: eval(expr = pairlist_1[[1]]) is not TRUE. I want to get the same error that I get with assertthat::assert_that(is.null(list())), which is: Error: list() is not NULL .

How can I apply testthat::test_that() directly on my pairlist elements? Alternatively, how can I extract the pairlist elements and successfully apply testthat::test_that() on them?

Thanks.

dkarlez
  • 1
  • 1
  • I don't understand what conditions you want to test. Your attempts don't make any sense and I can't infer your intentions from them. – Roland Apr 26 '23 at 06:28
  • I want to test the conditions included in the pairlist. I want to be able to apply assert_thatI() on conditions that are elements of a pairlist. I would like to run assertthat::assert_that(pairlist_1[[1]]) and get an assertthat error message. Making sense now? – dkarlez Apr 26 '23 at 06:51
  • 1
    Does that mean you want to `eval`uate the expressions in the pairlist? And then assert the return value? – Roland Apr 26 '23 at 08:51
  • I tried that, but if I do `assertthat::assert_that(eval(pairlist_1[[1]]))` I get `Error: eval(expr = pairlist_1[[1]]) is not TRUE`. I want to get the same error that I get with `assertthat::assert_that(is.null(list()))`, which is: `Error: list() is not NULL` . – dkarlez Apr 26 '23 at 09:36
  • I have added this explanation in the text of the question above as it might be helpful. – dkarlez Apr 26 '23 at 09:46
  • Just a note: pairlists are deprecated https://cran.r-project.org/doc/manuals/r-release/R-lang.html#Pairlist-objects – jyr Apr 26 '23 at 09:56

1 Answers1

1

Just compute on the language:

eval(substitute(assertthat::assert_that(x), 
                list(x = pairlist_1[[1]])))
#Error: list() is not NULL
Roland
  • 127,288
  • 10
  • 191
  • 288