-
Notifications
You must be signed in to change notification settings - Fork 0
/
option_test.go
150 lines (133 loc) · 3.04 KB
/
option_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
package cfg_test
import (
"testing"
"time"
"github.com/zalgonoise/cfg"
)
type testConfig struct {
target string
dur time.Duration
ratio float64
}
func nilOption() cfg.Option[testConfig] {
return cfg.Register[testConfig](nil)
}
func withTarget(target string) cfg.Option[testConfig] {
return cfg.Register(func(cfg testConfig) testConfig {
cfg.target = target
return cfg
})
}
func withDuration(dur time.Duration) cfg.Option[testConfig] {
return cfg.Register(func(cfg testConfig) testConfig {
cfg.dur = dur
return cfg
})
}
func withRatio(ratio float64) cfg.Option[testConfig] {
return cfg.Register(func(cfg testConfig) testConfig {
cfg.ratio = ratio
return cfg
})
}
func TestNew(t *testing.T) {
for _, testcase := range []struct {
name string
options []cfg.Option[testConfig]
wants testConfig
}{
{
name: "ZeroOptions",
wants: testConfig{},
},
{
name: "OneOption",
options: []cfg.Option[testConfig]{
withDuration(time.Minute),
},
wants: testConfig{
target: "",
dur: time.Minute,
ratio: 0,
},
},
{
name: "AllOptions",
options: []cfg.Option[testConfig]{
withTarget("someTarget"),
withDuration(time.Hour),
withRatio(0.5),
},
wants: testConfig{
target: "someTarget",
dur: time.Hour,
ratio: 0.5,
},
},
{
name: "WithOverrides",
options: []cfg.Option[testConfig]{
withTarget("someTarget"),
withDuration(time.Hour),
withRatio(0.5),
withTarget("otherTarget"),
withRatio(0.7),
withTarget("lastTarget"),
},
wants: testConfig{
target: "lastTarget",
dur: time.Hour,
ratio: 0.7,
},
},
{
name: "WithNilOptions",
options: []cfg.Option[testConfig]{
withTarget("someTarget"),
nilOption(),
withDuration(time.Hour),
nilOption(),
withRatio(0.5),
},
wants: testConfig{
target: "someTarget",
dur: time.Hour,
ratio: 0.5,
},
},
{
name: "OnlyNilOptions",
options: []cfg.Option[testConfig]{
nil,
nilOption(),
nil,
nilOption(),
nil,
},
wants: testConfig{},
},
} {
t.Run(testcase.name, func(t *testing.T) {
config := cfg.New[testConfig](testcase.options...)
mustMatch(t, testcase.wants, config)
})
}
}
func TestNoOp(t *testing.T) {
config := cfg.New[testConfig](cfg.NoOp[testConfig]{})
mustMatch(t, testConfig{}, config)
}
// mustMatch is an over-simplification of a testify/require.Equal() call, or a
// reflect.DeepEqual call; but leverages the generics in Go and the comparable type constraint.
//
// It is able to evaluate the types defined in the testConfig data structure, and should be replaced
// only in case it is no longer suitable. For the moment it evaluates the entire data structure as a
// drop-in replacement of testify/require.Equal; but it could also be used to evaluate on a
// field-by-field approach.
func mustMatch[T comparable](t *testing.T, wants, got T) {
if wants != got {
t.Errorf("output mismatch error: wanted %v -- got %v", wants, got)
return
}
t.Logf("item matched value %v", wants)
}