-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathfaker_test.go
99 lines (85 loc) · 2.17 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
package faker
import (
"encoding/json"
"net"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type testFakerStruct struct {
A int
B string
C *string
D time.Time
E any
F json.Number
}
func TestFaker(t *testing.T) {
a := testFakerStruct{}
if err := FakeObject(&a); err != nil {
t.Fatal(err)
}
assert.NotEmpty(t, a.A)
assert.NotEmpty(t, a.B)
assert.NotEmpty(t, a.C)
assert.NotEmpty(t, a.D)
assert.Empty(t, a.E) // empty interfaces are not faked
assert.Equal(t, json.Number("123456789"), a.F) // json numbers should be numbers
}
type customType string
type fakerStructWithCustomType struct {
A customType
B map[string]customType
C map[customType]string
D []customType
}
func TestFakerWithCustomType(t *testing.T) {
a := fakerStructWithCustomType{}
if err := FakeObject(&a); err != nil {
t.Fatal(err)
}
assert.NotEmpty(t, a.A)
assert.NotEmpty(t, a.B)
assert.NotEmpty(t, a.C)
assert.NotEmpty(t, a.D)
}
func TestFakerWithCustomTypePreserve(t *testing.T) {
a := fakerStructWithCustomType{
A: "A",
B: map[string]customType{"b": "B"},
C: map[customType]string{"c": "C"},
D: []customType{"D", "D2"},
}
if err := FakeObject(&a); err != nil {
t.Fatal(err)
}
assert.EqualValues(t, "A", a.A)
assert.EqualValues(t, map[string]customType{"b": "B"}, a.B)
assert.EqualValues(t, map[customType]string{"c": "C"}, a.C)
assert.EqualValues(t, []customType{"D", "D2"}, a.D)
}
type complexType struct {
IPAddress net.IP
IPAddresses []net.IP
PtrIPAddress *net.IP
PtrIPAddresses []*net.IP
NestedComplex struct {
IPAddress net.IP
}
}
func TestFakerCanFakeNetIP(t *testing.T) {
a := complexType{}
if err := FakeObject(&a); err != nil {
t.Fatal(err)
}
assert.NotEmpty(t, a.IPAddress)
assert.Equal(t, "1.1.1.1", a.IPAddress.String())
assert.Equal(t, 1, len(a.IPAddresses))
assert.Equal(t, "1.1.1.1", a.IPAddresses[0].String())
assert.NotEmpty(t, a.PtrIPAddress)
assert.Equal(t, "1.1.1.1", a.PtrIPAddress.String())
assert.Equal(t, 1, len(a.PtrIPAddresses))
assert.Equal(t, "1.1.1.1", a.PtrIPAddresses[0].String())
assert.NotEmpty(t, a.NestedComplex.IPAddress)
assert.Equal(t, "1.1.1.1", a.NestedComplex.IPAddress.String())
}