-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalue_test.go
More file actions
916 lines (872 loc) · 24.7 KB
/
value_test.go
File metadata and controls
916 lines (872 loc) · 24.7 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
package template
import (
"reflect"
"slices"
"testing"
)
func TestValue_IsNil(t *testing.T) {
tests := []struct {
name string
value any
want bool
}{
{"nil value", nil, true},
{"nil pointer", (*int)(nil), true},
{"nil slice", ([]int)(nil), false},
{"nil map", (map[string]int)(nil), false},
{"nil interface", (any)(nil), true},
{"zero int", 0, false},
{"non-zero int", 42, false},
{"pointer to zero int", new(0), false},
{"pointer to non-zero int", new(42), false},
{"empty string", "", false},
{"non-empty string", "hello", false},
{"empty slice", []int{}, false},
{"non-empty slice", []int{1, 2, 3}, false},
{"empty map", map[string]int{}, false},
{"non-empty map", map[string]int{"a": 1}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewValue(tt.value).IsNil(); got != tt.want {
t.Errorf("NewValue(%v).IsNil() = %v, want %v", tt.value, got, tt.want)
}
})
}
}
func TestValue_IsTrue(t *testing.T) {
tests := []struct {
name string
value any
want bool
}{
{"nil", nil, false},
{"nil pointer", (*int)(nil), false},
{"bool true", true, true},
{"bool false", false, false},
{"pointer to bool true", new(true), true},
{"pointer to bool false", new(false), false},
{"int zero", 0, false},
{"int positive", 42, true},
{"int negative", -42, true},
{"pointer to int zero", new(0), false},
{"pointer to int positive", new(42), true},
{"int8 zero", int8(0), false},
{"int8 non-zero", int8(42), true},
{"int64 zero", int64(0), false},
{"int64 non-zero", int64(42), true},
{"uint zero", uint(0), false},
{"uint non-zero", uint(42), true},
{"uint64 zero", uint64(0), false},
{"uint64 non-zero", uint64(42), true},
{"float32 zero", float32(0), false},
{"float32 non-zero", float32(3.14), true},
{"float64 zero", float64(0), false},
{"float64 non-zero", float64(3.14), true},
{"pointer to float64 zero", new(float64(0)), false},
{"pointer to float64 non-zero", new(3.14), true},
{"empty string", "", false},
{"non-empty string", "hello", true},
{"pointer to empty string", new(""), false},
{"pointer to non-empty string", new("hello"), true},
{"nil slice", ([]int)(nil), false},
{"empty slice", []int{}, false},
{"non-empty slice", []int{1, 2, 3}, true},
{"nil map", (map[string]int)(nil), false},
{"empty map", map[string]int{}, false},
{"non-empty map", map[string]int{"a": 1}, true},
{"empty array", [0]int{}, false},
{"non-empty array", [3]int{1, 2, 3}, true},
{"empty struct", struct{}{}, true},
{"non-empty struct", struct{ Name string }{Name: "test"}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewValue(tt.value).IsTrue(); got != tt.want {
t.Errorf("NewValue(%v).IsTrue() = %v, want %v", tt.value, got, tt.want)
}
})
}
}
func TestValue_String(t *testing.T) {
tests := []struct {
name string
value any
want string
}{
{"nil", nil, ""},
{"nil pointer", (*int)(nil), ""},
{"int", 42, "42"},
{"pointer to int", new(42), "42"},
{"negative int", -42, "-42"},
{"float", 3.14, "3.14"},
{"pointer to float", new(3.14), "3.14"},
{"string", "hello", "hello"},
{"pointer to string", new("hello"), "hello"},
{"empty string", "", ""},
{"bool true", true, "true"},
{"bool false", false, "false"},
{"pointer to bool", new(true), "true"},
{"slice", []int{1, 2, 3}, "[1,2,3]"},
{"map", map[string]int{"a": 1}, `{"a":1}`},
{"nested slice", [][]int{{1, 2}, {3, 4}}, "[[1,2],[3,4]]"},
{"empty slice", []int{}, "[]"},
{"bool slice", []bool{true, false, true}, "[true,false,true]"},
// Unsigned integer types.
{"uint", uint(100), "100"},
{"uint8", uint8(255), "255"},
{"uint16", uint16(1000), "1000"},
{"uint32", uint32(70000), "70000"},
{"uint64", uint64(99999), "99999"},
{"uint zero", uint(0), "0"},
// Float formatting.
{"whole number float", 42.0, "42"},
{"zero float", 0.0, "0"},
{"negative whole float", -10.0, "-10"},
{"decimal float", 3.14, "3.14"},
{"negative decimal float", -2.5, "-2.5"},
{"small decimal float", 0.001, "0.001"},
{"large whole float", 1000000.0, "1000000"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewValue(tt.value).String(); got != tt.want {
t.Errorf("NewValue(%v).String() = %q, want %q", tt.value, got, tt.want)
}
})
}
}
func TestValue_Int(t *testing.T) {
tests := []struct {
name string
value any
want int64
wantError bool
}{
{"nil", nil, 0, true},
{"nil pointer", (*int)(nil), 0, true},
{"int", 42, 42, false},
{"pointer to int", new(42), 42, false},
{"int8", int8(42), 42, false},
{"int16", int16(42), 42, false},
{"int32", int32(42), 42, false},
{"int64", int64(42), 42, false},
{"uint", uint(42), 42, false},
{"uint8", uint8(42), 42, false},
{"uint16", uint16(42), 42, false},
{"uint32", uint32(42), 42, false},
{"uint64", uint64(42), 42, false},
{"float32", float32(42.7), 42, false},
{"float64", float64(42.7), 42, false},
{"pointer to float64", new(42.7), 42, false},
{"bool true", true, 1, false},
{"bool false", false, 0, false},
{"pointer to bool true", new(true), 1, false},
{"string", "hello", 0, true},
{"negative int", -42, -42, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewValue(tt.value).Int()
if tt.wantError {
if err == nil {
t.Errorf("NewValue(%v).Int() error = nil, want error", tt.value)
}
return
}
if err != nil {
t.Errorf("NewValue(%v).Int() unexpected error: %v", tt.value, err)
return
}
if got != tt.want {
t.Errorf("NewValue(%v).Int() = %v, want %v", tt.value, got, tt.want)
}
})
}
}
func TestValue_Float(t *testing.T) {
tests := []struct {
name string
value any
want float64
wantError bool
}{
{"nil", nil, 0, true},
{"nil pointer", (*float64)(nil), 0, true},
{"float32", float32(3.14), float64(float32(3.14)), false},
{"float64", float64(3.14), 3.14, false},
{"pointer to float64", new(3.14), 3.14, false},
{"int", 42, 42.0, false},
{"pointer to int", new(42), 42.0, false},
{"int8", int8(42), 42.0, false},
{"int16", int16(42), 42.0, false},
{"int32", int32(42), 42.0, false},
{"int64", int64(42), 42.0, false},
{"uint", uint(42), 42.0, false},
{"uint8", uint8(42), 42.0, false},
{"uint16", uint16(42), 42.0, false},
{"uint32", uint32(42), 42.0, false},
{"uint64", uint64(42), 42.0, false},
{"string", "hello", 0, true},
{"negative float", -3.14, -3.14, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewValue(tt.value).Float()
if tt.wantError {
if err == nil {
t.Errorf("NewValue(%v).Float() error = nil, want error", tt.value)
}
return
}
if err != nil {
t.Errorf("NewValue(%v).Float() unexpected error: %v", tt.value, err)
return
}
if got != tt.want {
t.Errorf("NewValue(%v).Float() = %v, want %v", tt.value, got, tt.want)
}
})
}
}
func TestValue_Bool(t *testing.T) {
tests := []struct {
name string
value any
want bool
}{
{"nil", nil, false},
{"bool true", true, true},
{"bool false", false, false},
{"pointer to bool true", new(true), true},
{"pointer to bool false", new(false), false},
{"int zero", 0, false},
{"int non-zero", 42, true},
{"empty string", "", false},
{"non-empty string", "hello", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewValue(tt.value).Bool(); got != tt.want {
t.Errorf("NewValue(%v).Bool() = %v, want %v", tt.value, got, tt.want)
}
})
}
}
func TestValue_Len(t *testing.T) {
tests := []struct {
name string
value any
want int
wantError bool
}{
{"nil", nil, 0, false},
{"nil slice", ([]int)(nil), 0, false},
{"empty slice", []int{}, 0, false},
{"non-empty slice", []int{1, 2, 3}, 3, false},
{"pointer to slice", func() *[]int { s := []int{1, 2, 3}; return &s }(), 3, false},
{"empty string", "", 0, false},
{"non-empty string", "hello", 5, false},
{"pointer to string", new("hello"), 5, false},
{"empty map", map[string]int{}, 0, false},
{"non-empty map", map[string]int{"a": 1, "b": 2}, 2, false},
{"pointer to map", func() *map[string]int { m := map[string]int{"a": 1}; return &m }(), 1, false},
{"array", [3]int{1, 2, 3}, 3, false},
{"int", 42, 0, true},
{"bool", true, 0, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewValue(tt.value).Len()
if tt.wantError {
if err == nil {
t.Errorf("NewValue(%v).Len() error = nil, want error", tt.value)
}
return
}
if err != nil {
t.Errorf("NewValue(%v).Len() unexpected error: %v", tt.value, err)
return
}
if got != tt.want {
t.Errorf("NewValue(%v).Len() = %v, want %v", tt.value, got, tt.want)
}
})
}
}
func TestValue_Index(t *testing.T) {
tests := []struct {
name string
value any
index int
want any
wantError bool
}{
{"nil", nil, 0, nil, true},
{"slice-valid", []int{10, 20, 30}, 1, 20, false},
{"slice-first", []int{10, 20, 30}, 0, 10, false},
{"slice-last", []int{10, 20, 30}, 2, 30, false},
{"slice-negative", []int{10, 20, 30}, -1, nil, true},
{"slice-out-of-range", []int{10, 20, 30}, 3, nil, true},
{"pointer-to-slice", func() *[]int { s := []int{10, 20, 30}; return &s }(), 1, 20, false},
{"array-valid", [3]int{10, 20, 30}, 1, 20, false},
{"string-valid", "hello", 1, "e", false},
{"string-first", "hello", 0, "h", false},
{"string-last", "hello", 4, "o", false},
{"string-out-of-range", "hello", 5, nil, true},
{"pointer-to-string", new("hello"), 1, "e", false},
{"map-not-indexable", map[string]int{"a": 1}, 0, nil, true},
{"int-not-indexable", 42, 0, nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewValue(tt.value).Index(tt.index)
if tt.wantError {
if err == nil {
t.Errorf("NewValue(%v).Index(%d) error = nil, want error", tt.value, tt.index)
}
return
}
if err != nil {
t.Errorf("NewValue(%v).Index(%d) unexpected error: %v", tt.value, tt.index, err)
return
}
if got.Interface() != tt.want {
t.Errorf("NewValue(%v).Index(%d) = %v, want %v", tt.value, tt.index, got.Interface(), tt.want)
}
})
}
}
func TestValue_Key(t *testing.T) {
tests := []struct {
name string
value any
key any
want any
wantError bool
}{
{"nil", nil, "key", nil, true},
{"string-key-found", map[string]int{"a": 1, "b": 2}, "a", 1, false},
{"string-key-not-found", map[string]int{"a": 1, "b": 2}, "c", nil, false},
{"pointer-to-map", func() *map[string]int { m := map[string]int{"a": 1}; return &m }(), "a", 1, false},
{"int-key-found", map[int]string{1: "one", 2: "two"}, 1, "one", false},
{"int-key-not-found", map[int]string{1: "one", 2: "two"}, 3, nil, false},
{"slice-not-map", []int{1, 2, 3}, 0, nil, true},
{"string-not-map", "hello", 0, nil, true},
{"int-not-map", 42, 0, nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewValue(tt.value).Key(tt.key)
if tt.wantError {
if err == nil {
t.Errorf("NewValue(%v).Key(%v) error = nil, want error", tt.value, tt.key)
}
return
}
if err != nil {
t.Errorf("NewValue(%v).Key(%v) unexpected error: %v", tt.value, tt.key, err)
return
}
if got.Interface() != tt.want {
t.Errorf("NewValue(%v).Key(%v) = %v, want %v", tt.value, tt.key, got.Interface(), tt.want)
}
})
}
}
func TestValue_Field(t *testing.T) {
type testStruct struct {
Name string
Age int
Email string
}
tests := []struct {
name string
value any
field string
want any
wantError bool
}{
{"nil", nil, "Name", nil, true},
{"struct-field-exists", testStruct{Name: "Alice", Age: 30, Email: "alice@example.com"}, "Name", "Alice", false},
{"struct-field-not-exists", testStruct{Name: "Alice", Age: 30, Email: "alice@example.com"}, "Phone", nil, true},
{"pointer-to-struct", &testStruct{Name: "Bob", Age: 25, Email: "bob@example.com"}, "Age", 25, false},
{"map-key-exists", map[string]any{"Name": "Charlie", "Age": 35}, "Name", "Charlie", false},
{"map-key-not-exists", map[string]any{"Name": "Charlie", "Age": 35}, "Email", nil, false},
{"pointer-to-map", func() *map[string]string { m := map[string]string{"Name": "David"}; return &m }(), "Name", "David", false},
{"slice-no-field", []int{1, 2, 3}, "Name", nil, true},
{"int-no-field", 42, "Name", nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewValue(tt.value).Field(tt.field)
if tt.wantError {
if err == nil {
t.Errorf("NewValue(%v).Field(%q) error = nil, want error", tt.value, tt.field)
}
return
}
if err != nil {
t.Errorf("NewValue(%v).Field(%q) unexpected error: %v", tt.value, tt.field, err)
return
}
if got.Interface() != tt.want {
t.Errorf("NewValue(%v).Field(%q) = %v, want %v", tt.value, tt.field, got.Interface(), tt.want)
}
})
}
}
func TestValue_Field_JSONTag(t *testing.T) {
type tagged struct {
FullName string `json:"name"`
Age int `json:"age,omitempty"`
Hidden string `json:"-"`
NoTag string
}
val := tagged{
FullName: "Alice",
Age: 30,
Hidden: "secret",
NoTag: "visible",
}
tests := []struct {
name string
field string
want any
wantError bool
}{
{"json-tag-name", "name", "Alice", false},
{"json-tag-with-omitempty", "age", 30, false},
{"hidden-field-via-dash", "-", nil, true},
{"field-by-exported-name", "NoTag", "visible", false},
{"field-by-exported-name-FullName", "FullName", "Alice", false},
{"nonexistent-field", "missing", nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewValue(val).Field(tt.field)
if tt.wantError {
if err == nil {
t.Errorf("Value.Field(%q) error = nil, want error", tt.field)
}
return
}
if err != nil {
t.Errorf("Value.Field(%q) unexpected error: %v", tt.field, err)
return
}
if got.Interface() != tt.want {
t.Errorf("Value.Field(%q) = %v, want %v", tt.field, got.Interface(), tt.want)
}
})
}
}
func TestValue_Iterate(t *testing.T) {
tests := []struct {
name string
value any
wantKeys []any
wantVals []any
wantCount int
wantError bool
}{
{"nil", nil, nil, nil, 0, false},
{"empty slice", []int{}, nil, nil, 0, false},
{
"slice with elements",
[]int{10, 20, 30},
[]any{0, 1, 2},
[]any{10, 20, 30},
3, false,
},
{
"pointer to slice",
func() *[]int { s := []int{10, 20}; return &s }(),
[]any{0, 1},
[]any{10, 20},
2, false,
},
{
"array",
[3]string{"a", "b", "c"},
[]any{0, 1, 2},
[]any{"a", "b", "c"},
3, false,
},
{
"map",
map[string]int{"a": 1, "b": 2},
[]any{"a", "b"},
[]any{1, 2},
2, false,
},
{
"pointer to map",
func() *map[string]int {
m := map[string]int{"x": 10}
return &m
}(),
[]any{"x"},
[]any{10},
1, false,
},
{"int-not-iterable", 42, nil, nil, 0, true},
{
"string-iterable",
"hello",
[]any{0, 1, 2, 3, 4},
[]any{"h", "e", "l", "l", "o"},
5, false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
v := NewValue(tt.value)
var keys, vals []any
var count int
err := v.Iterate(func(_ int, c int, key, val *Value) bool {
count = c
keys = append(keys, key.Interface())
vals = append(vals, val.Interface())
return true
})
if tt.wantError {
if err == nil {
t.Errorf("NewValue(%v).Iterate() error = nil, want error", tt.value)
}
return
}
if err != nil {
t.Errorf("NewValue(%v).Iterate() unexpected error: %v", tt.value, err)
return
}
if count != tt.wantCount {
t.Errorf("NewValue(%v).Iterate() count = %d, want %d", tt.value, count, tt.wantCount)
}
if tt.wantCount > 0 {
if !elementsMatch(keys, tt.wantKeys) {
t.Errorf("NewValue(%v).Iterate() keys = %v, want %v", tt.value, keys, tt.wantKeys)
}
if !elementsMatch(vals, tt.wantVals) {
t.Errorf("NewValue(%v).Iterate() vals = %v, want %v", tt.value, vals, tt.wantVals)
}
}
})
}
}
// elementsMatch reports whether two slices contain the same elements
// regardless of order.
func elementsMatch(a, b []any) bool {
if len(a) != len(b) {
return false
}
used := make([]bool, len(b))
for _, av := range a {
found := false
for j, bv := range b {
if !used[j] && reflect.DeepEqual(av, bv) {
used[j] = true
found = true
break
}
}
if !found {
return false
}
}
return true
}
func TestValue_Iterate_EarlyExit(t *testing.T) {
v := NewValue([]int{1, 2, 3, 4, 5})
var got []int
err := v.Iterate(func(idx, _ int, _ *Value, val *Value) bool {
got = append(got, val.Interface().(int))
return idx < 2
})
if err != nil {
t.Fatalf("Iterate() unexpected error: %v", err)
}
want := []int{1, 2, 3}
if !reflect.DeepEqual(got, want) {
t.Errorf("Iterate() collected = %v, want %v", got, want)
}
}
func TestValue_Compare(t *testing.T) {
tests := []struct {
name string
a any
b any
want int
}{
{"both nil", nil, nil, 0},
{"first nil", nil, 42, -1},
{"second nil", 42, nil, 1},
{"int equal", 42, 42, 0},
{"int less", 10, 20, -1},
{"int greater", 20, 10, 1},
{"pointer-to-int less", new(10), new(20), -1},
{"float equal", 3.14, 3.14, 0},
{"float less", 3.14, 6.28, -1},
{"float greater", 6.28, 3.14, 1},
{"pointer-to-float less", new(3.14), new(6.28), -1},
{"mixed int-float equal", 42, 42.0, 0},
{"mixed int-float less", 10, 20.5, -1},
{"string equal", "hello", "hello", 0},
{"string less", "abc", "xyz", -1},
{"string greater", "xyz", "abc", 1},
{"pointer-to-string less", new("abc"), new("xyz"), -1},
{"negative numbers", -10, -5, -1},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewValue(tt.a).Compare(NewValue(tt.b))
if err != nil {
t.Fatalf("Compare(%v, %v) unexpected error: %v", tt.a, tt.b, err)
}
if got != tt.want {
t.Errorf("Compare(%v, %v) = %v, want %v", tt.a, tt.b, got, tt.want)
}
})
}
}
func TestValue_Equals(t *testing.T) {
tests := []struct {
name string
a any
b any
want bool
}{
{"both nil", nil, nil, true},
{"first nil", nil, 42, false},
{"second nil", 42, nil, false},
{"int equal", 42, 42, true},
{"int not equal", 42, 43, false},
{"pointer-to-int equal", new(42), new(42), true},
{"pointer-to-int not equal", new(42), new(43), false},
{"float equal", 3.14, 3.14, true},
{"float not equal", 3.14, 6.28, false},
{"pointer-to-float equal", new(3.14), new(3.14), true},
{"string equal", "hello", "hello", true},
{"string not equal", "hello", "world", false},
{"pointer-to-string equal", new("hello"), new("hello"), true},
{"bool equal", true, true, true},
{"bool not equal", true, false, false},
{"pointer-to-bool equal", new(true), new(true), true},
{"slice equal", []int{1, 2, 3}, []int{1, 2, 3}, true},
{"slice not equal", []int{1, 2, 3}, []int{1, 2, 4}, false},
{"map equal", map[string]int{"a": 1, "b": 2}, map[string]int{"a": 1, "b": 2}, true},
{"map not equal", map[string]int{"a": 1, "b": 2}, map[string]int{"a": 1, "b": 3}, false},
{"different types", 42, "42", false},
// Cross-numeric type comparisons.
{"int and float64 equal", 42, float64(42), true},
{"int and float64 not equal", 42, float64(42.5), false},
{"int64 and uint equal", int64(100), uint(100), true},
{"int32 and float32 equal", int32(7), float32(7), true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewValue(tt.a).Equals(NewValue(tt.b)); got != tt.want {
t.Errorf("Equals(%v, %v) = %v, want %v", tt.a, tt.b, got, tt.want)
}
})
}
}
func TestValue_Interface(t *testing.T) {
tests := []struct {
name string
value any
want any
}{
{"nil", nil, nil},
{"int", 42, 42},
{"string", "hello", "hello"},
{"bool", true, true},
{"slice", []int{1, 2, 3}, []int{1, 2, 3}},
{"map", map[string]int{"a": 1}, map[string]int{"a": 1}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := NewValue(tt.value).Interface()
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewValue(%v).Interface() = %v, want %v", tt.value, got, tt.want)
}
})
}
}
func TestNewValue(t *testing.T) {
tests := []struct {
name string
input any
}{
{"nil", nil},
{"int", 42},
{"string", "hello"},
{"bool", true},
{"slice", []int{1, 2, 3}},
{"map", map[string]int{"a": 1}},
{"pointer", new(42)},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
v := NewValue(tt.input)
if v == nil {
t.Fatal("NewValue() returned nil")
}
if !reflect.DeepEqual(v.Interface(), tt.input) {
t.Errorf("NewValue(%v).Interface() = %v, want %v", tt.input, v.Interface(), tt.input)
}
})
}
}
// =============================================================================
// Edge Case Tests for Coverage
// =============================================================================
func TestValueIsTrueEdgeCases(t *testing.T) {
tests := []struct {
name string
input any
expected bool
}{
{"uint zero", uint(0), false},
{"uint nonzero", uint(1), true},
{"empty array", [0]int{}, false},
{"nonempty array", [2]int{1, 2}, true},
{"struct", struct{ X int }{1}, true},
{"func", func() {}, true},
{"channel", make(chan int), true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
v := NewValue(tt.input)
if got := v.IsTrue(); got != tt.expected {
t.Errorf("IsTrue() = %v, want %v", got, tt.expected)
}
})
}
}
func TestValueFormatSliceItemEdgeCases(t *testing.T) {
// Nested slices.
v := NewValue([][]int{{1, 2}, {3, 4}})
got := v.String()
if got != "[[1,2],[3,4]]" {
t.Errorf("String() = %q, want %q", got, "[[1,2],[3,4]]")
}
// Slice of maps.
v2 := NewValue([]map[string]int{{"a": 1}})
s := v2.String()
if s == "" {
t.Error("String() returned empty for slice of maps")
}
// Slice of bools.
v3 := NewValue([]bool{true, false})
if got := v3.String(); got != "[true,false]" {
t.Errorf("String() = %q, want %q", got, "[true,false]")
}
// Slice of uints.
v4 := NewValue([]uint{10, 20})
if got := v4.String(); got != "[10,20]" {
t.Errorf("String() = %q, want %q", got, "[10,20]")
}
// Slice of floats.
v5 := NewValue([]float64{1.5, 2.5})
if got := v5.String(); got != "[1.5,2.5]" {
t.Errorf("String() = %q, want %q", got, "[1.5,2.5]")
}
}
func TestValueSortingEdgeCases(t *testing.T) {
// Test string keys sorting
keys := []reflect.Value{reflect.ValueOf("banana"), reflect.ValueOf("apple")}
slices.SortFunc(keys, func(a, b reflect.Value) int {
va, vb := NewValue(a.Interface()), NewValue(b.Interface())
if fa, err := va.Float(); err == nil {
if fb, err := vb.Float(); err == nil {
if fa < fb {
return -1
} else if fa > fb {
return 1
}
return 0
}
}
sa, sb := va.String(), vb.String()
if sa < sb {
return -1
} else if sa > sb {
return 1
}
return 0
})
if keys[0].String() != "apple" || keys[1].String() != "banana" {
t.Error("String keys should be sorted alphabetically")
}
// Test numeric keys sorting
numKeys := []reflect.Value{reflect.ValueOf(3), reflect.ValueOf(1)}
slices.SortFunc(numKeys, func(a, b reflect.Value) int {
va, vb := NewValue(a.Interface()), NewValue(b.Interface())
if fa, err := va.Float(); err == nil {
if fb, err := vb.Float(); err == nil {
if fa < fb {
return -1
} else if fa > fb {
return 1
}
return 0
}
}
sa, sb := va.String(), vb.String()
if sa < sb {
return -1
} else if sa > sb {
return 1
}
return 0
})
if numKeys[0].Interface().(int) != 1 || numKeys[1].Interface().(int) != 3 {
t.Error("Numeric keys should be sorted numerically")
}
// Test mixed keys: one numeric, one not — falls back to string
mixedKeys := []reflect.Value{reflect.ValueOf("abc"), reflect.ValueOf(1)}
slices.SortFunc(mixedKeys, func(a, b reflect.Value) int {
va, vb := NewValue(a.Interface()), NewValue(b.Interface())
if fa, err := va.Float(); err == nil {
if fb, err := vb.Float(); err == nil {
if fa < fb {
return -1
} else if fa > fb {
return 1
}
return 0
}
}
sa, sb := va.String(), vb.String()
if sa < sb {
return -1
} else if sa > sb {
return 1
}
return 0
})
// Just verify it doesn't panic
_ = mixedKeys
}
func TestValueStringUint(t *testing.T) {
v := NewValue(uint(42))
if got := v.String(); got != "42" {
t.Errorf("String() = %q, want %q", got, "42")
}
}
type stringerVal string
func (s stringerVal) String() string { return "stringer:" + string(s) }
func TestValueStringStringer(t *testing.T) {
v := NewValue(stringerVal("hello"))
if got := v.String(); got != "stringer:hello" {
t.Errorf("String() = %q, want %q", got, "stringer:hello")
}
}
func TestValueStringJSONFallback(t *testing.T) {
v := NewValue(map[string]int{"x": 1})
got := v.String()
if got == "" {
t.Error("String() returned empty for map")
}
}