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