assertCondition | R Documentation |
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.
assertError(expr, classes = "error", verbose = FALSE) assertWarning(expr, classes = "warning", verbose = FALSE) assertCondition(expr, ..., .exprString = , verbose = FALSE)
expr |
an unevaluated R expression which will be evaluated via
|
classes, ... |
|
.exprString |
The string to be printed corresponding to
|
verbose |
If |
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.
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.
John Chambers and Martin Maechler
stop
, warning
;
signalCondition
, tryCatch
.
assertError(sqrt("abc")) assertWarning(matrix(1:8, 4,3)) assertCondition( ""-1 ) # ok, any condition would satisfy this try( assertCondition(sqrt(2), "warning") ) ## .. Failed to get warning in evaluating sqrt(2) assertCondition(sqrt("abc"), "error") # ok try( assertCondition(sqrt("abc"), "warning") )# -> error: had no warning assertCondition(sqrt("abc"), "error") ## identical to assertError() call above assertCondition(matrix(1:5, 2,3), "warning") try( assertCondition(matrix(1:8, 4,3), "error") ) ## .. Failed to get expected error .... ## either warning or worse: assertCondition(matrix(1:8, 4,3), "error","warning") # OK assertCondition(matrix(1:8, 4, 3), "warning") # OK ## when both are signalled: ff <- function() { warning("my warning"); stop("my error") } assertCondition(ff(), "warning") ## but assertWarning does not allow an error to follow try(assertWarning(ff())) assertCondition(ff(), "error") # ok assertCondition(ff(), "error", "warning") # ok (quietly, catching warning) ## assert that assertC..() does not assert [and use *one* argument only] assertCondition( assertCondition(sqrt( 2 ), "warning") ) assertCondition( assertCondition(sqrt("abc"), "warning"), "error") assertCondition( assertCondition(matrix(1:8, 4,3), "error"), "error")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.