-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmatching_test.go
140 lines (126 loc) · 2.79 KB
/
matching_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
package sx_test
import (
"testing"
sx "github.com/travelaudience/go-sx"
)
// The tests in this file test that the correct panics are generated. The tests in helpers_test.go test for
// the correct results.
func TestMatching(t *testing.T) {
type test1 struct {
A int
Lollipop bool
ChocolateID float64
FOOBarBAZ string
}
type test2 struct {
A int `sx:"-"`
}
type test3 struct {
}
type test4 struct {
A int `sx:"-"`
B int `sx:"foo"`
C int `sx:"bar"`
_ int `sx:"baz"`
}
t.Run("panics on bad input", func(t *testing.T) {
var testCases = []struct {
name string
data interface{}
wantPanic string
}{
{
name: "pass a struct, not a pointer",
data: test1{},
wantPanic: "sx: expected a pointer to a struct",
},
{
name: "pass nil",
data: nil,
wantPanic: "sx: expected a pointer to a struct",
},
{
name: "pass something else",
data: "hello",
wantPanic: "sx: expected a pointer to a struct",
},
{
name: "no usable fields",
data: &test2{},
wantPanic: "sx: struct test2 has no usable fields",
},
{
name: "no exported fields",
data: &test3{},
wantPanic: "sx: struct test3 has no usable fields",
},
}
for _, c := range testCases {
func() {
defer func() {
r := recover()
if r == nil {
t.Errorf("case %s: expected a panic", c.name)
return
}
if s, ok := r.(string); ok {
if s != c.wantPanic {
t.Errorf("case %s: expected \"%s\", got \"%s\"", c.name, c.wantPanic, s)
}
return
}
panic(r)
}()
// this calls matchingOf(c.data) straight away
sx.Values(c.data)
}()
}
})
t.Run("ColumnOf panics on unknown field", func(t *testing.T) {
var testCases = []struct {
name string
data interface{}
field string
wantPanic string
}{
{
name: "unknown field",
data: &test1{},
field: "Zzzzz",
wantPanic: "sx: struct test1 has no usable field Zzzzz",
},
{
name: "ignored field",
data: &test4{},
field: "A",
wantPanic: "sx: struct test4 has no usable field A",
},
{
name: "unexported field",
data: &test4{},
field: "d",
wantPanic: "sx: struct test4 has no usable field d",
},
}
for _, c := range testCases {
func() {
defer func() {
r := recover()
if r == nil {
t.Errorf("case %s: expected a panic", c.name)
return
}
if s, ok := r.(string); ok {
if s != c.wantPanic {
t.Errorf("case %s: expected %q, got %q", c.name, c.wantPanic, s)
}
return
}
panic(r)
}()
// this calls matchingOf(c.data).ColumnOf(c.field)
sx.ValueOf(c.data, c.field)
}()
}
})
}