-
Notifications
You must be signed in to change notification settings - Fork 217
/
keys_test.go
142 lines (136 loc) · 2.91 KB
/
keys_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
package miniredis
import (
"testing"
)
func TestKeysSel(t *testing.T) {
// Helper to test the selection behind KEYS
// pattern -> cases -> should match?
test := func(pat string, chk map[string]bool) {
t.Helper()
patRe := patternRE(pat)
if patRe == nil {
t.Errorf("'%v' won't match anything. Didn't expect that.", pat)
return
}
for key, expected := range chk {
match := patRe.MatchString(key)
if have, want := match, expected; have != want {
t.Errorf("'%v' -> '%v'. have %v, want %v", pat, key, have, want)
}
}
}
test("aap", map[string]bool{
"aap": true,
"aapnoot": false,
"nootaap": false,
"nootaapnoot": false,
"AAP": false,
})
test("aap*", map[string]bool{
"aap": true,
"aapnoot": true,
"nootaap": false,
"nootaapnoot": false,
"AAP": false,
})
// No problem with regexp meta chars?
test("(?:a)ap*", map[string]bool{
"(?:a)ap!": true,
"aap": false,
})
test("*aap*", map[string]bool{
"aap": true,
"aapnoot": true,
"nootaap": true,
"nootaapnoot": true,
"AAP": false,
"a_a_p": false,
})
test(`\*aap*`, map[string]bool{
"*aap": true,
"aap": false,
"*aapnoot": true,
"aapnoot": false,
})
test(`aa?`, map[string]bool{
"aap": true,
"aal": true,
"aaf": true,
"aa?": true,
"aap!": false,
})
test(`aa\?`, map[string]bool{
"aap": false,
"aa?": true,
"aa?!": false,
})
test("aa[pl]", map[string]bool{
"aap": true,
"aal": true,
"aaf": false,
"aa?": false,
"aap!": false,
})
test("[ab]a[pl]", map[string]bool{
"aap": true,
"aal": true,
"bap": true,
"bal": true,
"aaf": false,
"cap": false,
"aa?": false,
"aap!": false,
})
test(`\[ab\]`, map[string]bool{
"[ab]": true,
"a": false,
})
test(`[\[ab]`, map[string]bool{
"[": true,
"a": true,
"b": true,
"c": false,
"]": false,
})
test(`[\[\]]`, map[string]bool{
"[": true,
"]": true,
"c": false,
})
test(`\\ap`, map[string]bool{
`\ap`: true,
`\\ap`: false,
})
// Escape a normal char
test(`\foo`, map[string]bool{
`foo`: true,
`\foo`: false,
})
// Patterns which won't match anything.
test2 := func(pat string) {
t.Helper()
if patternRE(pat) != nil {
t.Errorf("'%v' will match something. Didn't expect that.", pat)
}
}
test2(`ap[\`) // trailing \ in char class
test2(`ap[`) // open char class
test2(`[]ap`) // empty char class
test2(`ap\`) // trailing \
}
func TestMatchKeys(t *testing.T) {
t.Run("simple", func(t *testing.T) {
m, ok := matchKeys([]string{"a", "b", "c"}, "*")
equals(t, true, ok)
equals(t, []string{"a", "b", "c"}, m)
})
t.Run("newlines", func(t *testing.T) {
m, ok := matchKeys([]string{"a", "b\nb", "c"}, "*")
equals(t, true, ok)
equals(t, []string{"a", "b\nb", "c"}, m)
})
t.Run("invalid", func(t *testing.T) {
_, ok := matchKeys([]string{"a", "b", "c"}, "[")
equals(t, false, ok)
})
}