Skip to content

Commit

Permalink
Better expect_output checks for NA and NULL.
Browse files Browse the repository at this point in the history
Fixes #323.
  • Loading branch information
hadley committed Feb 18, 2016
1 parent 3fa7869 commit 9ae7c4f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# testthat 0.11.0.9000

* `expect_output(f(), NA)` will fail if `f()` produces output;
`expect_output(f(), NULL)` will fail if `f()` doesn't produce output (#323).

* `expect_output()` no longer prints the output by default. You need to do this
yourself.

* `expect_type()` checks the _type_ of the object (#316).

* `not()` has been deprecated.
Expand Down
20 changes: 13 additions & 7 deletions R/expectations-matches.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ matches <- function(regexp, all = TRUE, ...) {

#' @export
#' @rdname matching-expectations
expect_output <- function(object, regexp, ..., info = NULL, label = NULL) {
expect_output <- function(object, regexp = NULL, ..., info = NULL, label = NULL) {
if (is.null(label)) {
label <- find_expr("object")
}
Expand All @@ -104,17 +104,23 @@ expect_output <- function(object, regexp, ..., info = NULL, label = NULL) {
#' @rdname oldskool
prints_text <- function(regexp, ...) {
function(expr) {
output <- evaluate_promise(expr, print = TRUE)$output
output <- evaluate_promise(expr)$output

if (identical(regexp, NA)) {
return(expectation(
!is.null(output),
expectation(
identical(output, ""),
paste0("produced output: ", encodeString(output)),
"didn't produce output"
))
)
} else if (is.null(regexp)) {
expectation(
!identical(output, ""),
"didn't produce output",
"produced output"
)
} else {
matches(regexp, ...)(output)
}

matches(regexp, ...)(output)
}
}

Expand Down
1 change: 1 addition & 0 deletions R/test-that.r
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ test_that <- function(desc, code) {
}



# Executes a test.
#
# @keywords internal
Expand Down
2 changes: 1 addition & 1 deletion man/matching-expectations.Rd

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

4 changes: 0 additions & 4 deletions tests/testthat/test-expectations.r
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@ test_that("expect_output captures multiline code", {
expect_output(cat("1\n2"), "1\n2")
})

test_that("expect_output prints by default", {
expect_output(1, "1")
})

test_that("expect_equals fails with useful message if objects equal but not identical", {
f <- function() x
g <- function() x
Expand Down

0 comments on commit 9ae7c4f

Please sign in to comment.