The R Project SVN R

Rev

Rev 63197 | Rev 76421 | Go to most recent revision | 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{
63205 jmc 16
assertError(expr, verbose = FALSE)
17
assertWarning(expr, verbose = FALSE)
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, ..)}.}
63197 jmc 23
  \item{\dots}{\code{\link{character}} strings corresponding to the
24
      classes of the conditions that would satisfy the assertion; e.g., \code{"error"} or
25
    \code{"warning"}. If none are specified, any condition will
26
    satisfy the assertion.  See the details section. }
27
  \item{.exprString}{The string to be printed corresponding to
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 
32
        function, with the actual expression passed as an argument to
33
        that function, supply the deparsed version.}
63205 jmc 34
  \item{verbose}{If \code{TRUE}, a message is printed when the
35
        condition is satisfied.}
63062 maechler 36
}
63197 jmc 37
\details{
38
  \code{assertCondition()} uses the general condition mechanism to
39
  check all the conditions generated in evaluating \code{expr}.  The
40
  occurrence of any of the supplied condition classes among these satisfies the
41
  assertion regardless of what other conditions may be signalled.
42
 
43
  \code{assertError()} is a convenience function for asserting errors;
44
  it calls \code{assertCondition()}.
45
 
46
  \code{assertWarning()} asserts that a warning will be signalled, but
47
  \emph{not} an error, whereas \code{assertCondition(expr, "warning")}
48
  will be satisfied even if an error follows the warning.  See the examples.
49
 }
63062 maechler 50
\value{
63197 jmc 51
  If the assertion is satisfied, a list of all the condition objects
52
  signalled is returned, invisibly. See \code{\link{conditionMessage}} for the
53
  interpretation of these objects.  Note that \emph{all} conditions
54
  signalled during the evaluation are returned, whether or not they
55
  were among the requirements.
63062 maechler 56
}
63197 jmc 57
\author{John Chambers and Martin Maechler}
63062 maechler 58
\seealso{
59
  \code{\link{stop}}, \code{\link{warning}};
63197 jmc 60
  \code{\link{signalCondition}}, \code{\link{tryCatch}}.
63062 maechler 61
}
62
\examples{
63197 jmc 63
  assertError(sqrt("abc"))
64
  assertWarning(matrix(1:8, 4,3))
63062 maechler 65
 
63197 jmc 66
  assertCondition( ""-1 ) # ok, any condition would satisfy this
67
 
63062 maechler 68
try( assertCondition(sqrt(2), "warning") )
63197 jmc 69
## .. Failed to get warning in evaluating sqrt(2)
63062 maechler 70
     assertCondition(sqrt("abc"), "error")   # ok
71
try( assertCondition(sqrt("abc"), "warning") )# -> error: had no warning
63197 jmc 72
     assertCondition(sqrt("abc"), "error") 
73
  ## identical to assertError() call above
63062 maechler 74
 
63142 maechler 75
assertCondition(matrix(1:5, 2,3), "warning")
63062 maechler 76
try( assertCondition(matrix(1:8, 4,3), "error") )
63142 maechler 77
## .. Failed to get expected error ....
63062 maechler 78
 
79
## either warning or worse:
63197 jmc 80
assertCondition(matrix(1:8, 4,3), "error","warning") # OK
81
assertCondition(matrix(1:8, 4, 3), "warning") # OK 
63142 maechler 82
 
83
## when both are signalled:
84
ff <- function() { warning("my warning"); stop("my error") }
63144 maechler 85
    assertCondition(ff(), "warning")
63197 jmc 86
## but assertWarning does not allow an error to follow
87
try(assertWarning(ff()))
63173 maechler 88
    assertCondition(ff(), "error")          # ok
63143 maechler 89
assertCondition(ff(), "error", "warning") # ok (quietly, catching warning)
63173 maechler 90
 
91
## assert that assertC..() does not assert [and use *one* argument only]
92
assertCondition( assertCondition(sqrt( 2   ), "warning") )
93
assertCondition( assertCondition(sqrt("abc"), "warning"), "error")
94
assertCondition( assertCondition(matrix(1:8, 4,3), "error"),
63197 jmc 95
                "error")
63062 maechler 96
}
97
\keyword{programming}
98
\keyword{error}