Skip to content

Commit

Permalink
tests: Add first tests for stopifnot
Browse files Browse the repository at this point in the history
  • Loading branch information
MEO265 committed Jan 10, 2024
1 parent 95dc566 commit 59bc12f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
24 changes: 13 additions & 11 deletions tests/testthat/setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,40 @@ expect_identical_condition <- function(actual, expected, type = c("message", "wa
error = testthat::capture_error
)

actual <- capture(actual)
expected <- capture(expected)
capture.output({
actual <- capture(actual)
expected <- capture(expected)
})

if(is.null(actual)){
if (is.null(actual)) {
testthat::fail("Actual don't throws an error.")
}
if(is.null(expected)){
if (is.null(expected)) {
testthat::fail("Expected don't throws an error.")
}

if(actual$message != expected$message){
if (actual$message != expected$message) {
testthat::fail(sprintf("Actual message is '%s' and expected is '%s'.", actual$message, expected$message))
}

if(xor(is.null(actual$call), is.null(expected$call))){
if(is.null(actual$call)) {
if (xor(is.null(actual$call), is.null(expected$call))) {
if (is.null(actual$call)) {
fail(sprintf("Actual has no call, but expected has '%s'.", deparse(expected$call)))
} else {
fail(sprintf("Actual has call '%s', and expected has non.", deparse(actual$call)))
}
}

if(actual$call != expected$call) {
if (actual$call != expected$call) {
fail(sprintf("Actual has call '%s', but expected has '%s'", deparse(actual$call), deparse(expected$call)))
}

testthat::succeed()
return(invisible())
}

expect_identical_error <- function (actual, expected) expect_identical_condition(actual, expected, type = "error")
expect_identical_error <- function(actual, expected) expect_identical_condition(actual, expected, type = "error")

expect_identical_warning <- function (actual, expected) expect_identical_condition(actual, expected, type = "warning")
expect_identical_warning <- function(actual, expected) expect_identical_condition(actual, expected, type = "warning")

expect_identical_message <- function (actual, expected) expect_identical_condition(actual, expected, type = "message")
expect_identical_message <- function(actual, expected) expect_identical_condition(actual, expected, type = "message")
12 changes: 12 additions & 0 deletions tests/testthat/test-handlers.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,15 @@ test_that("stop works as it does in base R", {
})

cleanup()

test_that("stopifnot",{
# stopifnot works as in base R
expect_identical_error(stopifnot(FALSE),base::stopifnot(FALSE))
f <- function (x, ...) x
expect_identical_error(stopifnot(f(x = FALSE)), base::stopifnot(f(x = FALSE)))
g <- function () f(FALSE)
expect_identical_error(stopifnot(4 == 4, g()), base::stopifnot(4==4, g()))
expect_no_error(stopifnot(TRUE, 3 == 3))
})

cleanup()

0 comments on commit 59bc12f

Please sign in to comment.