-
Notifications
You must be signed in to change notification settings - Fork 41
/
binder_test.go
192 lines (170 loc) · 3.91 KB
/
binder_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
package bob
import (
"database/sql/driver"
"errors"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
type customString string
type binderTester interface {
Run(t *testing.T, origin []any)
}
type binderTests[Arg any] struct {
args Arg
final []any
err error
}
func (s binderTests[Arg]) Run(t *testing.T, origin []any) {
t.Helper()
t.Run("", func(t *testing.T) {
binder, err := makeBinder[Arg](origin)
if !errors.Is(err, s.err) {
t.Fatal(err)
}
if s.err != nil {
return
}
if diff := cmp.Diff(
s.final, binder.toArgs(s.args), cmpopts.EquateEmpty(),
); diff != "" {
t.Fatal(diff)
}
})
}
func testBinder(t *testing.T, origin []any, tests []binderTester) {
t.Helper()
for _, test := range tests {
test.Run(t, origin)
}
}
func TestBinding(t *testing.T) {
t.Run("no args", func(t *testing.T) {
testBinder(t, []any{}, []binderTester{
binderTests[struct{}]{
args: struct{}{},
final: []any{},
},
binderTests[map[customString]any]{
args: nil,
final: []any{},
},
binderTests[int]{},
})
})
t.Run("no named", func(t *testing.T) {
testBinder(t, []any{1, 2, 3, 4}, []binderTester{
binderTests[struct{}]{
args: struct{}{},
final: []any{1, 2, 3, 4},
},
binderTests[map[string]any]{
args: nil,
final: []any{1, 2, 3, 4},
},
binderTests[int]{
args: 0,
final: []any{1, 2, 3, 4},
},
})
})
t.Run("all named", func(t *testing.T) {
testBinder(t, []any{namedArg("one"), namedArg("two"), namedArg("three"), namedArg("four")}, []binderTester{
binderTests[struct{ One, Two, Three, Four int }]{
args: struct{ One, Two, Three, Four int }{
One: 1, Two: 2, Three: 3, Four: 4,
},
final: []any{1, 2, 3, 4},
},
binderTests[map[string]int]{
args: map[string]int{"one": 1, "two": 2, "three": 3, "four": 4},
final: []any{1, 2, 3, 4},
},
binderTests[int]{
err: ErrBadArgType,
},
})
})
t.Run("mixed named", func(t *testing.T) {
testBinder(t, []any{1, 2, namedArg("three"), 4}, []binderTester{
binderTests[struct{ Three int }]{
args: struct{ Three int }{Three: 3},
final: []any{1, 2, 3, 4},
},
binderTests[map[string]int]{
args: map[string]int{"three": 3},
final: []any{1, 2, 3, 4},
},
binderTests[int]{
args: 3,
final: []any{1, 2, 3, 4},
},
})
})
t.Run("mixed named with nil arg", func(t *testing.T) {
testBinder(t, []any{1, 2, namedArg("three"), 4}, []binderTester{
binderTests[*struct{ Three int }]{
args: nil,
final: []any{1, 2, nil, 4},
},
binderTests[map[string]int]{
args: nil,
final: []any{1, 2, nil, 4},
},
binderTests[*int]{
args: nil,
final: []any{1, 2, (*int)(nil), 4},
},
})
})
t.Run("varaitions of single binder", func(t *testing.T) {
timeVal, err := time.Parse(time.RFC3339, "2021-01-01T00:00:00Z")
if err != nil {
t.Fatal(err)
}
testBinder(t, []any{1, 2, namedArg("three"), 4}, []binderTester{
binderTests[int]{
args: 3,
final: []any{1, 2, 3, 4},
},
binderTests[*int]{
args: nil,
final: []any{1, 2, (*int)(nil), 4},
},
binderTests[time.Time]{
args: timeVal,
final: []any{1, 2, timeVal, 4},
},
binderTests[valuable]{
args: valuable{3},
final: []any{1, 2, valuable{3}, 4},
},
})
})
t.Run("reuse names", func(t *testing.T) {
testBinder(t, []any{1, 2, namedArg("three"), 4, namedArg("three")}, []binderTester{
binderTests[struct{ Three int }]{
args: struct{ Three int }{Three: 3},
final: []any{1, 2, 3, 4, 3},
},
binderTests[map[string]int]{
args: map[string]int{"three": 3},
final: []any{1, 2, 3, 4, 3},
},
binderTests[int]{
args: 3,
final: []any{1, 2, 3, 4, 3},
},
})
})
}
type valuable struct {
val int
}
func (v valuable) Value() (driver.Value, error) {
return v.val, nil
}
func (v valuable) Equal(other valuable) bool {
return v.val == other.val
}