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

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

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

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.

Examples

ok(ut_cmp_error({
  stop("Hammer time")
}, "hammer", ignore.case = TRUE), "Returned a hammer-based error")
#> ok - 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")
#> not ok - Returned a MC Hammer based error
#> # Test resulted in error:
#> #  unused argument (expected_mesage = "Hammer")
#> # Traceback:
#> #  (none)