-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathjsonpath_tokenize_test.go
More file actions
92 lines (87 loc) · 2.53 KB
/
jsonpath_tokenize_test.go
File metadata and controls
92 lines (87 loc) · 2.53 KB
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
// Copyright 2015, 2021; oliver, DoltHub Authors
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
package jsonpath
import "testing"
// Tokenizer tests - verify that JSONPath queries are correctly tokenized
var token_cases = []map[string]interface{}{
map[string]interface{}{
"query": "$.store.*",
"tokens": []string{"$", "store", "*"},
},
map[string]interface{}{
"query": "$.store..price",
"tokens": []string{"$", "store", "..", "price"},
},
map[string]interface{}{
"query": "$.store.book[*].author",
"tokens": []string{"$", "store", "book[*]", "author"},
},
map[string]interface{}{
"query": "$..book[2]",
"tokens": []string{"$", "..", "book[2]"},
},
map[string]interface{}{
"query": "$..book[(@.length-1)]",
"tokens": []string{"$", "..", "book[(@.length-1)]"},
},
map[string]interface{}{
"query": "$..book[0,1]",
"tokens": []string{"$", "..", "book[0,1]"},
},
map[string]interface{}{
"query": "$..book[:2]",
"tokens": []string{"$", "..", "book[:2]"},
},
map[string]interface{}{
"query": "$..book[?(@.isbn)]",
"tokens": []string{"$", "..", "book[?(@.isbn)]"},
},
map[string]interface{}{
"query": "$..book[?(@.price <= $.expensive)]",
"tokens": []string{"$", "..", "book[?(@.price <= $.expensive)]"},
},
map[string]interface{}{
"query": "$..book[?(@.author =~ /.*REES/i)]",
"tokens": []string{"$", "..", "book[?(@.author =~ /.*REES/i)]"},
},
map[string]interface{}{
"query": "$..book[?(@.author =~ /.*REES\\]/i)]",
"tokens": []string{"$", "..", "book[?(@.author =~ /.*REES\\]/i)]"},
},
map[string]interface{}{
"query": "$..*",
"tokens": []string{"$", ".."},
},
// New test cases for recursive descent
map[string]interface{}{
"query": "$..author",
"tokens": []string{"$", "..", "author"},
},
map[string]interface{}{
"query": "$....author",
"tokens": []string{"$", "..", "author"},
},
}
func Test_jsonpath_tokenize(t *testing.T) {
for _, tcase := range token_cases {
query := tcase["query"].(string)
expected := tcase["tokens"].([]string)
t.Run(query, func(t *testing.T) {
tokens, err := tokenize(query)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if len(tokens) != len(expected) {
t.Errorf("expected %d tokens, got %d: %v", len(expected), len(tokens), tokens)
}
for i, token := range tokens {
if i < len(expected) && token != expected[i] {
t.Errorf("token[%d]: expected %q, got %q", i, expected[i], token)
}
}
})
}
}