The R Project SVN R

Rev

Rev 76421 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
63062 maechler 1
\name{assertCondition}
2
\alias{assertCondition}
63197 jmc 3
\alias{assertWarning}
4
\alias{assertError}
63062 maechler 5
\title{Asserting Error Conditions}
6
\description{
7
  When testing code, it is not sufficient to check that results are correct,
8
  but also that errors or warnings are signalled in appropriate
63197 jmc 9
  situations.  The functions described here provide a convenient
10
  facility for doing so.  The three functions check that evaluating the
11
  supplied expression produces an error, a warning or one of a
12
  specified list of conditions, respectively.  If the assertion fails,
13
  an error is signalled.
63062 maechler 14
}
15
\usage{
76421 maechler 16
assertError(expr, classes = "error", verbose = FALSE)
17
assertWarning(expr, classes = "warning", verbose = FALSE)
63205 jmc 18
assertCondition(expr, ..., .exprString = , verbose = FALSE)
63062 maechler 19
}
20
\arguments{
21
  \item{expr}{an unevaluated \R expression which will be evaluated via
22
    \code{\link{tryCatch}(expr, ..)}.}
76435 maechler 23
  \item{classes, \dots}{\code{\link{character}} strings corresponding to the
63451 maechler 24
    classes of the conditions that would satisfy the assertion; e.g., \code{"error"} or
76421 maechler 25
    \code{"warning"}.  If none are specified, any condition will
63197 jmc 26
    satisfy the assertion.  See the details section. }
27
  \item{.exprString}{The string to be printed corresponding to
63451 maechler 28
    \code{expr}. By default, the actual \code{expr} will be
29
    deparsed.  Will be omitted if the function is supplied with the
30
    actual expression to be tested.  If \code{assertCondition()} is
31
    called from another function, with the actual expression passed as
32
    an argument to that function, supply the deparsed version.}
63205 jmc 33
  \item{verbose}{If \code{TRUE}, a message is printed when the
63451 maechler 34
    condition is satisfied.}
63062 maechler 35
}
63197 jmc 36
\details{
37
  \code{assertCondition()} uses the general condition mechanism to
38
  check all the conditions generated in evaluating \code{expr}.  The
39
  occurrence of any of the supplied condition classes among these satisfies the
40
  assertion regardless of what other conditions may be signalled.
41
 
42
  \code{assertError()} is a convenience function for asserting errors;
43
  it calls \code{assertCondition()}.
44
 
45
  \code{assertWarning()} asserts that a warning will be signalled, but
46
  \emph{not} an error, whereas \code{assertCondition(expr, "warning")}
47
  will be satisfied even if an error follows the warning.  See the examples.
48
 }
63062 maechler 49
\value{
63197 jmc 50
  If the assertion is satisfied, a list of all the condition objects
51
  signalled is returned, invisibly. See \code{\link{conditionMessage}} for the
52
  interpretation of these objects.  Note that \emph{all} conditions
53
  signalled during the evaluation are returned, whether or not they
54
  were among the requirements.
63062 maechler 55
}
63197 jmc 56
\author{John Chambers and Martin Maechler}
63062 maechler 57
\seealso{
58
  \code{\link{stop}}, \code{\link{warning}};
63197 jmc 59
  \code{\link{signalCondition}}, \code{\link{tryCatch}}.
63062 maechler 60
}
61
\examples{
63197 jmc 62
  assertError(sqrt("abc"))
63
  assertWarning(matrix(1:8, 4,3))
63062 maechler 64
 
63197 jmc 65
  assertCondition( ""-1 ) # ok, any condition would satisfy this
66
 
63062 maechler 67
try( assertCondition(sqrt(2), "warning") )
63197 jmc 68
## .. Failed to get warning in evaluating sqrt(2)
63062 maechler 69
     assertCondition(sqrt("abc"), "error")   # ok
70
try( assertCondition(sqrt("abc"), "warning") )# -> error: had no warning
63451 maechler 71
     assertCondition(sqrt("abc"), "error")
63197 jmc 72
  ## identical to assertError() call above
63062 maechler 73
 
63142 maechler 74
assertCondition(matrix(1:5, 2,3), "warning")
63062 maechler 75
try( assertCondition(matrix(1:8, 4,3), "error") )
63142 maechler 76
## .. Failed to get expected error ....
63062 maechler 77
 
78
## either warning or worse:
63197 jmc 79
assertCondition(matrix(1:8, 4,3), "error","warning") # OK
63451 maechler 80
assertCondition(matrix(1:8, 4, 3), "warning") # OK
63142 maechler 81
 
82
## when both are signalled:
83
ff <- function() { warning("my warning"); stop("my error") }
63144 maechler 84
    assertCondition(ff(), "warning")
63197 jmc 85
## but assertWarning does not allow an error to follow
86
try(assertWarning(ff()))
63173 maechler 87
    assertCondition(ff(), "error")          # ok
63143 maechler 88
assertCondition(ff(), "error", "warning") # ok (quietly, catching warning)
63173 maechler 89
 
90
## assert that assertC..() does not assert [and use *one* argument only]
91
assertCondition( assertCondition(sqrt( 2   ), "warning") )
92
assertCondition( assertCondition(sqrt("abc"), "warning"), "error")
93
assertCondition( assertCondition(matrix(1:8, 4,3), "error"),
63197 jmc 94
                "error")
63062 maechler 95
}
96
\keyword{programming}
97
\keyword{error}