File: assert.Rd

package info (click to toggle)
r-cran-testit 0.15-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 140 kB
  • sloc: sh: 8; makefile: 5
file content (76 lines) | stat: -rw-r--r-- 3,118 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/testit.R
\name{assert}
\alias{assert}
\alias{\%==\%}
\title{Assertions with an optional message}
\usage{
assert(fact, ...)

x \%==\% y
}
\arguments{
\item{fact}{A message for the assertions when any of them fails; treated the
same way as expressions in \code{...} if it is not a character string,
which means you are not required to provide a message to this function.}

\item{...}{An R expression; see Details.}

\item{x, y}{two R objects to be compared}
}
\value{
For \code{assert()}, invisible \code{NULL} if all expressions
  returned \code{TRUE}, otherwise an error is signaled and the user-provided
  message is emitted. For \code{\%==\%}, \code{TRUE} or \code{FALSE}.
}
\description{
The function \code{assert()} was inspired by \code{\link{stopifnot}()}. It
emits a message in case of errors, which can be a helpful hint for diagnosing
the errors (\code{stopifnot()} only prints the possibly truncated source code
of the expressions).

The infix operator \code{\%==\%} is simply an alias of the
  \code{\link{identical}()} function to make it slightly easier and intuitive
  to write test conditions. \code{x \%==\% y} is the same as
  \code{identical(x, y)}. When it is used inside \code{assert()}, a message
  will be printed if the returned value is not \code{TRUE}, to show the
  values of the LHS (\code{x}) and RHS (\code{y}) via \code{\link{str}()},
  which can be helpful for you to check why the assertion failed.
}
\details{
For the \code{...} argument, it should be a single R expression wrapped in
\code{{}}. This expression may contain multiple sub-expressions. A
sub-expression is treated as a test condition if it is wrapped in \code{()}
(meaning its value will be checked to see if it is a logical vector
containing any \code{FALSE} values) , otherwise it is evaluated in the normal
way and its value will not be checked. If the value of the last
sub-expression is logical, it will also be treated as a test condition.
}
\note{
The internal implementation of \code{assert()} is different with the
  \code{stopifnot()} function in R \pkg{base}: (1) the custom message
  \code{fact} is emitted if an error occurs; (2) \code{assert()} requires the
  logical values to be non-empty (\code{logical(0)} will trigger an error);
  (3) if \code{...} contains a compound expression in \code{{}} that returns
  \code{FALSE} (e.g., \code{if (TRUE) {1+1; FALSE}}), the first and the last
  but one line of the source code from \code{\link{deparse}()} are printed in
  the error message, otherwise the first line is printed; (4) the arguments
  in \code{...} are evaluated sequentially, and \code{assert()} will signal
  an error upon the first failed assertion, and will ignore the rest of
  assertions.
}
\examples{
library(testit)
assert("T is bad for TRUE, and so is F for FALSE", {
    T = FALSE
    F = TRUE
    (T != TRUE)  # note the parentheses
    (F != FALSE)
})

assert("A Poisson random number is non-negative", {
    x = rpois(1, 10)
    (x >= 0)
    (x > -1)  # () is optional because it's the last expression
})
}