Skip to content

Commit

Permalink
expect_output_file overhaul
Browse files Browse the repository at this point in the history
* Set width to 80. Fixes #514
* Save output on first run. Fixes #554
* Supply print argument. Fixes #517
  • Loading branch information
hadley committed Dec 15, 2016
1 parent 5fdf26b commit db9a22c
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 19 deletions.
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# testthat 1.0.2.9000

* `expect_output_file()` now:

* Sets the width to 80 to ensure consistent output (#514)
* Saves the output on the first run (#554)
* Correctly calls internal `expect_output_file()` to avoid error (#517)

* `expect_error()` gains a `class` argument that allows you to make an assertion
about the class of the error object (#530).

Expand Down
10 changes: 7 additions & 3 deletions R/evaluate-promise.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,19 @@ get_messages <- function(x) {

#' @export
#' @rdname evaluate_promise
capture_output <- function(code, print = FALSE) {
output <- capture_output_as_vector(code, print)
#' @param width Number of characters per line of output
capture_output <- function(code, print = FALSE, width = 80) {
output <- capture_output_as_vector(code, print, width = width)
paste0(output, collapse = "\n")
}

capture_output_as_vector <- function(code, print) {
capture_output_as_vector <- function(code, print, width = 80) {
temp <- file()
on.exit(close(temp), add = TRUE)

old <- options(width = width)
on.exit(options(old), add = TRUE)

result <- with_sink(temp, withVisible(code))
if (result$visible && print) {
with_sink(temp, print(result$value))
Expand Down
19 changes: 14 additions & 5 deletions R/expect-output.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,24 @@ expect_output <- function(object, regexp = NULL, ..., info = NULL, label = NULL)
#' @export
#' @rdname output-expectations
#' @param file Path to a "golden" text file that contains the desired output.
#' @param update Should the "golden" text file be updated? Default: \code{FALSE}.
#' @param update Should the "golden" text file be updated? Defaults to
#' \code{FALSE}, but that it will automatically create output if the file
#' does not exist (i.e. on the first run).
#' @inheritParams capture_output
expect_output_file <- function(object, file, update = FALSE, ...,
info = NULL, label = NULL) {
info = NULL, label = NULL, width = 80) {
lab <- make_label(object, label)
output <- capture_output_as_vector(object)

output <- capture_output_as_vector(object, print = FALSE, width = width)
if (!file.exists(file)) {
writeLines(output, file)
}

expr <- bquote(
expect_equal(output, safe_read_lines(.(file)), ..., info = info, label = lab)
)
withCallingHandlers(
eval(bquote(
expect_equal(output, safe_read_lines(.(file)), ..., info = info, label = lab))),
eval(expr),
expectation = function(e) {
if (update && expectation_failure(e)) {
tryCatch(writeLines(output, file), error = function(e) NULL)
Expand Down
4 changes: 3 additions & 1 deletion man/evaluate_promise.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions man/output-expectations.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 17 additions & 8 deletions tests/testthat/test-expect-output.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ test_that("... passed on to grepl", {
expect_success(expect_output(print("X"), "x", ignore.case = TRUE))
})


# expect_output_file ------------------------------------------------------

test_that("expect_output_file uses specified width", {
old <- options(width = 20)
on.exit(options(old), add = TRUE)

x <- 1:100
expect_output_file(print(x), "width-80.txt")
})

test_that("expect_output_file creates file on first run", {
file <- tempfile()
expect_success(expect_output_file(cat("ok!\n"), file))
expect_true(file.exists(file))
})

test_that("expect_output_file works, also with incomplete last line", {
file <- tempfile()
writeLines("Hi!", file)
Expand All @@ -44,11 +61,3 @@ test_that("expect_output_file can update file but does not by default", {
expect_failure(expect_output_file(cat("oops"), file, update = TRUE))
expect_success(expect_output_file(cat("oops"), file))
})

test_that("expect_output_file warns if file missing, but can update it", {
file <- tempfile()
expect_warning(expect_failure(expect_output_file(cat("oops"), file)))
expect_false(file.exists(file))
expect_warning(expect_failure(expect_output_file(cat("oops"), file, update = TRUE)))
expect_success(expect_output_file(cat("oops"), file))
})
6 changes: 6 additions & 0 deletions tests/testthat/width-80.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
[19] 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
[37] 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
[55] 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
[73] 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
[91] 91 92 93 94 95 96 97 98 99 100

0 comments on commit db9a22c

Please sign in to comment.