-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefine_test.go
109 lines (94 loc) · 1.56 KB
/
refine_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
package refine
import (
"errors"
"testing"
)
func TestCheck(t *testing.T) {
type checkDependent struct {
A int `refine:"A > B"`
B int `refine:"B >= 0"`
}
type checkNil struct {
A *int `refine:"A != nil"`
B *int `refine:"B == nil"`
C *int `refine:"C == B"`
D []string `refine:"D != nil"`
E map[int]string `refine:"E != nil"`
}
type checkString struct {
S string "refine:\"S == `foo`\""
}
type checkNestedStruct struct {
C struct {
A *int
} `refine:"C.A == nil"`
}
testCases := []struct {
name string
value any
want error
}{
{
name: "NotStructErr",
value: 2,
want: ErrNotStruct,
},
{
name: "DependentFields",
value: checkDependent{
A: 2,
B: 1,
},
want: nil,
},
{
name: "DependentFieldsErr",
value: checkDependent{
A: 2,
B: 2,
},
want: ErrNotMet,
},
{
name: "NilFields",
value: checkNil{
A: func(x int) *int { return &x }(2),
B: nil,
C: nil,
D: []string{},
E: map[int]string{},
},
want: nil,
},
{
name: "StringMet",
value: checkString{
S: "foo",
},
want: nil,
},
{
name: "StringNotMet",
value: checkString{
S: "not foo",
},
want: ErrNotMet,
},
{
name: "CheckNestedStruct",
value: checkNestedStruct{
C: struct{ A *int }{A: nil},
},
want: ErrEval,
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
got := Check(tc.value)
if !errors.Is(got, tc.want) {
t.Fatalf("got %v; want %v", got, tc.want)
}
})
}
}