Skip to content

Commit

Permalink
added qexpect and qexpectr
Browse files Browse the repository at this point in the history
  • Loading branch information
mllg committed Jul 16, 2015
1 parent 484f588 commit be1f5cd
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 42 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ export(expect_subset)
export(expect_vector)
export(qassert)
export(qassertr)
export(qexpect)
export(qexpectr)
export(qtest)
export(qtestr)
export(testAccess)
Expand Down
73 changes: 37 additions & 36 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -3,81 +3,82 @@ Version 1.6.1 (2015-07-16)
* New family of functions: `expect_*` is intended to be used in combination
with testthat. But note that functions `expect_null()` and `expect_named()`
are not provided to avoid name clashes with testthat.
* Also added `qexpect()` and `qexpectr()`.
* Added argument `all.missing` for checks of matricies and data frames.
* Added anyNaN
* Clearified documentation for `assert()` and `allMissing()`
* Added `anyNaN()`.
* Clearified documentation for `assert()` and `allMissing()`.
* Fixed a bug where bound checks were performed on missing values.
* Fixed a bug where missingness was not correctly detected in data frames.

Version 1.6.0 (2015-06-19)
-------------------------------------------------------------------------------
* Started to support long vectors
* Added a short vignette
* Improved documentation
* New argument "combine" for assert() to allow combining check functions with
an AND instead of an OR
* Started to support long vectors.
* Added a short vignette.
* Improved documentation.
* New argument "combine" for `assert()` to allow combining check functions with
an AND instead of an OR.

Version 1.5.3 (2015-05-13)
-------------------------------------------------------------------------------
* Fixed a bug regarding the number of rows in zero-column data frames.
* Fixed a bug where the type of lists with dimension attribute where
reported as "array" or "matrix"
* Family *Array: new arguments "min.d" and "max.d"
reported as "array" or "matrix".
* Family *Array: new arguments "min.d" and "max.d".
* Family *Array and *Matrix: Argument "mode" now additionally accepts strings
"list" and "atomic"
"list" and "atomic".

Version 1.5.2 (2015-03-19)
-------------------------------------------------------------------------------
* Fixed: (assert|check|test)Character(NA_character_, min.chars = 1) does not
eval to TRUE anymore.
* New arguments for *Factor functions: (n|min|max).levels.
* Improved error messages for type and length checks
* Improved error messages for missing arguments
* Improved error messages for type and length checks.
* Improved error messages for missing arguments.

Version 1.5.1 (2014-12-13)
-------------------------------------------------------------------------------
* Included a workaround for R's nrow and ncol to properly worh with data frames
* Fixed a bug handling complex number in checks for integerish values
* Improved documentation
* Included a workaround for R's nrow and ncol to properly worh with data frames.
* Fixed a bug handling complex number in checks for integerish values.
* Improved documentation.

Version 1.5.0 (2014-10-18)
-------------------------------------------------------------------------------
* Added checkNames
* Added checkPercentage
* Added anyInfinite
* Fixed error messages for some dimension checks
* Fixed an error while checking numerics for finiteness
* Added checkNames.
* Added checkPercentage.
* Added anyInfinite.
* Fixed error messages for some dimension checks.
* Fixed an error while checking numerics for finiteness.

Version 1.4 (2014-09-03)
-------------------------------------------------------------------------------
* Fixed a bug where rownames and colnames of data.frames where not retrieved
correctly
* Fixed a bug in checkVector (wrong order of arguments in call to C)
correctly.
* Fixed a bug in checkVector (wrong order of arguments in call to C).
* Filesystem access: checks for write and executable rights are now disabled
on windows
on windows.

Version 1.3 (2014-08-15)
-------------------------------------------------------------------------------
* Fixed a bug where logical values passed a check for numerics in "qassert"
* Family *SetEqual: new argument "ordered"
* checkPathForOutput: new argument "overwrite"
* Fixed a bug where logical values passed a check for numerics in "qassert".
* Family *SetEqual: new argument "ordered".
* checkPathForOutput: new argument "overwrite".

Version 1.2 (2014-07-21)
-------------------------------------------------------------------------------
* Fixed bug in checkList
* Fixed dimnames check on empty matrices and data frames
* Added *SetEqual functions
* Fixed bug in checkList.
* Fixed dimnames check on empty matrices and data frames.
* Added *SetEqual functions.

Version 1.1 (2014-06-28)
-------------------------------------------------------------------------------
* Improved error messages in assert* functions
* New argument 'empty.ok' for *Subset functions
* assert() now returns TRUE invisibly (as documented)
* Fixed handling of zero-length arguments in checkFunction
* Fixed error message if duplicated values where found
* Fixed a missing check for row names in checkMatrix and checkDataFrame
* Improved error messages in assert* functions.
* New argument 'empty.ok' for *Subset functions.
* assert() now returns TRUE invisibly (as documented).
* Fixed handling of zero-length arguments in checkFunction.
* Fixed error message if duplicated values where found.
* Fixed a missing check for row names in checkMatrix and checkDataFrame.


Version 1.0 (2014-06-17)
-------------------------------------------------------------------------------
* Initial release on CRAN
* Initial release on CRAN.
11 changes: 11 additions & 0 deletions R/qassert.r
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#' otherwise.
#' \code{qtest} behaves the same way but returns \code{FALSE} if none of the
#' \code{rules} comply.
#' \code{qexpect} is intended to be inside the unit test framework \code{\link[testthat]{testthat}} and
#' returns an \code{\link[testthat]{expectation}}.
#'
#' The rule is specified in up to three parts.
#' \enumerate{
Expand Down Expand Up @@ -113,3 +115,12 @@ qassert = function(x, rules, .var.name) {
qtest = function(x, rules) {
.Call("c_qtest", x, rules, FALSE, PACKAGE = "checkmate")
}

#' @useDynLib checkmate c_qassert
#' @template expect
#' @rdname qassert
#' @export
qexpect = function(x, rules, info = NULL, label = NULL) {
res = .Call("c_qassert", x, rules, FALSE, PACKAGE = "checkmate")
makeExpectation(res, info = info, label = vname(x, label))
}
15 changes: 12 additions & 3 deletions R/qassertr.r
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#' Quick recursive arguments checks on lists and data frames
#'
#' @description
#' These functions are the tuned counterparts of \code{\link{qtest}} and
#' \code{\link{qassert}} tailored for recursive checks of list
#' elements or data frame columns.
#' These functions are the tuned counterparts of \code{\link{qtest}},
#' \code{\link{qassert}} and \code{\link{qexpect}} tailored for recursive
#' checks of list elements or data frame columns.
#'
#' @param x [\code{list} or \code{data.frame}]\cr
#' List or data frame to check for compliance with at least one of \code{rules}.
Expand Down Expand Up @@ -34,3 +34,12 @@ qassertr = function(x, rules, .var.name) {
qtestr = function(x, rules) {
.Call("c_qtest", x, rules, TRUE, PACKAGE = "checkmate")
}

#' @useDynLib checkmate c_qassert
#' @template expect
#' @rdname qassertr
#' @export
qexpectr = function(x, rules, info = NULL, label = NULL) {
res = .Call("c_qassert", x, rules, TRUE, PACKAGE = "checkmate")
makeExpectation(res, info = info, label = vname(x, label))
}
12 changes: 12 additions & 0 deletions man/qassert.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
% Please edit documentation in R/qassert.r
\name{qassert}
\alias{qassert}
\alias{qexpect}
\alias{qtest}
\title{Quick argument checks on (builtin) R types}
\usage{
qassert(x, rules, .var.name)

qtest(x, rules)

qexpect(x, rules, info = NULL, label = NULL)
}
\arguments{
\item{x}{[any]\cr
Expand All @@ -19,6 +22,13 @@ Set of rules. See details.}
\item{.var.name}{[\code{logical(1)}]\cr
Argument name to print in error message. If missing,
the name of \code{x} will be retrieved via \code{\link[base]{substitute}}.}

\item{info}{[character(1)]\cr
Extra information to be included in the message for the testthat reporter.
See \code{\link[testthat]{expect_that}}.}

\item{label}{[character(1)]\cr
Same as \code{.var.name}, but passed down to \code{\link[testthat]{expect_that}}.}
}
\value{
[logical(1)]: \code{TRUE} on success, \code{FALSE} (or a thrown exception) otherwise.
Expand All @@ -33,6 +43,8 @@ not comply to at least one of the \code{rules} and returns \code{TRUE}
otherwise.
\code{qtest} behaves the same way but returns \code{FALSE} if none of the
\code{rules} comply.
\code{qexpect} is intended to be inside the unit test framework \code{\link[testthat]{testthat}} and
returns an \code{\link[testthat]{expectation}}.

The rule is specified in up to three parts.
\enumerate{
Expand Down
16 changes: 13 additions & 3 deletions man/qassertr.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
% Please edit documentation in R/qassertr.r
\name{qassertr}
\alias{qassertr}
\alias{qexpectr}
\alias{qtestr}
\title{Quick recursive arguments checks on lists and data frames}
\usage{
qassertr(x, rules, .var.name)

qtestr(x, rules)

qexpectr(x, rules, info = NULL, label = NULL)
}
\arguments{
\item{x}{[\code{list} or \code{data.frame}]\cr
Expand All @@ -20,14 +23,21 @@ Set of rules. See \code{\link{qtest}}}
\item{.var.name}{[\code{logical(1)}]\cr
Argument name to print in error message. If missing,
the name of \code{x} will be retrieved via \code{\link[base]{substitute}}.}

\item{info}{[character(1)]\cr
Extra information to be included in the message for the testthat reporter.
See \code{\link[testthat]{expect_that}}.}

\item{label}{[character(1)]\cr
Same as \code{.var.name}, but passed down to \code{\link[testthat]{expect_that}}.}
}
\value{
[logical(1)]: \code{TRUE} on success, \code{FALSE} (or a thrown exception) otherwise.
}
\description{
These functions are the tuned counterparts of \code{\link{qtest}} and
\code{\link{qassert}} tailored for recursive checks of list
elements or data frame columns.
These functions are the tuned counterparts of \code{\link{qtest}},
\code{\link{qassert}} and \code{\link{qexpect}} tailored for recursive
checks of list elements or data frame columns.
}
\examples{
qtestr(as.list(1:10), "i+")
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test_qtest.r
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ expect_succ_all = function(x, rules) {
info=sprintf("vector %s, rules: %s", deparse(substitute(x)), paste(rules, collapse=",")))
expect_true(qassert(x, rules),
info=sprintf("vector %s, rules: %s", deparse(substitute(x)), paste(rules, collapse=",")))
expect_true(is.expectation(qexpect(x, rules)),
info=sprintf("vector %s, rules: %s", deparse(substitute(x)), paste(rules, collapse=",")))
}

expect_fail_all = function(x, rules) {
expect_false(qtest(x, rules),
info=sprintf("vector %s, rules: %s", deparse(substitute(x)), paste(rules, collapse=",")))
expect_true(inherits(try(qassert(x, rules), silent=TRUE), "try-error"),
info=sprintf("vector %s, rules: %s", deparse(substitute(x)), paste(rules, collapse=",")))
expect_error(with_reporter(SilentReporter(), qexpect(x, rules)),
info=sprintf("vector %s, rules: %s", deparse(substitute(x)), paste(rules, collapse=",")))
}

test_that("type and missingness", {
Expand Down Expand Up @@ -210,3 +214,10 @@ test_that("logicals are not numeric", {
expect_fail_all(TRUE, "n")
expect_fail_all(TRUE, "N")
})

test_that("qexpect works", {
expect_fail_all(TRUE, "i")
expect_fail_all(TRUE, "I")
expect_fail_all(TRUE, "n")
expect_fail_all(TRUE, "N")
})
4 changes: 4 additions & 0 deletions tests/testthat/test_qtestr.r
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ expect_succ_all = function(x, rules) {
info=sprintf("vector %s, rules: %s", deparse(substitute(x)), paste(rules, collapse=",")))
expect_true(qassertr(x, rules),
info=sprintf("vector %s, rules: %s", deparse(substitute(x)), paste(rules, collapse=",")))
expect_true(is.expectation(qexpectr(x, rules)),
info=sprintf("vector %s, rules: %s", deparse(substitute(x)), paste(rules, collapse=",")))
}

expect_fail_all = function(x, rules) {
expect_false(qtestr(x, rules),
info=sprintf("vector %s, rules: %s", deparse(substitute(x)), paste(rules, collapse=",")))
expect_true(inherits(try(qassertr(x, rules), silent=TRUE), "try-error"),
info=sprintf("vector %s, rules: %s", deparse(substitute(x)), paste(rules, collapse=",")))
expect_error(with_reporter(SilentReporter(), qexpectr(x, rules)),
info=sprintf("vector %s, rules: %s", deparse(substitute(x)), paste(rules, collapse=",")))
}

test_that("qtestr", {
Expand Down

0 comments on commit be1f5cd

Please sign in to comment.