Debian Codegraph @

tests/test-cran/test-knitr.R

raw
1 library(testit)
2
3 assert('csv_options() parses chunk options to a list', {
4 (csv_options('a-s-d,b=TRUE,c=def') %==% alist(label = 'a-s-d', b = TRUE, c = def))
5 (csv_options('a,opt=c(1,3,5)') %==% alist(label = 'a', opt = c(1, 3, 5)))
6 (csv_options('label="xx",opt=zz') %==% alist(label = 'xx', opt = zz))
7 (csv_options('label=foo') %==% alist(label = 'foo'))
8 (csv_options('a,b=2,c="qwer",asdf="efg"') %==%
9 alist(label = 'a', b = 2, c = 'qwer', asdf = 'efg'))
10 (csv_options('2a') %==% alist(label = '2a'))
11 (csv_options('abc-function,fig.path="foo/bar-"') %==%
12 alist(label = 'abc-function', fig.path = "foo/bar-"))
13 (has_error(csv_options('a,b')))
14 (has_error(csv_options('a,b,c=qwer')))
15 # empty options string: returns an empty list (no label)
16 (length(csv_options('')) %==% 0L)
17 # trailing comma: empty option is removed, only named args kept
18 res = csv_options('a, b=1,')
19 (res$label %==% 'a')
20 (res$b %==% 1)
21 (length(res) %==% 2L)
22 # invalid R syntax in options triggers error with a message
23 (has_error(csv_options('a=1+')))
24 })
25
26 assert('divide_chunk() parses YAML-style chunk options', {
27 yaml_like = c('#| label: mine', '#| echo: true', '#| fig.width: 8', '1 + 1')
28 res = divide_chunk('r', yaml_like, use_yaml = FALSE)
29 (res$options$label %==% 'mine')
30 (isTRUE(res$options$echo))
31 (res$options$fig.width %==% 8L)
32 (res$code %==% '1 + 1')
33 })
34
35 assert('divide_chunk() parses CSV-style chunk options', {
36 csv_like = c("#| mine, echo = TRUE, fig.width = 8", "1 + 1")
37 res = divide_chunk('r', csv_like)
38 (res$options$label %==% 'mine')
39 (isTRUE(res$options$echo))
40 (res$options$fig.width %==% 8)
41 (res$code %==% '1 + 1')
42 })
43
44 assert('divide_chunk() returns empty options for empty code', {
45 res = divide_chunk('r', character(0))
46 (is.null(res$options))
47 (length(res$code) %==% 0L)
48 })
49
50 assert('divide_chunk() returns code unchanged when no option comments', {
51 code = c('x = 1', 'y = 2')
52 res = divide_chunk('r', code)
53 (is.null(res$options))
54 (res$code %==% code)
55 })
56
57 assert('divide_chunk() falls back to #| for non-R engines', {
58 # js uses //| but falls back to #| when strict=FALSE
59 js_like = c('#| echo: true', 'console.log(1)')
60 res = divide_chunk('js', js_like, use_yaml = FALSE)
61 (isTRUE(res$options$echo))
62 (res$code %==% 'console.log(1)')
63 # with strict=TRUE, #| for a non-# engine is an error
64 (has_error(divide_chunk('js', js_like, strict = TRUE, use_yaml = FALSE)))
65 })
66
67 assert('divide_chunk() handles block-comment engines (s2 != empty)', {
68 # C engine uses /*| */ style: s2 != ''
69 c_like = c('/*| echo: true */', 'int x = 1;')
70 res = divide_chunk('c', c_like, use_yaml = FALSE)
71 (isTRUE(res$options$echo))
72 (res$code %==% 'int x = 1;')
73 })
74
75 assert('divide_chunk() strips leading blank line from code', {
76 # a blank line immediately after the options block is removed from code
77 yaml_like = c('#| echo: true', '', '1 + 1')
78 res = divide_chunk('r', yaml_like, use_yaml = FALSE)
79 (isTRUE(res$options$echo))
80 (res$code %==% '1 + 1') # blank line stripped
81 (length(res$src) %==% 2L) # blank line moved to src
82 })
83
84 assert('divide_chunk() when all lines are option comments', {
85 # all lines match #| so n2 = length(code)
86 all_opts = c('#| echo: true', '#| eval: false')
87 res = divide_chunk('r', all_opts, use_yaml = FALSE)
88 (isTRUE(res$options$echo))
89 (isFALSE(res$options$eval))
90 (length(res$code) %==% 0L)
91 })
92
93 assert('divide_chunk() normalizes id field to label', {
94 yaml_like = c('#| id: my-chunk', '1 + 1')
95 res = divide_chunk('r', yaml_like, use_yaml = FALSE)
96 (res$options$label %==% 'my-chunk')
97 (is.null(res$options$id))
98 })