Rev 76421 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
\name{assertCondition}\alias{assertCondition}\alias{assertWarning}\alias{assertError}\title{Asserting Error Conditions}\description{When testing code, it is not sufficient to check that results are correct,but also that errors or warnings are signalled in appropriatesituations. The functions described here provide a convenientfacility for doing so. The three functions check that evaluating thesupplied expression produces an error, a warning or one of aspecified list of conditions, respectively. If the assertion fails,an error is signalled.}\usage{assertError(expr, classes = "error", verbose = FALSE)assertWarning(expr, classes = "warning", verbose = FALSE)assertCondition(expr, ..., .exprString = , verbose = FALSE)}\arguments{\item{expr}{an unevaluated \R expression which will be evaluated via\code{\link{tryCatch}(expr, ..)}.}\item{classes, \dots}{\code{\link{character}} strings corresponding to theclasses of the conditions that would satisfy the assertion; e.g., \code{"error"} or\code{"warning"}. If none are specified, any condition willsatisfy the assertion. See the details section. }\item{.exprString}{The string to be printed corresponding to\code{expr}. By default, the actual \code{expr} will bedeparsed. Will be omitted if the function is supplied with theactual expression to be tested. If \code{assertCondition()} iscalled from another function, with the actual expression passed asan argument to that function, supply the deparsed version.}\item{verbose}{If \code{TRUE}, a message is printed when thecondition is satisfied.}}\details{\code{assertCondition()} uses the general condition mechanism tocheck all the conditions generated in evaluating \code{expr}. Theoccurrence of any of the supplied condition classes among these satisfies theassertion regardless of what other conditions may be signalled.\code{assertError()} is a convenience function for asserting errors;it calls \code{assertCondition()}.\code{assertWarning()} asserts that a warning will be signalled, but\emph{not} an error, whereas \code{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 objectssignalled is returned, invisibly. See \code{\link{conditionMessage}} for theinterpretation of these objects. Note that \emph{all} conditionssignalled during the evaluation are returned, whether or not theywere among the requirements.}\author{John Chambers and Martin Maechler}\seealso{\code{\link{stop}}, \code{\link{warning}};\code{\link{signalCondition}}, \code{\link{tryCatch}}.}\examples{assertError(sqrt("abc"))assertWarning(matrix(1:8, 4,3))assertCondition( ""-1 ) # ok, any condition would satisfy thistry( assertCondition(sqrt(2), "warning") )## .. Failed to get warning in evaluating sqrt(2)assertCondition(sqrt("abc"), "error") # oktry( assertCondition(sqrt("abc"), "warning") )# -> error: had no warningassertCondition(sqrt("abc"), "error")## identical to assertError() call aboveassertCondition(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") # OKassertCondition(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 followtry(assertWarning(ff()))assertCondition(ff(), "error") # okassertCondition(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")}\keyword{programming}\keyword{error}