forked from aymerick/raymond
-
Notifications
You must be signed in to change notification settings - Fork 9
/
json_visitor_test.go
170 lines (157 loc) · 5.62 KB
/
json_visitor_test.go
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package raymond
import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)
func TestJSONVisitor(t *testing.T) {
for _, tt := range []struct {
name string
source string
want map[string]interface{}
}{{
name: "basic",
source: sourceBasic,
want: map[string]interface{}{
"title": "test_title",
"body": "test_body",
},
}, {
name: "nested vars",
source: `<div class="entry">
<h1>{{title.name}}</h1>
<div class="body">
{{body.content}}
</div>
</div>`,
want: map[string]interface{}{
"title": map[string]interface{}{"name": "test_name"},
"body": map[string]interface{}{"content": "test_content"},
},
}, {
name: "block params",
source: `{{#foo as |bar|}}
{{bar.baz}}
{{/foo}}`,
want: map[string]interface{}{
"bar": map[string]interface{}{"baz": "test_baz"},
},
}, {
name: "with block",
source: `{{#with people.[0].[0]}}
{{name}}
{{/with}}`,
want: map[string]interface{}{"people": newList(newList(map[string]interface{}{"name": "test_name"}))},
}, {
name: "if block",
source: `{{#if people.name}} {{people.name}}{{/if}}`,
want: map[string]interface{}{
"people": map[string]interface{}{"name": "test_name"},
},
}, {
name: "if block with incomplete foo path and complete foo path",
source: `{{#if floo}} {{floo.blar.blaz}} {{/if}}`,
want: map[string]interface{}{"floo": map[string]interface{}{"blar": map[string]interface{}{"blaz": "test_blaz"}}},
}, {
name: "accesses multiple elements of a map in multiple paths",
source: `{{bar.baz}} {{name.first}}{{name.last}}`,
want: map[string]interface{}{
"bar": map[string]interface{}{"baz": "test_baz"},
"name": map[string]interface{}{"first": "test_first", "last": "test_last"}},
}, {
name: "large template",
source: largeTemplate,
want: map[string]interface{}{"bar": "test_bar", "foo": "test_foo", "name": "test_name", "phone": "test_phone"},
}, {
name: "multi with",
source: "{{#with foo}}{{#with bar}}{{baz}}{{/with}}{{/with}}",
want: map[string]interface{}{"foo": map[string]interface{}{"bar": map[string]interface{}{"baz": "test_baz"}}},
}, {
name: "multi as",
source: "{{#with foo as |bee|}}{{#with bee.bar as |bazinga|}}{{bazinga.baz}}{{/with}}{{/with}}",
want: map[string]interface{}{"foo": map[string]interface{}{"bar": map[string]interface{}{"baz": "test_baz"}}},
}, {
name: "each this",
source: "{{#each user.services}}{{this.service}}{{this.date}}{{/each}}",
want: map[string]interface{}{"user": map[string]interface{}{"services": newList(map[string]interface{}{"service": "test_service", "date": "test_date"})}},
}, {
name: "multi multi with",
source: "{{#with fizz}}{{#with foo}}{{#with bar}}{{baz}}{{bop}}{{/with}}{{/with}}{{/with}}",
want: map[string]interface{}{"fizz": map[string]interface{}{"foo": map[string]interface{}{"bar": map[string]interface{}{"baz": "test_baz", "bop": "test_bop"}}}},
}, {
name: "multi with same names",
source: "{{#with foo}}{{#with foo}}{{baz}}{{/with}}{{/with}}",
want: map[string]interface{}{"foo": map[string]interface{}{"foo": map[string]interface{}{"baz": "test_baz"}}},
}, {
name: "up a level",
source: "{{#with foo}}{{#with foo}}{{../baz}}{{/with}}{{/with}}",
want: map[string]interface{}{"foo": map[string]interface{}{"baz": "test_baz"}},
}, {
name: "each lookup",
source: "{{#each people}} {{.}} lives in {{lookup ../cities @index}}{{/each}}",
want: map[string]interface{}{"people": newList("test_people"), "cities": newList("test_cities")},
}, {
name: "each lookup complex",
source: "{{#each people}} {{./fioo/biar/biaz}} lives in {{lookup ../cities @index}}{{/each}}",
want: map[string]interface{}{"people": newList(map[string]interface{}{"fioo": map[string]interface{}{"biar": map[string]interface{}{"biaz": "test_biaz"}}}), "cities": newList("test_cities")},
}, {
name: "each",
source: "{{#with foo}}{{#each foo}}{{baz}}{{/each}}{{/with}}",
want: map[string]interface{}{"foo": map[string]interface{}{"foo": newList(map[string]interface{}{"baz": "test_baz"})}},
}, {
name: "multiple paths in a non-block helper block",
source: `{{#foo bar baz}} {{name.first name.last}} {{/foo}}`,
want: map[string]interface{}{
"bar": "test_bar",
"baz": "test_baz",
"name": map[string]interface{}{
"first": "test_first",
"last": "test_last"}},
}} {
t.Run(tt.name, func(t *testing.T) {
tpl, err := Parse(tt.source)
require.NoError(t, err)
require.Equal(t, tpl.source, tt.source)
//fmt.Println(tpl.PrintAST())
vars, err := tpl.ExtractTemplateVars()
require.NoError(t, err)
assert.Equal(t, tt.want, vars)
})
}
}
var largeTemplate = `<html>
{{#if name}}
<div>Hello {{name}}!</dev>
{{else}}
<div>Hello there!</div>
{{/if}}
{{#ifGt foo bar}}
<br><br><div>foo is greater than bar</div>
{{/ifGt}}
{{#ifGt foo 10}}
<br><br><div>foo is greater than 10</div>
{{else}}
<br><br><div>foo is not greater than 10</div>
{{/ifGt}}
{{#ifLt foo bar}}
<br><br><div>foo is less than bar</div><br><br>
{{/ifLt}}
{{#ifLt foo 10}}
<div>foo is less than 10</div>
{{else}}
<div>foo is not less than 10</div>
{{/ifLt}}
{{#ifEq foo bar}}
<br><br><div>foo is equal to bar</div><br><br>
{{/ifEq}}
{{#ifEq foo 10}}
<div>foo is equal to 10</div>
{{else}}
<div>foo is not equal to 10</div>
{{/ifEq}}
{{#ifMatchesRegexStr "^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$" phone}}
<br><div>phone var is a phone number</div>
{{else}}
<br><div>phone var is not a phone number</div>
{{/ifMatchesRegexStr}}
</html>`