-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
parser_test.go
212 lines (200 loc) · 4.5 KB
/
parser_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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright (c) Liam Stanley <me@liamstanley.io>. All rights reserved. Use
// of this source code is governed by the MIT license that can be found in
// the LICENSE file.
package queryparser
import (
"reflect"
"testing"
)
type caseArgs struct {
name string
input string
tokens []tokenRef
allowed []string
query *Query
}
var cases = []caseArgs{
{
name: "quoted fields",
input: `foo:"bar" bar:"baz baz1"`,
tokens: []tokenRef{
{tok: tokenIDENT, lit: "foo"},
{tok: tokenDELIM, lit: ":"},
{tok: tokenFIELD, lit: `"bar"`},
{tok: tokenWS, lit: " "},
{tok: tokenIDENT, lit: "bar"},
{tok: tokenDELIM, lit: ":"},
{tok: tokenFIELD, lit: `"baz baz1"`},
},
allowed: []string{"foo", "bar"},
query: &Query{
Raw: "",
Filters: map[string][]string{
"foo": {"bar"},
"bar": {"baz baz1"},
},
},
},
{
name: "unquoted fields",
input: `foo:1,2 bar:3`,
tokens: []tokenRef{
{tok: tokenIDENT, lit: "foo"},
{tok: tokenDELIM, lit: ":"},
{tok: tokenIDENT, lit: "1,2"},
{tok: tokenWS, lit: " "},
{tok: tokenIDENT, lit: "bar"},
{tok: tokenDELIM, lit: ":"},
{tok: tokenIDENT, lit: "3"},
},
query: &Query{
Raw: "",
Filters: map[string][]string{
"foo": {"1", "2"},
"bar": {"3"},
},
},
},
{
name: "trailing",
input: `foo:"bar" test`,
tokens: []tokenRef{
{tok: tokenIDENT, lit: "foo"},
{tok: tokenDELIM, lit: ":"},
{tok: tokenFIELD, lit: `"bar"`},
{tok: tokenWS, lit: " "},
{tok: tokenIDENT, lit: "test"},
},
query: &Query{
Raw: "test",
Filters: map[string][]string{
"foo": {"bar"},
},
},
},
{
name: "trailing with single quote",
input: `foo:'bar' test`,
tokens: []tokenRef{
{tok: tokenIDENT, lit: "foo"},
{tok: tokenDELIM, lit: ":"},
{tok: tokenFIELD, lit: `'bar'`},
{tok: tokenWS, lit: " "},
{tok: tokenIDENT, lit: "test"},
},
query: &Query{
Raw: "test",
Filters: map[string][]string{
"foo": {"bar"},
},
},
},
{
name: "trailing with single and inner double quote",
input: `foo:'bar' test`,
tokens: []tokenRef{
{tok: tokenIDENT, lit: "foo"},
{tok: tokenDELIM, lit: ":"},
{tok: tokenFIELD, lit: `'ba"r'`},
{tok: tokenWS, lit: " "},
{tok: tokenIDENT, lit: "test"},
},
query: &Query{
Raw: "test",
Filters: map[string][]string{
"foo": {"bar"},
},
},
},
{
name: "trailing with random double quotes",
input: `foo:"bar" test " :" a:"`,
tokens: []tokenRef{
{tok: tokenIDENT, lit: "foo"},
{tok: tokenDELIM, lit: ":"},
{tok: tokenFIELD, lit: `"bar"`},
{tok: tokenWS, lit: " "},
{tok: tokenIDENT, lit: "test"},
{tok: tokenWS, lit: " "},
{tok: tokenFIELD, lit: `" :"`},
{tok: tokenWS, lit: " "},
{tok: tokenIDENT, lit: "a"},
{tok: tokenDELIM, lit: ":"},
{tok: tokenFIELD, lit: `"`},
},
query: &Query{
Raw: "test : ",
Filters: map[string][]string{
"foo": {"bar"},
"a": {},
},
},
},
{
name: "strip DefaultCut",
input: `foo:"bar" te$st#!`,
tokens: []tokenRef{
{tok: tokenIDENT, lit: "foo"},
{tok: tokenDELIM, lit: ":"},
{tok: tokenFIELD, lit: `"bar"`},
{tok: tokenWS, lit: " "},
{tok: tokenIDENT, lit: "test"},
},
query: &Query{
Raw: "test",
Filters: map[string][]string{
"foo": {"bar"},
},
},
},
{
name: "text only",
input: `test test1`,
tokens: []tokenRef{
{tok: tokenIDENT, lit: "test"},
{tok: tokenWS, lit: " "},
{tok: tokenIDENT, lit: "test1"},
},
query: &Query{
Raw: "test test1",
Filters: map[string][]string{},
},
},
{
name: "empty",
input: ``,
tokens: []tokenRef{},
query: &Query{Filters: map[string][]string{}},
},
}
func TestScanner(t *testing.T) {
for _, tt := range cases {
t.Run("scanner_"+tt.name, func(t *testing.T) {
s := newScanner(tt.input)
for _, valid := range tt.tokens {
tr := s.nextToken()
// Make sure both have had their fair share of cutsets.
tr.lit = cutsetFunc(tr.lit, DefaultCut)
valid.lit = cutsetFunc(valid.lit, DefaultCut)
if tr.lit != valid.lit || tr.tok != valid.tok {
t.Fatalf("expected %#v but got %#v", valid, tr)
}
}
if tr := s.nextToken(); tr.tok != tokenEOF {
t.Fatalf("expected EOF, got %#v", tr)
}
s.drain()
})
}
}
func TestParser(t *testing.T) {
for _, tt := range cases {
t.Run("parser_"+tt.name, func(t *testing.T) {
p := New(tt.input, Options{Allowed: tt.allowed, CutFn: DefaultCut})
qp := p.Parse()
if !reflect.DeepEqual(tt.query, qp) {
t.Fatalf("expected query %#v, but got %#v", tt.query, qp)
}
})
}
}