has_message {testit}R Documentation

Test whether an expression signals a condition

Description

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'))
has_message(1 + 1)
has_message(message('hello world'), 'hello')

has_warning(1 + 1)
has_warning(1:2 + 1:3)
has_warning(1:2 + 1:3, 'longer object length')

has_error(2 - 3)
has_error(1 + 'a')
has_error(stop('err'), 'err')
has_error(stop('error occurred'), 'error')

[Package testit version 1.0 Index]