| 1 |
library(testit) |
| 2 |
|
| 3 |
res = mapply( |
| 4 |
auto_out_name, |
| 5 |
c('abc.Rnw', 'abc.rnw', 'abc.rtex', 'abc.Rmd', 'abc.rhtm', 'abc.Rhtml', 'foo.abc.rhtml'), |
| 6 |
USE.NAMES = FALSE |
| 7 |
) |
| 8 |
assert('auto_out_name() converts .Rfoo to .foo', { |
| 9 |
(res %==% c('abc.tex', 'abc.tex', 'abc.tex', 'abc.md', 'abc.htm', 'abc.html', 'foo.abc.html')) |
| 10 |
}) |
| 11 |
|
| 12 |
res = mapply( |
| 13 |
auto_out_name, |
| 14 |
c('abc.tex', '_knit_abc.tex', '_knit_abc.md', 'foo_knit_.html'), |
| 15 |
USE.NAMES = FALSE |
| 16 |
) |
| 17 |
assert('auto_out_name() converts .tex/.unknown to .txt, and removes _knit_', { |
| 18 |
(res %==% c('abc.txt', 'abc.tex', 'abc.md', 'foo.html')) |
| 19 |
(auto_out_name('foo.bar') %==% 'foo.txt') |
| 20 |
}) |
| 21 |
|
| 22 |
assert('chunks with include=FALSE should stop on error', { |
| 23 |
(suppressMessages( |
| 24 |
has_error(knit(text = c('<<include=F>>=', '1+"a"', '@'), quiet = TRUE)) |
| 25 |
)) |
| 26 |
}) |
| 27 |
|
| 28 |
assert('tidy=FALSE + eval=numeric should work', { |
| 29 |
('[1] 1\n[1] 2\n' %==% knit( |
| 30 |
text = c('<<tidy=FALSE, eval=1:2, echo=FALSE, results="asis">>=', '1', '1+', '1', '1', '@'), |
| 31 |
quiet = TRUE |
| 32 |
)) |
| 33 |
}) |
| 34 |
|
| 35 |
assert('opts_template options are used', { |
| 36 |
("\n[1] 2\n" %==% knit( |
| 37 |
text = c( |
| 38 |
"<<include=FALSE>>=","opts_template$set(quiet = list(echo=FALSE))", "@", |
| 39 |
"<<results='asis', opts.label='quiet'>>=", "1+1", "@" |
| 40 |
), |
| 41 |
quiet = TRUE |
| 42 |
)) |
| 43 |
}) |
| 44 |
|
| 45 |
assert('local chunk options override opts_template', { |
| 46 |
("\n\\begin{kframe}\n\\begin{alltt}\n\\hlnum{1}\\hlopt{+}\\hlnum{1}\n\\end{alltt}\n\\end{kframe}[1] 2\n" %==% knit( |
| 47 |
text = c( |
| 48 |
"<<include=FALSE>>=","opts_template$set(quiet = list(echo=FALSE))", "@", |
| 49 |
"<<results='asis', opts.label='quiet', echo=TRUE, tidy=FALSE>>=", "1+1", "@" |
| 50 |
), |
| 51 |
quiet = TRUE |
| 52 |
)) |
| 53 |
}) |
| 54 |
|
| 55 |
# a shortcut |
| 56 |
k = function(text) { |
| 57 |
on.exit(opts_chunk$restore()) |
| 58 |
knit(text = c('```{r}', text, '```'), quiet = TRUE, envir = parent.frame()) |
| 59 |
} |
| 60 |
k(c( |
| 61 |
'knit_child(text = "```{r}\nopts_chunk$set(echo=FALSE)\n```", options=list())', |
| 62 |
'x1 = opts_chunk$get("echo")' |
| 63 |
)) |
| 64 |
k(c( |
| 65 |
'knit_child(text = "```{r}\nopts_chunk$set(dpi=200)\n```", options=list(dpi=100))', |
| 66 |
'x2 = opts_chunk$get("dpi")' |
| 67 |
)) |
| 68 |
|
| 69 |
assert('using knit_child() does not reset global chunk options set in child documents', { |
| 70 |
(x1 %==% FALSE) |
| 71 |
(x2 %==% 200) |
| 72 |
}) |
| 73 |
|
| 74 |
txt = '%\\documentclass{article} |
| 75 |
\\documentclass{article} |
| 76 |
\\begin{document} |
| 77 |
\\Sexpr{pi} |
| 78 |
\\end{document}' |
| 79 |
res = strsplit(knit(text = txt, quiet = TRUE), '\n')[[1]] |
| 80 |
|
| 81 |
assert('insert_header_latex() finds the correct \\documentclass{}', { |
| 82 |
(res[1] %==% '%\\documentclass{article}') |
| 83 |
}) |
| 84 |
|
| 85 |
txt = '\\documentclass{article} |
| 86 |
\\begin{document} |
| 87 |
\\begin{verbatim} |
| 88 |
\\documentclass{article} |
| 89 |
\\end{verbatim} |
| 90 |
\\Sexpr{pi} |
| 91 |
\\end{document}' |
| 92 |
res = strsplit(knit(text = txt, quiet = TRUE), '\n')[[1]] |
| 93 |
|
| 94 |
assert('insert_header_latex() finds the correct \\documentclass{}', { |
| 95 |
(res[length(res) - 3] %==% '\\documentclass{article}') |
| 96 |
}) |
| 97 |
|
| 98 |
assert('knit_meta_add() adds meta objects with the correct number of labels', { |
| 99 |
knit_meta(clean = TRUE) |
| 100 |
knit_meta_add(list(1, 2)) |
| 101 |
m = attr(knit_meta_add(list(3, 4), label = c('a', 'b')), 'knit_meta_id') |
| 102 |
knit_meta(clean = TRUE) |
| 103 |
(m %==% c('', '', 'a', 'b')) |
| 104 |
}) |
| 105 |
|
| 106 |
local({ |
| 107 |
out = NULL |
| 108 |
hnd = function(cnd) out <<- cnd |
| 109 |
|
| 110 |
on.exit(file.remove('knit-handlers.md')) |
| 111 |
knit('knit-handlers.Rmd', quiet = TRUE) |
| 112 |
|
| 113 |
assert('knit() passes calling handlers to evaluate()', { |
| 114 |
(inherits(out, 'error')) |
| 115 |
}) |
| 116 |
}) |