-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
json_test.v
74 lines (68 loc) · 1.72 KB
/
json_test.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
module main
import datamodel { DataModel }
import template
import os
import x.json2
fn test_simple_json_api() {
// Demonstrates the following:
// 1. Delimiter swap to [ and ]
// 2. Raw tag usage using & prefix
// 3. If the data is a string, pass in the quotes too.
input := '
{{=[ ]=}}
{
"value": [&value]
}'.trim_indent()
test_data := [
DataModel({
'value': DataModel('42')
}),
DataModel({
'value': DataModel('-147')
}),
DataModel({
'value': DataModel('0')
}),
DataModel({
'value': DataModel('"Valid string value"')
}),
]
expected_values := [
'
{
"value": 42
}'.trim_indent(),
'
{
"value": -147
}'.trim_indent(),
'
{
"value": 0
}'.trim_indent(),
'
{
"value": "Valid string value"
}'.trim_indent(),
]
json_template := template.from_strings(input: input)!
for index, data in test_data {
output := json_template.run(data)!
assert output == expected_values[index]
}
}
fn test_json_template_files() {
partials := {
'article': 'spec/template_files/json/Article.partial.wskr.json'
'author': 'spec/template_files/json/Author.partial.wskr.json'
}
input := 'spec/template_files/json/List of Articles.wskr.json'
json_template := template.load_file(input: input, partials: partials)!
data := datamodel.from_json(os.read_file('spec/template_files/json/Articles.data.wskr.json')!)!
raw_output_string := json_template.run(data)!
pretty_output_string := json2.raw_decode(raw_output_string)!.prettify_json_str()
raw_expected_string := os.read_file('spec/template_files/json/List of Articles.json')!
pretty_expected_string := json2.raw_decode(raw_expected_string)!.prettify_json_str()
// dump(pretty_output_string)
assert pretty_output_string == pretty_expected_string
}