Learn R Programming

unittest (version 1.7-0)

ut_cmp_error: Test for and compare errors generated by code

Description

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

Usage

ut_cmp_error(code, expected_regexp = NULL, expected_class = NULL,
               ignore.case = FALSE, perl = FALSE, fixed = FALSE)

Value

Returns TRUE if code generates an error and, if they are specified (not NULL), the error matches expected_regexp and/or expected_class. If an error is thrown by code and expected_regexp and/or expected_class

are given but do not match the error, returns a string with the expected and actual error message, and the expected and actual error class generated. Returns "No error returned" if code does not generate an error.

Arguments

code

Code expression to test, should generate an error

expected_regexp

Regular expression the error should match. If NULL the error message will not be checked.

expected_class

Error class(es) that the error should match. If NULL the error class will not be checked. If expected_class is a vector then the error must inherit from all of the expected_classes.

ignore.case

Passed to grepl

perl

Passed to grepl

fixed

Passed to grepl

Examples

Run this code
ok(ut_cmp_error({
  stop("Hammer time")
}, "hammer", ignore.case = TRUE), "Returned a hammer-based error")

ok(ut_cmp_error({
  stop(errorCondition("Hammer time", class = "MC"))
}, expected_mesage = "Hammer", expected_class = "MC"), "Returned a MC Hammer based error")

# \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