-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfactory.go
More file actions
130 lines (113 loc) · 3.15 KB
/
factory.go
File metadata and controls
130 lines (113 loc) · 3.15 KB
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
package testcraft
import (
"errors"
"reflect"
)
type AttrGenerator[T any] func(instance *T) error
type Factory[T any] struct {
object T
sequence map[string]int
attrsGen []AttrGenerator[T]
valuer Valuer
t reflect.Type
}
// NewFactory creates a new factory for the given object.
func NewFactory[T any](object T) *Factory[T] {
return &Factory[T]{
object: object,
sequence: make(map[string]int),
valuer: defaultValuer(),
}
}
// Attr adds an attribute generator to the factory.
func (f *Factory[T]) Attr(attrsGen ...AttrGenerator[T]) *Factory[T] {
f.attrsGen = append(f.attrsGen, attrsGen...)
return f
}
// With returns a new factory with the given traits applied.
// The original factory is not affected.
func (f *Factory[T]) With(trait ...AttrGenerator[T]) *Factory[T] {
clone := f.clone()
clone.attrsGen = append(clone.attrsGen, trait...)
return clone
}
// Build creates a new instance of the object with the given attributes.
func (f *Factory[T]) Build() (T, error) {
return f.build()
}
// MustBuild creates a new instance of the object with the given attributes and panics on error.
func (f *Factory[T]) MustBuild() T {
v, err := f.build()
if err != nil {
panic(err)
}
return v
}
// Randomize creates a new instance of the object with random values.
func (f *Factory[T]) Randomize() (T, error) {
return f.randomize(false)
}
// MustRandomize creates a new instance of the object with random values and panics on error.
func (f *Factory[T]) MustRandomize() T {
res, err := f.randomize(false)
if err != nil {
panic(err)
}
return reflect.Indirect(reflect.ValueOf(res)).Interface().(T)
}
// RandomizeWithAttrs creates a new instance of the object with random values and applies given attributes.
func (f *Factory[T]) RandomizeWithAttrs() (T, error) {
return f.randomize(true)
}
// MustRandomizeWithAttrs creates a new instance of the object with random values and applies given attributes and panics on error.
func (f *Factory[T]) MustRandomizeWithAttrs() T {
res, err := f.randomize(true)
if err != nil {
panic(err)
}
return reflect.Indirect(reflect.ValueOf(res)).Interface().(T)
}
func (f *Factory[T]) typeOf() reflect.Type {
if f.t == nil {
f.t = reflect.TypeOf(f.object)
}
return f.t
}
func (f *Factory[T]) build() (T, error) {
t := f.typeOf()
v := reflect.New(t)
tp := v.Interface().(*T)
errs := f.applyAttrs(tp)
return reflect.Indirect(v).Interface().(T), errors.Join(errs...)
}
func (f *Factory[T]) applyAttrs(tp *T) []error {
var errs []error
for _, attr := range f.attrsGen {
err := attr(tp)
if err != nil {
errs = append(errs, err)
}
}
return errs
}
func (f *Factory[T]) randomize(applyAttr bool) (T, error) {
res, err := randomize(f.object, f.valuer)
if err != nil {
return reflect.Indirect(reflect.ValueOf(res)).Interface().(T), err
}
if applyAttr {
f.applyAttrs(res.(*T))
}
return reflect.Indirect(reflect.ValueOf(res)).Interface().(T), err
}
func (f *Factory[T]) clone() *Factory[T] {
newAttrs := make([]AttrGenerator[T], len(f.attrsGen))
copy(newAttrs, f.attrsGen)
return &Factory[T]{
object: f.object,
sequence: make(map[string]int),
valuer: f.valuer,
t: f.t,
attrsGen: newAttrs,
}
}