path_test.gno
3.67 Kb ยท 62 lines
1package json
2
3import "testing"
4
5func TestParseJSONPath(t *testing.T) {
6 tests := []struct {
7 name string
8 path string
9 expected []string
10 }{
11 {name: "Empty string path", path: "", expected: []string{}},
12 {name: "Root only path", path: "$", expected: []string{"$"}},
13 {name: "Root with dot path", path: "$.", expected: []string{"$"}},
14 {name: "All objects in path", path: "$..", expected: []string{"$", ".."}},
15 {name: "Only children in path", path: "$.*", expected: []string{"$", "*"}},
16 {name: "All objects' children in path", path: "$..*", expected: []string{"$", "..", "*"}},
17 {name: "Simple dot notation path", path: "$.root.element", expected: []string{"$", "root", "element"}},
18 {name: "Complex dot notation path with wildcard", path: "$.root.*.element", expected: []string{"$", "root", "*", "element"}},
19 {name: "Path with array wildcard", path: "$.phoneNumbers[*].type", expected: []string{"$", "phoneNumbers", "*", "type"}},
20 {name: "Path with filter expression", path: "$.store.book[?(@.price < 10)].title", expected: []string{"$", "store", "book", "?(@.price < 10)", "title"}},
21 {name: "Path with formula", path: "$..phoneNumbers..('ty' + 'pe')", expected: []string{"$", "..", "phoneNumbers", "..", "('ty' + 'pe')"}},
22 {name: "Simple bracket notation path", path: "$['root']['element']", expected: []string{"$", "'root'", "'element'"}},
23 {name: "Complex bracket notation path with wildcard", path: "$['root'][*]['element']", expected: []string{"$", "'root'", "*", "'element'"}},
24 {name: "Bracket notation path with integer index", path: "$['store']['book'][0]['title']", expected: []string{"$", "'store'", "'book'", "0", "'title'"}},
25 {name: "Complex path with wildcard in bracket notation", path: "$['root'].*['element']", expected: []string{"$", "'root'", "*", "'element'"}},
26 {name: "Mixed notation path with dot after bracket", path: "$.['root'].*.['element']", expected: []string{"$", "'root'", "*", "'element'"}},
27 {name: "Mixed notation path with dot before bracket", path: "$['root'].*.['element']", expected: []string{"$", "'root'", "*", "'element'"}},
28 {name: "Single character path with root", path: "$.a", expected: []string{"$", "a"}},
29 {name: "Multiple characters path with root", path: "$.abc", expected: []string{"$", "abc"}},
30 {name: "Multiple segments path with root", path: "$.a.b.c", expected: []string{"$", "a", "b", "c"}},
31 {name: "Multiple segments path with wildcard and root", path: "$.a.*.c", expected: []string{"$", "a", "*", "c"}},
32 {name: "Multiple segments path with filter and root", path: "$.a[?(@.b == 'c')].d", expected: []string{"$", "a", "?(@.b == 'c')", "d"}},
33 {name: "Complex path with multiple filters", path: "$.a[?(@.b == 'c')].d[?(@.e == 'f')].g", expected: []string{"$", "a", "?(@.b == 'c')", "d", "?(@.e == 'f')", "g"}},
34 {name: "Complex path with multiple filters and wildcards", path: "$.a[?(@.b == 'c')].*.d[?(@.e == 'f')].g", expected: []string{"$", "a", "?(@.b == 'c')", "*", "d", "?(@.e == 'f')", "g"}},
35 {name: "Path with array index and root", path: "$.a[0].b", expected: []string{"$", "a", "0", "b"}},
36 {name: "Path with multiple array indices and root", path: "$.a[0].b[1].c", expected: []string{"$", "a", "0", "b", "1", "c"}},
37 {name: "Path with array index, wildcard and root", path: "$.a[0].*.c", expected: []string{"$", "a", "0", "*", "c"}},
38 }
39
40 for _, tt := range tests {
41 t.Run(tt.name, func(t *testing.T) {
42 reult, _ := ParsePath(tt.path)
43 if !isEqualSlice(reult, tt.expected) {
44 t.Errorf("ParsePath(%s) expected: %v, got: %v", tt.path, tt.expected, reult)
45 }
46 })
47 }
48}
49
50func isEqualSlice(a, b []string) bool {
51 if len(a) != len(b) {
52 return false
53 }
54
55 for i, v := range a {
56 if v != b[i] {
57 return false
58 }
59 }
60
61 return true
62}