|
1 | | -package jsonpath |
| 1 | +package jsonpath_test |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "github.com/speakeasy-api/jsonpath/pkg/jsonpath/token" |
| 4 | + "github.com/speakeasy-api/jsonpath/pkg/jsonpath" |
| 5 | + "github.com/stretchr/testify/require" |
5 | 6 | "testing" |
6 | 7 | ) |
7 | 8 |
|
8 | 9 | func TestParser(t *testing.T) { |
9 | 10 | tests := []struct { |
10 | | - name string |
11 | | - input string |
12 | | - expected string |
| 11 | + name string |
| 12 | + input string |
| 13 | + invalid bool |
13 | 14 | }{ |
14 | 15 | { |
15 | | - name: "Root node", |
16 | | - input: "$", |
17 | | - expected: "$\n", |
| 16 | + name: "Root node", |
| 17 | + input: "$", |
18 | 18 | }, |
19 | 19 | { |
20 | | - name: "Single Dot child", |
21 | | - input: "$.store", |
22 | | - expected: "$\n├── store\n└── book\n", |
| 20 | + name: "Single Dot child", |
| 21 | + input: "$.store", |
23 | 22 | }, |
24 | 23 | { |
25 | | - name: "Single Bracket child", |
26 | | - input: "$['store']", |
27 | | - expected: "$\n├── store\n", |
| 24 | + name: "Single Bracket child", |
| 25 | + input: "$['store']", |
28 | 26 | }, |
29 | 27 | { |
30 | | - name: "Bracket child", |
31 | | - input: "$['store']['book']", |
32 | | - expected: "$\n├── ['store']\n└── ['book']\n", |
| 28 | + name: "Bracket child", |
| 29 | + input: "$['store']['book']", |
33 | 30 | }, |
34 | 31 | { |
35 | | - name: "Array index", |
36 | | - input: "$[0]", |
37 | | - expected: "$\n└── [0]\n", |
| 32 | + name: "Array index", |
| 33 | + input: "$[0]", |
38 | 34 | }, |
39 | 35 | { |
40 | | - name: "Array slice", |
41 | | - input: "$[1:3]", |
42 | | - expected: "$\n└── [1:3]\n ├── 1\n └── 3\n", |
| 36 | + name: "Array slice", |
| 37 | + input: "$[1:3]", |
43 | 38 | }, |
44 | 39 | { |
45 | | - name: "Array slice with step", |
46 | | - input: "$[0:5:2]", |
47 | | - expected: "$\n└── [0:5:2]\n ├── 0\n ├── 5\n └── 2\n", |
| 40 | + name: "Array slice with step", |
| 41 | + input: "$[0:5:2]", |
48 | 42 | }, |
49 | 43 | { |
50 | | - name: "Array slice with negative step", |
51 | | - input: "$[5:1:-2]", |
52 | | - expected: "$\n└── [5:1:-2]\n ├── 5\n ├── 1\n └── -2\n", |
| 44 | + name: "Array slice with negative step", |
| 45 | + input: "$[5:1:-2]", |
53 | 46 | }, |
54 | 47 | { |
55 | | - name: "Filter expression", |
56 | | - input: "$[?(@.price < 10)]", |
57 | | - expected: "$\n└── [?@ < 10]\n ├── @\n └── 10\n", |
| 48 | + name: "Filter expression", |
| 49 | + input: "$[?(@.price < 10)]", |
58 | 50 | }, |
59 | 51 | { |
60 | | - name: "Nested filter expression", |
61 | | - input: "$[?(@.price < 10 && @.category == 'fiction')]", |
62 | | - expected: "$\n└── [?@ < 10 && @ == 'fiction']\n ├── @ < 10\n │ ├── @\n │ └── 10\n └── @ == 'fiction'\n ├── @\n └── 'fiction'\n", |
| 52 | + name: "Nested filter expression", |
| 53 | + input: "$[?(@.price < 10 && @.category == 'fiction')]", |
63 | 54 | }, |
64 | 55 | { |
65 | | - name: "Function call", |
66 | | - input: "$.books[?(length(@) > 100)]", |
67 | | - expected: "$\n├── books\n└── [?length() > 100]\n └── length() > 100\n ├── length()\n └── 100\n", |
| 56 | + name: "Function call", |
| 57 | + input: "$.books[?(length(@) > 100)]", |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "Invalid missing closing ]", |
| 61 | + input: "$.paths.['/pet'", |
| 62 | + invalid: true, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "Invalid extra input", |
| 66 | + input: "$.paths.['/pet')", |
| 67 | + invalid: true, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "Valid filter", |
| 71 | + input: "$.paths[?(1 == 1)]", |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "Invalid filter", |
| 75 | + input: "$.paths[?(true]", |
| 76 | + invalid: true, |
68 | 77 | }, |
69 | 78 | } |
70 | 79 |
|
71 | 80 | for _, test := range tests { |
72 | 81 | t.Run(test.name, func(t *testing.T) { |
73 | | - tokenizer := token.NewTokenizer(test.input) |
74 | | - |
75 | | - parser := newParserPrivate(tokenizer, tokenizer.Tokenize()) |
76 | | - err := parser.parse() |
77 | | - |
78 | | - if err != nil { |
79 | | - t.Errorf("Unexpected error: %v", err) |
| 82 | + path, err := jsonpath.NewPath(test.input) |
| 83 | + if test.invalid { |
| 84 | + require.Error(t, err) |
80 | 85 | return |
81 | 86 | } |
82 | | - // |
83 | | - //actual := PrintSegments(parser.segments) |
84 | | - //if actual != test.expected { |
85 | | - // t.Errorf("Expected:\n%s\nGot:\n%s", test.expected, actual) |
86 | | - //} |
| 87 | + require.NoError(t, err) |
| 88 | + require.Equal(t, test.input, path.String()) |
87 | 89 | }) |
88 | 90 | } |
89 | 91 | } |
0 commit comments