Skip to content

Commit

Permalink
Merge branch 'feature/78-knitr-test'. Closes #78.
Browse files Browse the repository at this point in the history
- Don't rely on `knitr` internals for testing (#78).
  • Loading branch information
Kirill Müller committed May 17, 2016
2 parents 9040a60 + 6a3552d commit 19235d2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
13 changes: 13 additions & 0 deletions tests/testthat/helper-output.R
Expand Up @@ -3,3 +3,16 @@ output_file <- function(filename) file.path("output", filename)
expect_output_file_rel <- function(x, filename) {
expect_output_file(x, output_file(filename), update = TRUE)
}

expect_output_knit <- function(knit, filename, envir = parent.frame()) {
expect_asis_output_with_cacheable(substitute(knit), envir)
expect_output_file_rel(cat(knit), filename)
}

expect_asis_output_with_cacheable <- function(knit_call, envir) {
asis_output_args <- with_mock(
`knitr::asis_output` = function(...) list(...),
eval(knit_call, envir = envir)
)
expect_true(asis_output_args[["cacheable"]])
}
21 changes: 6 additions & 15 deletions tests/testthat/test-trunc-mat.R
Expand Up @@ -33,24 +33,15 @@ test_that("trunc_mat output matches known output", {
print(trunc_mat(df_all, n = 1L, n_extra = 2L, width = 30L)),
"trunc_mat/all-1-30-2.txt")

knit <- knitr::knit_print(trunc_mat(df_all, width = 60L))
expect_output_file_rel(
cat(knit),
expect_output_knit(
knitr::knit_print(trunc_mat(df_all, width = 60L)),
"trunc_mat/all-knit-60.txt")
expect_is(knit, "knit_asis")
expect_true(attr(knit, "knit_cacheable"))

knit <- knitr::knit_print(trunc_mat(df_all, width = 120L))
expect_output_file_rel(
cat(knit),
expect_output_knit(
knitr::knit_print(trunc_mat(df_all, width = 120L)),
"trunc_mat/all-knit-120.txt")
expect_is(knit, "knit_asis")
expect_true(attr(knit, "knit_cacheable"))

knit <- knitr::knit_print(trunc_mat(mtcars, width = 60L))
expect_output_file_rel(
cat(knit),
expect_output_knit(
knitr::knit_print(trunc_mat(mtcars, width = 60L)),
"trunc_mat/mtcars-knit-60.txt")
expect_is(knit, "knit_asis")
expect_true(attr(knit, "knit_cacheable"))
})

0 comments on commit 19235d2

Please sign in to comment.