forked from spyzhov/ajson
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_test.go
More file actions
1132 lines (1064 loc) · 39.1 KB
/
Copy pathstring_test.go
File metadata and controls
1132 lines (1064 loc) · 39.1 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 ajson
import (
"testing"
)
// TestSingleArgStringFunctions tests single-argument string functions (upper, lower, trim, reverse)
func TestSingleArgStringFunctions(t *testing.T) {
tests := []struct {
name string
fname string
value *Node
result *Node
fail bool
}{
// upper
{name: "upper: lowercase", fname: "upper", value: StringNode("", "hello"), result: StringNode("", "HELLO")},
{name: "upper: uppercase", fname: "upper", value: StringNode("", "HELLO"), result: StringNode("", "HELLO")},
{name: "upper: mixed", fname: "upper", value: StringNode("", "HeLLo WoRLd"), result: StringNode("", "HELLO WORLD")},
{name: "upper: empty", fname: "upper", value: StringNode("", ""), result: StringNode("", "")},
{name: "upper: unicode", fname: "upper", value: StringNode("", "héllo"), result: StringNode("", "HÉLLO")},
{name: "upper: nil", fname: "upper", value: nil, result: NullNode("")},
{name: "upper: numeric error", fname: "upper", value: NumericNode("", 123), fail: true},
{name: "upper: bool error", fname: "upper", value: BoolNode("", true), fail: true},
// lower
{name: "lower: uppercase", fname: "lower", value: StringNode("", "HELLO"), result: StringNode("", "hello")},
{name: "lower: lowercase", fname: "lower", value: StringNode("", "hello"), result: StringNode("", "hello")},
{name: "lower: mixed", fname: "lower", value: StringNode("", "HeLLo WoRLd"), result: StringNode("", "hello world")},
{name: "lower: empty", fname: "lower", value: StringNode("", ""), result: StringNode("", "")},
{name: "lower: unicode", fname: "lower", value: StringNode("", "HÉLLO"), result: StringNode("", "héllo")},
{name: "lower: nil", fname: "lower", value: nil, result: NullNode("")},
{name: "lower: numeric error", fname: "lower", value: NumericNode("", 123), fail: true},
{name: "lower: array error", fname: "lower", value: ArrayNode("", []*Node{}), fail: true},
// trim
{name: "trim: spaces", fname: "trim", value: StringNode("", " hello "), result: StringNode("", "hello")},
{name: "trim: tabs", fname: "trim", value: StringNode("", "\thello\t"), result: StringNode("", "hello")},
{name: "trim: newlines", fname: "trim", value: StringNode("", "\nhello\n"), result: StringNode("", "hello")},
{name: "trim: mixed whitespace", fname: "trim", value: StringNode("", " \t\nhello \t\n"), result: StringNode("", "hello")},
{name: "trim: no whitespace", fname: "trim", value: StringNode("", "hello"), result: StringNode("", "hello")},
{name: "trim: empty", fname: "trim", value: StringNode("", ""), result: StringNode("", "")},
{name: "trim: only whitespace", fname: "trim", value: StringNode("", " \t\n "), result: StringNode("", "")},
{name: "trim: nil", fname: "trim", value: nil, result: NullNode("")},
{name: "trim: numeric error", fname: "trim", value: NumericNode("", 123), fail: true},
// reverse
{name: "reverse: simple", fname: "reverse", value: StringNode("", "hello"), result: StringNode("", "olleh")},
{name: "reverse: palindrome", fname: "reverse", value: StringNode("", "racecar"), result: StringNode("", "racecar")},
{name: "reverse: empty", fname: "reverse", value: StringNode("", ""), result: StringNode("", "")},
{name: "reverse: single char", fname: "reverse", value: StringNode("", "a"), result: StringNode("", "a")},
{name: "reverse: unicode", fname: "reverse", value: StringNode("", "héllo"), result: StringNode("", "olléh")},
{name: "reverse: with spaces", fname: "reverse", value: StringNode("", "hello world"), result: StringNode("", "dlrow olleh")},
{name: "reverse: nil", fname: "reverse", value: nil, result: NullNode("")},
{name: "reverse: numeric error", fname: "reverse", value: NumericNode("", 123), fail: true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result, err := functions[test.fname](test.value)
if test.fail {
if err == nil {
t.Error("Expected error: nil given")
}
} else if err != nil {
t.Errorf("Unexpected error: %s", err.Error())
} else if ok, err := result.Eq(test.result); !ok {
if err != nil {
t.Errorf("Unexpected error on comparison: %s", err.Error())
}
t.Errorf("Wrong value: %v != %v", result.value.Load(), test.result.value.Load())
}
})
}
}
// TestMultiArgFunctionHelpers tests the helper functions for multi-arg functions
func TestMultiArgFunctionHelpers(t *testing.T) {
t.Run("IsMultiArgFunction", func(t *testing.T) {
// Should return true for known multi-arg functions
knownFuncs := []string{"split", "join", "contains", "hasprefix", "hassuffix", "replace", "substr", "index"}
for _, fname := range knownFuncs {
if !IsMultiArgFunction(fname) {
t.Errorf("IsMultiArgFunction(%s) should return true", fname)
}
// Test case insensitivity
if !IsMultiArgFunction(fname[:1]+fname[1:]) {
t.Errorf("IsMultiArgFunction should be case insensitive for %s", fname)
}
}
// Should return false for unknown functions
if IsMultiArgFunction("unknownfunc") {
t.Error("IsMultiArgFunction(unknownfunc) should return false")
}
// Should return false for single-arg functions
if IsMultiArgFunction("upper") {
t.Error("IsMultiArgFunction(upper) should return false")
}
})
t.Run("GetMultiArgFunction", func(t *testing.T) {
fn, ok := GetMultiArgFunction("split")
if !ok || fn == nil {
t.Error("GetMultiArgFunction(split) should return a function")
}
_, ok = GetMultiArgFunction("unknownfunc")
if ok {
t.Error("GetMultiArgFunction(unknownfunc) should return false")
}
})
t.Run("AddMultiArgFunction", func(t *testing.T) {
name := "testmultiargfunc"
if IsMultiArgFunction(name) {
t.Error("test function should not exist yet")
}
AddMultiArgFunction(name, func(args []*Node) (*Node, error) {
return NumericNode("", 42), nil
})
if !IsMultiArgFunction(name) {
t.Error("test function should exist after adding")
}
})
}
// TestSplitFunction tests the split multi-arg function
func TestSplitFunction(t *testing.T) {
tests := []struct {
name string
args []*Node
result *Node
fail bool
}{
{
name: "split: basic",
args: []*Node{StringNode("", "a,b,c"), StringNode("", ",")},
result: ArrayNode("", []*Node{StringNode("", "a"), StringNode("", "b"), StringNode("", "c")}),
},
{
name: "split: single element",
args: []*Node{StringNode("", "hello"), StringNode("", ",")},
result: ArrayNode("", []*Node{StringNode("", "hello")}),
},
{
name: "split: empty separator",
args: []*Node{StringNode("", "abc"), StringNode("", "")},
result: ArrayNode("", []*Node{StringNode("", "a"), StringNode("", "b"), StringNode("", "c")}),
},
{
name: "split: empty string",
args: []*Node{StringNode("", ""), StringNode("", ",")},
result: ArrayNode("", []*Node{StringNode("", "")}),
},
{
name: "split: space separator",
args: []*Node{StringNode("", "hello world foo"), StringNode("", " ")},
result: ArrayNode("", []*Node{StringNode("", "hello"), StringNode("", "world"), StringNode("", "foo")}),
},
{
name: "split: multi-char separator",
args: []*Node{StringNode("", "a::b::c"), StringNode("", "::")},
result: ArrayNode("", []*Node{StringNode("", "a"), StringNode("", "b"), StringNode("", "c")}),
},
{
name: "split: too few args",
args: []*Node{StringNode("", "hello")},
fail: true,
},
{
name: "split: first arg not string",
args: []*Node{NumericNode("", 123), StringNode("", ",")},
fail: true,
},
{
name: "split: second arg not string",
args: []*Node{StringNode("", "hello"), NumericNode("", 123)},
fail: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
fn, _ := GetMultiArgFunction("split")
result, err := fn(test.args)
if test.fail {
if err == nil {
t.Error("Expected error: nil given")
}
} else if err != nil {
t.Errorf("Unexpected error: %s", err.Error())
} else {
// Compare arrays element by element
if !result.IsArray() {
t.Error("Result should be an array")
return
}
resultArr := result.Inheritors()
expectedArr := test.result.Inheritors()
if len(resultArr) != len(expectedArr) {
t.Errorf("Array length mismatch: got %d, expected %d", len(resultArr), len(expectedArr))
return
}
for i := range resultArr {
if ok, _ := resultArr[i].Eq(expectedArr[i]); !ok {
t.Errorf("Element %d mismatch: got %v, expected %v", i, resultArr[i].value.Load(), expectedArr[i].value.Load())
}
}
}
})
}
}
// TestJoinFunction tests the join multi-arg function
func TestJoinFunction(t *testing.T) {
tests := []struct {
name string
args []*Node
result *Node
fail bool
}{
{
name: "join: basic",
args: []*Node{ArrayNode("", []*Node{StringNode("", "a"), StringNode("", "b"), StringNode("", "c")}), StringNode("", ",")},
result: StringNode("", "a,b,c"),
},
{
name: "join: single element",
args: []*Node{ArrayNode("", []*Node{StringNode("", "hello")}), StringNode("", ",")},
result: StringNode("", "hello"),
},
{
name: "join: empty separator",
args: []*Node{ArrayNode("", []*Node{StringNode("", "a"), StringNode("", "b"), StringNode("", "c")}), StringNode("", "")},
result: StringNode("", "abc"),
},
{
name: "join: empty array",
args: []*Node{ArrayNode("", []*Node{}), StringNode("", ",")},
result: StringNode("", ""),
},
{
name: "join: space separator",
args: []*Node{ArrayNode("", []*Node{StringNode("", "hello"), StringNode("", "world")}), StringNode("", " ")},
result: StringNode("", "hello world"),
},
{
name: "join: multi-char separator",
args: []*Node{ArrayNode("", []*Node{StringNode("", "a"), StringNode("", "b")}), StringNode("", "::")},
result: StringNode("", "a::b"),
},
{
name: "join: too few args",
args: []*Node{ArrayNode("", []*Node{StringNode("", "hello")})},
fail: true,
},
{
name: "join: first arg not array",
args: []*Node{StringNode("", "hello"), StringNode("", ",")},
fail: true,
},
{
name: "join: second arg not string",
args: []*Node{ArrayNode("", []*Node{StringNode("", "a")}), NumericNode("", 123)},
fail: true,
},
{
name: "join: array element not string",
args: []*Node{ArrayNode("", []*Node{StringNode("", "a"), NumericNode("", 123)}), StringNode("", ",")},
fail: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
fn, _ := GetMultiArgFunction("join")
result, err := fn(test.args)
if test.fail {
if err == nil {
t.Error("Expected error: nil given")
}
} else if err != nil {
t.Errorf("Unexpected error: %s", err.Error())
} else if ok, err := result.Eq(test.result); !ok {
if err != nil {
t.Errorf("Unexpected error on comparison: %s", err.Error())
}
t.Errorf("Wrong value: %v != %v", result.value.Load(), test.result.value.Load())
}
})
}
}
// TestContainsFunction tests the contains multi-arg function
func TestContainsFunction(t *testing.T) {
tests := []struct {
name string
args []*Node
result *Node
fail bool
}{
{name: "contains: found", args: []*Node{StringNode("", "hello world"), StringNode("", "world")}, result: BoolNode("", true)},
{name: "contains: not found", args: []*Node{StringNode("", "hello world"), StringNode("", "foo")}, result: BoolNode("", false)},
{name: "contains: at start", args: []*Node{StringNode("", "hello world"), StringNode("", "hello")}, result: BoolNode("", true)},
{name: "contains: at end", args: []*Node{StringNode("", "hello world"), StringNode("", "world")}, result: BoolNode("", true)},
{name: "contains: empty substring", args: []*Node{StringNode("", "hello"), StringNode("", "")}, result: BoolNode("", true)},
{name: "contains: empty string", args: []*Node{StringNode("", ""), StringNode("", "hello")}, result: BoolNode("", false)},
{name: "contains: both empty", args: []*Node{StringNode("", ""), StringNode("", "")}, result: BoolNode("", true)},
{name: "contains: case sensitive", args: []*Node{StringNode("", "Hello"), StringNode("", "hello")}, result: BoolNode("", false)},
{name: "contains: too few args", args: []*Node{StringNode("", "hello")}, fail: true},
{name: "contains: first arg not string", args: []*Node{NumericNode("", 123), StringNode("", "1")}, fail: true},
{name: "contains: second arg not string", args: []*Node{StringNode("", "hello"), NumericNode("", 123)}, fail: true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
fn, _ := GetMultiArgFunction("contains")
result, err := fn(test.args)
if test.fail {
if err == nil {
t.Error("Expected error: nil given")
}
} else if err != nil {
t.Errorf("Unexpected error: %s", err.Error())
} else if ok, err := result.Eq(test.result); !ok {
if err != nil {
t.Errorf("Unexpected error on comparison: %s", err.Error())
}
t.Errorf("Wrong value: %v != %v", result.value.Load(), test.result.value.Load())
}
})
}
}
// TestHasPrefixFunction tests the hasprefix multi-arg function
func TestHasPrefixFunction(t *testing.T) {
tests := []struct {
name string
args []*Node
result *Node
fail bool
}{
{name: "hasprefix: true", args: []*Node{StringNode("", "hello world"), StringNode("", "hello")}, result: BoolNode("", true)},
{name: "hasprefix: false", args: []*Node{StringNode("", "hello world"), StringNode("", "world")}, result: BoolNode("", false)},
{name: "hasprefix: empty prefix", args: []*Node{StringNode("", "hello"), StringNode("", "")}, result: BoolNode("", true)},
{name: "hasprefix: empty string", args: []*Node{StringNode("", ""), StringNode("", "hello")}, result: BoolNode("", false)},
{name: "hasprefix: equal strings", args: []*Node{StringNode("", "hello"), StringNode("", "hello")}, result: BoolNode("", true)},
{name: "hasprefix: case sensitive", args: []*Node{StringNode("", "Hello"), StringNode("", "hello")}, result: BoolNode("", false)},
{name: "hasprefix: too few args", args: []*Node{StringNode("", "hello")}, fail: true},
{name: "hasprefix: first arg not string", args: []*Node{NumericNode("", 123), StringNode("", "1")}, fail: true},
{name: "hasprefix: second arg not string", args: []*Node{StringNode("", "hello"), NumericNode("", 123)}, fail: true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
fn, _ := GetMultiArgFunction("hasprefix")
result, err := fn(test.args)
if test.fail {
if err == nil {
t.Error("Expected error: nil given")
}
} else if err != nil {
t.Errorf("Unexpected error: %s", err.Error())
} else if ok, err := result.Eq(test.result); !ok {
if err != nil {
t.Errorf("Unexpected error on comparison: %s", err.Error())
}
t.Errorf("Wrong value: %v != %v", result.value.Load(), test.result.value.Load())
}
})
}
}
// TestHasSuffixFunction tests the hassuffix multi-arg function
func TestHasSuffixFunction(t *testing.T) {
tests := []struct {
name string
args []*Node
result *Node
fail bool
}{
{name: "hassuffix: true", args: []*Node{StringNode("", "hello world"), StringNode("", "world")}, result: BoolNode("", true)},
{name: "hassuffix: false", args: []*Node{StringNode("", "hello world"), StringNode("", "hello")}, result: BoolNode("", false)},
{name: "hassuffix: empty suffix", args: []*Node{StringNode("", "hello"), StringNode("", "")}, result: BoolNode("", true)},
{name: "hassuffix: empty string", args: []*Node{StringNode("", ""), StringNode("", "hello")}, result: BoolNode("", false)},
{name: "hassuffix: equal strings", args: []*Node{StringNode("", "hello"), StringNode("", "hello")}, result: BoolNode("", true)},
{name: "hassuffix: case sensitive", args: []*Node{StringNode("", "helloWorld"), StringNode("", "world")}, result: BoolNode("", false)},
{name: "hassuffix: too few args", args: []*Node{StringNode("", "hello")}, fail: true},
{name: "hassuffix: first arg not string", args: []*Node{NumericNode("", 123), StringNode("", "1")}, fail: true},
{name: "hassuffix: second arg not string", args: []*Node{StringNode("", "hello"), NumericNode("", 123)}, fail: true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
fn, _ := GetMultiArgFunction("hassuffix")
result, err := fn(test.args)
if test.fail {
if err == nil {
t.Error("Expected error: nil given")
}
} else if err != nil {
t.Errorf("Unexpected error: %s", err.Error())
} else if ok, err := result.Eq(test.result); !ok {
if err != nil {
t.Errorf("Unexpected error on comparison: %s", err.Error())
}
t.Errorf("Wrong value: %v != %v", result.value.Load(), test.result.value.Load())
}
})
}
}
// TestReplaceFunction tests the replace multi-arg function
func TestReplaceFunction(t *testing.T) {
tests := []struct {
name string
args []*Node
result *Node
fail bool
}{
{name: "replace: basic", args: []*Node{StringNode("", "hello world"), StringNode("", "world"), StringNode("", "there")}, result: StringNode("", "hello there")},
{name: "replace: multiple", args: []*Node{StringNode("", "foo foo foo"), StringNode("", "foo"), StringNode("", "bar")}, result: StringNode("", "bar bar bar")},
{name: "replace: not found", args: []*Node{StringNode("", "hello"), StringNode("", "xyz"), StringNode("", "abc")}, result: StringNode("", "hello")},
{name: "replace: empty old", args: []*Node{StringNode("", "abc"), StringNode("", ""), StringNode("", "X")}, result: StringNode("", "XaXbXcX")},
{name: "replace: empty new", args: []*Node{StringNode("", "hello"), StringNode("", "l"), StringNode("", "")}, result: StringNode("", "heo")},
{name: "replace: empty string", args: []*Node{StringNode("", ""), StringNode("", "x"), StringNode("", "y")}, result: StringNode("", "")},
{name: "replace: too few args 1", args: []*Node{StringNode("", "hello")}, fail: true},
{name: "replace: too few args 2", args: []*Node{StringNode("", "hello"), StringNode("", "l")}, fail: true},
{name: "replace: first arg not string", args: []*Node{NumericNode("", 123), StringNode("", "1"), StringNode("", "2")}, fail: true},
{name: "replace: second arg not string", args: []*Node{StringNode("", "hello"), NumericNode("", 1), StringNode("", "2")}, fail: true},
{name: "replace: third arg not string", args: []*Node{StringNode("", "hello"), StringNode("", "l"), NumericNode("", 2)}, fail: true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
fn, _ := GetMultiArgFunction("replace")
result, err := fn(test.args)
if test.fail {
if err == nil {
t.Error("Expected error: nil given")
}
} else if err != nil {
t.Errorf("Unexpected error: %s", err.Error())
} else if ok, err := result.Eq(test.result); !ok {
if err != nil {
t.Errorf("Unexpected error on comparison: %s", err.Error())
}
t.Errorf("Wrong value: %v != %v", result.value.Load(), test.result.value.Load())
}
})
}
}
// TestSubstrFunction tests the substr multi-arg function
func TestSubstrFunction(t *testing.T) {
tests := []struct {
name string
args []*Node
result *Node
fail bool
}{
{name: "substr: from start", args: []*Node{StringNode("", "hello"), NumericNode("", 0)}, result: StringNode("", "hello")},
{name: "substr: from middle", args: []*Node{StringNode("", "hello"), NumericNode("", 2)}, result: StringNode("", "llo")},
{name: "substr: from end", args: []*Node{StringNode("", "hello"), NumericNode("", 5)}, result: StringNode("", "")},
{name: "substr: negative index", args: []*Node{StringNode("", "hello"), NumericNode("", -2)}, result: StringNode("", "lo")},
{name: "substr: negative beyond start", args: []*Node{StringNode("", "hello"), NumericNode("", -10)}, result: StringNode("", "hello")},
{name: "substr: with length", args: []*Node{StringNode("", "hello"), NumericNode("", 1), NumericNode("", 3)}, result: StringNode("", "ell")},
{name: "substr: length beyond end", args: []*Node{StringNode("", "hello"), NumericNode("", 3), NumericNode("", 10)}, result: StringNode("", "lo")},
{name: "substr: zero length", args: []*Node{StringNode("", "hello"), NumericNode("", 2), NumericNode("", 0)}, result: StringNode("", "")},
{name: "substr: empty string", args: []*Node{StringNode("", ""), NumericNode("", 0)}, result: StringNode("", "")},
{name: "substr: too few args", args: []*Node{StringNode("", "hello")}, fail: true},
{name: "substr: first arg not string", args: []*Node{NumericNode("", 123), NumericNode("", 0)}, fail: true},
{name: "substr: second arg not number", args: []*Node{StringNode("", "hello"), StringNode("", "0")}, fail: true},
{name: "substr: third arg not number", args: []*Node{StringNode("", "hello"), NumericNode("", 0), StringNode("", "3")}, fail: true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
fn, _ := GetMultiArgFunction("substr")
result, err := fn(test.args)
if test.fail {
if err == nil {
t.Error("Expected error: nil given")
}
} else if err != nil {
t.Errorf("Unexpected error: %s", err.Error())
} else if ok, err := result.Eq(test.result); !ok {
if err != nil {
t.Errorf("Unexpected error on comparison: %s", err.Error())
}
t.Errorf("Wrong value: %v != %v", result.value.Load(), test.result.value.Load())
}
})
}
}
// TestIndexFunction tests the index multi-arg function
func TestIndexFunction(t *testing.T) {
tests := []struct {
name string
args []*Node
result *Node
fail bool
}{
{name: "index: found at start", args: []*Node{StringNode("", "hello"), StringNode("", "h")}, result: NumericNode("", 0)},
{name: "index: found at middle", args: []*Node{StringNode("", "hello"), StringNode("", "ll")}, result: NumericNode("", 2)},
{name: "index: found at end", args: []*Node{StringNode("", "hello"), StringNode("", "o")}, result: NumericNode("", 4)},
{name: "index: not found", args: []*Node{StringNode("", "hello"), StringNode("", "xyz")}, result: NumericNode("", -1)},
{name: "index: empty substring", args: []*Node{StringNode("", "hello"), StringNode("", "")}, result: NumericNode("", 0)},
{name: "index: empty string", args: []*Node{StringNode("", ""), StringNode("", "hello")}, result: NumericNode("", -1)},
{name: "index: both empty", args: []*Node{StringNode("", ""), StringNode("", "")}, result: NumericNode("", 0)},
{name: "index: case sensitive", args: []*Node{StringNode("", "Hello"), StringNode("", "h")}, result: NumericNode("", -1)},
{name: "index: too few args", args: []*Node{StringNode("", "hello")}, fail: true},
{name: "index: first arg not string", args: []*Node{NumericNode("", 123), StringNode("", "1")}, fail: true},
{name: "index: second arg not string", args: []*Node{StringNode("", "hello"), NumericNode("", 1)}, fail: true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
fn, _ := GetMultiArgFunction("index")
result, err := fn(test.args)
if test.fail {
if err == nil {
t.Error("Expected error: nil given")
}
} else if err != nil {
t.Errorf("Unexpected error: %s", err.Error())
} else if ok, err := result.Eq(test.result); !ok {
if err != nil {
t.Errorf("Unexpected error on comparison: %s", err.Error())
}
t.Errorf("Wrong value: %v != %v", result.value.Load(), test.result.value.Load())
}
})
}
}
// TestStringFunctionsViaEval tests the string functions through the Eval function
// This ensures the RPN parser and eval correctly handle these functions
func TestStringFunctionsViaEval(t *testing.T) {
tests := []struct {
name string
json string
expression string
expected interface{}
fail bool
}{
// Single-arg string functions
{name: "eval upper", json: `{"name":"hello"}`, expression: `upper($.name)`, expected: "HELLO"},
{name: "eval lower", json: `{"name":"HELLO"}`, expression: `lower($.name)`, expected: "hello"},
{name: "eval trim", json: `{"name":" hello "}`, expression: `trim($.name)`, expected: "hello"},
{name: "eval reverse", json: `{"name":"hello"}`, expression: `reverse($.name)`, expected: "olleh"},
// Multi-arg string functions
{name: "eval split", json: `{"data":"a,b,c"}`, expression: `length(split($.data, ','))`, expected: float64(3)},
{name: "eval contains true", json: `{"text":"hello world"}`, expression: `contains($.text, 'world')`, expected: true},
{name: "eval contains false", json: `{"text":"hello world"}`, expression: `contains($.text, 'foo')`, expected: false},
{name: "eval hasprefix true", json: `{"text":"hello world"}`, expression: `hasprefix($.text, 'hello')`, expected: true},
{name: "eval hasprefix false", json: `{"text":"hello world"}`, expression: `hasprefix($.text, 'world')`, expected: false},
{name: "eval hassuffix true", json: `{"text":"hello world"}`, expression: `hassuffix($.text, 'world')`, expected: true},
{name: "eval hassuffix false", json: `{"text":"hello world"}`, expression: `hassuffix($.text, 'hello')`, expected: false},
{name: "eval replace", json: `{"text":"hello world"}`, expression: `replace($.text, 'world', 'there')`, expected: "hello there"},
{name: "eval substr 2 args", json: `{"text":"hello"}`, expression: `substr($.text, 2)`, expected: "llo"},
{name: "eval substr 3 args", json: `{"text":"hello"}`, expression: `substr($.text, 1, 3)`, expected: "ell"},
{name: "eval index found", json: `{"text":"hello"}`, expression: `index($.text, 'l')`, expected: float64(2)},
{name: "eval index not found", json: `{"text":"hello"}`, expression: `index($.text, 'x')`, expected: float64(-1)},
// Combined operations
{name: "eval upper + contains", json: `{"name":"Hello"}`, expression: `contains(upper($.name), 'ELLO')`, expected: true},
{name: "eval split + first", json: `{"data":"a/b/c"}`, expression: `first(split($.data, '/'))`, expected: "a"},
{name: "eval split + last", json: `{"data":"a/b/c"}`, expression: `last(split($.data, '/'))`, expected: "c"},
// Ternary with string functions
{name: "eval ternary contains", json: `{"text":"error: failed"}`, expression: `contains($.text, 'error') ? 'has error' : 'ok'`, expected: "has error"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
root, err := Unmarshal([]byte(test.json))
if err != nil {
t.Fatalf("Failed to unmarshal JSON: %s", err.Error())
}
result, err := Eval(root, test.expression)
if test.fail {
if err == nil {
t.Error("Expected error: nil given")
}
return
}
if err != nil {
t.Errorf("Unexpected error: %s", err.Error())
return
}
switch expected := test.expected.(type) {
case string:
val, err := result.GetString()
if err != nil {
t.Errorf("Expected string result: %s", err.Error())
return
}
if val != expected {
t.Errorf("Wrong value: got %q, expected %q", val, expected)
}
case bool:
val, err := result.GetBool()
if err != nil {
t.Errorf("Expected bool result: %s", err.Error())
return
}
if val != expected {
t.Errorf("Wrong value: got %v, expected %v", val, expected)
}
case float64:
val, err := result.GetNumeric()
if err != nil {
t.Errorf("Expected numeric result: %s", err.Error())
return
}
if val != expected {
t.Errorf("Wrong value: got %v, expected %v", val, expected)
}
default:
t.Errorf("Unknown expected type: %T", test.expected)
}
})
}
}
// TestSplitJoinRoundtrip tests that split and join are inverse operations
func TestSplitJoinRoundtrip(t *testing.T) {
tests := []struct {
original string
separator string
}{
{"a,b,c", ","},
{"hello world", " "},
{"one::two::three", "::"},
{"single", ","},
{"", ","},
}
splitFn, _ := GetMultiArgFunction("split")
joinFn, _ := GetMultiArgFunction("join")
for _, test := range tests {
t.Run(test.original, func(t *testing.T) {
// Split
splitResult, err := splitFn([]*Node{StringNode("", test.original), StringNode("", test.separator)})
if err != nil {
t.Fatalf("Split failed: %s", err.Error())
}
// Join back
joinResult, err := joinFn([]*Node{splitResult, StringNode("", test.separator)})
if err != nil {
t.Fatalf("Join failed: %s", err.Error())
}
// Compare
resultStr, _ := joinResult.GetString()
if resultStr != test.original {
t.Errorf("Roundtrip failed: got %q, expected %q", resultStr, test.original)
}
})
}
}
// TestNestedStringFunctions tests deeply nested function calls
func TestNestedStringFunctions(t *testing.T) {
tests := []struct {
name string
json string
expression string
expected interface{}
}{
// Nested single-arg functions
{
name: "upper(lower(upper))",
json: `{"text":"Hello"}`,
expression: `upper(lower(upper($.text)))`,
expected: "HELLO",
},
{
name: "reverse(reverse)",
json: `{"text":"hello"}`,
expression: `reverse(reverse($.text))`,
expected: "hello",
},
{
name: "trim(upper)",
json: `{"text":" hello "}`,
expression: `upper(trim($.text))`,
expected: "HELLO",
},
// Nested multi-arg with single-arg
{
name: "split with upper",
json: `{"text":"a,b,c"}`,
expression: `first(split(upper($.text), ','))`,
expected: "A",
},
{
name: "contains with lower",
json: `{"text":"HELLO WORLD"}`,
expression: `contains(lower($.text), 'hello')`,
expected: true,
},
{
name: "replace with trim",
json: `{"text":" hello world "}`,
expression: `replace(trim($.text), 'world', 'there')`,
expected: "hello there",
},
// Complex expressions
{
name: "split and get second element",
json: `{"path":"deployment/myapp"}`,
expression: `last(split($.path, '/'))`,
expected: "myapp",
},
{
name: "split and get first element",
json: `{"path":"deployment/myapp"}`,
expression: `first(split($.path, '/'))`,
expected: "deployment",
},
{
name: "conditional with hasprefix",
json: `{"cmd":"scale deployment/nginx --replicas=3"}`,
expression: `hasprefix($.cmd, 'scale') ? 'scaling' : 'other'`,
expected: "scaling",
},
{
name: "extract namespace from command",
json: `{"cmd":"restart deployment/myapp -n production"}`,
expression: `contains($.cmd, ' -n ') ? 'has namespace' : 'no namespace'`,
expected: "has namespace",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
root, err := Unmarshal([]byte(test.json))
if err != nil {
t.Fatalf("Failed to unmarshal JSON: %s", err.Error())
}
result, err := Eval(root, test.expression)
if err != nil {
t.Errorf("Unexpected error: %s", err.Error())
return
}
switch expected := test.expected.(type) {
case string:
val, err := result.GetString()
if err != nil {
t.Errorf("Expected string result: %s", err.Error())
return
}
if val != expected {
t.Errorf("Wrong value: got %q, expected %q", val, expected)
}
case bool:
val, err := result.GetBool()
if err != nil {
t.Errorf("Expected bool result: %s", err.Error())
return
}
if val != expected {
t.Errorf("Wrong value: got %v, expected %v", val, expected)
}
}
})
}
}
// TestStringFunctionsWithJSONPath tests string functions combined with JSONPath queries
func TestStringFunctionsWithJSONPath(t *testing.T) {
tests := []struct {
name string
json string
expression string
expected interface{}
}{
{
name: "split path from array element",
json: `{"items":[{"path":"a/b"},{"path":"c/d"}]}`,
expression: `first(split($.items[0].path, '/'))`,
expected: "a",
},
{
name: "contains with array access",
json: `{"tags":["production","critical"]}`,
expression: `contains($.tags[0], 'prod')`,
expected: true,
},
{
name: "replace in nested object",
json: `{"config":{"endpoint":"http://localhost:8080"}}`,
expression: `replace($.config.endpoint, 'localhost', '127.0.0.1')`,
expected: "http://127.0.0.1:8080",
},
{
name: "upper with nested access",
json: `{"user":{"name":"john doe"}}`,
expression: `upper($.user.name)`,
expected: "JOHN DOE",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
root, err := Unmarshal([]byte(test.json))
if err != nil {
t.Fatalf("Failed to unmarshal JSON: %s", err.Error())
}
result, err := Eval(root, test.expression)
if err != nil {
t.Errorf("Unexpected error: %s", err.Error())
return
}
switch expected := test.expected.(type) {
case string:
val, err := result.GetString()
if err != nil {
t.Errorf("Expected string result: %s", err.Error())
return
}
if val != expected {
t.Errorf("Wrong value: got %q, expected %q", val, expected)
}
case bool:
val, err := result.GetBool()
if err != nil {
t.Errorf("Expected bool result: %s", err.Error())
return
}
if val != expected {
t.Errorf("Wrong value: got %v, expected %v", val, expected)
}
}
})
}
}
// TestStringFunctionsWithArithmetic tests string functions combined with arithmetic
func TestStringFunctionsWithArithmetic(t *testing.T) {
tests := []struct {
name string
json string
expression string
expected float64
}{
{
name: "length of split result",
json: `{"csv":"a,b,c,d,e"}`,
expression: `length(split($.csv, ','))`,
expected: 5,
},
{
name: "index + 1",
json: `{"text":"hello"}`,
expression: `index($.text, 'l') + 1`,
expected: 3,
},
{
name: "substr length",
json: `{"text":"hello world"}`,
expression: `length(substr($.text, 0, 5))`,
expected: 5,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
root, err := Unmarshal([]byte(test.json))
if err != nil {
t.Fatalf("Failed to unmarshal JSON: %s", err.Error())
}
result, err := Eval(root, test.expression)
if err != nil {
t.Errorf("Unexpected error: %s", err.Error())
return
}
val, err := result.GetNumeric()
if err != nil {
t.Errorf("Expected numeric result: %s", err.Error())
return
}
if val != test.expected {
t.Errorf("Wrong value: got %v, expected %v", val, test.expected)
}
})
}
}
// TestStringFunctionsEdgeCases tests edge cases for string functions
func TestStringFunctionsEdgeCases(t *testing.T) {
tests := []struct {
name string
json string
expression string
expected interface{}
fail bool
}{
// Unicode handling
{
name: "split unicode",
json: `{"text":"hello→world→foo"}`,
expression: `length(split($.text, '→'))`,
expected: float64(3),
},
{
name: "upper unicode",
json: `{"text":"café"}`,
expression: `upper($.text)`,
expected: "CAFÉ",
},
{
name: "reverse unicode",
json: `{"text":"hello世界"}`,
expression: `reverse($.text)`,
expected: "界世olleh",
},
// Empty and whitespace
{
name: "trim only spaces",
json: `{"text":" "}`,
expression: `trim($.text)`,
expected: "",
},
{
name: "split empty result elements",
json: `{"text":"a,,b"}`,
expression: `length(split($.text, ','))`,
expected: float64(3),
},
// String literals in expressions
{
name: "contains with literal",
json: `{}`,
expression: `contains('hello world', 'world')`,
expected: true,
},
{
name: "split with literal",
json: `{}`,
expression: `first(split('a:b:c', ':'))`,
expected: "a",
},
{
name: "replace with literal",
json: `{}`,
expression: `replace('hello', 'l', 'L')`,
expected: "heLLo",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
root, err := Unmarshal([]byte(test.json))
if err != nil {
t.Fatalf("Failed to unmarshal JSON: %s", err.Error())
}
result, err := Eval(root, test.expression)
if test.fail {
if err == nil {
t.Error("Expected error: nil given")
}
return
}
if err != nil {
t.Errorf("Unexpected error: %s", err.Error())
return
}
switch expected := test.expected.(type) {
case string:
val, err := result.GetString()
if err != nil {
t.Errorf("Expected string result: %s", err.Error())
return
}
if val != expected {
t.Errorf("Wrong value: got %q, expected %q", val, expected)
}