-
Notifications
You must be signed in to change notification settings - Fork 337
Add code to test test_that calls. #83
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
Changes from all commits
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,35 @@ | ||
#' @include reporter.r | ||
NULL | ||
|
||
#' Test reporter: gather all errors silently. | ||
#' | ||
#' This reporter quietly runs all tests, simply gathering the results | ||
#' for later use. This is helpful for programmatically inspecting errors | ||
#' after a test run. | ||
#' | ||
#' @export | ||
#' @exportClass SilentReporter | ||
#' @aliases SilentReporter-class | ||
#' @keywords debugging | ||
#' @param ... Arguments used to initialise class | ||
SilentReporter <- setRefClass("SilentReporter", contains = "Reporter", | ||
fields = c("failures"), | ||
methods = list( | ||
initialize = function(...) { | ||
failures <<- list() | ||
callSuper(...) | ||
}, | ||
start_test = function(desc) { | ||
test <<- desc | ||
}, | ||
|
||
end_test = function() { | ||
test <<- NULL | ||
}, | ||
|
||
add_result = function(result) { | ||
if (result$passed) return() | ||
failures[[test]] <<- new_failure | ||
} | ||
) | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,18 @@ watch <- function(path, callback, pattern = NULL, hash = TRUE) { | |
} | ||
} | ||
|
||
#' Compute a digest of a filename, returning NA if the file doesn't | ||
#' exist. | ||
#' | ||
#' @param filename filename to compute digest on | ||
#' @return a digest of the file, or NA if it doesn't exist. | ||
#' @keywords internal | ||
safe_digest <- function(path) { | ||
result <- NA_character_ | ||
try(result <- digest(path, file = TRUE), silent = TRUE) | ||
result | ||
} | ||
|
||
#' Capture the state of a directory. | ||
#' | ||
#' @param path path to directory | ||
|
@@ -50,8 +62,12 @@ watch <- function(path, callback, pattern = NULL, hash = TRUE) { | |
dir_state <- function(path, pattern = NULL, hash = TRUE) { | ||
files <- dir(path, pattern, full.names = TRUE) | ||
|
||
# It's possible for any of the files to be deleted between the dir() | ||
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. Nice, thanks! |
||
# call above and the calls below; `file.info` handles this | ||
# gracefully, but digest::digest doesn't -- so we wrap it. Both | ||
# cases will return NA for files that have gone missing. | ||
if (hash) { | ||
sapply(files, digest::digest, file = TRUE) | ||
vapply(files, safe_digest, character(1)) | ||
} else { | ||
setNames(file.info(files)$mtime, files) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,60 @@ | ||
if (interactive()) { | ||
# Test that test_that succeeds or fails as expected. | ||
test_test_that <- function(desc, expr, failure_expected = TRUE) { | ||
reporter <- SilentReporter$new() | ||
old_reporter <- set_reporter(reporter) | ||
test_that(desc, expr) | ||
set_reporter(old_reporter) | ||
test_that(desc, { | ||
if (failure_expected) { | ||
info <- 'Test succeeded when failure expected' | ||
expect_equal(length(reporter$failures), 1, info = info) | ||
} else { | ||
info <- sprintf( | ||
'Test failed unexpectedly: %s', | ||
as.character(reporter$failures[[desc]])) | ||
expect_equal(length(reporter$failures), 0, info = info) | ||
} | ||
}) | ||
} | ||
|
||
context("Should fail") | ||
context("Testing test_that") | ||
|
||
test_that("false is not true (should fail)", { | ||
expect_that(FALSE, is_true()) | ||
}) | ||
test_test_that("false is false", { | ||
expect_that(FALSE, is_false()) | ||
}, failure_expected = FALSE) | ||
|
||
test_that("true is not false (should fail)", { | ||
expect_that(TRUE, is_false()) | ||
}) | ||
test_test_that("false is not true", { | ||
expect_that(FALSE, is_true()) | ||
}) | ||
|
||
test_that("fail fails (should fail)", { | ||
fail() | ||
}) | ||
test_test_that("true is not false", { | ||
expect_that(TRUE, is_false()) | ||
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. Copy and pasto? 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. Oh, no it's just that the ordering has changed. |
||
}) | ||
|
||
test_that("random errors are caught (should err)", { | ||
function_that_doesnt_exist() | ||
}) | ||
test_test_that("1 equals 1", { | ||
expect_that(1, equals(1)) | ||
}, failure_expected = FALSE) | ||
|
||
f <- function() g() | ||
g <- function() stop("I made a mistake", call. = FALSE) | ||
test_test_that("1 does not equal 2", { | ||
expect_that(1, equals(2)) | ||
}) | ||
|
||
test_that("errors are captured (should err)", { | ||
f() | ||
}) | ||
test_test_that("fail fails", { | ||
fail() | ||
}) | ||
|
||
test_that("errors when looking for warnings propagte (should err)", { | ||
f <- function() stop("!") | ||
expect_warning(f()) | ||
test_test_that("random errors are caught", { | ||
function_that_doesnt_exist() | ||
}) | ||
|
||
}) | ||
f <- function() g() | ||
g <- function() stop("I made a mistake", call. = FALSE) | ||
|
||
expect_that(1, equals(2)) | ||
test_test_that("errors are captured", { | ||
f() | ||
}) | ||
|
||
} | ||
test_test_that("errors when looking for warnings propagte", { | ||
f <- function() stop("!") | ||
expect_warning(f()) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
\name{SilentReporter} | ||
\alias{SilentReporter} | ||
\alias{SilentReporter-class} | ||
\title{Test reporter: gather all errors silently.} | ||
\usage{ | ||
SilentReporter(...) | ||
} | ||
\arguments{ | ||
\item{...}{Arguments used to initialise class} | ||
} | ||
\description{ | ||
This reporter quietly runs all tests, simply gathering | ||
the results for later use. This is helpful for | ||
programmatically inspecting errors after a test run. | ||
} | ||
\keyword{debugging} | ||
|
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.
Nice idea!