Use expect_message() and expect_warning() to check if the messages or warnings match the given regular expression.

expect_message(object, regexp = NULL, ..., all = FALSE, info = NULL,
  label = NULL)

expect_warning(object, regexp = NULL, ..., all = FALSE, info = NULL,
  label = NULL)

Arguments

object

Object to test.

Supports limited unquoting to make it easier to generate readable failures within a function or for loop. See quasi_label for more details.

regexp

Regular expression to test against.

  • A character vector giving a regular expression that must match the message/warning

  • If NULL, the default, asserts that there should be a messsage/warning, but doesn't test for a specific value.

  • If NA, asserts that there shouldn't be any messages or warnings.

...

Arguments passed on to expect_match

all

Should all elements of actual value match regexp (TRUE), or does only one need to match (FALSE)

perl

logical. Should Perl-compatible regexps be used?

fixed

logical. If TRUE, pattern is a string to be matched as is. Overrides all conflicting arguments.

all

Do messages/warnings need to match the regexp (TRUE), or does only one need to match (FALSE)?

info

Extra information to be included in the message. This argument is soft-deprecated and should not be used in new code. Instead see alternatives in quasi_label.

label

Used to customise failure messages. For expert use only.

Value

The first argument, invisibly.

See also

Examples

# Messages ------------------------------------------------------------------ f <- function(x) { if (x < 0) { message("*x* is already negative") return(x) } -x } expect_message(f(-1)) expect_message(f(-1), "already negative") expect_message(f(1), NA) # To test message and output, store results to a variable expect_message(out <- f(-1), "already negative") expect_equal(out, -1) # You can use the arguments of grepl to control the matching expect_message(f(-1), "*x*", fixed = TRUE) expect_message(f(-1), "NEGATIVE", ignore.case = TRUE) # Warnings ------------------------------------------------------------------ f <- function(x) { if (x < 0) { warning("*x* is already negative") return(x) } -x } expect_warning(f(-1)) expect_warning(f(-1), "already negative") expect_warning(f(1), NA) # To test message and output, store results to a variable expect_warning(out <- f(-1), "already negative") expect_equal(out, -1) # You can use the arguments of grepl to control the matching expect_warning(f(-1), "*x*", fixed = TRUE) expect_warning(f(-1), "NEGATIVE", ignore.case = TRUE)