-
Notifications
You must be signed in to change notification settings - Fork 3
/
selection_test.go
151 lines (147 loc) · 2.69 KB
/
selection_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
package jsonmask
import "testing"
type selectionCase struct {
fields string
shouldErr bool
res Selection
}
// a select a field 'a'
// a,b,c comma-separated list will select multiple fields
// a/b/c path will select a field from its parent
// a(b,c) sub-selection will select many fields from a parent
// a/*/c the star * wildcard will select all items in a field
// a,b/c(d,e(f,g/h)),i
var selectionCases = []selectionCase{
{
fields: "/",
shouldErr: true,
},
{
fields: "(",
shouldErr: true,
},
{
fields: ")",
shouldErr: true,
},
{
fields: ",",
shouldErr: true,
},
{
fields: string([]byte("非utf8")[1:]),
shouldErr: true,
},
{
fields: "a/",
shouldErr: true,
},
{
fields: "a(",
shouldErr: true,
},
{
fields: "a)",
shouldErr: true,
},
{
fields: "a,",
shouldErr: true,
},
{
fields: "a(b",
shouldErr: true,
},
{
fields: "a(b))",
shouldErr: true,
},
{
fields: "a(b(c)",
shouldErr: true,
},
{
fields: "a(b(c),)",
shouldErr: true,
},
{
fields: "a(b/(c))",
shouldErr: true,
},
{
fields: "a(b(c)d)",
shouldErr: true,
},
{
fields: "a/(b,c)/d",
shouldErr: true,
},
{
fields: "a",
shouldErr: false,
res: Selection{"a": Selection{}},
},
{
fields: "*",
shouldErr: false,
res: Selection{"*": Selection{}},
},
{
fields: "a,b,c",
shouldErr: false,
res: Selection{
"a": Selection{},
"b": Selection{},
"c": Selection{},
},
},
{
fields: "a/b/c",
shouldErr: false,
res: Selection{"a": Selection{"b": Selection{"c": Selection{}}}},
},
{
fields: "a(b,c)",
shouldErr: false,
res: Selection{"a": Selection{"b": Selection{}, "c": Selection{}}},
},
{
fields: "a(b(c))",
shouldErr: false,
res: Selection{"a": Selection{"b": Selection{"c": Selection{}}}},
},
{
fields: "a/*/c",
shouldErr: false,
res: Selection{"a": Selection{"*": Selection{"c": Selection{}}}},
},
{
fields: "a,b/c(d,e(f,g/h)),i",
shouldErr: false,
res: Selection{
"a": Selection{},
"b": Selection{"c": Selection{
"d": Selection{},
"e": Selection{
"f": Selection{},
"g": Selection{"h": Selection{}},
},
}},
"i": Selection{},
},
},
}
func TestCompile(t *testing.T) {
for _, c := range selectionCases {
res, err := Compile(c.fields)
if c.shouldErr {
if err == nil {
t.Errorf("Testing case[%s] failed: should error but got: %#v", c.fields, res)
}
} else if err != nil {
t.Errorf("Testing case[%s] failed: %s", c.fields, err)
} else if !c.res.equal(res) {
t.Errorf("Testing case[%s] failed, expected: %#v, got: %#v", c.fields, c.res, res)
}
}
}