-
Notifications
You must be signed in to change notification settings - Fork 1
/
xtemplate_test.go
334 lines (289 loc) · 7.68 KB
/
xtemplate_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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package xcore
import (
"fmt"
"testing"
"time"
"golang.org/x/text/language"
)
func ExampleNewXTemplateFromString() {
tmpl, _ := NewXTemplateFromString(`
%-- This is a comment. It will not appear in the final code. --%
Let's put your name here: {{clientname}}<br />
And lets put your hobbies here:<br />
%-- note the 1rst id is the entry into the data to inject and the second one is the name of the sub-template to use --%
@@hobbies:hobby@@
%-- And you need the template for each hobby:--%
[[hobby]]
I love {{name}}<br />
[[]]
`)
// The creation of the data is obviously tedious here, in real life it should come from a JSON, a Database, etc
data := XDataset{
"clientname": "Fred",
"hobbies": &XDatasetCollection{
&XDataset{"name": "Football"},
&XDataset{"name": "Ping-pong"},
&XDataset{"name": "Swimming"},
&XDataset{"name": "Videogames"},
},
}
fmt.Println(tmpl.Execute(&data))
// Output:
// Let's put your name here: Fred<br />
// And lets put your hobbies here:<br />
// I love Football<br />
// I love Ping-pong<br />
// I love Swimming<br />
// I love Videogames<br />
}
func TestNewXTemplateFromString(t *testing.T) {
tmpl, _ := NewXTemplateFromString(`
%-- This is a comment. It will not appear in the final code. --%
Let's put your name here: {{clientname}}<br />
And lets put your hobbies here:<br />
%-- note the 1rst id is the entry into the data to inject and the second one is the name of the sub-template to use --%
@@hobbies:hobby@@
%-- And you need the template for each hobby:--%
[[hobby]]
I love {{name}}<br />
[[]]
`)
// The creation of the data is obviously tedious here, in real life it should come from a JSON, a Database, etc
data := XDataset{
"clientname": "Fred",
"hobbies": &XDatasetCollection{
&XDataset{"name": "Football"},
&XDataset{"name": "Ping-pong"},
&XDataset{"name": "Swimming"},
&XDataset{"name": "Videogames"},
},
}
str := tmpl.Execute(&data)
if str != `
Let's put your name here: Fred<br />
And lets put your hobbies here:<br />
I love Football<br />
I love Ping-pong<br />
I love Swimming<br />
I love Videogames<br />
` {
t.Error("Error loading and running the template from string " + str)
return
}
}
func TestNewXTemplateFromFile(t *testing.T) {
tmpl, _ := NewXTemplateFromFile("testunit/a.template")
// The creation of the data is obviously tedious here, in real life it should come from a JSON, a Database, etc
data := XDataset{
"clientname": "Fred",
"hobbies": &XDatasetCollection{
&XDataset{"name": "Football"},
&XDataset{"name": "Ping-pong"},
&XDataset{"name": "Swimming"},
&XDataset{"name": "Videogames"},
},
}
str := tmpl.Execute(&data)
if str != `
Let's put your name here: Fred<br />
And lets put your hobbies here:<br />
I love Football<br />
I love Ping-pong<br />
I love Swimming<br />
I love Videogames<br />
` {
t.Error("Error loading and running the template from file " + str)
return
}
}
func TestXTemplateErrors(t *testing.T) {
tmpl1, _ := NewXTemplateFromString("template [[subtemplate]]")
if tmpl1 != nil {
t.Error("Error compiling a template with error, the template should be nil")
return
}
}
func TestXTemplateComments(t *testing.T) {
tmpl1, _ := NewXTemplateFromString("abcdefg")
tmpl2, _ := NewXTemplateFromString(`a%--comment1--%b%--comment 2
[[]]
[[subtemplate]]
--%c%--comment3 --%
defg%-- ending @@subtemplate@@ comment --%
`)
if tmpl1.Execute(nil) != tmpl2.Execute(nil) {
t.Error("Error comparing templates for comments")
return
}
}
func TestXTemplateLanguageParam(t *testing.T) {
tmpl, _ := NewXTemplateFromString("Test with ##some## ##languages## here")
data := &XDataset{}
l, _ := NewXLanguageFromString("some=a tiny table\nlanguages=of english language\n")
data.Set("#", l)
result := tmpl.Execute(data)
if result != "Test with a tiny table of english language here" {
t.Errorf("The language table has not been inserted correctly")
}
}
func TestXTemplateSimple(t *testing.T) {
tmpl, err := NewXTemplateFromFile("testunit/b.template")
if err != nil {
t.Error(err)
return
}
tmp, _ := time.Parse(time.RFC3339, "2020-01-01T12:00:00")
lang := NewXLanguage("mainpage", language.English)
lang.Set("welcome", "Welcome to you")
data := XDataset{
"clientname": "Fred",
"clientpicture": "face.jpg",
"hobbies": &XDatasetCollection{
&XDataset{"name": "Football", "sport": "yes"},
&XDataset{"name": "Ping-pong", "sport": "yes"},
&XDataset{"name": "Swimming", "sport": "yes"},
&XDataset{"name": "Videogames", "sport": "no"},
&XDataset{"name": "other 1", "sport": "no"},
&XDataset{"name": "other 2", "sport": "no"},
&XDataset{"name": "other 3", "sport": "yes"},
&XDataset{"name": "other 4", "sport": "no"},
&XDataset{"name": "other 5", "sport": nil},
},
"preferredhobby": &XDataset{
"name": "Baseball",
"sport": "yes",
},
"metadata": &XDataset{
"preferred-color": "blue",
"Salary": 3568.65,
"hiredate": tmp,
},
"#": lang,
}
// fmt.Println(data)
str := tmpl.Execute(&data)
if str == "" {
t.Errorf("Error build complex template")
}
// fmt.Println(str)
}
/*
package main
import (
"fmt"
"github.com/webability-go/xcore"
"testing"
// "unsafe"
)
// TEST XTEMPLATE
func TestReferenceParam(t *testing.T) {
tmpl, _ := xcore.NewXTemplateFromString(`
The sub template starts here: &&template1&&. End.
[[template1]]
This is the template 1
[[]]
`)
fmt.Println(tmpl)
fmt.Println(tmpl.Root)
result := tmpl.Execute(&xcore.XDataset{})
fmt.Println("Result: ", result)
}
func TestComplexReferenceParam(t *testing.T) {
tmpl, err := xcore.NewXTemplateFromString(`
The sub template starts here: &&template2&&. End.
[[template1]]
This is the template 1
[[]]
[[template2]]
This is the template 2
[[template3]]
This is the subtemplate 3
[[]]
[[template4|template5]]
These are the subtemplates 4 and 5
[[template6.first]]
This is the subtemplate 6 first element for a loop
[[]]
[[template6]]
This is the subtemplate 6 any element for a loop
[[]]
[[template6.last]]
This is the subtemplate 6 last element for a loop
[[]]
[[]]
[[]]
[[template7|template7.status.false]]
This is the template 7 for field status false and any other values
[[]]
[[template7.status.true]]
This is the template 7 for field status true
[[]]
`)
if err != nil {
fmt.Println(err)
return
}
result := tmpl.Execute(&xcore.XDataset{})
fmt.Println("Result: ", result)
}
func TestVariableParam(t *testing.T) {
tmpl, err := xcore.NewXTemplateFromString(`
Some data:
{{data1}}
{{data2}}
{{data3>data31}}
{{data4}}
{{data5}}
{{data6}}
{{data7}}
{{data8}}
@@data8@@
[[data8]]
* test {{data81}} and {{data82}} and {{data83}} and {{data1}}
[[]]
??data9??
[[data9]]
* Data 9 exists and is {{data9}}
[[]]
??data10??
[[data10]]
* Data 10 does not exist
[[]]
!!dump!!
`)
if err != nil {
fmt.Println(err)
return
}
data := xcore.XDataset{}
data["data1"] = "DATA1"
data["data2"] = "DATA1"
sm := xcore.XDataset{}
sm["data31"] = "DATA31"
data["data3"] = sm
data["data4"] = 123
data["data5"] = 123.432
data["data6"] = true
data["data7"] = func() string { return "ABC" }
d8r1 := &xcore.XDataset{}
d8r1.Set("data81", "rec 1: Entry 8-1")
d8r1.Set("data82", "rec 1: Entry 8-2")
d8r2 := &xcore.XDataset{}
d8r2.Set("data81", "rec 2: Entry 8-1")
d8r2.Set("data82", "rec 2: Entry 8-2")
d8r2.Set("data83", "rec 2: Entry 8-3")
d8r3 := &xcore.XDataset{}
d8r3.Set("data81", "rec 3: Entry 8-1")
d8r3.Set("data82", "rec 3: Entry 8-2")
d := xcore.XDatasetCollection{}
d.Push(d8r1)
d.Push(d8r2)
d.Push(d8r3)
data["data8"] = &d
data["data9"] = "I exist"
fmt.Printf("Data: %v\n", data)
// fmt.Printf("ADDRESS DATA8 / GET R1: %p", data.GetCollection("data8").Get(0))
result := tmpl.Execute(&data)
fmt.Println("Result: ", result)
}
*/