Check if evaluating an expression produces a message, warning, or error.
These functions are designed to be used inside assert() to verify that code
signals the expected conditions. Optionally, you can match against the
condition's text to ensure the right message/warning/error was signaled.
Usage
has_message(expr, message = NULL, ...)
has_warning(expr, message = NULL, ...)
has_error(expr, message = NULL, ...)Arguments
- expr
An R expression to evaluate.
- message
An optional string to match against the condition text. Uses fixed (literal) matching by default. If provided, the function returns
TRUEonly when the condition is signaled and the message matches.- ...
Additional arguments passed to
grepl()for matching (e.g.,fixed = FALSEto use regex, orignore.case = TRUE). Note thatfixed = TRUEis the default.
Examples
has_message(message('hello'))
#> [1] TRUE
has_message(1 + 1)
#> [1] FALSE
has_message(message('hello world'), 'hello')
#> [1] TRUE
has_warning(1 + 1)
#> [1] FALSE
has_warning(1:2 + 1:3)
#> [1] TRUE
has_warning(1:2 + 1:3, 'longer object length')
#> [1] TRUE
has_error(2 - 3)
#> [1] FALSE
has_error(1 + 'a')
#> [1] TRUE
has_error(stop('err'), 'err')
#> [1] TRUE
has_error(stop('error occurred'), 'error')
#> [1] TRUE