| 1 |
test_that("all elements can be NULL", { |
| 2 |
out_fmt <- output_format( |
| 3 |
knitr = NULL, pandoc = NULL, keep_md = NULL, clean_supporting = NULL |
| 4 |
) |
| 5 |
lapply(out_fmt, expect_null) |
| 6 |
}) |
| 7 |
|
| 8 |
test_that("inherits base format", { |
| 9 |
base_fmt <- html_document() |
| 10 |
base_fmt$file_scope <- identity |
| 11 |
out_fmt <- output_format( |
| 12 |
knitr = NULL, pandoc = NULL, keep_md = NULL, clean_supporting = NULL, |
| 13 |
base_format = base_fmt |
| 14 |
) |
| 15 |
classes <- lapply(base_fmt, class) |
| 16 |
expect_identical(lapply(out_fmt, class), classes) |
| 17 |
not_fun <- vapply(classes, function(x) !"function" %in% x, NA) |
| 18 |
expect_identical(out_fmt[not_fun], base_fmt[not_fun]) |
| 19 |
}) |
| 20 |
|
| 21 |
test_that("file_scope replaces base format", { |
| 22 |
base_fmt <- html_document() |
| 23 |
base_fmt$file_scope <- function(input_file, file_scope, ...) { |
| 24 |
return(list("foo" = "foo")) |
| 25 |
} |
| 26 |
out_fmt <- output_format( |
| 27 |
knitr = NULL, pandoc = NULL, keep_md = NULL, clean_supporting = NULL, |
| 28 |
file_scope = function(input_file, file_scope, ...) { |
| 29 |
file_scope$bar <- "bar" |
| 30 |
return(file_scope) |
| 31 |
}, |
| 32 |
base_format = base_fmt |
| 33 |
) |
| 34 |
expect_identical(out_fmt$file_scope("input", NULL), |
| 35 |
list("foo" = "foo", "bar" = "bar")) |
| 36 |
}) |
| 37 |
|
| 38 |
test_that("clean_supporting is coerced to FALSE only if keep_md is TRUE", { |
| 39 |
args_combinations <- expand.grid( |
| 40 |
knitr = list(NULL), |
| 41 |
pandoc = list(NULL), |
| 42 |
keep_md = list(TRUE, FALSE, NULL), |
| 43 |
clean_supporting = list(TRUE, FALSE, NULL) |
| 44 |
) |
| 45 |
|
| 46 |
results <- do.call( |
| 47 |
function(...) Map(function(...) output_format(...)$clean_supporting, ...), |
| 48 |
args_combinations |
| 49 |
) |
| 50 |
|
| 51 |
expected <- ifelse( |
| 52 |
vapply(args_combinations$keep_md, isTRUE, NA), |
| 53 |
FALSE, |
| 54 |
args_combinations$clean_supporting |
| 55 |
) |
| 56 |
|
| 57 |
expect_identical(results, expected) |
| 58 |
}) |
| 59 |
|
| 60 |
test_that("Keep only args and Lua filter while merging pandoc options", { |
| 61 |
# non default opt |
| 62 |
foo_opt <- pandoc_options( |
| 63 |
to = "foo", from = "init", args = "--foo", |
| 64 |
ext = "foo", keep_tex = TRUE, latex_engine = "xelatex", |
| 65 |
lua_filters = "foo.lua") |
| 66 |
bar_opt <- pandoc_options(to = "bar") |
| 67 |
res_opt <- merge_pandoc_options(foo_opt, bar_opt) |
| 68 |
# all options are merged except these |
| 69 |
kept <- c("args", "lua_filters") |
| 70 |
expect_equal(res_opt[!names(res_opt) %in% kept], |
| 71 |
bar_opt[!names(bar_opt) %in% kept]) |
| 72 |
expect_equal(res_opt[kept], foo_opt[kept]) |
| 73 |
# Instead they are appended |
| 74 |
bar_opt$args <- "--another" |
| 75 |
bar_opt$lua_filters <- "bar.lua" |
| 76 |
res_opt <- merge_pandoc_options(foo_opt, bar_opt) |
| 77 |
lapply(kept, function(x) expect_equal(res_opt[[x]], c(foo_opt[[x]], bar_opt[[x]]))) |
| 78 |
}) |
| 79 |
|
| 80 |
test_that("citeproc is enable/disable correctly", { |
| 81 |
test_citeproc_not_required <- function(yml, lines = NULL) { |
| 82 |
expect_false(citeproc_required(!!yml, !!lines)) |
| 83 |
} |
| 84 |
test_citeproc_required <- function(yml, lines = NULL) { |
| 85 |
expect_true(citeproc_required(!!yml, !!lines)) |
| 86 |
} |
| 87 |
test_citeproc_not_required(list(title = "dummy")) |
| 88 |
test_citeproc_not_required(list(title = "dummy", citeproc = TRUE)) |
| 89 |
test_citeproc_required(list(bibliography = "a.bib")) |
| 90 |
test_citeproc_not_required(list(bibliography = "a.bib", citeproc = FALSE)) |
| 91 |
test_citeproc_required(list(references = "a.bib")) |
| 92 |
test_citeproc_not_required(list(references = "a.bib", citeproc = FALSE)) |
| 93 |
test_citeproc_required(list(references = list(type = "book"))) |
| 94 |
test_citeproc_not_required(list(references = list(type = "book"), citeproc = FALSE)) |
| 95 |
test_citeproc_required(list(), c("references:", " - type: book")) |
| 96 |
test_citeproc_not_required(list(citeproc = FALSE), c("references:", " - type: book")) |
| 97 |
test_citeproc_required(list(), c("bibliography:", " - a.bib")) |
| 98 |
test_citeproc_not_required(list(citeproc = FALSE), c("bibliography:", " - a.bib")) |
| 99 |
test_citeproc_not_required(list(title = "dummy"), c("bibliography: a.bib")) |
| 100 |
}) |
| 101 |
|
| 102 |
test_that("default output_format is guessed from output file extension", { |
| 103 |
expect_equal(output_format_string_from_ext("test.pdf"), "pdf_document") |
| 104 |
expect_equal(output_format_string_from_ext("test.html"), "html_document") |
| 105 |
expect_equal(output_format_string_from_ext("test.docx"), "word_document") |
| 106 |
expect_equal(output_format_string_from_ext("test.any"), "html_document") |
| 107 |
expect_equal(output_format_string_from_ext(NULL), "html_document") |
| 108 |
}) |
| 109 |
|
| 110 |
test_that("output format dependency has no effect on merge if it is empty", { |
| 111 |
fmt <- html_document_base() |
| 112 |
dep <- output_format_dependency("empty") |
| 113 |
merged <- merge_output_format_dependencies(fmt, list(dep)) |
| 114 |
testthat::expect_named(merged, names(fmt)) |
| 115 |
fmt$on_exit <- merged$on_exit |
| 116 |
expect_identical(fmt, merged) |
| 117 |
}) |
| 118 |
|
| 119 |
test_that("output format dependencies can be merged", { |
| 120 |
empty_format <- output_format( |
| 121 |
knitr = NULL, pandoc = NULL, keep_md = NULL, clean_supporting = NULL, |
| 122 |
pre_processor = NULL, post_processor = NULL, |
| 123 |
file_scope = NULL, on_exit = NULL |
| 124 |
) |
| 125 |
|
| 126 |
f1 <- function(...) cat("d1") |
| 127 |
f2 <- function(...) cat("d2") |
| 128 |
d1 <- output_format_dependency( |
| 129 |
name = "d1", |
| 130 |
pandoc = list(to = "html", lua_filters = "d1.lua"), |
| 131 |
pre_processor = f1, |
| 132 |
post_processor = f1, |
| 133 |
file_scope = f1, |
| 134 |
on_exit = f1 |
| 135 |
) |
| 136 |
d2 <- output_format_dependency( |
| 137 |
name = "d2", |
| 138 |
pandoc = list(to = "markdown", lua_filters = "d2.lua"), |
| 139 |
pre_processor = f2, |
| 140 |
post_processor = f2, |
| 141 |
file_scope = f2, |
| 142 |
on_exit = f2 |
| 143 |
) |
| 144 |
|
| 145 |
m1 <- merge_output_format_dependencies(empty_format, list(d1, d2)) |
| 146 |
m2 <- merge_output_format_dependencies(empty_format, list(d1, d2, d2)) |
| 147 |
expect_identical( |
| 148 |
m1$pandoc, list(to = "markdown", lua_filters = c("d1.lua", "d2.lua")) |
| 149 |
) |
| 150 |
expect_equal(m1, m2) |
| 151 |
|
| 152 |
expect_cat <- function(obj, expected) { |
| 153 |
expect_identical(capture.output(obj), expected) |
| 154 |
} |
| 155 |
expect_cat(invisible(m1$pre_processor()), "d1d2") |
| 156 |
expect_cat(m1$post_processor(output_file = "example"), "d2d1") |
| 157 |
expect_cat(m1$file_scope(), "d1d2") |
| 158 |
expect_cat(m1$on_exit(), "d1d2") |
| 159 |
}) |
| 160 |
|
| 161 |
test_that("output format dependencies are merged once per name", { |
| 162 |
fmt <- html_document_base() |
| 163 |
d1 <- output_format_dependency("d1", pandoc = list(lua_filters = "d1.lua")) |
| 164 |
d2 <- output_format_dependency("d2", pandoc = list(lua_filters = "d2.lua")) |
| 165 |
expect_equal( |
| 166 |
merge_output_format_dependencies(fmt, list(d1, d2)), |
| 167 |
merge_output_format_dependencies(fmt, list(d1, d1, d2, d1, d2)) |
| 168 |
) |
| 169 |
}) |