forked from brianvoe/gofakeit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_test.go
315 lines (269 loc) · 7.96 KB
/
generate_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
package gofakeit
import (
"fmt"
"regexp"
"strings"
"testing"
)
func TestGenerate(t *testing.T) {
output := ""
numTests := 1000
for i := 0; i < numTests; i++ {
output = Generate("{firstname} {lastname} {email} #?#?#?")
if strings.Contains(output, "{") || strings.Contains(output, "}") {
t.Error("Output should not contain either { or }. Output: ", output)
}
}
}
func TestGenerate_Sub(t *testing.T) {
t.Run("Simple", func(t *testing.T) {
Seed(11)
output := Generate("{randomstring:[{firstname},{lastname}]}")
if output != "Moen" {
t.Error("Did not generate what was expected. Got: ", output)
}
})
t.Run("Complex", func(t *testing.T) {
Seed(11)
output := Generate("{randomstring:[{randomstring:[{firstname},{lastname}]},{randomstring:[{firstname},{lastname}]}]}")
if output != "Kozey" {
t.Error("Did not generate what was expected. Got: ", output)
}
})
}
func ExampleGenerate() {
Seed(11)
fmt.Println(Generate("{firstname} {lastname} ssn is {ssn} and lives at {street}"))
fmt.Println(Generate("{sentence:3}"))
fmt.Println(Generate("{shuffleints:[1,2,3]}"))
fmt.Println(Generate("{number:1,50}"))
fmt.Println(Generate("{shufflestrings:[key:value,int:string,1:2,a:b]}"))
// Output: Markus Moen ssn is 526643139 and lives at 599 Dale ton
// Niche backwards caused.
// [1 3 2]
// 27
// [a:b key:value int:string 1:2]
}
func ExampleFaker_Generate() {
f := New(11)
fmt.Println(f.Generate("{firstname} {lastname} ssn is {ssn} and lives at {street}"))
fmt.Println(f.Generate("{sentence:3}"))
fmt.Println(f.Generate("{shuffleints:[1,2,3]}"))
fmt.Println(f.Generate("{number:1,50}"))
fmt.Println(f.Generate("{shufflestrings:[key:value,int:string,1:2,a:b]}"))
// Output: Markus Moen ssn is 526643139 and lives at 599 Dale ton
// Niche backwards caused.
// [1 3 2]
// 27
// [a:b key:value int:string 1:2]
}
func BenchmarkGenerate(b *testing.B) {
b.Run("package", func(b *testing.B) {
for i := 0; i < b.N; i++ {
Generate("{firstname} {lastname} {email} #?#?#?")
}
})
b.Run("Complex", func(b *testing.B) {
for i := 0; i < b.N; i++ {
Generate("{randomstring:[{randomstring:[{firstname},{lastname}]},{randomstring:[{firstname},{lastname}]}]}")
}
})
b.Run("Faker math", func(b *testing.B) {
f := New(0)
for i := 0; i < b.N; i++ {
f.Generate("{firstname} {lastname} {email} #?#?#?")
}
})
b.Run("Faker crypto", func(b *testing.B) {
f := NewCrypto()
for i := 0; i < b.N; i++ {
f.Generate("{firstname} {lastname} {email} #?#?#?")
}
})
}
func ExampleRegex() {
Seed(11)
fmt.Println(Regex("[abcdef]{5}"))
fmt.Println(Regex("[[:upper:]]{5}"))
fmt.Println(Regex("(hello|world|whats|up)"))
fmt.Println(Regex(`^[a-z]{5,10}@[a-z]{5,10}\.(com|net|org)$`))
// Output: affec
// RXHKI
// world
}
func ExampleFaker_Regex() {
f := New(11)
fmt.Println(f.Regex("[abcdef]{5}"))
fmt.Println(f.Regex("[[:upper:]]{5}"))
fmt.Println(f.Regex("(hello|world|whats|up)"))
fmt.Println(f.Regex(`^[a-z]{5,10}@[a-z]{5,10}\.(com|net|org)$`))
// Output: affec
// RXHKI
// world
}
var regexes = []struct{ test string }{
{`^\d+$`},
{`\D{3}`},
{`Z{2,5}`},
{`[^1]{3,5}`},
{`(ab|bc)def`},
{`((123)?){3}`},
{`[^abcdef]{5}`},
{`[a-zA-Z]{100}`},
{`[[:upper:]]{5}`},
{`[^0-5a-z\s]{5}`},
{`123[0-2]+.*\w{3}`},
{`(hello|world|whats|up)`},
{`^\d{1,2}[/](1[0-2]|[1-9])[/]((19|20)\d{2})$`},
{`^((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])$`},
{"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"},
}
func TestRegex(t *testing.T) {
for _, r := range regexes {
for i := 0; i < 100; i++ {
// Try to compile regexTest
regCompile, err := regexp.Compile(r.test)
if err != nil {
t.Fatal("Error compiling. regex: ", r.test, " failed to compile:", err)
}
// Generate string and test it matches the regex syntax
reg := Regex(r.test)
if !regCompile.MatchString(reg) {
t.Error("Generated data does not match regex. Regex: ", r.test, " output: ", reg)
}
}
}
}
func TestRegex_Struct(t *testing.T) {
Seed(11)
type Reggy struct {
Str1 string `fake:"{regex:^\\d+$}"`
Str2 string `fake:"{regex:\\D{3}}"`
Str3 string `fake:"{regex:Z{2,5}}"`
Str4 string `fake:"{regex:[^1]{3,5}}"`
Str5 string `fake:"{regex:(ab|bc)def}"`
Str6 string `fake:"{regex:((123)?){3}}"`
Str7 string `fake:"{regex:[^abcdef]{5}}"`
Str8 string `fake:"{regex:[a-zA-Z]{10}}"`
Str9 string `fake:"{regex:[[:upper:]]{5}}"`
Str10 string `fake:"{regex:[^0-5a-z\\s]{5}}"`
Str11 string `fake:"{regex:123[0-2]+.*\\w{3}}"`
Str12 string `fake:"{regex:(hello|world|whats|up)}"`
Str13 string `fake:"{regex:^\\d{1,2}[/](1[0-2]|[1-9])[/]((19|20)\\d{2})$}"`
}
rg := Reggy{}
Struct(&rg)
if rg.Str1 != "16" {
t.Errorf("Str1 should be 16 got: %s", rg.Str1)
}
if rg.Str2 != "fP?" {
t.Errorf("Str2 should be fP? got: %s", rg.Str2)
}
if rg.Str3 != "ZZ" {
t.Errorf("Str3 should be ZZ got: %s", rg.Str3)
}
if rg.Str4 != "$ z" {
t.Errorf("Str4 should be $ z got: %s", rg.Str4)
}
if rg.Str5 != "abdef" {
t.Errorf("Str5 should be abdef got: %s", rg.Str5)
}
if rg.Str6 != "123a123a123a" {
t.Errorf("Str6 should be 123a123a123a got: %s", rg.Str6)
}
if rg.Str7 != ";,}(l" {
t.Errorf("Str7 should be ;,}(l got: %s", rg.Str7)
}
if rg.Str8 != "nSMKgtlxwn" {
t.Errorf("Str8 should be nSMKgtlxwn got: %s", rg.Str8)
}
if rg.Str9 != "QHQCL" {
t.Errorf("Str9 should be QHQCL got: %s", rg.Str9)
}
if rg.Str10 != "-T?6X" {
t.Errorf("Str10 should be -T?6X got: %s", rg.Str10)
}
if rg.Str11 != "123120aeD" {
t.Errorf("Str11 should be 123120aeD got: %s", rg.Str11)
}
if rg.Str12 != "hello" {
t.Errorf("Str12 should be hello got: %s", rg.Str12)
}
if rg.Str13 != "8/10/2022" {
t.Errorf("Str13 should be 8/10/2022 got: %s", rg.Str13)
}
}
func BenchmarkRegex(b *testing.B) {
b.Run("package", func(b *testing.B) {
for i := 0; i < b.N; i++ {
Regex(`(hello|world|whats|up`)
}
})
b.Run("Faker math", func(b *testing.B) {
f := New(0)
for i := 0; i < b.N; i++ {
f.Regex(`(hello|world|whats|up`)
}
})
b.Run("Faker crypto", func(b *testing.B) {
f := NewCrypto()
for i := 0; i < b.N; i++ {
f.Regex(`(hello|world|whats|up`)
}
})
}
func BenchmarkRegexEmail(b *testing.B) {
b.Run("package", func(b *testing.B) {
for i := 0; i < b.N; i++ {
Regex("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
}
})
b.Run("Faker math", func(b *testing.B) {
f := New(0)
for i := 0; i < b.N; i++ {
f.Regex("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
}
})
b.Run("Faker crypto", func(b *testing.B) {
f := NewCrypto()
for i := 0; i < b.N; i++ {
f.Regex("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
}
})
}
func ExampleMap() {
Seed(11)
fmt.Println(Map())
// Output: map[contrast:Associate gauva:map[jump:scale] generally:8504801 him:[them whomever ours hence here his] its:8335564 table:map[did:[these delay these]] therefore:map[nearly:784141.8] thing:map[everything:[where caused why week hourly]]]
}
func ExampleFaker_Map() {
f := New(11)
fmt.Println(f.Map())
// Output: map[contrast:Associate gauva:map[jump:scale] generally:8504801 him:[them whomever ours hence here his] its:8335564 table:map[did:[these delay these]] therefore:map[nearly:784141.8] thing:map[everything:[where caused why week hourly]]]
}
func TestMap(t *testing.T) {
for i := 0; i < 100; i++ {
Map()
}
}
func BenchmarkMap(b *testing.B) {
b.Run("package", func(b *testing.B) {
for i := 0; i < b.N; i++ {
Map()
}
})
b.Run("Faker math", func(b *testing.B) {
f := New(0)
for i := 0; i < b.N; i++ {
f.Map()
}
})
b.Run("Faker crypto", func(b *testing.B) {
f := NewCrypto()
for i := 0; i < b.N; i++ {
f.Map()
}
})
}