forked from brianvoe/gofakeit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
faker_test.go
158 lines (133 loc) · 3.86 KB
/
faker_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
package gofakeit
import (
"fmt"
"sync"
"testing"
)
func Example() {
Seed(11)
fmt.Println("Name:", Name())
fmt.Println("Email:", Email())
fmt.Println("Phone:", Phone())
fmt.Println("Address:", Address().Address)
fmt.Println("BS:", BS())
fmt.Println("Beer Name:", BeerName())
fmt.Println("Color:", Color())
fmt.Println("Company:", Company())
fmt.Println("Credit Card:", CreditCardNumber(nil))
fmt.Println("Hacker Phrase:", HackerPhrase())
fmt.Println("Job Title:", JobTitle())
fmt.Println("Password:", Password(true, true, true, true, false, 32))
// Output:
// Name: Markus Moen
// Email: [email protected]
// Phone: 9948995369
// Address: 35300 South Roads haven, Miami, Tennessee 58302
// BS: streamline
// Beer Name: Pliny The Elder
// Color: Gray
// Company: Center for Responsive Politics
// Credit Card: 3821714800889989
// Hacker Phrase: Overriding the capacitor won't do anything, we need to compress the online SMTP protocol!
// Job Title: Supervisor
// Password: a8TyS<2l(pRLB4QU7V,O9nKUYcMD0(*g
}
func ExampleNew() {
// Create new pseudo random faker struct and set initial seed
fake := New(11)
// All global functions are also available in the structs methods
fmt.Println("Name:", fake.Name())
fmt.Println("Email:", fake.Email())
fmt.Println("Phone:", fake.Phone())
// Output:
// Name: Markus Moen
// Email: [email protected]
// Phone: 9948995369
}
func ExampleNewUnlocked() {
fake := NewUnlocked(11)
// All global functions are also available in the structs methods
fmt.Println("Name:", fake.Name())
fmt.Println("Email:", fake.Email())
fmt.Println("Phone:", fake.Phone())
// Output:
// Name: Markus Moen
// Email: [email protected]
// Phone: 9948995369
}
func TestNewUnlocked(t *testing.T) {
fake := NewUnlocked(0)
if fake.Name() == "" {
t.Error("Name was empty")
}
}
func ExampleNewCrypto() {
// Create new crypto faker struct
fake := NewCrypto()
// All global functions are also available in the structs methods
fmt.Println("Name:", fake.Name())
fmt.Println("Email:", fake.Email())
fmt.Println("Phone:", fake.Phone())
// Cannot output example as crypto/rand cant be predicted
}
func TestNewCrypto(t *testing.T) {
// Create new crypto faker struct
fake := NewCrypto()
// All global functions are also available in the structs methods
name := fake.Name()
email := fake.Email()
phone := fake.Phone()
if name == "" || email == "" || phone == "" {
t.Error("One of the values was empty")
}
}
type customRand struct{}
func (c *customRand) Seed(seed int64) {}
func (c *customRand) Uint64() uint64 { return 8675309 }
func (c *customRand) Int63() int64 { return int64(c.Uint64() & ^uint64(1<<63)) }
func ExampleNewCustom() {
// Setup stuct and methods required to meet interface needs
// type customRand struct {}
// func (c *customRand) Seed(seed int64) {}
// func (c *customRand) Uint64() uint64 { return 8675309 }
// func (c *customRand) Int63() int64 { return int64(c.Uint64() & ^uint64(1<<63)) }
// Create new custom faker struct
fake := NewCustom(&customRand{})
// All global functions are also available in the structs methods
fmt.Println("Name:", fake.Name())
fmt.Println("Email:", fake.Email())
fmt.Println("Phone:", fake.Phone())
// Output:
// Name: Aaliyah Abbott
// Email: [email protected]
// Phone: 1000000000
}
func ExampleSetGlobalFaker() {
cryptoFaker := NewCrypto()
SetGlobalFaker(cryptoFaker)
}
func TestSetGlobalFaker(t *testing.T) {
cryptoFaker := NewCrypto()
SetGlobalFaker(cryptoFaker)
name := Name()
if name == "" {
t.Error("Name was empty")
}
// Set global back to psuedo
SetGlobalFaker(New(0))
}
func TestConcurrency(t *testing.T) {
var setupComplete sync.WaitGroup
setupComplete.Add(1)
var wg sync.WaitGroup
for i := 0; i < 10000; i++ {
wg.Add(1)
go func() {
setupComplete.Wait()
Paragraph(1, 5, 20, " ")
wg.Done()
}()
}
setupComplete.Done()
wg.Wait()
}