Skip to contents

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 TRUE only when the condition is signaled and the message matches.

...

Additional arguments passed to grepl() for matching (e.g., fixed = FALSE to use regex, or ignore.case = TRUE). Note that fixed = TRUE is the default.

Value

TRUE if the condition was signaled (and the message matched, if provided), FALSE otherwise.

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