Title: | Easy Pre and Post Assertions |
---|---|
Description: | An extension to stopifnot() that makes it easy to declare the pre and post conditions that you code should satisfy, while also producing friendly error messages so that your users know what's gone wrong. |
Authors: | Hadley Wickham [aut, cre] |
Maintainer: | Hadley Wickham <[email protected]> |
License: | GPL-3 |
Version: | 0.2.1.9000 |
Built: | 2024-11-15 02:50:02 UTC |
Source: | https://github.com/hadley/assertthat |
Are two objects equal?
are_equal(x, y, ...)
are_equal(x, y, ...)
x , y
|
objects to compare |
... |
additional arguments passed to |
Other assertions: is.error
,
is.scalar
, noNA
,
not_empty
x <- 2 see_if(are_equal(x, 1.9)) see_if(are_equal(x, 1.999, tol = 0.01)) see_if(are_equal(x, 2))
x <- 2 see_if(are_equal(x, 1.9)) see_if(are_equal(x, 1.999, tol = 0.01)) see_if(are_equal(x, 2))
assert_that
is a drop-in replacement for stopifnot
but
is designed to give informative error messages.
assert_that(..., env = parent.frame(), msg = NULL) see_if(..., env = parent.frame(), msg = NULL)
assert_that(..., env = parent.frame(), msg = NULL) see_if(..., env = parent.frame(), msg = NULL)
... |
unnamed expressions that describe the conditions to be tested.
Rather than combining expressions with |
env |
(advanced use only) the environment in which to evaluate the assertions. |
msg |
a custom error message to be printed if one of the conditions is false. |
Assertion functions should return a single TRUE
or FALSE
:
any other result is an error, and assert_that
will complain about
it. This will always be the case for the assertions provided by
assertthat
, but you may need be a more careful for
base R functions.
To make your own assertions that work with assert_that
,
see the help for on_failure
. Alternatively, a custom message
can be specified for each call.
validate_that
, which returns a message (not an error)
if the condition is false.
x <- 1 # assert_that() generates errors, so can't be usefully run in # examples ## Not run: assert_that(is.character(x)) assert_that(length(x) == 3) assert_that(is.dir("asdf")) y <- tempfile() writeLines("", y) assert_that(is.dir(y)) assert_that(FALSE, msg = "Custom error message") ## End(Not run) # But see_if just returns the values, so you'll see that a lot # in the examples: but remember to use assert_that in your code. see_if(is.character(x)) see_if(length(x) == 3) see_if(is.dir(17)) see_if(is.dir("asdf")) see_if(5 < 3, msg = "Five is not smaller than three")
x <- 1 # assert_that() generates errors, so can't be usefully run in # examples ## Not run: assert_that(is.character(x)) assert_that(length(x) == 3) assert_that(is.dir("asdf")) y <- tempfile() writeLines("", y) assert_that(is.dir(y)) assert_that(FALSE, msg = "Custom error message") ## End(Not run) # But see_if just returns the values, so you'll see that a lot # in the examples: but remember to use assert_that in your code. see_if(is.character(x)) see_if(length(x) == 3) see_if(is.dir(17)) see_if(is.dir("asdf")) see_if(5 < 3, msg = "Five is not smaller than three")
Missing is functions.
is.error(x) is.time(x) is.date(x)
is.error(x) is.time(x) is.date(x)
x |
object to test |
Other assertions: are_equal
,
is.scalar
, noNA
,
not_empty
a <- Sys.time() is.time(a) b <- Sys.Date() is.date(b) c <- try(stop("!!")) is.error(c)
a <- Sys.time() is.time(a) b <- Sys.Date() is.date(b) c <- try(stop("!!")) is.error(c)
Useful test related to files
is.dir(path) is.writeable(path) is.readable(path) has_extension(path, ext)
is.dir(path) is.writeable(path) is.readable(path) has_extension(path, ext)
path |
a file path to examine |
ext |
extension to test for ( |
see_if(is.dir(1)) tmp <- tempfile() see_if(file.exists(tmp)) see_if(is.dir(tmp)) writeLines("x", tmp) see_if(file.exists(tmp)) see_if(is.dir(tmp)) see_if(is.writeable(tmp)) see_if(is.readable(tmp)) unlink(tmp) see_if(is.readable(tmp))
see_if(is.dir(1)) tmp <- tempfile() see_if(file.exists(tmp)) see_if(is.dir(tmp)) writeLines("x", tmp) see_if(file.exists(tmp)) see_if(is.dir(tmp)) see_if(is.writeable(tmp)) see_if(is.readable(tmp)) unlink(tmp) see_if(is.readable(tmp))
Check a function has specified arguments
has_args(f, args, exact = FALSE) f %has_args% args
has_args(f, args, exact = FALSE) f %has_args% args
f |
a function |
args |
a character vector of argument names |
exact |
if |
has_args(mean, "x") has_args(mean, "x", exact = TRUE) see_if(mean %has_args% "x") see_if(mean %has_args% "y")
has_args(mean, "x") has_args(mean, "x", exact = TRUE) see_if(mean %has_args% "x") see_if(mean %has_args% "y")
Has attribute or name?
has_attr(x, which) x %has_attr% which has_name(x, which) x %has_name% which
has_attr(x, which) x %has_attr% which has_name(x, which) x %has_name% which
x |
object to test |
which |
name or attribute |
has_attr(has_attr, "fail") x <- 10 x %has_attr% "a" y <- list(a = 1, b = 2) see_if(y %has_name% "c")
has_attr(has_attr, "fail") x <- 10 x %has_attr% "a" y <- list(a = 1, b = 2) see_if(y %has_name% "c")
Does object contain any missing values?
noNA(x)
noNA(x)
x |
object to test |
Other assertions: are_equal
,
is.error
, is.scalar
,
not_empty
see_if(noNA("a")) see_if(noNA(c(TRUE, NA))) x <- sample(c(1:10, NA), 100, rep = TRUE) see_if(noNA(x))
see_if(noNA("a")) see_if(noNA(c(TRUE, NA))) x <- sample(c(1:10, NA), 100, rep = TRUE) see_if(noNA(x))
Check an object doesn't have any empty dimensions
not_empty(x)
not_empty(x)
x |
object to test |
Other assertions: are_equal
,
is.error
, is.scalar
,
noNA
not_empty(numeric()) not_empty(mtcars[0, ]) not_empty(mtcars[, 0])
not_empty(numeric()) not_empty(mtcars[0, ]) not_empty(mtcars[, 0])
Custom failure messages for assertions.
on_failure(x) on_failure(x) <- value
on_failure(x) on_failure(x) <- value
x |
a assertion function that returns |
value |
a function with parameters |
is_odd <- function(x) { assert_that(is.numeric(x), length(x) == 1) x %% 2 == 1 } see_if(is_odd(2)) on_failure(is_odd) <- function(call, env) { paste0(deparse(call$x), " is even") } see_if(is_odd(2))
is_odd <- function(x) { assert_that(is.numeric(x), length(x) == 1) x %% 2 == 1 } see_if(is_odd(2)) on_failure(is_odd) <- function(call, env) { paste0(deparse(call$x), " is even") } see_if(is_odd(2))
is.scalar
provides a generic method for checking input is a scalar.
is.string
, is.flag
, is.number
and is.count
provide tests for specific types.
is.scalar(x) is.string(x) is.number(x) is.flag(x) is.count(x)
is.scalar(x) is.string(x) is.number(x) is.flag(x) is.count(x)
x |
object to test |
Other assertions: are_equal
,
is.error
, noNA
,
not_empty
# Generic check for scalars see_if(is.scalar("a")) see_if(is.scalar(1:10)) # string = scalar character vector see_if(is.string(1:3)) see_if(is.string(c("a", "b"))) see_if(is.string("x")) # number = scalar numeric/integer vector see_if(is.number(1:3)) see_if(is.number(1.5)) # flag = scalar logical vector see_if(is.flag(1:3)) see_if(is.flag("a")) see_if(is.flag(c(FALSE, FALSE, TRUE))) see_if(is.flag(FALSE)) # count = scalar positive integer see_if(is.count("a")) see_if(is.count(-1)) see_if(is.count(1:5)) see_if(is.count(1.5)) see_if(is.count(1))
# Generic check for scalars see_if(is.scalar("a")) see_if(is.scalar(1:10)) # string = scalar character vector see_if(is.string(1:3)) see_if(is.string(c("a", "b"))) see_if(is.string("x")) # number = scalar numeric/integer vector see_if(is.number(1:3)) see_if(is.number(1.5)) # flag = scalar logical vector see_if(is.flag(1:3)) see_if(is.flag("a")) see_if(is.flag(c(FALSE, FALSE, TRUE))) see_if(is.flag(FALSE)) # count = scalar positive integer see_if(is.count("a")) see_if(is.count(-1)) see_if(is.count(1:5)) see_if(is.count(1.5)) see_if(is.count(1))
validate_that
is an alternative to the function
assert_that
, that returns a character
vector. This
makes them easier to use within S4 "validate"
methods.
validate_that(..., env = parent.frame(), msg = NULL)
validate_that(..., env = parent.frame(), msg = NULL)
... |
unnamed expressions that describe the conditions to be tested.
Rather than combining expressions with |
env |
(advanced use only) the environment in which to evaluate the assertions. |
msg |
a custom error message to be printed if one of the conditions is false. |
A character
vector if the assertion is false, or TRUE
if the assertion is true.
assert_that
, which returns an error if the condition
is false.
x <- 1 # assert_that() generates errors, so can't be usefully run in # examples validate_that(is.numeric(x)) validate_that(is.character(x)) validate_that(length(x) == 3) validate_that(is.dir("asdf"))
x <- 1 # assert_that() generates errors, so can't be usefully run in # examples validate_that(is.numeric(x)) validate_that(is.character(x)) validate_that(length(x) == 3) validate_that(is.dir("asdf"))