forked from trapajim/testcraft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory.go
110 lines (95 loc) · 2.64 KB
/
factory.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
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
}
// 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
}