-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patht_value_system_test.go
More file actions
1126 lines (1029 loc) · 32.5 KB
/
Copy patht_value_system_test.go
File metadata and controls
1126 lines (1029 loc) · 32.5 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 script
import (
"strconv"
"strings"
"testing"
)
// ========== Value 类型系统完整测试 ==========
// 本文件覆盖 value.go 中所有公开方法与关键内部函数
// 包括: NewValue 类型推断, 类型访问器, Equal, GoString, 边界, 类型转换链, 复合值操作, formatValueForPrint
// ---------- NewValue 类型推断 ----------
// Test_ValueSystem_NewValue_Int int 推断为 TypeInt
func Test_ValueSystem_NewValue_Int(t *testing.T) {
t.Run("正整数", func(t *testing.T) {
v := NewValue(42)
if v.Type != TypeInt {
t.Errorf("NewValue(42).Type = %v, want TypeInt", v.Type)
}
if v.Int() != 42 {
t.Errorf("Int() = %d, want 42", v.Int())
}
})
t.Run("零", func(t *testing.T) {
v := NewValue(0)
if v.Type != TypeInt {
t.Errorf("NewValue(0).Type = %v, want TypeInt", v.Type)
}
})
}
// Test_ValueSystem_NewValue_Int64 int64 推断为 TypeInt
func Test_ValueSystem_NewValue_Int64(t *testing.T) {
v := NewValue(int64(100))
if v.Type != TypeInt {
t.Errorf("NewValue(int64).Type = %v, want TypeInt", v.Type)
}
if v.Int() != 100 {
t.Errorf("Int() = %d, want 100", v.Int())
}
}
// Test_ValueSystem_NewValue_Float64 float64 推断为 TypeFloat
func Test_ValueSystem_NewValue_Float64(t *testing.T) {
v := NewValue(3.14)
if v.Type != TypeFloat {
t.Errorf("NewValue(3.14).Type = %v, want TypeFloat", v.Type)
}
if v.Float() != 3.14 {
t.Errorf("Float() = %f, want 3.14", v.Float())
}
}
// Test_ValueSystem_NewValue_String string 推断为 TypeString
func Test_ValueSystem_NewValue_String(t *testing.T) {
v := NewValue("hello")
if v.Type != TypeString {
t.Errorf("NewValue(\"hello\").Type = %v, want TypeString", v.Type)
}
if v.String() != "hello" {
t.Errorf("String() = %q, want \"hello\"", v.String())
}
}
// Test_ValueSystem_NewValue_Bool bool 推断为 TypeBool
func Test_ValueSystem_NewValue_Bool(t *testing.T) {
v := NewValue(true)
if v.Type != TypeBool {
t.Errorf("NewValue(true).Type = %v, want TypeBool", v.Type)
}
if !v.Bool() {
t.Error("Bool() = false, want true")
}
}
// Test_ValueSystem_NewValue_Nil nil 推断为 TypeNil
func Test_ValueSystem_NewValue_Nil(t *testing.T) {
v := NewValue(nil)
if v.Type != TypeNil {
t.Errorf("NewValue(nil).Type = %v, want TypeNil", v.Type)
}
if !v.IsNil() {
t.Error("IsNil() = false, want true")
}
}
// Test_ValueSystem_NewValue_SliceValue []Value 推断为 TypeArray
func Test_ValueSystem_NewValue_SliceValue(t *testing.T) {
elems := []Value{NewValue(1), NewValue(2), NewValue(3)}
v := NewValue(elems)
if v.Type != TypeArray {
t.Errorf("NewValue([]Value).Type = %v, want TypeArray", v.Type)
}
arr := v.Array()
if arr == nil {
t.Fatal("Array() = nil, want non-nil")
}
if len(arr.Elements) != 3 {
t.Errorf("len(Elements) = %d, want 3", len(arr.Elements))
}
}
// Test_ValueSystem_NewValue_ArrayValuePtr *ArrayValue 推断为 TypeArray
func Test_ValueSystem_NewValue_ArrayValuePtr(t *testing.T) {
av := &ArrayValue{Elements: []Value{NewValue(10)}}
v := NewValue(av)
if v.Type != TypeArray {
t.Errorf("NewValue(*ArrayValue).Type = %v, want TypeArray", v.Type)
}
// 验证共享同一指针
if v.Array() != av {
t.Error("Array() 应返回同一个指针")
}
}
// Test_ValueSystem_NewValue_MapValuePtr *MapValue 推断为 TypeMap
func Test_ValueSystem_NewValue_MapValuePtr(t *testing.T) {
mv := &MapValue{Pairs: map[string]Value{"k": NewValue(1)}, KeyType: TypeString}
v := NewValue(mv)
if v.Type != TypeMap {
t.Errorf("NewValue(*MapValue).Type = %v, want TypeMap", v.Type)
}
if v.Map() != mv {
t.Error("Map() 应返回同一个指针")
}
}
// Test_ValueSystem_NewValue_FunctionValuePtr *FunctionValue 推断为 TypeFunction
func Test_ValueSystem_NewValue_FunctionValuePtr(t *testing.T) {
fv := &FunctionValue{Compiled: &CompiledFunction{Name: "fn"}}
v := NewValue(fv)
if v.Type != TypeFunction {
t.Errorf("NewValue(*FunctionValue).Type = %v, want TypeFunction", v.Type)
}
if v.Function() != fv {
t.Error("Function() 应返回同一个指针")
}
}
// Test_ValueSystem_NewValue_UnsupportedSlices 不支持的切片类型降级为 TypeNil
// 引擎仅处理 []Value, 其他切片类型不经过自动转换
func Test_ValueSystem_NewValue_UnsupportedSlices(t *testing.T) {
tests := []struct {
name string
input any
}{
{"[]int", []int{1, 2, 3}},
{"[]string", []string{"a", "b"}},
{"[]interface{}", []interface{}{1, "x", true}},
{"[]int64", []int64{1, 2}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
v := NewValue(tt.input)
if v.Type != TypeNil {
t.Errorf("NewValue(%s).Type = %v, want TypeNil(不支持类型降级)", tt.name, v.Type)
}
})
}
}
// Test_ValueSystem_NewValue_UnsupportedMaps 不支持的 map 类型降级为 TypeNil
func Test_ValueSystem_NewValue_UnsupportedMaps(t *testing.T) {
tests := []struct {
name string
input any
}{
{"map[string]int", map[string]int{"a": 1}},
{"map[string]interface{}", map[string]interface{}{"a": 1}},
{"map[int]string", map[int]string{1: "a"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
v := NewValue(tt.input)
if v.Type != TypeNil {
t.Errorf("NewValue(%s).Type = %v, want TypeNil", tt.name, v.Type)
}
})
}
}
// Test_ValueSystem_NewValue_UnknownType 未知结构体类型降级为 TypeNil
func Test_ValueSystem_NewValue_UnknownType(t *testing.T) {
type custom struct{ X int }
v := NewValue(custom{X: 1})
if !v.IsNil() {
t.Errorf("未知类型应降级为 TypeNil, got Type=%v", v.Type)
}
}
// Test_ValueSystem_NewValue_ZeroValues 零值类型正确
func Test_ValueSystem_NewValue_ZeroValues(t *testing.T) {
t.Run("int零值", func(t *testing.T) {
if NewValue(0).Type != TypeInt {
t.Error("NewValue(0) 应为 TypeInt")
}
})
t.Run("string零值", func(t *testing.T) {
if NewValue("").Type != TypeString {
t.Error("NewValue(\"\") 应为 TypeString")
}
})
t.Run("bool零值", func(t *testing.T) {
if NewValue(false).Type != TypeBool {
t.Error("NewValue(false) 应为 TypeBool")
}
})
t.Run("float零值", func(t *testing.T) {
if NewValue(0.0).Type != TypeFloat {
t.Error("NewValue(0.0) 应为 TypeFloat")
}
})
}
// ---------- 类型访问器方法 ----------
// Test_ValueSystem_Int_Accessor Int() 正常路径
func Test_ValueSystem_Int_Accessor(t *testing.T) {
v := NewValue(99)
if v.Int() != 99 {
t.Errorf("Int() = %d, want 99", v.Int())
}
}
// Test_ValueSystem_Int_OnFloat Int() 对 float 值返回零值(无自动转换)
func Test_ValueSystem_Int_OnFloat(t *testing.T) {
v := NewValue(3.14)
if v.Int() != 0 {
t.Errorf("float 的 Int() = %d, want 0 (getTyped 类型不匹配返回零值)", v.Int())
}
}
// Test_ValueSystem_Int_OnString Int() 对 string 值返回零值
func Test_ValueSystem_Int_OnString(t *testing.T) {
v := NewValue("123")
if v.Int() != 0 {
t.Errorf("string 的 Int() = %d, want 0", v.Int())
}
}
// Test_ValueSystem_Int_OnBool Int() 对 bool 值返回零值
func Test_ValueSystem_Int_OnBool(t *testing.T) {
v := NewValue(true)
if v.Int() != 0 {
t.Errorf("bool 的 Int() = %d, want 0", v.Int())
}
}
// Test_ValueSystem_Int_OnNil Int() 对 nil 值返回零值
func Test_ValueSystem_Int_OnNil(t *testing.T) {
v := NewValue(nil)
if v.Int() != 0 {
t.Errorf("nil 的 Int() = %d, want 0", v.Int())
}
}
// Test_ValueSystem_Float_Accessor Float() 正常路径
func Test_ValueSystem_Float_Accessor(t *testing.T) {
v := NewValue(2.5)
if v.Float() != 2.5 {
t.Errorf("Float() = %f, want 2.5", v.Float())
}
}
// Test_ValueSystem_Float_OnInt Float() 对 int 值自动转换为float64
func Test_ValueSystem_Float_OnInt(t *testing.T) {
v := NewValue(42)
if v.Float() != 42.0 {
t.Errorf("int 的 Float() = %f, want 42.0 (自动转换)", v.Float())
}
}
// Test_ValueSystem_Float_OnNil Float() 对 nil 值返回零值
func Test_ValueSystem_Float_OnNil(t *testing.T) {
v := NewValue(nil)
if v.Float() != 0 {
t.Errorf("nil 的 Float() = %f, want 0", v.Float())
}
}
// Test_ValueSystem_String_Accessor String() 正常路径
func Test_ValueSystem_String_Accessor(t *testing.T) {
v := NewValue("world")
if v.String() != "world" {
t.Errorf("String() = %q, want \"world\"", v.String())
}
}
// Test_ValueSystem_String_OnInt String() 对 int 值返回空字符串
func Test_ValueSystem_String_OnInt(t *testing.T) {
v := NewValue(42)
if v.String() != "" {
t.Errorf("int 的 String() = %q, want \"\"", v.String())
}
}
// Test_ValueSystem_Bool_Accessor Bool() 正常路径
func Test_ValueSystem_Bool_Accessor(t *testing.T) {
t.Run("true", func(t *testing.T) {
if !NewValue(true).Bool() {
t.Error("Bool() = false, want true")
}
})
t.Run("false", func(t *testing.T) {
if NewValue(false).Bool() {
t.Error("Bool() = true, want false")
}
})
}
// Test_ValueSystem_Bool_OnInt Bool() 对 int 值返回 false
func Test_ValueSystem_Bool_OnInt(t *testing.T) {
v := NewValue(1)
if v.Bool() {
t.Error("int 的 Bool() = true, want false")
}
}
// Test_ValueSystem_Array_Accessor Array() 正常路径
func Test_ValueSystem_Array_Accessor(t *testing.T) {
elems := []Value{NewValue(1), NewValue(2)}
v := NewValue(elems)
arr := v.Array()
if arr == nil {
t.Fatal("Array() = nil")
}
if len(arr.Elements) != 2 {
t.Errorf("len = %d, want 2", len(arr.Elements))
}
if arr.Elements[1].Int() != 2 {
t.Errorf("Elements[1] = %d, want 2", arr.Elements[1].Int())
}
}
// Test_ValueSystem_Array_OnNonArray Array() 对非数组类型返回 nil
func Test_ValueSystem_Array_OnNonArray(t *testing.T) {
tests := []struct {
name string
v Value
}{
{"int", NewValue(1)},
{"string", NewValue("x")},
{"nil", NewValue(nil)},
{"bool", NewValue(true)},
{"float", NewValue(1.0)},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.v.Array() != nil {
t.Errorf("%s 的 Array() 应返回 nil", tt.name)
}
})
}
}
// Test_ValueSystem_Map_Accessor Map() 正常路径
func Test_ValueSystem_Map_Accessor(t *testing.T) {
mv := &MapValue{Pairs: map[string]Value{"key": NewValue(42)}, KeyType: TypeString}
v := NewValue(mv)
result := v.Map()
if result == nil {
t.Fatal("Map() = nil")
}
if result.Pairs["key"].Int() != 42 {
t.Errorf("Pairs[\"key\"] = %d, want 42", result.Pairs["key"].Int())
}
}
// Test_ValueSystem_Map_OnNonMap Map() 对非 Map 类型返回 nil
func Test_ValueSystem_Map_OnNonMap(t *testing.T) {
tests := []struct {
name string
v Value
}{
{"int", NewValue(1)},
{"string", NewValue("x")},
{"array", NewValue([]Value{NewValue(1)})},
{"nil", NewValue(nil)},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.v.Map() != nil {
t.Errorf("%s 的 Map() 应返回 nil", tt.name)
}
})
}
}
// Test_ValueSystem_Function_Accessor Function() 正常路径
func Test_ValueSystem_Function_Accessor(t *testing.T) {
fv := &FunctionValue{Compiled: &CompiledFunction{Name: "myFn"}}
v := NewValue(fv)
if v.Function() == nil {
t.Fatal("Function() = nil")
}
if v.Function().Compiled.Name != "myFn" {
t.Errorf("Compiled.Name = %q, want \"myFn\"", v.Function().Compiled.Name)
}
}
// Test_ValueSystem_Function_OnNonFunction Function() 对非函数类型返回 nil
func Test_ValueSystem_Function_OnNonFunction(t *testing.T) {
v := NewValue(42)
if v.Function() != nil {
t.Error("int 的 Function() 应返回 nil")
}
}
// Test_ValueSystem_ExternalFunc_Accessor ExternalFunc() 正常路径
func Test_ValueSystem_ExternalFunc_Accessor(t *testing.T) {
ef := &ExternalFuncValue{Name: "ext", Func: func() {}}
v := Value{Type: TypeExternalFunc, Data: ef}
result := v.ExternalFunc()
if result == nil {
t.Fatal("ExternalFunc() = nil")
}
if result.Name != "ext" {
t.Errorf("Name = %q, want \"ext\"", result.Name)
}
}
// Test_ValueSystem_ExternalFunc_OnNonExternalFunc ExternalFunc() 对非外部函数类型返回 nil
func Test_ValueSystem_ExternalFunc_OnNonExternalFunc(t *testing.T) {
v := NewValue(42)
if v.ExternalFunc() != nil {
t.Error("int 的 ExternalFunc() 应返回 nil")
}
}
// ---------- Equal 相等性比较 ----------
// Test_ValueSystem_Equal_Int int 相等比较
func Test_ValueSystem_Equal_Int(t *testing.T) {
t.Run("相同值", func(t *testing.T) {
if !NewValue(1).Equal(NewValue(1)) {
t.Error("1 == 1 应为 true")
}
})
t.Run("不同值", func(t *testing.T) {
if NewValue(1).Equal(NewValue(2)) {
t.Error("1 == 2 应为 false")
}
})
}
// Test_ValueSystem_Equal_Float float 相等比较
func Test_ValueSystem_Equal_Float(t *testing.T) {
t.Run("相同值", func(t *testing.T) {
if !NewValue(1.5).Equal(NewValue(1.5)) {
t.Error("1.5 == 1.5 应为 true")
}
})
t.Run("不同值", func(t *testing.T) {
if NewValue(1.5).Equal(NewValue(2.5)) {
t.Error("1.5 == 2.5 应为 false")
}
})
}
// Test_ValueSystem_Equal_String string 相等比较
func Test_ValueSystem_Equal_String(t *testing.T) {
t.Run("相同值", func(t *testing.T) {
if !NewValue("abc").Equal(NewValue("abc")) {
t.Error("\"abc\" == \"abc\" 应为 true")
}
})
t.Run("不同值", func(t *testing.T) {
if NewValue("abc").Equal(NewValue("abd")) {
t.Error("\"abc\" == \"abd\" 应为 false")
}
})
}
// Test_ValueSystem_Equal_Bool bool 相等比较
func Test_ValueSystem_Equal_Bool(t *testing.T) {
t.Run("相同值true", func(t *testing.T) {
if !NewValue(true).Equal(NewValue(true)) {
t.Error("true == true 应为 true")
}
})
t.Run("不同值", func(t *testing.T) {
if NewValue(true).Equal(NewValue(false)) {
t.Error("true == false 应为 false")
}
})
}
// Test_ValueSystem_Equal_Nil nil 相等比较
func Test_ValueSystem_Equal_Nil(t *testing.T) {
if !NewValue(nil).Equal(NewValue(nil)) {
t.Error("nil == nil 应为 true")
}
}
// Test_ValueSystem_Equal_CrossType 跨类型比较均为 false
// 注意: int/float数值比较自动提升, 不在此测试中
func Test_ValueSystem_Equal_CrossType(t *testing.T) {
tests := []struct {
name string
a, b Value
}{
{"int vs string", NewValue(1), NewValue("1")},
{"int vs bool", NewValue(1), NewValue(true)},
{"int vs nil", NewValue(0), NewValue(nil)},
{"string vs bool", NewValue("true"), NewValue(true)},
{"float vs string", NewValue(1.0), NewValue("1.0")},
{"nil vs int", NewValue(nil), NewValue(0)},
{"array vs map", NewValue([]Value{}), NewValue(&MapValue{Pairs: map[string]Value{}})},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.a.Equal(tt.b) {
t.Error("跨类型比较应为 false")
}
})
}
}
// Test_ValueSystem_Equal_Array 数组相等比较
func Test_ValueSystem_Equal_Array(t *testing.T) {
t.Run("元素相同", func(t *testing.T) {
a := NewValue([]Value{NewValue(1), NewValue(2), NewValue(3)})
b := NewValue([]Value{NewValue(1), NewValue(2), NewValue(3)})
if !a.Equal(b) {
t.Error("元素相同的数组应相等")
}
})
t.Run("元素不同", func(t *testing.T) {
a := NewValue([]Value{NewValue(1), NewValue(2), NewValue(3)})
b := NewValue([]Value{NewValue(1), NewValue(2), NewValue(4)})
if a.Equal(b) {
t.Error("元素不同的数组不应相等")
}
})
t.Run("长度不同", func(t *testing.T) {
a := NewValue([]Value{NewValue(1), NewValue(2), NewValue(3)})
b := NewValue([]Value{NewValue(1), NewValue(2)})
if a.Equal(b) {
t.Error("长度不同的数组不应相等")
}
})
}
// Test_ValueSystem_Equal_Map Map 相等比较
func Test_ValueSystem_Equal_Map(t *testing.T) {
t.Run("相同键值", func(t *testing.T) {
a := NewValue(&MapValue{Pairs: map[string]Value{"a": NewValue(1), "b": NewValue(2)}, KeyType: TypeString})
b := NewValue(&MapValue{Pairs: map[string]Value{"a": NewValue(1), "b": NewValue(2)}, KeyType: TypeString})
if !a.Equal(b) {
t.Error("相同键值的 Map 应相等")
}
})
t.Run("不同键", func(t *testing.T) {
a := NewValue(&MapValue{Pairs: map[string]Value{"a": NewValue(1)}, KeyType: TypeString})
b := NewValue(&MapValue{Pairs: map[string]Value{"b": NewValue(1)}, KeyType: TypeString})
if a.Equal(b) {
t.Error("键不同的 Map 不应相等")
}
})
t.Run("不同值", func(t *testing.T) {
a := NewValue(&MapValue{Pairs: map[string]Value{"a": NewValue(1)}, KeyType: TypeString})
b := NewValue(&MapValue{Pairs: map[string]Value{"a": NewValue(2)}, KeyType: TypeString})
if a.Equal(b) {
t.Error("值不同的 Map 不应相等")
}
})
}
// Test_ValueSystem_Equal_NestedArray 嵌套数组递归比较
func Test_ValueSystem_Equal_NestedArray(t *testing.T) {
a := NewValue([]Value{
NewValue([]Value{NewValue(1), NewValue(2)}),
NewValue([]Value{NewValue(3), NewValue(4)}),
})
b := NewValue([]Value{
NewValue([]Value{NewValue(1), NewValue(2)}),
NewValue([]Value{NewValue(3), NewValue(4)}),
})
if !a.Equal(b) {
t.Error("嵌套数组内容相同应递归相等")
}
c := NewValue([]Value{
NewValue([]Value{NewValue(1), NewValue(99)}),
NewValue([]Value{NewValue(3), NewValue(4)}),
})
if a.Equal(c) {
t.Error("嵌套数组内层元素不同不应相等")
}
}
// Test_ValueSystem_Equal_NestedMap 嵌套 Map 递归比较
func Test_ValueSystem_Equal_NestedMap(t *testing.T) {
a := NewValue(&MapValue{
Pairs: map[string]Value{
"inner": NewValue(&MapValue{Pairs: map[string]Value{"x": NewValue(1)}, KeyType: TypeString}),
},
KeyType: TypeString,
})
b := NewValue(&MapValue{
Pairs: map[string]Value{
"inner": NewValue(&MapValue{Pairs: map[string]Value{"x": NewValue(1)}, KeyType: TypeString}),
},
KeyType: TypeString,
})
if !a.Equal(b) {
t.Error("嵌套 Map 内容相同应递归相等")
}
}
// Test_ValueSystem_Equal_NilVsOther nil 与其他类型比较
func Test_ValueSystem_Equal_NilVsOther(t *testing.T) {
others := []Value{
NewValue(0),
NewValue(""),
NewValue(false),
NewValue(0.0),
}
for i, other := range others {
if NewValue(nil).Equal(other) {
t.Errorf("nil 与类型 %v 不应相等 (case %d)", other.Type, i)
}
}
}
// Test_ValueSystem_Equal_Function 函数类型无相等处理器, 同类型比较返回 false
func Test_ValueSystem_Equal_Function(t *testing.T) {
fv := &FunctionValue{Compiled: &CompiledFunction{Name: "f"}}
a := NewValue(fv)
b := NewValue(fv)
// TypeFunction 未注册 equalHandler, 即使指向同一指针也返回 false
if a.Equal(b) {
t.Error("TypeFunction 无 Equal 处理器, 同类型比较应返回 false")
}
}
// ---------- GoString 格式化 ----------
// Test_ValueSystem_GoString_Int int 的 GoString
func Test_ValueSystem_GoString_Int(t *testing.T) {
if NewValue(42).GoString() != "42" {
t.Errorf("GoString() = %q, want \"42\"", NewValue(42).GoString())
}
}
// Test_ValueSystem_GoString_Float float 的 GoString
func Test_ValueSystem_GoString_Float(t *testing.T) {
v := NewValue(3.14)
want := strconv.FormatFloat(3.14, 'f', -1, 64)
if v.GoString() != want {
t.Errorf("GoString() = %q, want %q", v.GoString(), want)
}
}
// Test_ValueSystem_GoString_String string 的 GoString (带引号)
func Test_ValueSystem_GoString_String(t *testing.T) {
if NewValue("hello").GoString() != "\"hello\"" {
t.Errorf("GoString() = %q, want \"\\\"hello\\\"\"", NewValue("hello").GoString())
}
}
// Test_ValueSystem_GoString_Bool bool 的 GoString
func Test_ValueSystem_GoString_Bool(t *testing.T) {
if NewValue(true).GoString() != "true" {
t.Errorf("GoString() = %q, want \"true\"", NewValue(true).GoString())
}
if NewValue(false).GoString() != "false" {
t.Errorf("GoString() = %q, want \"false\"", NewValue(false).GoString())
}
}
// Test_ValueSystem_GoString_Nil nil 的 GoString
func Test_ValueSystem_GoString_Nil(t *testing.T) {
if NewValue(nil).GoString() != "nil" {
t.Errorf("GoString() = %q, want \"nil\"", NewValue(nil).GoString())
}
}
// Test_ValueSystem_GoString_Array 数组的 GoString
func Test_ValueSystem_GoString_Array(t *testing.T) {
v := NewValue([]Value{NewValue(1), NewValue(2), NewValue(3)})
if v.GoString() != "[1, 2, 3]" {
t.Errorf("GoString() = %q, want \"[1, 2, 3]\"", v.GoString())
}
}
// Test_ValueSystem_GoString_EmptyArray 空数组的 GoString
func Test_ValueSystem_GoString_EmptyArray(t *testing.T) {
v := NewValue([]Value{})
if v.GoString() != "[]" {
t.Errorf("GoString() = %q, want \"[]\"", v.GoString())
}
}
// Test_ValueSystem_GoString_Map 单键 Map 的 GoString (多键顺序不确定)
func Test_ValueSystem_GoString_Map(t *testing.T) {
v := NewValue(&MapValue{Pairs: map[string]Value{"a": NewValue(1)}, KeyType: TypeString})
// 单键确定: {"a": 1}
want := "{\"a\": 1}"
if v.GoString() != want {
t.Errorf("GoString() = %q, want %q", v.GoString(), want)
}
}
// Test_ValueSystem_GoString_EmptyMap 空 Map 的 GoString
func Test_ValueSystem_GoString_EmptyMap(t *testing.T) {
v := NewValue(&MapValue{Pairs: map[string]Value{}, KeyType: TypeString})
if v.GoString() != "{}" {
t.Errorf("GoString() = %q, want \"{}\"", v.GoString())
}
}
// Test_ValueSystem_GoString_NestedArray 嵌套数组的 GoString
func Test_ValueSystem_GoString_NestedArray(t *testing.T) {
inner := &ArrayValue{Elements: []Value{NewValue(1), NewValue(2)}}
outer := &ArrayValue{Elements: []Value{
{Type: TypeArray, Data: inner},
NewValue(3),
}}
v := Value{Type: TypeArray, Data: outer}
if v.GoString() != "[[1, 2], 3]" {
t.Errorf("GoString() = %q, want \"[[1, 2], 3]\"", v.GoString())
}
}
// Test_ValueSystem_GoString_NestedMap 嵌套 Map 的 GoString
func Test_ValueSystem_GoString_NestedMap(t *testing.T) {
inner := &MapValue{Pairs: map[string]Value{"y": NewValue(2)}, KeyType: TypeString}
outer := &MapValue{
Pairs: map[string]Value{"inner": {Type: TypeMap, Data: inner}},
KeyType: TypeString,
}
v := Value{Type: TypeMap, Data: outer}
got := v.GoString()
// 单键嵌套, 输出确定
want := "{\"inner\": {\"y\": 2}}"
if got != want {
t.Errorf("GoString() = %q, want %q", got, want)
}
}
// Test_ValueSystem_GoString_Function 函数的 GoString
func Test_ValueSystem_GoString_Function(t *testing.T) {
v := NewValue(&FunctionValue{Compiled: &CompiledFunction{Name: "testFunc"}})
if v.GoString() != "<function testFunc>" {
t.Errorf("GoString() = %q, want \"<function testFunc>\"", v.GoString())
}
}
// Test_ValueSystem_GoString_ExternalFunc 外部函数的 GoString
func Test_ValueSystem_GoString_ExternalFunc(t *testing.T) {
ef := &ExternalFuncValue{Name: "extFunc", Func: func() {}}
v := Value{Type: TypeExternalFunc, Data: ef}
if v.GoString() != "<external extFunc>" {
t.Errorf("GoString() = %q, want \"<external extFunc>\"", v.GoString())
}
}
// Test_ValueSystem_GoString_UnknownType 未知类型的 GoString
func Test_ValueSystem_GoString_UnknownType(t *testing.T) {
v := Value{Type: ValueType(999), Data: nil}
if v.GoString() != "<unknown>" {
t.Errorf("GoString() = %q, want \"<unknown>\"", v.GoString())
}
}
// ---------- intVal / intValSlow 与类型边界 ----------
// Test_ValueSystem_IntVal_CacheHit 0-255 命中缓存
func Test_ValueSystem_IntVal_CacheHit(t *testing.T) {
for i := 0; i < 256; i++ {
v := intVal(i)
if v.Type != TypeInt {
t.Errorf("intVal(%d).Type = %v, want TypeInt", i, v.Type)
}
if v.Int() != i {
t.Errorf("intVal(%d).Int() = %d, want %d", i, v.Int(), i)
}
}
}
// Test_ValueSystem_IntVal_CacheMiss 256 走 intValSlow
func Test_ValueSystem_IntVal_CacheMiss(t *testing.T) {
v := intVal(256)
if v.Int() != 256 {
t.Errorf("intVal(256).Int() = %d, want 256", v.Int())
}
}
// Test_ValueSystem_IntVal_Negative 负数走 intValSlow
func Test_ValueSystem_IntVal_Negative(t *testing.T) {
v := intVal(-1)
if v.Int() != -1 {
t.Errorf("intVal(-1).Int() = %d, want -1", v.Int())
}
}
// Test_ValueSystem_IntValSlow 直接构造 Value
func Test_ValueSystem_IntValSlow(t *testing.T) {
v := intValSlow(1000)
if v.Type != TypeInt || v.Int() != 1000 {
t.Errorf("intValSlow(1000) = {Type:%v, Int:%d}, want TypeInt/1000", v.Type, v.Int())
}
}
// Test_ValueSystem_Boundary_LargeInt 大整数
func Test_ValueSystem_Boundary_LargeInt(t *testing.T) {
big := 1<<62 + 1
v := NewValue(big)
if v.Int() != big {
t.Errorf("Int() = %d, want %d", v.Int(), big)
}
if v.Type != TypeInt {
t.Errorf("Type = %v, want TypeInt", v.Type)
}
}
// Test_ValueSystem_Boundary_NegativeInt 负数
func Test_ValueSystem_Boundary_NegativeInt(t *testing.T) {
v := NewValue(-500)
if v.Int() != -500 {
t.Errorf("Int() = %d, want -500", v.Int())
}
}
// Test_ValueSystem_Boundary_EmptyString 空字符串
func Test_ValueSystem_Boundary_EmptyString(t *testing.T) {
v := NewValue("")
if v.Type != TypeString {
t.Errorf("Type = %v, want TypeString", v.Type)
}
if v.String() != "" {
t.Errorf("String() = %q, want \"\"", v.String())
}
if v.IsNil() {
t.Error("空字符串不应 IsNil")
}
}
// Test_ValueSystem_Boundary_EmptyArray 空数组
func Test_ValueSystem_Boundary_EmptyArray(t *testing.T) {
v := NewValue([]Value{})
if v.Type != TypeArray {
t.Errorf("Type = %v, want TypeArray", v.Type)
}
arr := v.Array()
if arr == nil || len(arr.Elements) != 0 {
t.Errorf("空数组 Elements 长度应 0, got %v", arr)
}
}
// Test_ValueSystem_Boundary_EmptyMap 空 Map
func Test_ValueSystem_Boundary_EmptyMap(t *testing.T) {
v := NewValue(&MapValue{Pairs: map[string]Value{}, KeyType: TypeString})
if v.Type != TypeMap {
t.Errorf("Type = %v, want TypeMap", v.Type)
}
mv := v.Map()
if mv == nil || len(mv.Pairs) != 0 {
t.Errorf("空 Map Pairs 长度应 0, got %v", mv)
}
}
// ---------- 类型转换链 (验证无自动转换) ----------
// Test_ValueSystem_Conversion_IntToFloat int.Float() 自动转换为float64
func Test_ValueSystem_Conversion_IntToFloat(t *testing.T) {
v := NewValue(42)
if v.Float() != 42.0 {
t.Errorf("int 的 Float() = %f, want 42.0 (自动转换)", v.Float())
}
}
// Test_ValueSystem_Conversion_FloatToInt float.Int() 不自动转换, 返回零值
func Test_ValueSystem_Conversion_FloatToInt(t *testing.T) {
v := NewValue(3.99)
if v.Int() != 0 {
t.Errorf("float 的 Int() = %d, want 0 (无自动转换, 不截断也不四舍五入)", v.Int())
}
}
// Test_ValueSystem_Conversion_BoolToInt bool.Int() 不自动转换, 返回零值
func Test_ValueSystem_Conversion_BoolToInt(t *testing.T) {
if NewValue(true).Int() != 0 {
t.Error("bool 的 Int() 应返回 0 (无自动转换)")
}
}
// Test_ValueSystem_Conversion_StringToBool string.Bool() 不自动转换, 返回 false
func Test_ValueSystem_Conversion_StringToBool(t *testing.T) {
if NewValue("true").Bool() {
t.Error("string 的 Bool() 应返回 false (无自动转换)")
}
}
// ---------- 复合值操作 ----------
// Test_ValueSystem_Composite_ArrayElementAccess 数组元素访问
func Test_ValueSystem_Composite_ArrayElementAccess(t *testing.T) {
v := NewValue([]Value{NewValue(10), NewValue(20), NewValue(30)})
arr := v.Array()
t.Run("首元素", func(t *testing.T) {
if arr.Elements[0].Int() != 10 {
t.Errorf("Elements[0] = %d, want 10", arr.Elements[0].Int())
}
})
t.Run("尾元素", func(t *testing.T) {
if arr.Elements[2].Int() != 30 {
t.Errorf("Elements[2] = %d, want 30", arr.Elements[2].Int())
}
})
}
// Test_ValueSystem_Composite_MapKVAccess Map 键值访问
func Test_ValueSystem_Composite_MapKVAccess(t *testing.T) {
mv := &MapValue{
Pairs: map[string]Value{"name": NewValue("alice"), "age": NewValue(30)},
KeyType: TypeString,
}
v := NewValue(mv)
m := v.Map()
t.Run("字符串值", func(t *testing.T) {
if m.Pairs["name"].String() != "alice" {
t.Errorf("Pairs[name] = %q, want \"alice\"", m.Pairs["name"].String())
}
})
t.Run("整数值", func(t *testing.T) {
if m.Pairs["age"].Int() != 30 {
t.Errorf("Pairs[age] = %d, want 30", m.Pairs["age"].Int())
}
})
}
// Test_ValueSystem_Composite_NestedArray 嵌套数组
func Test_ValueSystem_Composite_NestedArray(t *testing.T) {
inner := []Value{NewValue(1), NewValue(2)}
outer := []Value{NewValue(inner), NewValue(3)}
v := NewValue(outer)
arr := v.Array()
innerArr := arr.Elements[0].Array()
if innerArr == nil {
t.Fatal("嵌套内层 Array() = nil")
}
if innerArr.Elements[1].Int() != 2 {
t.Errorf("内层 Elements[1] = %d, want 2", innerArr.Elements[1].Int())
}
}
// Test_ValueSystem_Composite_NestedMap 嵌套 Map
func Test_ValueSystem_Composite_NestedMap(t *testing.T) {
inner := &MapValue{Pairs: map[string]Value{"deep": NewValue(99)}, KeyType: TypeString}
outer := &MapValue{
Pairs: map[string]Value{"nested": NewValue(inner)},
KeyType: TypeString,
}
v := NewValue(outer)
innerMap := v.Map().Pairs["nested"].Map()
if innerMap == nil {
t.Fatal("嵌套内层 Map() = nil")
}
if innerMap.Pairs["deep"].Int() != 99 {
t.Errorf("内层 Pairs[deep] = %d, want 99", innerMap.Pairs["deep"].Int())
}
}
// Test_ValueSystem_Composite_ArrayInMap Map 值为数组
func Test_ValueSystem_Composite_ArrayInMap(t *testing.T) {
mv := &MapValue{
Pairs: map[string]Value{
"list": NewValue([]Value{NewValue(1), NewValue(2)}),
},
KeyType: TypeString,
}
v := NewValue(mv)
arr := v.Map().Pairs["list"].Array()
if arr == nil || len(arr.Elements) != 2 {
t.Errorf("Map 中的数组元素访问失败, arr=%v", arr)
}
}
// Test_ValueSystem_Composite_MapInArray 数组元素为 Map
func Test_ValueSystem_Composite_MapInArray(t *testing.T) {
mv := &MapValue{Pairs: map[string]Value{"k": NewValue(1)}, KeyType: TypeString}
v := NewValue([]Value{NewValue(mv), NewValue(42)})
arr := v.Array()
mapInArray := arr.Elements[0].Map()
if mapInArray == nil {
t.Fatal("数组中的 Map 元素访问失败")
}
if mapInArray.Pairs["k"].Int() != 1 {
t.Errorf("Pairs[k] = %d, want 1", mapInArray.Pairs["k"].Int())
}
}
// ---------- formatValueForPrint / formatValue ----------
// Test_ValueSystem_FormatValueForPrint_AllTypes 各类型打印格式
func Test_ValueSystem_FormatValueForPrint_AllTypes(t *testing.T) {
tests := []struct {