Examples for 'tools::assertCondition'


Asserting Error Conditions

Aliases: assertCondition assertWarning assertError

Keywords: programming error

### ** Examples

  assertError(sqrt("abc"))
  assertWarning(matrix(1:8, 4,3))

  assertCondition( ""-1 ) # ok, any condition would satisfy this

try( assertCondition(sqrt(2), "warning") )
Error in assertCondition(sqrt(2), "warning") : 
  Failed to get warning in evaluating sqrt(2)
## .. Failed to get warning in evaluating sqrt(2)
     assertCondition(sqrt("abc"), "error")   # ok
try( assertCondition(sqrt("abc"), "warning") )# -> error: had no warning
Error in assertCondition(sqrt("abc"), "warning") : 
  Got simpleError in evaluating sqrt("abc"); wanted warning
     assertCondition(sqrt("abc"), "error")
  ## identical to assertError() call above

assertCondition(matrix(1:5, 2,3), "warning")
try( assertCondition(matrix(1:8, 4,3), "error") )
Error in assertCondition(matrix(1:8, 4, 3), "error") : 
  Got simpleWarning in evaluating matrix(1:8, 4, 3); wanted error
## .. Failed to get expected error ....

## either warning or worse:
assertCondition(matrix(1:8, 4,3), "error","warning") # OK
assertCondition(matrix(1:8, 4, 3), "warning") # OK

## when both are signalled:
ff <- function() { warning("my warning"); stop("my error") }
    assertCondition(ff(), "warning")
## but assertWarning does not allow an error to follow
try(assertWarning(ff()))
Error in assertWarning(ff()) : 
  Got warning in evaluating ff(), but also an error
    assertCondition(ff(), "error")          # ok
assertCondition(ff(), "error", "warning") # ok (quietly, catching warning)

## assert that assertC..() does not assert [and use *one* argument only]
assertCondition( assertCondition(sqrt( 2   ), "warning") )
assertCondition( assertCondition(sqrt("abc"), "warning"), "error")
assertCondition( assertCondition(matrix(1:8, 4,3), "error"),
                "error")

[Package tools version 4.2.3 Index]