checkWarning checks if a warning is generated by an expression, and optionally follows an expected regular expression pattern.

checkWarning(expr, expected = TRUE, msg = NULL)

Arguments

expr

an R expression

expected

expected value as regular expression pattern. If a logical, then it specifies if a warning is expected or not.

For backward compatibility, a NULL value is equivalent to TRUE.

msg

informative message to add to the error in case of failure

Examples

# check warnings checkWarning({ warning('ah ah'); 3})
#> Using RUnit framework provider: RUnit
#> Warning: ah ah
#> [1] TRUE
checkWarning({ warning('ah oh ah'); 3}, 'oh')
#> Warning: ah oh ah
#> [1] TRUE
try( checkWarning(3) )
#> Error in checkWarning(3) : Warning not generated as expected #>
try( checkWarning({ warning('ah ah'); 3}, 'warn you') )
#> Warning: ah ah
#> Error in checkWarning({ : Warning does not match expected pattern: #> - Warning(s): 'ah ah' #> - Pattern: 'warn you' #>