check {svUnit}R Documentation

SciViews-R Unit assertions (check functions)

Description

These functions define the assertions in test functions. They are designed to check the result of some test calculation.

Usage

checkEquals(
  target,
  current,
  msg = "",
  tolerance = .Machine$double.eps^0.5,
  checkNames = TRUE,
  ...
)

checkEqualsNumeric(
  target,
  current,
  msg = "",
  tolerance = .Machine$double.eps^0.5,
  ...
)

checkIdentical(target, current, msg = "")

checkTrue(expr, msg = "")

checkException(expr, msg = "", silent = getOption("svUnit.silentException"))

DEACTIVATED(msg = "")

Arguments

target

a target object as reference for comparison.

current

An object created for comparison (not an S4 class object).

msg

An optional (short!) message to document a test. This message is stored in the log and printed in front of each test report.

tolerance

numeric >= 0. A numeric check does not fail if differences are smaller than 'tolerance'.

checkNames

Flag, if FALSE the names attributes are set to NULL for both current and target before performing the check.

...

Optional arguments passed to all.equal() or all.equal.numeric().

expr

Syntactically valid R expression which can be evaluated and must return a logical vector (TRUE|FALSE). A named expression is also allowed but the name is disregarded. In checkException(), expr is supposed to generate an error to pass the test.

silent

Flag passed on to try, which determines if the error message generated by the checked function is displayed at the R console. By default, it is FALSE.

Details

These check functions are equivalent to various methods of the class junit.framework.Assert of Java junit framework. They should be code-compatible with functions of same name in 'RUnit' 0.4.17, except for checkTrue() that is vectorized here, but accept only a scalar result in 'RUnit'. For scalar test, the behavior of the function is the same in both packages. See svTest() for examples of use of these functions in actual test cases attached to R objects. See also the note about S4 objects in the RUnit::checkTrue() online help of the 'RUnit' package.

Value

These function return TRUE if the test succeeds, FALSE if it fails, possibly with a 'result' attribute containing more information about the problem. This is very different from corresponding functions in 'RUnit' that stop with an error in case of test failure. Consequently, current functions do not require the complex evaluation framework designed in 'RUnit' for that reason.

Author(s)

Written by Ph. Grosjean, inspired from the general design of the 'RUnit' package by Thomas Konig, Klaus Junemann & Matthias Burger.

See Also

svTest(), Log(), guiTestReport(), RUnit::checkTrue

Examples

clearLog()     # Clear the svUnit log

# All these tests are correct
(checkEquals(c("A", "B", "C"), LETTERS[1:3]))
(checkEqualsNumeric(1:10, seq(1, 10)))
(checkIdentical(iris[1:50, ], iris[iris$Species == "setosa",]))
(checkTrue(1 < 2))
(checkException(log("a")))
Log()    # See what's recorded in the log

# ... but these ones fail
(checkEquals("A", LETTERS[1:3]))
(checkEqualsNumeric(2:11, seq(1, 10)))
(checkIdentical(iris[1:49, ], iris[iris$Species == "setosa",]))
(checkTrue(1 > 2))
(checkException(log(1)))
Log()    # See what's recorded in the log

# Create a test function and run it
foo <- function(x, y = 2)
  return(x * y)
test(foo) <- function() {
  #DEACTIVATED()
  checkEqualsNumeric(5, foo(2))
  checkEqualsNumeric(6, foo(2, 3))
  checkTrue(is.test(foo))
  checkTrue(is.test(test(foo)))
  checkIdentical(test(foo), attr(foo, "test"))
  checkException(foo("b"))
  checkException(foo(2, "a"))
}
(runTest(foo))

# Of course, everything is recorded in the log
Log()

clearLog()

[Package svUnit version 1.0.6 Index]