-
Notifications
You must be signed in to change notification settings - Fork 5
/
factory_test.go
214 lines (201 loc) · 4.95 KB
/
factory_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
package fixtory
import (
"reflect"
"testing"
)
type testStruct struct {
String string
StringPtr *string
Int int
Float float64
Array []int
Map map[string]bool
ChildStruct *childStruct
}
type childStruct struct {
String string
Int int
}
func TestNewFactory(t *testing.T) {
type args struct {
t *testing.T
v testStruct
}
tests := []struct {
name string
args args
want *Factory[testStruct]
}{
{
name: "initializes new factory",
args: args{
t: t,
v: testStruct{},
},
want: &Factory[testStruct]{
t: t,
productType: reflect.PtrTo(reflect.TypeOf(testStruct{})),
last: testStruct{},
index: 0,
OnBuild: nil,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewFactory(tt.args.t, tt.args.v); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewFactory() = %v, want %v", got, tt.want)
}
})
}
}
func TestBuilder_Build(t *testing.T) {
fac := NewFactory(t, testStruct{})
fac.OnBuild = func(t *testing.T, v *testStruct) {
if v.Int == 0 {
t.Errorf("OnBuild = %d, want not zero", v.Int)
}
}
bluePrint := func(i int, last testStruct) testStruct {
return testStruct{
String: "setByBlueprint",
Int: last.Int + 1,
Float: 0.5,
Array: []int{1, 2, 3},
Map: map[string]bool{"a": true},
ChildStruct: &childStruct{
String: "child",
Int: 10,
},
}
}
tests := []struct {
name string
builder *Builder[testStruct]
want *testStruct
}{
{
name: "struct can be initialized with nil blueprint",
builder: fac.NewBuilder(nil, testStruct{Int: 5}).ResetAfter(),
want: &testStruct{Int: 5},
},
{
name: "struct is overwritten by traits, zero, each param",
builder: fac.NewBuilder(bluePrint, testStruct{String: "setByTrait1", Int: 10}, testStruct{String: "setByTrait2", Array: []int{1, 2, 3}}).Zero("Map").EachParam(testStruct{Float: 10.9}),
want: &testStruct{
String: "setByTrait2",
Int: 10,
Float: 10.9,
Array: []int{1, 2, 3},
Map: nil,
ChildStruct: &childStruct{
String: "child",
Int: 10,
},
},
},
{
name: "empty fields do not overwrite",
builder: fac.NewBuilder(bluePrint, testStruct{}).EachParam(testStruct{}),
want: &testStruct{
String: "setByBlueprint",
Int: 11,
Float: 0.5,
Array: []int{1, 2, 3},
Map: map[string]bool{"a": true},
ChildStruct: &childStruct{
String: "child",
Int: 10,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.builder.Build(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Build() = %v, want %v", got, tt.want)
}
})
}
}
func TestFactory_OnBuild(t *testing.T) {
fac := NewFactory(t, testStruct{})
want := testStruct{
String: "a",
StringPtr: func() *string { s := "a"; return &s }(),
Int: 5,
Float: 0,
Array: nil,
Map: nil,
ChildStruct: nil,
}
t.Run("onBuild is called after product is all set", func(t *testing.T) {
fac.OnBuild = func(t *testing.T, got *testStruct) {
if !reflect.DeepEqual(got, &want) {
t.Errorf("OnBuild() = %v, want %v", got, &want)
}
}
fac.NewBuilder(nil, want).Build()
})
}
func TestBuilder_BuildList(t *testing.T) {
fac := NewFactory(t, testStruct{})
type args struct {
n int
}
tests := []struct {
name string
builder *Builder[testStruct]
args args
want []*testStruct
}{
{
name: "initializes struct list with reset",
builder: fac.NewBuilder(func(i int, last testStruct) testStruct {
lastChild := &childStruct{}
if last.ChildStruct != nil {
lastChild = last.ChildStruct
}
return testStruct{
String: "test",
Int: i + 1,
Map: map[string]bool{"a": true},
ChildStruct: &childStruct{
String: lastChild.String + "a",
},
}
}, testStruct{}).
EachParam(testStruct{Float: 0.1}, testStruct{Float: 0.2}, testStruct{Float: 0.3}).
Zero("Map").
ResetAfter(),
args: args{n: 3},
want: []*testStruct{
{String: "test", Int: 1, Float: 0.1, ChildStruct: &childStruct{String: "a"}},
{String: "test", Int: 2, Float: 0.2, ChildStruct: &childStruct{String: "aa"}},
{String: "test", Int: 3, Float: 0.3, ChildStruct: &childStruct{String: "aaa"}},
},
},
{
name: "initialize struct list with initial index 0 (since index is reset on above test)",
builder: fac.NewBuilder(func(i int, last testStruct) testStruct {
return testStruct{Int: i + 1}
}).
EachParam(testStruct{Float: 0.1}, testStruct{}, testStruct{Float: 0.3}).
Zero("Map").
ResetAfter(),
args: args{n: 3},
want: []*testStruct{
{Int: 1, Float: 0.1},
{Int: 2},
{Int: 3, Float: 0.3},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.builder.BuildList(tt.args.n); !reflect.DeepEqual(got, tt.want) {
t.Errorf("BuildList() = %v, want %v", got, tt.want)
}
})
}
}