New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Additional expectations #77
Changes from 4 commits
cde7a72
af296ca
8ce064c
8f4c98f
b49680a
520b155
01d671b
18b6a09
30f4d7a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.Rproj.user | ||
.Rhistory | ||
.RData | ||
testthat.Rproj | ||
.Rbuildignore |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -466,3 +466,210 @@ takes_less_than <- function(amount) { | |
) | ||
} | ||
} | ||
|
||
|
||
|
||
#' Expectation: is returned value less than specified value? | ||
#' | ||
#' This is useful for ensuring returned value is below a ceiling. | ||
# | ||
#' @param expected Expected value | ||
#' @param label For full form, label of expected object used in error | ||
#' messages. Useful to override default (deparsed expected expression) when | ||
#' doing tests in a loop. For short cut form, object label. When | ||
#' \code{NULL}, computed from deparsed object. | ||
#' @param expected.label Equivalent of \code{label} for shortcut form. | ||
#' @param ... other values passed to \code{\link{all.equal}} | ||
#' @family expectations | ||
#' @export | ||
#' @examples | ||
#' a <- 9 | ||
#' expect_that(a, is_less_than(10)) | ||
#' expect_less_than(a, 10) | ||
is_less_than <- function(expected, label=NULL, ...) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. = needs spaces around it. |
||
find_expr <- function(name, env = parent.frame()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please remove find_expr? |
||
subs <- do.call("substitute", list(as.name(name), env)) | ||
str_c(deparse(subs, width.cutoff = 500), collapse = "\n") | ||
} | ||
if (is.null(label)) { | ||
label <- find_expr("expected") | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please start the else on the same line as |
||
else if (!is.character(label) || length(label) != 1) { | ||
label <- deparse(label) | ||
} | ||
function(actual) { | ||
less <- expected > actual | ||
expectation(identical(less, TRUE), str_c("not less than ", | ||
label, "\n", | ||
str_c(less, collapse = "\n"))) | ||
} | ||
} | ||
#' @export | ||
#' @rdname less_than | ||
#' @inheritParams expect_that | ||
expect_less_than <- function(object, expected, ..., info = NULL, label = NULL, | ||
expected.label = NULL) { | ||
if (is.null(label)) { | ||
label <- find_expr("object") | ||
} | ||
if (is.null(expected.label)) { | ||
expected.label <- find_expr("expected") | ||
} | ||
expect_that(object, is_less_than(expected, label = expected.label, ...), | ||
info = info, label = label) | ||
} | ||
|
||
#' Expectation: is returned value more than specified value? | ||
#' | ||
#' This is useful for ensuring returned value is above a floor. | ||
# | ||
#' @param expected Expected value | ||
#' @param label For full form, label of expected object used in error | ||
#' messages. Useful to override default (deparsed expected expression) when | ||
#' doing tests in a loop. For short cut form, object label. When | ||
#' \code{NULL}, computed from deparsed object. | ||
#' @param expected.label Equivalent of \code{label} for shortcut form. | ||
#' @param ... other values passed to \code{\link{all.equal}} | ||
#' @family expectations | ||
#' @export | ||
#' @examples | ||
#' a <- 11 | ||
#' expect_that(a, is_more_than(10)) | ||
#' expect_more_than(a, 10) | ||
is_more_than <- function(expected, label=NULL, ...) { | ||
find_expr <- function(name, env = parent.frame()) { | ||
subs <- do.call("substitute", list(as.name(name), env)) | ||
str_c(deparse(subs, width.cutoff = 500), collapse = "\n") | ||
} | ||
if (is.null(label)) { | ||
label <- find_expr("expected") | ||
} | ||
else if (!is.character(label) || length(label) != 1) { | ||
label <- deparse(label) | ||
} | ||
function(actual) { | ||
more <- expected < actual | ||
expectation(identical(more, TRUE), str_c("not more than ", | ||
label, "\n", | ||
str_c(more, collapse = "\n"))) | ||
} | ||
} | ||
#' @export | ||
#' @rdname more_than | ||
#' @inheritParams expect_that | ||
expect_more_than <- function(object, expected, ..., info = NULL, label = NULL, | ||
expected.label = NULL) { | ||
if (is.null(label)) { | ||
label <- find_expr("object") | ||
} | ||
if (is.null(expected.label)) { | ||
expected.label <- find_expr("expected") | ||
} | ||
expect_that(object, is_more_than(expected, label = expected.label, ...), | ||
info = info, label = label) | ||
} | ||
|
||
#' Expectation: is returned value unequal to some specified value? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please remove this for now? It will get incorporated as part of the general negation of assertions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So expect_unequal and unequal should both be removed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right |
||
#' | ||
#' This is useful for ensuring returned value is anything other than expected value. | ||
# | ||
#' @param expected Expected value | ||
#' @param label For full form, label of expected object used in error | ||
#' messages. Useful to override default (deparsed expected expression) when | ||
#' doing tests in a loop. For short cut form, object label. When | ||
#' \code{NULL}, computed from deparsed object. | ||
#' @param expected.label Equivalent of \code{label} for shortcut form. | ||
#' @param ... other values passed to \code{\link{all.equal}} | ||
#' @family expectations | ||
#' @export | ||
#' @examples | ||
#' a <- 11 | ||
#' expect_that(a, unequal(10)) | ||
#' expect_unequal(a, 10) | ||
unequal <- function(expected, label = NULL, ...) | ||
{ | ||
if (is.null(label)) { | ||
label <- find_expr("expected") | ||
} | ||
else if (!is.character(label) || length(label) != 1) { | ||
label <- deparse(label) | ||
} | ||
function(actual) { | ||
unequal <- !isTRUE(all.equal(expected, actual, ...)) | ||
expectation(identical(unequal, TRUE), str_c("not equal to ", | ||
label, "\n", str_c(unequal, collapse = "\n"))) | ||
} | ||
} | ||
#' @export | ||
#' @rdname unequal | ||
#' @inheritParams expect_that | ||
expect_unequal <- function (object, expected, ..., info = NULL, label = NULL, expected.label = NULL) | ||
{ | ||
find_expr <- function(name, env = parent.frame()) { | ||
subs <- do.call("substitute", list(as.name(name), env)) | ||
str_c(deparse(subs, width.cutoff = 500), collapse = "\n") | ||
} | ||
if (is.null(label)) { | ||
label <- find_expr("object") | ||
} | ||
if (is.null(expected.label)) { | ||
expected.label <- find_expr("expected") | ||
} | ||
expect_that(object, unequal(expected, label = expected.label, | ||
...), info = info, label = label) | ||
} | ||
|
||
#' Expectation: is returned value equal within a tolerance to some specified value? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is already achieveable with |
||
#' | ||
#' This is useful for testing whether a returned value is within a user specified distance | ||
#' from an expected value. | ||
# | ||
#' @param expected Expected value | ||
#' @param tol tolerance for returned value to be distant from expected value | ||
#' @param label For full form, label of expected object used in error | ||
#' messages. Useful to override default (deparsed expected expression) when | ||
#' doing tests in a loop. For short cut form, object label. When | ||
#' \code{NULL}, computed from deparsed object. | ||
#' @param expected.label Equivalent of \code{label} for shortcut form. | ||
#' @param ... other values passed to \code{\link{all.equal}} | ||
#' @family expectations | ||
#' @export | ||
#' @examples | ||
#' a <- 11 | ||
#' expect_that(a, unequal(10)) | ||
#' expect_unequal(a, 10) | ||
#' expect_that(a, approxto(10, tol=2)) # TRUE | ||
#' expect_approxto(a, 10, tol=2) | ||
approxto <- function(expected, tol, label=NULL, ...) { | ||
if (is.null(label)) { | ||
label <- find_expr("expected") | ||
} | ||
else if (!is.character(label) || length(label) != 1) { | ||
label <- deparse(label) | ||
} | ||
function(actual) { | ||
app <- (expected - tol) < actual & actual < (expected + tol) | ||
expectation(identical(app, TRUE), stringr::str_c("not within tolerance ", | ||
label, "\n", | ||
stringr::str_c(app, collapse = "\n"))) | ||
} | ||
} | ||
|
||
#' @export | ||
#' @rdname approxto | ||
#' @inheritParams expect_that | ||
expect_approxto <- function (object, expected, ..., info = NULL, label = NULL, expected.label = NULL) | ||
{ | ||
find_expr <- function(name, env = parent.frame()) { | ||
subs <- do.call("substitute", list(as.name(name), env)) | ||
stringr::str_c(deparse(subs, width.cutoff = 500), collapse = "\n") | ||
} | ||
if (is.null(label)) { | ||
label <- find_expr("object") | ||
} | ||
if (is.null(expected.label)) { | ||
expected.label <- find_expr("expected") | ||
} | ||
expect_that(object, approxto(expected, tol, label = expected.label, | ||
...), info = info, label = label) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please remove this file from the pull request?