@@ -11,14 +11,27 @@ const (
11
11
// Can be set to `true` to skip tests that stress test the parser
12
12
// by having large data amounts - these pass - but slow down the test run
13
13
skip_large_files = false
14
+ // Can be set to `true` to skip tests that triggers a slow conversion
15
+ // process that uses `python` to convert from YAML to JSON.
16
+ skip_yaml_conversion = false
14
17
15
18
// Kept for easier handling of future updates to the tests
16
19
valid_exceptions = []string {}
17
20
invalid_exceptions = []string {}
18
21
19
- valid_value_exceptions = []string {}
22
+ valid_value_exceptions = [
23
+ 'values/spec-string-basic.toml' ,
24
+ ]
25
+
26
+ yaml_value_exceptions = [
27
+ 'values/spec-float-5.toml' , // YAML: "1e6", V: 1000000
28
+ 'values/spec-float-9.toml' , // YAML: "-0e0", V: 0
29
+ 'values/spec-float-6.toml' , // YAML: "-2E-2", V: -0.02
30
+ 'values/spec-float-4.toml' , // YAML: "5e+22", V: 50000000000000004000000
31
+ ]
20
32
21
33
jq = os.find_abs_path_of_executable ('jq' ) or { '' }
34
+ python = os.find_abs_path_of_executable ('python' ) or { '' }
22
35
compare_work_dir_root = os.join_path (os.temp_dir (), 'v' , 'toml' , 'iarna' )
23
36
// From: https://stackoverflow.com/a/38266731/1904615
24
37
jq_normalize = r '# Apply f to composite entities recursively using keys[], and to atoms
@@ -98,25 +111,71 @@ fn test_iarna_toml_spec_tests() {
98
111
$if windows {
99
112
relative = relative.replace ('/' , '\\ ' )
100
113
}
101
- if ! os.exists (valid_test_file.all_before_last ('.' ) + '.json' ) {
102
- println ('N/A [${i + 1} /$valid_test_files.len ] "$valid_test_file "...' )
103
- continue
104
- }
114
+
105
115
// Skip the file if we know it can't be parsed or we know that the value retrieval needs work.
106
116
if relative ! in valid_exceptions && relative ! in valid_value_exceptions {
117
+ valid_test_file_name := os.file_name (valid_test_file).all_before_last ('.' )
118
+ uses_json_format := os.exists (valid_test_file.all_before_last ('.' ) + '.json' )
119
+
120
+ // Use python to convert the YAML files to json - it yields some inconsistencies
121
+ // so we skip some of them
122
+ mut converted_from_yaml := false
123
+ mut converted_json_path := ''
124
+ if ! uses_json_format {
125
+ $if windows {
126
+ println ('N/A [${i + 1} /$valid_test_files.len ] "$valid_test_file "...' )
127
+ continue
128
+ }
129
+ if python == '' {
130
+ println ('N/A [${i + 1} /$valid_test_files.len ] "$valid_test_file "...' )
131
+ continue
132
+ }
133
+ if skip_yaml_conversion || relative in yaml_value_exceptions
134
+ || valid_test_file.contains ('qa-' ) {
135
+ e++
136
+ println ('SKIP [${i + 1} /$valid_test_files.len ] "$valid_test_file " EXCEPTION [$e /$valid_value_exceptions.len ]...' )
137
+ continue
138
+ }
139
+
140
+ iarna_yaml_path := valid_test_file.all_before_last ('.' ) + '.yaml'
141
+ if os.exists (iarna_yaml_path) {
142
+ // python -c 'import sys, yaml, json; json.dump(yaml.load(sys.stdin), sys.stdout, indent=4)' < file.yaml > file.json
143
+
144
+ converted_json_path = os.join_path (compare_work_dir_root,
145
+ valid_test_file_name + '.yaml.json' )
146
+
147
+ run ([python, '-c' ,
148
+ "'import sys, yaml, json; json.dump(yaml.load(sys.stdin), sys.stdout, indent=4)'" ,
149
+ '<' , iarna_yaml_path, '>' , converted_json_path]) or {
150
+ contents := os.read_file (iarna_yaml_path) or { panic (err) }
151
+ // NOTE there's known errors with the python convertion method.
152
+ // For now we just ignore them as it's a broken tool - not a wrong test-case.
153
+ // Uncomment this print to see/check them.
154
+ // eprintln(err.msg + '\n$contents')
155
+ e++
156
+ println ('ERR [${i + 1} /$valid_test_files.len ] "$valid_test_file " EXCEPTION [$e /$valid_value_exceptions.len ]...' )
157
+ continue
158
+ }
159
+ converted_from_yaml = true
160
+ }
161
+ }
162
+
107
163
println ('OK [${i + 1} /$valid_test_files.len ] "$valid_test_file "...' )
108
164
toml_doc := toml.parse_file (valid_test_file) or { panic (err) }
109
165
110
- v_toml_json_path := os.join_path (compare_work_dir_root,
111
- os. file_name (valid_test_file). all_before_last ( '.' ) + '.v.json' )
166
+ v_toml_json_path := os.join_path (compare_work_dir_root, valid_test_file_name +
167
+ '.v.json' )
112
168
iarna_toml_json_path := os.join_path (compare_work_dir_root,
113
- os. file_name (valid_test_file). all_before_last ( '.' ) + '.json' )
169
+ valid_test_file_name + '.json' )
114
170
115
- os.write_file (v_toml_json_path, to_iarna (toml_doc.ast.table)) or { panic (err) }
116
-
117
- iarna_json := os.read_file (valid_test_file.all_before_last ('.' ) + '.json' ) or {
171
+ os.write_file (v_toml_json_path, to_iarna (toml_doc.ast.table, converted_from_yaml)) or {
118
172
panic (err)
119
173
}
174
+
175
+ if converted_json_path == '' {
176
+ converted_json_path = valid_test_file.all_before_last ('.' ) + '.json'
177
+ }
178
+ iarna_json := os.read_file (converted_json_path) or { panic (err) }
120
179
os.write_file (iarna_toml_json_path, iarna_json) or { panic (err) }
121
180
122
181
v_normalized_json := run ([jq, '-S' , '-f "$jq_normalize_path "' , v_toml_json_path]) or {
@@ -197,10 +256,13 @@ fn to_iarna_time(time_str string) string {
197
256
}
198
257
199
258
// to_iarna returns a iarna compatible json string converted from the `value` ast.Value.
200
- fn to_iarna (value ast.Value) string {
259
+ fn to_iarna (value ast.Value, skip_value_map bool ) string {
201
260
match value {
202
261
ast.Quoted {
203
262
json_text := json2 .Any (value.text).json_str ()
263
+ if skip_value_map {
264
+ return '"$json_text "'
265
+ }
204
266
return '{ "type": "string", "value": "$json_text " }'
205
267
}
206
268
ast.DateTime {
@@ -218,49 +280,84 @@ fn to_iarna(value ast.Value) string {
218
280
// date-time values are represented in detail. For now we follow the BurntSushi format
219
281
// that expands to 6 digits which is also a valid RFC 3339 representation.
220
282
json_text = to_iarna_time (json_text)
283
+ if skip_value_map {
284
+ return '"$json_text "'
285
+ }
221
286
return '{ "type": "$typ ", "value": "$json_text " }'
222
287
}
223
288
ast.Date {
224
289
json_text := json2 .Any (value.text).json_str ()
290
+ if skip_value_map {
291
+ return '"$json_text "'
292
+ }
225
293
return '{ "type": "date", "value": "$json_text " }'
226
294
}
227
295
ast.Time {
228
296
mut json_text := json2 .Any (value.text).json_str ()
229
297
json_text = to_iarna_time (json_text)
298
+ if skip_value_map {
299
+ return '"$json_text "'
300
+ }
230
301
return '{ "type": "time", "value": "$json_text " }'
231
302
}
232
303
ast.Bool {
233
304
json_text := json2 .Any (value.text.bool ()).json_str ()
305
+ if skip_value_map {
306
+ return '$json_text '
307
+ }
234
308
return '{ "type": "bool", "value": "$json_text " }'
235
309
}
236
310
ast.Null {
237
311
json_text := json2 .Any (value.text).json_str ()
312
+ if skip_value_map {
313
+ return '$json_text '
314
+ }
238
315
return '{ "type": "null", "value": "$json_text " }'
239
316
}
240
317
ast.Number {
241
- if value.text.contains ('inf' ) || value.text.contains ('nan' ) {
242
- return '{ "type": "float", "value": "$value.text " }'
318
+ if value.text.contains ('inf' ) {
319
+ mut json_text := value.text.replace ('inf' , '1.7976931348623157e+308' ) // Inconsistency ???
320
+ if skip_value_map {
321
+ return '$json_text '
322
+ }
323
+ return '{ "type": "float", "value": "$json_text " }'
324
+ }
325
+ if value.text.contains ('nan' ) {
326
+ mut json_text := 'null'
327
+ if skip_value_map {
328
+ return '$json_text '
329
+ }
330
+ return '{ "type": "float", "value": "$json_text " }'
243
331
}
244
332
if ! value.text.starts_with ('0x' )
245
333
&& (value.text.contains ('.' ) || value.text.to_lower ().contains ('e' )) {
246
334
mut val := '$value.f64 ()' .replace ('.e+' , '.0e' ) // json notation
247
335
if ! val.contains ('.' ) && val != '0' { // json notation
248
336
val + = '.0'
249
337
}
338
+ if skip_value_map {
339
+ return '$val '
340
+ }
250
341
return '{ "type": "float", "value": "$val " }'
251
342
}
252
343
v := value.i64 ()
253
344
// TODO workaround https://github.com/vlang/v/issues/9507
254
345
if v == i64 (- 9223372036854775807 - 1 ) {
346
+ if skip_value_map {
347
+ return '-9223372036854775808'
348
+ }
255
349
return '{ "type": "integer", "value": "-9223372036854775808" }'
256
350
}
351
+ if skip_value_map {
352
+ return '$v '
353
+ }
257
354
return '{ "type": "integer", "value": "$v " }'
258
355
}
259
356
map [string ]ast.Value {
260
357
mut str := '{ '
261
358
for key, val in value {
262
359
json_key := json2 .Any (key).json_str ()
263
- str + = ' "$json_key ": ${to_iarna(val)} ,'
360
+ str + = ' "$json_key ": ${to_iarna(val, skip_value_map )} ,'
264
361
}
265
362
str = str.trim_right (',' )
266
363
str + = ' }'
@@ -269,7 +366,7 @@ fn to_iarna(value ast.Value) string {
269
366
[]ast.Value {
270
367
mut str := '[ '
271
368
for val in value {
272
- str + = ' ${to_iarna(val)} ,'
369
+ str + = ' ${to_iarna(val, skip_value_map )} ,'
273
370
}
274
371
str = str.trim_right (',' )
275
372
str + = ' ]\n '
0 commit comments