| 1 |
library(testit) |
| 2 |
|
| 3 |
opts_knit$set(out.format = 'markdown') |
| 4 |
assert('parse_block() parses the language engine from ```{lang}', { |
| 5 |
(parse_block(NULL, '', 'r, foo, a=1,')$params %==% alist(label = 'foo', a = 1)) |
| 6 |
(parse_block(NULL, '', 'Rcpp, foo, a=1,')$params %==% alist(label = 'foo', a = 1, engine = 'Rcpp')) |
| 7 |
}) |
| 8 |
|
| 9 |
res = split_file( |
| 10 |
c('abc', '```{r foo}', '1+1', '```{r bar}', '2+2', '```', 'def'), |
| 11 |
patterns = all_patterns$md |
| 12 |
) |
| 13 |
assert('split_file() treats ``` as part of code chunk instead of beginning of text chunk', { |
| 14 |
# the foo chunk does not have a closing mark |
| 15 |
(c(knit_code$get('foo')) %==% '1+1') # use c() to drop attributes |
| 16 |
(c(knit_code$get('bar')) %==% '2+2') |
| 17 |
# before knitr v1.6, the text chunk was c('', 'def') |
| 18 |
(res[[4]][['input']] %==% 'def') |
| 19 |
}) |
| 20 |
opts_knit$restore() |
| 21 |
knit_code$restore(); knit_concord$restore() |
| 22 |
|
| 23 |
res = parse_inline(c('aaa \\Sexpr{x}', 'bbb \\Sexpr{NA} and \\Sexpr{1+2}', |
| 24 |
'another expression \\Sexpr{rnorm(10)}'), all_patterns$rnw) |
| 25 |
assert('parse_inline() parses inline text', { |
| 26 |
(res$code %==% c('x', 'NA', '1+2', 'rnorm(10)')) |
| 27 |
(nchar(res$input) %==% 81L) |
| 28 |
# empty inline code is not recognized |
| 29 |
(parse_inline('\\Sexpr{}', all_patterns$rnw)$code %==% character(0)) |
| 30 |
# can use > in HTML inline code |
| 31 |
(parse_inline('<!--rinline "<a>" -->', all_patterns$html)$code %==% ' "<a>" ') |
| 32 |
}) |
| 33 |
|
| 34 |
res = parse_inline('inline expressions `r pi+1`, +r cos(0)+ in AsciiDoc', |
| 35 |
all_patterns$asciidoc) |
| 36 |
assert('both `r expression` and +r expression+ work for AsciiDoc', { |
| 37 |
(res$code %==% c('pi+1', 'cos(0)')) |
| 38 |
}) |
| 39 |
|
| 40 |
knit_code$restore() |
| 41 |
|
| 42 |
read_chunk(lines = c('1+1')) |
| 43 |
assert('read_chunk() does not discard code without chunk headers', { |
| 44 |
(knit_code$get() %==% list('unnamed-chunk-1' = '1+1')) |
| 45 |
}) |
| 46 |
|
| 47 |
knit_code$restore() |
| 48 |
|
| 49 |
read_chunk(lines = c('# ---- foo ----', '1+1')) |
| 50 |
assert('read_chunk() can identify chunk labels', { |
| 51 |
(knit_code$get() %==% list(foo = '1+1')) |
| 52 |
}) |
| 53 |
|
| 54 |
knit_code$restore() |
| 55 |
|
| 56 |
# chunk references with <<>> -------------------------------------------------- |
| 57 |
|
| 58 |
knit_code$restore(list( |
| 59 |
a = '1+1', b = '2-2', c = c('if (T)', ' <<a>>'), d = c('function() {', ' <<c>>', '}') |
| 60 |
)) |
| 61 |
pc = function(x) parse_chunk(x, all_patterns$rnw$ref.chunk) |
| 62 |
|
| 63 |
assert('parse_chunk() preserves indentation', { |
| 64 |
(pc(c('3*3', '<<a>>', ' <<b>>', 'if (T)', ' <<a>>')) %==% c("3*3", "1+1", " 2-2", "if (T)", " 1+1" )) |
| 65 |
(pc('<<d>>') %==% c('function() {', ' if (T)', ' 1+1', '}')) |
| 66 |
}) |
| 67 |
|
| 68 |
assert('parse_chunk() ignores labels not found in knit_code', { |
| 69 |
# chunk 'e' doesn't exist |
| 70 |
(pc(c('3*3', '<<a>>', ' <<e>>', '<<b>> ')) %==% c("3*3", "1+1", " <<e>>", "2-2")) |
| 71 |
}) |
| 72 |
|
| 73 |
knit_code$restore() |
| 74 |
|
| 75 |
# duplication of labels |
| 76 |
|
| 77 |
knit_code$restore(list(a = '1+1')) |
| 78 |
|
| 79 |
assert('duplicated labels are not allowed by default', { |
| 80 |
(!has_error(parse_block(NULL, '', 'label = "a"'))) |
| 81 |
(has_error(parse_block('2+2', '', 'label = "a"'))) |
| 82 |
(has_error(parse_block(NULL, '','label = "a", code = "2+2"'))) |
| 83 |
(has_error(parse_block(NULL, '','label = "a", file = "dummy.R"'))) |
| 84 |
}) |
| 85 |
op = options(knitr.duplicate.label = 'allow') |
| 86 |
assert('duplicated labels are allowed after setting an option', { |
| 87 |
(!has_error(parse_block('2+2', '', 'label = "a"'))) |
| 88 |
(!has_error(parse_block(NULL, '','label = "a", code = "2+2"'))) |
| 89 |
(!has_error(parse_block(NULL, '','label = "a", file = "dummy.R"'))) |
| 90 |
}) |
| 91 |
options(op) |
| 92 |
|
| 93 |
knit_code$restore() |