forked from jmespath/go-jmespath
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lexer_test.go
162 lines (145 loc) · 5.74 KB
/
lexer_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
package jmespath
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
var lexingTests = []struct {
expression string
expected []token
}{
{"*", []token{{tStar, "*", 0, 1}}},
{".", []token{{tDot, ".", 0, 1}}},
{"..", []token{{tRecursive, "..", 0, 2}}},
{"[?", []token{{tFilter, "[?", 0, 2}}},
{"[]", []token{{tFlatten, "[]", 0, 2}}},
{"(", []token{{tLparen, "(", 0, 1}}},
{")", []token{{tRparen, ")", 0, 1}}},
{"[", []token{{tLbracket, "[", 0, 1}}},
{"]", []token{{tRbracket, "]", 0, 1}}},
{"{", []token{{tLbrace, "{", 0, 1}}},
{"}", []token{{tRbrace, "}", 0, 1}}},
{"||", []token{{tOr, "||", 0, 2}}},
{"|", []token{{tPipe, "|", 0, 1}}},
{"29", []token{{tNumber, "29", 0, 2}}},
{"2", []token{{tNumber, "2", 0, 1}}},
{"0", []token{{tNumber, "0", 0, 1}}},
{"-20", []token{{tNumber, "-20", 0, 3}}},
{"foo", []token{{tUnquotedIdentifier, "foo", 0, 3}}},
{`"bar"`, []token{{tQuotedIdentifier, "bar", 0, 3}}},
// Escaping the delimiter
{`"bar\"baz"`, []token{{tQuotedIdentifier, `bar"baz`, 0, 7}}},
{",", []token{{tComma, ",", 0, 1}}},
{":", []token{{tColon, ":", 0, 1}}},
{"<", []token{{tLT, "<", 0, 1}}},
{"<=", []token{{tLTE, "<=", 0, 2}}},
{">", []token{{tGT, ">", 0, 1}}},
{">=", []token{{tGTE, ">=", 0, 2}}},
{"==", []token{{tEQ, "==", 0, 2}}},
{"!=", []token{{tNE, "!=", 0, 2}}},
{"`[0, 1, 2]`", []token{{tJSONLiteral, "[0, 1, 2]", 1, 9}}},
{"'foo'", []token{{tStringLiteral, "foo", 1, 3}}},
{"'a'", []token{{tStringLiteral, "a", 1, 1}}},
{`'foo\'bar'`, []token{{tStringLiteral, "foo'bar", 1, 7}}},
{"@", []token{{tCurrent, "@", 0, 1}}},
{"&", []token{{tExpref, "&", 0, 1}}},
// Quoted identifier unicode escape sequences
{`"\u2713"`, []token{{tQuotedIdentifier, "✓", 0, 3}}},
{`"\\"`, []token{{tQuotedIdentifier, `\`, 0, 1}}},
{"`\"foo\"`", []token{{tJSONLiteral, "\"foo\"", 1, 5}}},
// Combinations of tokens.
{"foo.bar", []token{
{tUnquotedIdentifier, "foo", 0, 3},
{tDot, ".", 3, 1},
{tUnquotedIdentifier, "bar", 4, 3},
}},
{"foo[0]", []token{
{tUnquotedIdentifier, "foo", 0, 3},
{tLbracket, "[", 3, 1},
{tNumber, "0", 4, 1},
{tRbracket, "]", 5, 1},
}},
{"foo[?a<b]", []token{
{tUnquotedIdentifier, "foo", 0, 3},
{tFilter, "[?", 3, 2},
{tUnquotedIdentifier, "a", 5, 1},
{tLT, "<", 6, 1},
{tUnquotedIdentifier, "b", 7, 1},
{tRbracket, "]", 8, 1},
}},
}
func TestCanLexTokens(t *testing.T) {
assert := assert.New(t)
lexer := NewLexer()
for _, tt := range lexingTests {
tokens, err := lexer.tokenize(tt.expression)
if assert.Nil(err) {
errMsg := fmt.Sprintf("Mismatch expected number of tokens: (expected: %s, actual: %s)",
tt.expected, tokens)
tt.expected = append(tt.expected, token{tEOF, "", len(tt.expression), 0})
if assert.Equal(len(tt.expected), len(tokens), errMsg) {
for i, token := range tokens {
expected := tt.expected[i]
assert.Equal(expected, token, "Token not equal")
}
}
}
}
}
var lexingErrorTests = []struct {
expression string
msg string
}{
{"'foo", "Missing closing single quote"},
{"[?foo==bar?]", "Unknown char '?'"},
}
func TestLexingErrors(t *testing.T) {
assert := assert.New(t)
lexer := NewLexer()
for _, tt := range lexingErrorTests {
_, err := lexer.tokenize(tt.expression)
assert.NotNil(err, fmt.Sprintf("Expected lexing error: %s", tt.msg))
}
}
var exprIdentifier = "abcdefghijklmnopqrstuvwxyz"
var exprSubexpr = "abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz"
var deeplyNested50 = "j49.j48.j47.j46.j45.j44.j43.j42.j41.j40.j39.j38.j37.j36.j35.j34.j33.j32.j31.j30.j29.j28.j27.j26.j25.j24.j23.j22.j21.j20.j19.j18.j17.j16.j15.j14.j13.j12.j11.j10.j9.j8.j7.j6.j5.j4.j3.j2.j1.j0"
var deeplyNested50Pipe = "j49|j48|j47|j46|j45|j44|j43|j42|j41|j40|j39|j38|j37|j36|j35|j34|j33|j32|j31|j30|j29|j28|j27|j26|j25|j24|j23|j22|j21|j20|j19|j18|j17|j16|j15|j14|j13|j12|j11|j10|j9|j8|j7|j6|j5|j4|j3|j2|j1|j0"
var deeplyNested50Index = "[49][48][47][46][45][44][43][42][41][40][39][38][37][36][35][34][33][32][31][30][29][28][27][26][25][24][23][22][21][20][19][18][17][16][15][14][13][12][11][10][9][8][7][6][5][4][3][2][1][0]"
var deepProjection104 = "a[*].b[*].c[*].d[*].e[*].f[*].g[*].h[*].i[*].j[*].k[*].l[*].m[*].n[*].o[*].p[*].q[*].r[*].s[*].t[*].u[*].v[*].w[*].x[*].y[*].z[*].a[*].b[*].c[*].d[*].e[*].f[*].g[*].h[*].i[*].j[*].k[*].l[*].m[*].n[*].o[*].p[*].q[*].r[*].s[*].t[*].u[*].v[*].w[*].x[*].y[*].z[*].a[*].b[*].c[*].d[*].e[*].f[*].g[*].h[*].i[*].j[*].k[*].l[*].m[*].n[*].o[*].p[*].q[*].r[*].s[*].t[*].u[*].v[*].w[*].x[*].y[*].z[*].a[*].b[*].c[*].d[*].e[*].f[*].g[*].h[*].i[*].j[*].k[*].l[*].m[*].n[*].o[*].p[*].q[*].r[*].s[*].t[*].u[*].v[*].w[*].x[*].y[*].z[*]"
var exprQuotedIdentifier = `"abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz"`
var quotedIdentifierEscapes = `"\n\r\b\t\n\r\b\t\n\r\b\t\n\r\b\t\n\r\b\t\n\r\b\t\n\r\b\t"`
var rawStringLiteral = `'abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz'`
func BenchmarkLexIdentifier(b *testing.B) {
runLexBenchmark(b, exprIdentifier)
}
func BenchmarkLexSubexpression(b *testing.B) {
runLexBenchmark(b, exprSubexpr)
}
func BenchmarkLexDeeplyNested50(b *testing.B) {
runLexBenchmark(b, deeplyNested50)
}
func BenchmarkLexDeepNested50Pipe(b *testing.B) {
runLexBenchmark(b, deeplyNested50Pipe)
}
func BenchmarkLexDeepNested50Index(b *testing.B) {
runLexBenchmark(b, deeplyNested50Index)
}
func BenchmarkLexQuotedIdentifier(b *testing.B) {
runLexBenchmark(b, exprQuotedIdentifier)
}
func BenchmarkLexQuotedIdentifierEscapes(b *testing.B) {
runLexBenchmark(b, quotedIdentifierEscapes)
}
func BenchmarkLexRawStringLiteral(b *testing.B) {
runLexBenchmark(b, rawStringLiteral)
}
func BenchmarkLexDeepProjection104(b *testing.B) {
runLexBenchmark(b, deepProjection104)
}
func runLexBenchmark(b *testing.B, expression string) {
lexer := NewLexer()
for i := 0; i < b.N; i++ {
lexer.tokenize(expression)
}
}