A helper to catch expected warnings and ensure they match what is expected

ut_cmp_warning(code, expected_regexp = NULL, expected_count = 1L,
                 ignore.case = FALSE, perl = FALSE, fixed = FALSE)

Arguments

code

Code expression to test, should generate one or more warnings

expected_regexp

Regular expression(s) that the warning(s) should match. If NULL the warning message will not be checked. Multiple regexes can be given as a vector; see details.

expected_count

The number of warnings that should be issued. If NULL then one or more warnings should be issued. Setting expected_count to zero is not allowed.

ignore.case

Passed to grep

perl

Passed to grep

fixed

Passed to grep

Value

Returns TRUE if code generates warnings that match expected_regexp and expected_count. If code generates warnings that do not match expected_regexp and expected_count returns a vector of strings that detail the difference between the expected and actual warnings. Returns "No warnings issued" if code does not generate any warnings.

Details

If expected_regexp is a single regular expression, then all warnings must match the regular expression. If expected_regexp is a vector then:

  • all warnings must match at least one regular expression

  • all regular expressions must match at least one warning

Examples

ok(ut_cmp_warning({
  warning("Wooooo!")
}, "^woo", ignore.case = TRUE), "Issued a haunting warning")
#> ok - Issued a haunting warning

ok(ut_cmp_warning({
  warning("Woooo!")
  warning("Woooooo!")
}, "^Woo", expected_count = 2L), "Issued two haunting warnings")
#> ok - Issued two haunting warnings

ok(ut_cmp_warning({
  warning("Woooo!")
  warning("Boooo!")
}, c("^Woo", "^Boo"), expected_count = 2L), "Issued a haunting and a diapproving warning")
#> ok - Issued a haunting and a diapproving warning