-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
233 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#' Verify output | ||
#' | ||
#' This is a regression test records interwoven code and output into a file, | ||
#' similar to Rmd. It's designed particularly for testing print methods and | ||
#' error messages, where the primary goal is to ensure that the output is | ||
#' helpful to a human. Obviously, there's no way to test that automatically, | ||
#' so the best we can do is make the results explicit by saving to a text file. | ||
#' This makes the presentation easier to see in code reviews, and avoids | ||
#' changing it accidentally. | ||
#' | ||
#' @section CRAN: | ||
#' On CRAN, `verify_output()` will not fail if the output changes. This is | ||
#' beause tests of print methods and error messages are often fragile due to | ||
#' implicit dependencies on other packages, and failure does not imply | ||
#' incorrect computation, just a change in presentation. | ||
#' | ||
#' @section Differences to Rmd: | ||
#' `verify_output()` can only capture the abstract syntax tree, losing all | ||
#' whitespace and comments. To mildy offset this limitation, bare string | ||
#' are turned into comments. | ||
#' | ||
#' @param path Path to save file. Typically this will be a call to | ||
#' [test_path()] so that the same path when the code is run interactively. | ||
#' @param code Code to execute. | ||
#' @param width Width of console output | ||
#' @param crayon Enable crayon package colouring? | ||
#' @export | ||
verify_output <- function(path, code, width = 80, crayon = FALSE) { | ||
code <- enquo(code) | ||
|
||
env <- get_env(code) | ||
expr <- get_expr(code) | ||
|
||
if (is_call(expr, "{")) { | ||
exprs <- as.list(expr[-1]) | ||
} else { | ||
exprs <- list(expr) | ||
} | ||
|
||
withr::local_options(list(width = width, crayon.enabled = crayon)) | ||
withr::local_envvar(list(RSTUDIO_CONSOLE_WIDTH = width)) | ||
|
||
exprs <- lapply(exprs, function(x) if (is.character(x)) paste0("# ", x) else expr_deparse(x)) | ||
source <- unlist(exprs, recursive = FALSE) | ||
|
||
results <- evaluate::evaluate(source, envir = env) | ||
output <- unlist(lapply(results, output_replay)) | ||
|
||
if (!interactive()) { | ||
skip_on_cran() | ||
} | ||
compare_file(path, output, update = TRUE) | ||
invisible() | ||
} | ||
|
||
|
||
output_replay <- function(x) { | ||
UseMethod("output_replay", x) | ||
} | ||
|
||
#' @export | ||
output_replay.character <- function(x) { | ||
sub("\n$", "", x) | ||
} | ||
|
||
#' @export | ||
output_replay.source <- function(x) { | ||
lines <- strsplit(x$src, "\n")[[1]] | ||
n <- length(lines) | ||
|
||
lines[1] <- paste0("> ", lines[1]) | ||
if (n > 1) { | ||
lines[2:n] <- paste0("+ ", lines[2:n]) | ||
} | ||
|
||
lines | ||
} | ||
|
||
#' @export | ||
output_replay.error <- function(x) { | ||
if (is.null(x$call)) { | ||
paste0("Error: ", x$message) | ||
} else { | ||
call <- deparse(x$call) | ||
paste0("Error in ", call, ": ", x$message) | ||
} | ||
} | ||
|
||
#' @export | ||
output_replay.warning <- function(x) { | ||
if (is.null(x$call)) { | ||
paste0("Warning: ", x$message) | ||
} else { | ||
call <- deparse(x$call) | ||
paste0("Warning in ", call, ": ", x$message) | ||
} | ||
} | ||
|
||
#' @export | ||
output_replay.message <- function(x) { | ||
# Messages are the only conditions where a new line is appended automatically | ||
paste0("Message: ", sub("\n$", "", x$message)) | ||
} | ||
|
||
#' @export | ||
output_replay.recordedplot <- function(x) { | ||
abort("Plots are not supported") | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
> message("Message") | ||
Message: Message | ||
> # With calls | ||
> warning("Warning") | ||
Warning in eval(expr, envir, enclos): Warning | ||
> stop("Error") | ||
Error in eval(expr, envir, enclos): Error | ||
> # Without calls | ||
> warning("Warning", call. = FALSE) | ||
Warning: Warning | ||
> stop("Error", call. = FALSE) | ||
Error: Error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
test_that("can record all types of output", { | ||
verify_output(test_path("test-verify-output.txt"), { | ||
"Output" | ||
1 + 2 | ||
invisible(1:10) | ||
12345678 + 12345678 + 12345678 + 12345678 + 12345678 + 12345678 + | ||
12345678 + 12345678 + 12345678 + 12345678 + 12345678 | ||
}) | ||
}) | ||
|
||
test_that("can record all types of output", { | ||
verify_output(test_path("test-verify-conditions.txt"), { | ||
message("Message") | ||
|
||
"With calls" | ||
warning("Warning") | ||
stop("Error") | ||
|
||
"Without calls" | ||
warning("Warning", call. = FALSE) | ||
stop("Error", call. = FALSE) | ||
}) | ||
}) | ||
|
||
test_that("can't record plots", { | ||
skip_if(interactive()) | ||
expect_error(verify_output("path", plot(1:10)), "Plots") | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
> # Output | ||
> 1 + 2 | ||
[1] 3 | ||
> invisible(1:10) | ||
> 12345678 + 12345678 + 12345678 + 12345678 + 12345678 + 12345678 + 12345678 + | ||
+ 12345678 + 12345678 + 12345678 + 12345678 | ||
[1] 135802458 |