-
-
Notifications
You must be signed in to change notification settings - Fork 256
/
template_test.go
397 lines (339 loc) · 11.4 KB
/
template_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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package gofakeit
import (
"fmt"
"strings"
"testing"
"text/template"
)
func ExampleTemplate() {
Seed(11)
template := `{{range IntRange 1 4}}{{FirstName}} {{LastName}}\n{{end}}`
value, err := Template(template, &TemplateOptions{Data: 4})
if err != nil {
fmt.Println(err)
}
fmt.Println(string(value))
// Output:
// Markus Moen
// Alayna Wuckert
// Lura Lockman
// Sylvan Mraz
}
func ExampleFaker_Template() {
f := New(11)
template := `{{range IntRange 1 4}}{{FirstName}} {{LastName}}\n{{end}}`
value, err := f.Template(template, &TemplateOptions{Data: 4})
if err != nil {
fmt.Println(err)
}
fmt.Println(string(value))
// Output:
// Markus Moen
// Alayna Wuckert
// Lura Lockman
// Sylvan Mraz
}
func TestPassedInFunctionMap(t *testing.T) {
f := New(11)
funcMap := template.FuncMap{
"title": func(s string) string {
words := strings.Fields(s)
for i, word := range words {
letters := strings.Split(word, "")
letters[0] = strings.ToUpper(letters[0])
words[i] = strings.Join(letters, "")
}
return strings.Join(words, " ")
},
}
s := `{{printf "%q" (title .Data)}}`
value, err := f.Template(s, &TemplateOptions{Funcs: funcMap, Data: "the go programming language"})
if err != nil {
fmt.Println(err)
}
if value != "\"The Go Programming Language\"" {
t.Error("Expected \"The Go Programming Language\", got ", value)
}
}
func TestTemplate_misc(t *testing.T) {
f := New(11)
// functions with parameters
tests := map[string]string{
"Weighted": "{{Weighted (SliceAny `hello` 2 6.9) (SliceF32 1 2 3)}}",
"Dice": "{{Dice 3 (SliceUInt 1 5 3) }}",
"IntRange": "{{IntRange 1 5}}",
"UintRange": "{{UintRange 1 5}}",
"RandomInt": "{{RandomInt (SliceInt 1 5 3) }}",
"RandomString": "{{RandomString (SliceString `key1` `key2` `key3`)}}",
"RandomUint": "{{RandomUint (SliceUInt 2 6 9)}}",
"Teams": "{{Teams (SliceString `person_a` `person_b` `person_c`) (SliceString `team_a` `team_b` `team_c`)}}",
"SliceAny": "{{SliceAny `hello` 2 6.9}}",
"SliceString": "{{SliceString `testa` `testb` `testc`}}",
"SliceF32": "{{SliceF32 1.2 5.55 7.955}}",
"SliceInt": "{{SliceInt 1 5 9}}",
"SliceUInt": "{{SliceUInt 1 5 9}}",
"Password": "{{Password true false false false false 32}}",
"ToUpper": "{{ToUpper `this was lower`}}",
"ToLower": "{{ToLower `THIS WAS UPPER`}}",
"Sentence": "{{Sentence 3}}",
"LoremIpsumSentence": "{{LoremIpsumSentence 3}}",
"Image": "{{Image 1 1}}",
"ImageJpeg": "{{ImageJpeg 1 1}}",
"ImagePng": "{{ImagePng 1 1}}",
"Numerify": "{{Numerify `10` }}",
"LatitudeInRange": "{{LatitudeInRange 21 42 }}",
"Number": "{{Number 1 42 }}",
"Price": "{{Price 1 100 }}",
"Regex": "{{Regex `[abcdef]{5}` }}",
"ImageURL": "{{ImageURL 640 480 }}",
"DigitN": "{{DigitN 10 }}",
"LoremIpsumParagraph": "{{LoremIpsumParagraph 3 5 12 `\n` }}",
"Paragraph": "{{Paragraph 3 5 12 `\n` }}",
"Lexify": "{{Lexify `hello`}}",
"LogLevel": "{{LogLevel `apache`}}",
"CreditCardNumber": "{{CreditCardNumber nil}}",
"Markdown": "{{Markdown nil}}",
"EmailText": "{{EmailText nil}}",
"LetterN": "{{LetterN 10}}",
"FixedWidth": "{{FixedWidth nil}}",
"Float32Range": "{{Float32Range 2.2 55.6}}",
"Float64Range": "{{Float64Range 2.2 55.6}}",
"HipsterParagraph": "{{HipsterParagraph 3 5 12 `\n`}}",
"HipsterSentence": "{{HipsterParagraph 3 5 12 `\n`}}",
"LongitudeInRange": "{{LongitudeInRange 21 42 }}",
"Svg": "{{Svg nil}}",
"DateRange": "{{DateRange (ToDate `2006-01-02`) (ToDate `2006-01-02`)}}",
}
for k, v := range tests {
value, err := f.Template(v, nil)
if err != nil {
t.Error(k, err)
}
if string(value) == "" {
t.Errorf("expected a value for %s, got nothing", k)
}
}
}
func TestTemplateLookup(t *testing.T) {
f := New(11)
info := GetFuncLookup("template")
m := MapParams{
"template": {"{{range IntRange 1 (ToInt .Data)}}{{FirstName}} {{LastName}}\n{{end}}"},
"data": {"5"},
}
value, err := info.Generate(f.Rand, &m, info)
if err != nil {
t.Fatal(err.Error())
}
if value != "Markus Moen\nAlayna Wuckert\nLura Lockman\nSylvan Mraz\nPaolo Rutherford\n" {
t.Error("Expected `Markus Moen Pagac`, got ", value)
}
}
func TestTemplateInternalFunctions(t *testing.T) {
Seed(11)
type test struct {
template string
expected string
}
tests := []test{
// ToInt
{
template: `{{ ToInt 55 }}`,
expected: "55",
},
// ToFloat
{
template: `{{ ToFloat 55.55 }}`,
expected: "55.55",
},
// ToString
{
template: `{{ ToString 3.55 }}`,
expected: "3.55",
},
}
for k, v := range tests {
value, err := Template(v.template, nil)
if err != nil {
t.Fatal(err)
}
if value != v.expected {
t.Errorf("Test: %v expected `%s`, got `%s`", k, v.expected, value)
}
}
}
func TestTemplateNoOptions(t *testing.T) {
Seed(11)
tests := []string{
"Markus Moen",
"Alayna Wuckert",
"Lura Lockman",
"Sylvan Mraz",
"Paolo Rutherford",
}
for k, v := range tests {
// The above code is declaring a variable named "value" in the Go programming language.
value, err := Template("{{FirstName}} {{LastName}}", nil)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(value, v) {
t.Errorf("Test: %v could not find `%s`, in result %s", k, v, value)
}
}
}
func ExampleEmailText() {
Seed(11)
value, err := EmailText(&EmailOptions{})
if err != nil {
fmt.Println(err)
}
fmt.Println(string(value))
// Output:
// Subject: Greetings from Marcel!
//
// Dear Pagac,
//
// Hello there! Sending positive vibes your way.
//
// I hope you're doing great. May your week be filled with joy.
//
// This me far smile where was by army party riches. Theirs instead here mine whichever that those instance growth has. Ouch enough Swiss us since down he she aha us. You to upon how this this furniture way no play. Towel that us to accordingly theirs purse enough so though.
//
// Election often until eek weekly yet oops until conclude his. Stay elsewhere such that galaxy clean that last each stack. Reluctantly theirs wisp aid firstly highly butter accordingly should already. Calm shake according fade neither kuban upon this he fortnightly. Occasionally bunch on who elsewhere lastly hourly right there honesty.
//
// We is how result out Shakespearean have whom yearly another. Packet are behind late lot finally time themselves goodness quizzical. Our therefore could fact cackle yourselves zebra for whose enormously. All bowl out wandering secondly yellow another your hourly spit. Since tomorrow hers words little think will our by Polynesian.
//
// I'm curious to know what you think about it. If you have a moment, please feel free to check out the project on GitLab
//
// Your insights would be invaluable. Your thoughts matter to me.
//
// I appreciate your attention to this matter. Your feedback is greatly appreciated.
//
// Best wishes
// Daryl Leannon
// oceaneokuneva@roberts.org
// 1-816-608-2233
}
func ExampleFaker_EmailText() {
f := New(11)
value, err := f.EmailText(&EmailOptions{})
if err != nil {
fmt.Println(err)
}
fmt.Println(string(value))
// Output:
// Subject: Greetings from Marcel!
//
// Dear Pagac,
//
// Hello there! Sending positive vibes your way.
//
// I hope you're doing great. May your week be filled with joy.
//
// This me far smile where was by army party riches. Theirs instead here mine whichever that those instance growth has. Ouch enough Swiss us since down he she aha us. You to upon how this this furniture way no play. Towel that us to accordingly theirs purse enough so though.
//
// Election often until eek weekly yet oops until conclude his. Stay elsewhere such that galaxy clean that last each stack. Reluctantly theirs wisp aid firstly highly butter accordingly should already. Calm shake according fade neither kuban upon this he fortnightly. Occasionally bunch on who elsewhere lastly hourly right there honesty.
//
// We is how result out Shakespearean have whom yearly another. Packet are behind late lot finally time themselves goodness quizzical. Our therefore could fact cackle yourselves zebra for whose enormously. All bowl out wandering secondly yellow another your hourly spit. Since tomorrow hers words little think will our by Polynesian.
//
// I'm curious to know what you think about it. If you have a moment, please feel free to check out the project on GitLab
//
// Your insights would be invaluable. Your thoughts matter to me.
//
// I appreciate your attention to this matter. Your feedback is greatly appreciated.
//
// Best wishes
// Daryl Leannon
// oceaneokuneva@roberts.org
// 1-816-608-2233
}
// TemplateMarkdown examples and tests
func ExampleMarkdown() {
Seed(11)
value, err := Markdown(&MarkdownOptions{})
if err != nil {
fmt.Println(err)
}
fmt.Println(string(value))
// Output:
// # PurpleSheep5
//
// *Author: Amie Feil*
//
// Was by army party riches theirs instead. Here mine whichever that those instance growth. Has ouch enough Swiss us since down. He she aha us you to upon. How this this furniture way no play.
//
// Towel that us to accordingly theirs purse. Enough so though election often until eek. Weekly yet oops until conclude his stay. Elsewhere such that galaxy clean that last. Each stack reluctantly theirs wisp aid firstly.
//
// ## Table of Contents
// - [Installation](#installation)
// - [Usage](#usage)
// - [License](#license)
//
// ## Installation
// '''bash
// pip install PurpleSheep5
// '''
//
// ## Usage
// '''python
// result = purplesheep5.process("funny request")
// print("purplesheep5 result:", "in progress")
// '''
//
// ## License
// MIT
}
func ExampleFaker_Markdown() {
f := New(11)
value, err := f.Markdown(&MarkdownOptions{})
if err != nil {
fmt.Println(err)
}
fmt.Println(string(value))
// Output:
// # PurpleSheep5
//
// *Author: Amie Feil*
//
// Was by army party riches theirs instead. Here mine whichever that those instance growth. Has ouch enough Swiss us since down. He she aha us you to upon. How this this furniture way no play.
//
// Towel that us to accordingly theirs purse. Enough so though election often until eek. Weekly yet oops until conclude his stay. Elsewhere such that galaxy clean that last. Each stack reluctantly theirs wisp aid firstly.
//
// ## Table of Contents
// - [Installation](#installation)
// - [Usage](#usage)
// - [License](#license)
//
// ## Installation
// '''bash
// pip install PurpleSheep5
// '''
//
// ## Usage
// '''python
// result = purplesheep5.process("funny request")
// print("purplesheep5 result:", "in progress")
// '''
//
// ## License
// MIT
}
func BenchmarkTemplate100(b *testing.B) {
f := New(11)
for i := 0; i < 100; i++ {
_, err := f.Template("{{range IntRange 1 (ToInt .Data)}}{{FirstName}} {{LastName}}\n{{end}}", &TemplateOptions{Data: 5})
if err != nil {
b.Fatal(err.Error())
}
}
}
func BenchmarkTemplateLookup1000(b *testing.B) {
f := New(11)
for i := 0; i < 1000; i++ {
_, err := f.Template("{{range $y := IntRange 1 (ToInt .Data)}}{{FirstName}} {{LastName}}\n{{end}}", &TemplateOptions{Data: 5})
if err != nil {
b.Fatal(err.Error())
}
}
}