Debian Codegraph @

tests/test-cran/test-yaml.R

raw
1 library(testit)
2
3 yaml = '
4 a: 1
5 b: [1, 2, 3]
6 c: true
7 d:
8 e: !expr 1+1
9 f: null
10 '
11
12 yaml_long_indent = '
13 a: 1
14 b: [1, 2, 3]
15 c: true
16 d:
17 e: !expr 1+1
18 f: null
19 '
20
21
22 yaml_tabs = '
23 a: 1
24 b: [1, 2, 3]
25 c: true
26 d:
27 \te: !expr 1+1
28 \tf: null
29 '
30
31 # see https://github.com/yihui/xfun/issues/94
32 yaml_mre = '
33 output:
34 latex:
35 latex_engine: pdflatex
36 options:
37 toc: true
38 '
39
40
41 if (loadable('yaml')) assert('yaml_load() works with the yaml package', {
42 (yaml_load(yaml) %==% list(a = 1L, b = 1:3, c = TRUE, d = list(e = 2, f = NULL)))
43 (yaml_load(yaml, envir = FALSE)[[c('d', 'e')]] %==% expression(1 + 1))
44 f = function() {
45 foo = 1:10
46 yaml_load('a: !expr head(foo, 4)')
47 }
48 (f() %==% list(a = 1:4))
49 })
50
51 assert('taml_load() works', {
52 (taml_load(yaml) %==% list(a = 1L, b = 1:3, c = TRUE, d = list(e = 2, f = NULL)))
53 (taml_load(yaml, envir = FALSE)[[c('d', 'e')]] %==% expression(1 + 1))
54 f = function() {
55 foo = 1:10
56 taml_load('a: !expr head(foo, 4)')
57 }
58 (f() %==% list(a = 1:4))
59 res = list(a = c('A', 'B', 'C D'))
60 (taml_load('a: [A, B, C D]') %==% res)
61 (taml_load('a: ["A", \'B\', "C D"]') %==% res)
62 (taml_load("a: ['A', 'B', 'C D']") %==% res)
63 })
64
65 assert('taml_load() works with variable indent', {
66 (taml_load(yaml_long_indent) %==% list(a = 1L, b = 1:3, c = TRUE, d = list(e = 2, f = NULL)))
67 (taml_load(yaml_long_indent, envir = FALSE)[[c('d', 'e')]] %==% expression(1 + 1))
68 })
69
70 assert('taml_load() works with tabs indent', {
71 (taml_load(yaml_tabs) %==% list(a = 1L, b = 1:3, c = TRUE, d = list(e = 2, f = NULL)))
72 (taml_load(yaml_tabs, envir = FALSE)[[c('d', 'e')]] %==% expression(1 + 1))
73 })
74
75 assert('taml_load() works with non-uniform indent (varying indent sizes)', {
76 x = 'runs:
77 steps:
78 name: Install R
79 with:
80 use-public-rspm: true'
81 res = taml_load(x)
82 (res %==% list(runs = list(steps = list(
83 name = 'Install R', with = list(`use-public-rspm` = TRUE)
84 ))))
85 })
86
87 assert('taml_load() ignores array items (- ) and their indented children', {
88 # simple block sequence: items with no colon (already filtered by regex)
89 (taml_load('a:\n - b\n - c') %==% list(a = list()))
90 # block sequence items with sub-keys must also be ignored
91 x = 'runs:\n using: composite\n steps:\n - name: foo\n uses: bar\n with:\n key: val\n - name: baz\n'
92 res = taml_load(x)
93 (res %==% list(runs = list(using = 'composite', steps = list())))
94 # action.yml-style: inputs are kept, steps array items are dropped
95 action_yml = '
96 name: Build site
97 description: Build a site.
98 inputs:
99 site-dir:
100 description: Directory of the site source
101 default: site
102 runs:
103 using: composite
104 steps:
105 - name: Install R
106 uses: r-lib/actions/setup-r@HEAD
107 with:
108 use-public-rspm: true
109 - name: Build site
110 shell: bash
111 run: Rscript -e "litedown::fuse_site()"
112 '
113 res = taml_load(action_yml)
114 (res$name %==% 'Build site')
115 (res$inputs[['site-dir']]$default %==% 'site')
116 (res$runs$using %==% 'composite')
117 (res$runs$steps %==% list()) # array items ignored
118 })
119
120 assert('yaml_load() works with a complex output', {
121 expected = list(
122 output = list(
123 latex = list(
124 latex_engine = "pdflatex", options = list(toc = TRUE)
125 )
126 )
127 )
128
129 (yaml_load(yaml_mre) %==% expected)
130 (yaml_load(yaml_mre, use_yaml = FALSE) %==% expected)
131 })
132
133 assert('taml_load() handles empty input', {
134 (taml_load('') %==% list())
135 (taml_load(character(0)) %==% list())
136 (taml_load('# comment only') %==% list())
137 })
138
139 assert('taml_load() handles null value in nested key', {
140 # tests null value handling in nested keys
141 x = 'a:\n b: null\n c: 1'
142 res = taml_load(x)
143 (is.null(res[['a']][['b']]))
144 (res[['a']][['c']] %==% 1L)
145 # top-level null
146 (taml_load('a: null') %==% list(a = NULL))
147 })
148
149 assert('yaml_value() returns float for non-integer numerics', {
150 res = taml_load('a: 1.5')
151 (res %==% list(a = 1.5))
152 (is.double(res$a))
153 # empty array [] returns NULL (yaml_value line 156)
154 (taml_load('a: []') %==% list(a = NULL))
155 })
156
157 assert('taml_file() reads YAML from a file', {
158 f = tempfile()
159 writeLines('a: 1\nb: foo', f)
160 res = taml_file(f)
161 (res %==% list(a = 1L, b = 'foo'))
162 unlink(f)
163 })
164
165 assert('taml_save() converts a list to YAML', {
166 res = list(a = 1L, b = 'foo', c = TRUE, d = list(e = 2L, f = NULL))
167 yaml_str = as.character(taml_save(res))
168 (any(grepl('a: 1', yaml_str)))
169 (any(grepl('b: "foo"', yaml_str)))
170 (any(grepl('c: true', yaml_str)))
171 (any(grepl('f: null', yaml_str)))
172 # round-trip
173 (taml_load(yaml_str) %==% res)
174 })
175
176 assert('taml_save() supports custom indent', {
177 res = list(a = list(b = 1L))
178 yaml_str = as.character(taml_save(res, indent = '\t'))
179 (grepl('\t', yaml_str))
180 })
181
182 assert('taml_save() errors on unsupported types', {
183 (has_error(taml_save(list(a = list(1, 2))))) # unnamed sub-list
184 (has_error(taml_save(list(a = raw(1))))) # unsupported raw type
185 })
186
187 assert('taml_save() handles expressions and arrays', {
188 # expression value
189 yaml_expr = as.character(taml_save(list(a = expression(1 + 1))))
190 (any(grepl('!expr', yaml_expr)))
191 # vector value -> array syntax
192 yaml_arr = as.character(taml_save(list(a = c(1L, 2L, 3L))))
193 (any(grepl('^a: \\[', yaml_arr)))
194 # AsIs scalar -> still array
195 yaml_asis = as.character(taml_save(list(a = I(1L))))
196 (any(grepl('^a: \\[', yaml_asis)))
197 })
198
199 assert('taml_save() writes to a file when path is provided', {
200 res = list(a = 1L, b = 'foo')
201 f = tempfile()
202 taml_save(res, path = f)
203 (file.exists(f))
204 (taml_load(read_utf8(f)) %==% res)
205 unlink(f)
206 })
207
208 assert('yaml_bool() only matches exact boolean words (regression: ^true|false|na$ was parsed wrong)', {
209 # bug: '^true|false|na$' is '(^true)|(false)|(na$)', so 'extrafalse' matched 'false'
210 (yaml_bool('true'))
211 (yaml_bool('false'))
212 (yaml_bool('na'))
213 (!yaml_bool('extrafalse')) # contains 'false' but is not 'false'
214 (!yaml_bool('trueblue')) # starts with 'true' but is not 'true'
215 (!yaml_bool('final')) # ends in 'na' but is not 'na'
216 (!yaml_bool('banana')) # contains 'na' but is not 'na'
217 # vector: all elements must be boolean words
218 (yaml_bool(c('true', 'false', 'na')))
219 (!yaml_bool(c('true', 'maybe')))
220 # taml_load must not treat 'extrafalse' as boolean
221 (taml_load('key: extrafalse') %==% list(key = 'extrafalse'))
222 (taml_load('key: trueblue') %==% list(key = 'trueblue'))
223 (taml_load('key: banana') %==% list(key = 'banana'))
224 })
225
226 assert('yaml_body() splits YAML front matter from body', {
227 doc = c('---', 'title: Hello', 'output: html', '---', '', 'Content here.')
228 res = yaml_body(doc)
229 (res$yaml %==% list(title = 'Hello', output = 'html'))
230 (res$body %==% c('', '', '', '', '', 'Content here.'))
231 (res$lines %==% c(1L, 4L))
232 # no YAML
233 doc2 = c('# heading', 'text')
234 res2 = yaml_body(doc2)
235 (is.null(res2$yaml))
236 (res2$body %==% doc2)
237 # yaml_body with parse=FALSE
238 res3 = yaml_body(doc, parse = FALSE)
239 (res3$yaml %==% c('title: Hello', 'output: html'))
240 })