-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser_test.go
More file actions
1098 lines (1052 loc) · 25.6 KB
/
parser_test.go
File metadata and controls
1098 lines (1052 loc) · 25.6 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package jsonpath
import (
"encoding/json"
"reflect"
"strings"
"testing"
)
func TestParseRecursive(t *testing.T) {
tests := []struct {
name string
path string
wantLen int
wantErr bool
errType ErrorType
errContains string
}{
{
name: "empty path (bare recursive descent)",
path: "",
wantErr: true,
errType: ErrSyntax,
errContains: "bare recursive descent",
},
{
name: "simple recursive",
path: "name",
wantLen: 2, // 递归段 + 名称段
},
{
name: "recursive with dot notation",
path: ".name",
wantLen: 2,
},
{
name: "recursive with nested path",
path: "books.title",
wantLen: 3, // 递归段 + books段 + title段
},
{
name: "recursive with bracket notation",
path: "[0].name",
wantLen: 3, // 递归段 + 索引段 + 名称段
},
{
name: "recursive with filter",
path: "[?(@.price > 10)]",
wantLen: 2, // 递归段 + 过滤器段
},
{
name: "invalid filter syntax",
path: "[?(@.price >=)]",
wantErr: true,
errType: ErrInvalidFilter,
errContains: "invalid value",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := parseRecursive(tt.path)
if (err != nil) != tt.wantErr {
t.Errorf("parseRecursive() error = %v, wantErr %v", err, tt.wantErr)
return
}
if err != nil {
if jsonErr, ok := err.(*Error); ok {
if tt.errType != jsonErr.Type {
t.Errorf("parseRecursive() error type = %v, want %v", jsonErr.Type, tt.errType)
}
if tt.errContains != "" && !strings.Contains(jsonErr.Message, tt.errContains) {
t.Errorf("parseRecursive() error = %v, want error containing %v", jsonErr.Message, tt.errContains)
}
} else {
t.Errorf("parseRecursive() error is not a JSONPath error: %v", err)
}
return
}
if len(got) != tt.wantLen {
t.Errorf("parseRecursive() returned %d segments, want %d", len(got), tt.wantLen)
}
// 验证第一个段是否为递归段
if len(got) > 0 {
if _, ok := got[0].(*recursiveSegment); !ok {
t.Error("First segment is not a recursiveSegment")
}
}
})
}
}
func TestRecursiveSegmentString(t *testing.T) {
seg := &recursiveSegment{}
expected := ".."
if got := seg.String(); got != expected {
t.Errorf("recursiveSegment.String() = %v, want %v", got, expected)
}
}
func TestParseMultiIndexSegment(t *testing.T) {
tests := []struct {
name string
content string
want segment
wantErr bool
}{
{
name: "single index",
content: "1",
want: &multiIndexSegment{indices: []int{1}},
wantErr: false,
},
{
name: "multiple indices",
content: "1,2,3",
want: &multiIndexSegment{indices: []int{1, 2, 3}},
wantErr: false,
},
{
name: "negative indices",
content: "-1,-2,-3",
want: &multiIndexSegment{indices: []int{-1, -2, -3}},
wantErr: false,
},
{
name: "mixed indices",
content: "0,1,-1,2,-2",
want: &multiIndexSegment{indices: []int{0, 1, -1, 2, -2}},
wantErr: false,
},
{
name: "with spaces",
content: "1, 2, 3",
want: &multiIndexSegment{indices: []int{1, 2, 3}},
wantErr: false,
},
{
name: "invalid index",
content: "1,a,3",
want: &unionSegment{selectors: []segment{&indexSegment{index: 1}, &nameSegment{name: "a"}, &indexSegment{index: 3}}},
wantErr: false,
},
{
name: "empty index",
content: "1,,3",
wantErr: true,
},
{
name: "trailing comma",
content: "1,2,",
wantErr: true,
},
{
name: "leading comma",
content: ",1,2",
wantErr: true,
},
{
name: "single quoted name",
content: "'name'",
want: &multiNameSegment{names: []string{"name"}},
wantErr: false,
},
{
name: "multiple quoted names",
content: "'name','age'",
want: &multiNameSegment{names: []string{"name", "age"}},
wantErr: false,
},
{
name: "mixed quoted names and indices",
content: "'name',1,'age'",
want: &unionSegment{selectors: []segment{&nameSegment{name: "name"}, &indexSegment{index: 1}, &nameSegment{name: "age"}}},
wantErr: false,
},
{
name: "unquoted names",
content: "name,age",
want: &multiNameSegment{names: []string{"name", "age"}},
wantErr: false,
},
{
name: "quoted names with spaces",
content: "'first name','last name'",
want: &multiNameSegment{names: []string{"first name", "last name"}},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := parseMultiIndexSegment(tt.content)
if (err != nil) != tt.wantErr {
t.Errorf("parseMultiIndexSegment() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr {
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("parseMultiIndexSegment() = %v, want %v", got, tt.want)
}
}
})
}
}
func TestMultiIndexSegmentString(t *testing.T) {
seg := &multiIndexSegment{indices: []int{1, 2, 3}}
expected := "[1,2,3]"
if got := seg.String(); got != expected {
t.Errorf("multiIndexSegment.String() = %v, want %v", got, expected)
}
}
func TestParseSliceSegment(t *testing.T) {
tests := []struct {
name string
content string
want *sliceSegment
wantErr bool
}{
{
name: "empty slice",
content: ":",
want: &sliceSegment{start: 0, end: 0, step: 1, hasStart: false, hasEnd: false},
wantErr: false,
},
{
name: "start only",
content: "1:",
want: &sliceSegment{start: 1, end: 0, step: 1, hasStart: true, hasEnd: false},
wantErr: false,
},
{
name: "end only",
content: ":2",
want: &sliceSegment{start: 0, end: 2, step: 1, hasStart: false, hasEnd: true},
wantErr: false,
},
{
name: "start and end",
content: "1:2",
want: &sliceSegment{start: 1, end: 2, step: 1, hasStart: true, hasEnd: true},
wantErr: false,
},
{
name: "negative indices",
content: "-2:-1",
want: &sliceSegment{start: -2, end: -1, step: 1, hasStart: true, hasEnd: true},
wantErr: false,
},
{
name: "with step",
content: "1:5:2",
want: &sliceSegment{start: 1, end: 5, step: 2, hasStart: true, hasEnd: true},
wantErr: false,
},
{
name: "negative step",
content: "5:1:-1",
want: &sliceSegment{start: 5, end: 1, step: -1, hasStart: true, hasEnd: true},
wantErr: false,
},
{
name: "empty start with step",
content: ":5:2",
want: &sliceSegment{start: 0, end: 5, step: 2, hasStart: false, hasEnd: true},
wantErr: false,
},
{
name: "empty end with step",
content: "1::2",
want: &sliceSegment{start: 1, end: 0, step: 2, hasStart: true, hasEnd: false},
wantErr: false,
},
{
name: "invalid start",
content: "a:2",
wantErr: true,
},
{
name: "invalid end",
content: "1:b",
wantErr: true,
},
{
name: "invalid step",
content: "1:2:c",
wantErr: true,
},
{
name: "zero step",
content: "1:2:0",
want: &sliceSegment{start: 1, end: 2, step: 0, hasStart: true, hasEnd: true},
wantErr: false,
},
{
name: "too many parts",
content: "1:2:3:4",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := parseSliceSegment(tt.content)
if (err != nil) != tt.wantErr {
t.Errorf("parseSliceSegment() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr {
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("parseSliceSegment() = %v, want %v", got, tt.want)
}
}
})
}
}
func TestSliceSegmentString(t *testing.T) {
tests := []struct {
name string
segment *sliceSegment
expected string
}{
{
name: "full slice",
segment: &sliceSegment{start: 1, end: 5, step: 2},
expected: "[1:5:2]",
},
{
name: "without step",
segment: &sliceSegment{start: 1, end: 5, step: 1},
expected: "[1:5]",
},
{
name: "negative indices",
segment: &sliceSegment{start: -2, end: -1, step: 1},
expected: "[-2:-1]",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.segment.String(); got != tt.expected {
t.Errorf("sliceSegment.String() = %v, want %v", got, tt.expected)
}
})
}
}
func TestParseFunctionCall(t *testing.T) {
tests := []struct {
name string
content string
wantName string
wantArgs []interface{}
wantErr bool
errContains string
}{
// 无参数函数
{
name: "no args",
content: "length()",
wantName: "length",
wantArgs: []interface{}{},
},
{
name: "empty args",
content: "count( )",
wantName: "count",
wantArgs: []interface{}{},
},
// 数字参数
{
name: "single number arg",
content: "min(42)",
wantName: "min",
wantArgs: []interface{}{float64(42)},
},
{
name: "multiple number args",
content: "sum(1, 2, 3)",
wantName: "sum",
wantArgs: []interface{}{float64(1), float64(2), float64(3)},
},
{
name: "decimal number args",
content: "avg(1.5, 2.7, 3.9)",
wantName: "avg",
wantArgs: []interface{}{float64(1.5), float64(2.7), float64(3.9)},
},
{
name: "negative number args",
content: "range(-1, -2, -3)",
wantName: "range",
wantArgs: []interface{}{float64(-1), float64(-2), float64(-3)},
},
// 字符串参数
{
name: "single string arg",
content: "match('pattern')",
wantName: "match",
wantArgs: []interface{}{"pattern"},
},
{
name: "multiple string args",
content: "concat('hello', 'world')",
wantName: "concat",
wantArgs: []interface{}{"hello", "world"},
},
{
name: "string args with spaces",
content: "format('hello world', 'goodbye world')",
wantName: "format",
wantArgs: []interface{}{"hello world", "goodbye world"},
},
{
name: "empty string args",
content: "join('', '')",
wantName: "join",
wantArgs: []interface{}{"", ""},
},
// 混合参数
{
name: "mixed string and number args",
content: "format('value', 42)",
wantName: "format",
wantArgs: []interface{}{"value", float64(42)},
},
{
name: "complex mixed args",
content: "transform('data', 1.5, 'options', -3)",
wantName: "transform",
wantArgs: []interface{}{"data", float64(1.5), "options", float64(-3)},
},
// 错误情况
{
name: "missing opening parenthesis",
content: "length",
wantErr: true,
errContains: "missing opening parenthesis",
},
{
name: "missing closing parenthesis",
content: "length(",
wantErr: true,
errContains: "missing closing parenthesis",
},
{
name: "invalid argument type",
content: "func(invalid)",
wantErr: true,
errContains: "unsupported argument type",
},
{
name: "unclosed string argument",
content: "func('unclosed)",
wantErr: true,
errContains: "unsupported argument type",
},
{
name: "unmatched quotes",
content: "func('test\", 42)",
wantErr: true,
errContains: "unsupported argument type",
},
// 边界情况
{
name: "function name with underscore",
content: "my_func()",
wantName: "my_func",
wantArgs: []interface{}{},
},
{
name: "function name with numbers",
content: "func123()",
wantName: "func123",
wantArgs: []interface{}{},
},
{
name: "very large number",
content: "big(9999999999)",
wantName: "big",
wantArgs: []interface{}{float64(9999999999)},
},
{
name: "very small number",
content: "small(-9999999999)",
wantName: "small",
wantArgs: []interface{}{float64(-9999999999)},
},
{
name: "string with escaped quotes",
content: "escape('I''m here')",
wantName: "escape",
wantArgs: []interface{}{"I'm here"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := parseFunctionCall(tt.content)
if (err != nil) != tt.wantErr {
t.Errorf("parseFunctionCall() error = %v, wantErr %v", err, tt.wantErr)
return
}
if err != nil {
if !strings.Contains(err.Error(), tt.errContains) {
t.Errorf("parseFunctionCall() error = %v, want error containing %v", err, tt.errContains)
}
return
}
fs, ok := got.(*functionSegment)
if !ok {
t.Errorf("parseFunctionCall() returned %T, want *functionSegment", got)
return
}
if fs.name != tt.wantName {
t.Errorf("parseFunctionCall() name = %v, want %v", fs.name, tt.wantName)
}
if !reflect.DeepEqual(fs.args, tt.wantArgs) {
t.Errorf("parseFunctionCall() args = %v, want %v", fs.args, tt.wantArgs)
}
})
}
}
func TestFunctionSegmentString(t *testing.T) {
tests := []struct {
name string
segment *functionSegment
expected string
}{
{
name: "no args",
segment: &functionSegment{name: "length", args: []interface{}{}},
expected: "length()",
},
{
name: "single number arg",
segment: &functionSegment{name: "min", args: []interface{}{float64(1)}},
expected: "min(1)",
},
{
name: "multiple args",
segment: &functionSegment{name: "sum", args: []interface{}{float64(1), float64(2), float64(3)}},
expected: "sum(1,2,3)",
},
{
name: "string arg",
segment: &functionSegment{name: "match", args: []interface{}{"pattern"}},
expected: "match('pattern')",
},
{
name: "mixed args",
segment: &functionSegment{name: "format", args: []interface{}{"value", float64(42)}},
expected: "format('value',42)",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.segment.String(); got != tt.expected {
t.Errorf("functionSegment.String() = %v, want %v", got, tt.expected)
}
})
}
}
func TestParseIndexOrName(t *testing.T) {
tests := []struct {
name string
content string
wantType string
wantValue interface{}
wantErr bool
errContains string
}{
// 数字索引
{
name: "positive index",
content: "42",
wantType: "indexSegment",
wantValue: 42,
},
{
name: "negative index",
content: "-1",
wantType: "indexSegment",
wantValue: -1,
},
{
name: "zero index",
content: "0",
wantType: "indexSegment",
wantValue: 0,
},
{
name: "max int (RFC 9535 limit)",
content: "9007199254740991",
wantType: "indexSegment",
wantValue: 9007199254740991,
},
{
name: "min int (RFC 9535 limit)",
content: "-9007199254740991",
wantType: "indexSegment",
wantValue: -9007199254740991,
},
{
name: "max int overflow",
content: "99999999999999999999",
wantErr: true,
wantType: "indexSegment",
wantValue: 0,
},
{
name: "min int overflow",
content: "-99999999999999999999",
wantErr: true,
wantType: "indexSegment",
wantValue: 0,
},
// 字符串字面量
{
name: "quoted string",
content: "'hello'",
wantType: "nameSegment",
wantValue: "hello",
},
{
name: "empty quoted string",
content: "''",
wantType: "nameSegment",
wantValue: "",
},
{
name: "quoted string with spaces",
content: "'hello world'",
wantType: "nameSegment",
wantValue: "hello world",
},
{
name: "quoted string with special characters",
content: "'hello.world'",
wantType: "nameSegment",
wantValue: "hello.world",
},
{
name: "quoted string with numbers",
content: "'123'",
wantType: "nameSegment",
wantValue: "123",
},
{
name: "quoted string with unicode",
content: "'你好'",
wantType: "nameSegment",
wantValue: "你好",
},
// 双引号字符串字面量
{
name: "double quoted string",
content: "\"hello\"",
wantType: "nameSegment",
wantValue: "hello",
},
{
name: "empty double quoted string",
content: "\"\"",
wantType: "nameSegment",
wantValue: "",
},
{
name: "double quoted string with spaces",
content: "\"hello world\"",
wantType: "nameSegment",
wantValue: "hello world",
},
{
name: "double quoted string with special characters",
content: "\"Number of Moons\"",
wantType: "nameSegment",
wantValue: "Number of Moons",
},
{
name: "double quoted string with numbers",
content: "\"123\"",
wantType: "nameSegment",
wantValue: "123",
},
{
name: "double quoted string with unicode",
content: "\"你好\"",
wantType: "nameSegment",
wantValue: "你好",
},
// 普通名称
{
name: "unquoted string",
content: "hello",
wantType: "nameSegment",
wantValue: "hello",
},
{
name: "unquoted string with underscore",
content: "hello_world",
wantType: "nameSegment",
wantValue: "hello_world",
},
{
name: "unquoted string with numbers",
content: "hello123",
wantType: "nameSegment",
wantValue: "hello123",
},
{
name: "unquoted string starting with underscore",
content: "_hello",
wantType: "nameSegment",
wantValue: "_hello",
},
// 函数调用
{
name: "function call without args",
content: "length()",
wantType: "functionSegment",
wantValue: "length",
},
{
name: "function call with number arg",
content: "min(1)",
wantType: "functionSegment",
wantValue: "min",
},
{
name: "function call with string arg",
content: "match('pattern')",
wantType: "functionSegment",
wantValue: "match",
},
{
name: "function call with multiple args",
content: "format('value', 42)",
wantType: "functionSegment",
wantValue: "format",
},
// 边界情况和错误
{
name: "single quote",
content: "'",
wantType: "nameSegment",
wantValue: "'",
},
{
name: "unclosed quote",
content: "'hello",
wantType: "nameSegment",
wantValue: "'hello",
},
{
name: "empty string",
content: "",
wantType: "nameSegment",
wantValue: "",
},
{
name: "whitespace only",
content: " ",
wantType: "nameSegment",
wantValue: " ",
},
{
name: "special characters",
content: "@#$%",
wantType: "nameSegment",
wantValue: "@#$%",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := parseIndexOrName(tt.content)
if (err != nil) != tt.wantErr {
t.Errorf("parseIndexOrName() error = %v, wantErr %v", err, tt.wantErr)
return
}
if err != nil {
if !strings.Contains(err.Error(), tt.errContains) {
t.Errorf("parseIndexOrName() error = %v, want error containing %v", err, tt.errContains)
}
return
}
// 检查段类型
gotType := reflect.TypeOf(got).String()
if !strings.HasSuffix(gotType, tt.wantType) {
t.Errorf("parseIndexOrName() returned %v, want type %v", gotType, tt.wantType)
return
}
// 检查段值
switch v := got.(type) {
case *indexSegment:
if v.index != tt.wantValue.(int) {
t.Errorf("parseIndexOrName() index = %v, want %v", v.index, tt.wantValue)
}
case *nameSegment:
if v.name != tt.wantValue.(string) {
t.Errorf("parseIndexOrName() name = %v, want %v", v.name, tt.wantValue)
}
case *functionSegment:
if v.name != tt.wantValue.(string) {
t.Errorf("parseIndexOrName() function name = %v, want %v", v.name, tt.wantValue)
}
default:
t.Errorf("parseIndexOrName() returned unexpected type %T", got)
}
})
}
}
func TestParseFilterValue(t *testing.T) {
tests := []struct {
name string
value string
want interface{}
wantErr bool
}{
// 测试用例
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := parseFilterValue(tt.value)
if (err != nil) != tt.wantErr {
t.Errorf("parseFilterValue() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("parseFilterValue() = %v, want %v", got, tt.want)
}
})
}
}
func TestOperatorPrecedence(t *testing.T) {
tests := []struct {
name string
path string
document interface{}
expected []interface{}
}{
{
name: "AND higher than OR",
path: `$[?(@.a==1||@.b==2&&@.c==3)]`,
document: []interface{}{
map[string]interface{}{"a": 1, "b": 0, "c": 0},
map[string]interface{}{"a": 0, "b": 2, "c": 3},
map[string]interface{}{"a": 0, "b": 2, "c": 0},
},
expected: []interface{}{
map[string]interface{}{"a": 1, "b": 0, "c": 0},
map[string]interface{}{"a": 0, "b": 2, "c": 3},
},
},
{
name: "OR with AND",
path: `$[?(@.a==0||@.b==2&&@.c==3)]`,
document: []interface{}{
map[string]interface{}{"a": 0, "b": 2, "c": 3},
map[string]interface{}{"a": 1, "b": 0, "c": 0},
},
expected: []interface{}{
map[string]interface{}{"a": 0, "b": 2, "c": 3},
},
},
{
name: "AND with OR",
path: `$[?(@.a==0||@.b==2&&@.c==0)]`,
document: []interface{}{
map[string]interface{}{"a": 0, "b": 2, "c": 0},
map[string]interface{}{"a": 1, "b": 0, "c": 0},
},
expected: []interface{}{
map[string]interface{}{"a": 0, "b": 2, "c": 0},
},
},
{
name: "Parentheses override precedence",
path: `$[?((@.a==1||@.b==2)&&@.c==3)]`,
document: []interface{}{
map[string]interface{}{"a": 1, "b": 0, "c": 3},
map[string]interface{}{"a": 0, "b": 2, "c": 3},
map[string]interface{}{"a": 1, "b": 0, "c": 0},
},
expected: []interface{}{
map[string]interface{}{"a": 1, "b": 0, "c": 3},
map[string]interface{}{"a": 0, "b": 2, "c": 3},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := Query(tt.document, tt.path)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
// Extract values from NodeList for comparison
values := make([]interface{}, len(result))
for i, n := range result {
values[i] = n.Value
}
expectedJSON, _ := json.Marshal(tt.expected)
resultJSON, _ := json.Marshal(values)
if string(expectedJSON) != string(resultJSON) {
t.Errorf("Expected %s, got %s", expectedJSON, resultJSON)
}
})
}
}
func TestExistenceFilterParsing(t *testing.T) {
tests := []struct {
name string
path string
wantOperator string
wantField string
wantErr bool
}{
{
name: "simple existence test",
path: `$[?@.name]`,
wantOperator: "exists",
wantField: "name",
},
{
name: "existence test with parentheses",
path: `$[?(@.name)]`,
wantOperator: "exists",
wantField: "name",
},
{
name: "nested field existence",
path: `$[?@.nested.field]`,
wantOperator: "exists",
wantField: "nested.field",
},
{
name: "negated existence test",
path: `$[?!@.name]`,
wantOperator: "not_exists",
wantField: "name",
},
{
name: "negated existence with parentheses",
path: `$[?!(@.name)]`,
wantOperator: "not_exists",
wantField: "name",
},
{
name: "bare @ with > operator",
path: `$[?@>3]`,
wantOperator: ">",
wantField: "",
},
{
name: "bare @ with == operator",
path: `$[?@=="b"]`,
wantOperator: "==",
wantField: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
segments, err := parse(tt.path)
if (err != nil) != tt.wantErr {
t.Fatalf("parse() error = %v, wantErr %v", err, tt.wantErr)
}
if tt.wantErr {
return