assertCondition

Asserting Error Conditions

When testing code, it is not sufficient to check that results are correct,
but also that errors or warnings are signalled in appropriate
situations. The functions described here provide a convenient
facility for doing so. The three functions check that evaluating the
supplied expression produces an error, a warning or one of a
specified list of conditions, respectively. If the assertion fails,
an error is signalled.

Usage

Arguments

expr

an unevaluated R expression which will be evaluated via
tryCatch(expr, ..).

…

character strings corresponding to the
classes of the conditions that would satisfy the assertion; e.g., "error" or
"warning". If none are specified, any condition will
satisfy the assertion. See the details section.

.exprString

The string to be printed corresponding to
expr. By default, the actual expr will be
deparsed. Will be omitted if the function is supplied with the
actual expression to be tested. If assertCondition() is
called from another function, with the actual expression passed as
an argument to that function, supply the deparsed version.

verbose

If TRUE, a message is printed when the
condition is satisfied.

Details

assertCondition() uses the general condition mechanism to
check all the conditions generated in evaluating expr. The
occurrence of any of the supplied condition classes among these satisfies the
assertion regardless of what other conditions may be signalled.

assertError() is a convenience function for asserting errors;
it calls assertCondition().

assertWarning() asserts that a warning will be signalled, but
not an error, whereas assertCondition(expr, "warning")
will be satisfied even if an error follows the warning. See the examples.

Value

If the assertion is satisfied, a list of all the condition objects
signalled is returned, invisibly. See conditionMessage for the
interpretation of these objects. Note that all conditions
signalled during the evaluation are returned, whether or not they
were among the requirements.