Learn R Programming

unittest (version 1.7-0)

ut_cmp_warning: Test for and compare warnings generated by code

Description

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

Usage

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

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.

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

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

Run this code
ok(ut_cmp_warning({
  warning("Wooooo!")
}, "^woo", ignore.case = TRUE), "Issued a haunting warning")

ok(ut_cmp_warning({
  warning("Woooo!")
  warning("Woooooo!")
}, "^Woo", expected_count = 2L), "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")

# \dontshow{
  # Clear unittest result log, so our unittest failues don't fail example-building
  unittest:::clear_outcomes()
# }

Run the code above in your browser using DataLab