-
Notifications
You must be signed in to change notification settings - Fork 1
/
extension_test.go
1118 lines (977 loc) · 23.7 KB
/
extension_test.go
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 gp
import (
"fmt"
"strings"
"testing"
)
// TestStruct contains various types of fields for testing
type TestStruct struct {
// C-compatible basic types
BoolField bool
Int8Field int8
Int16Field int16
Int32Field int32
Int64Field int64
IntField int
Uint8Field uint8
Uint16Field uint16
Uint32Field uint32
Uint64Field uint64
UintField uint
Float32Field float32
Float64Field float64
Complex64Field complex64
Complex128Field complex128
// Non-C-compatible types
StringField string
SliceField []int
MapField map[string]int
StructField struct{ X int }
}
func (t *TestStruct) TestMethod() int {
return 42
}
func TestAddType(t *testing.T) {
setupTest(t)
m := MainModule()
// test add type
typ := m.AddType(TestStruct{}, nil, "TestStruct", "Test struct documentation")
if typ.Nil() {
t.Fatal("Failed to create type")
}
// test type by Python code
code := `
# create instance
obj = TestStruct()
# test C-compatible types
obj.bool_field = True
obj.int8_field = 127
obj.int16_field = 32767
obj.int32_field = 2147483647
obj.int64_field = 9223372036854775807
obj.int_field = 1234567890
obj.uint8_field = 255
obj.uint16_field = 65535
obj.uint32_field = 4294967295
obj.uint64_field = 18446744073709551615
obj.uint_field = 4294967295
obj.float32_field = 3.14
obj.float64_field = 3.14159265359
obj.complex64_field = 1.5 + 2.5j
obj.complex128_field = 3.14 + 2.718j
# test non-C-compatible types
obj.string_field = "test string"
assert obj.string_field == "test string"
obj.slice_field = [1, 2, 3]
assert obj.slice_field == [1, 2, 3]
obj.map_field = {"key": 42}
assert obj.map_field["key"] == 42
obj.struct_field = {"x": 100}
assert obj.struct_field.x == 100
# test method call
result = obj.test_method()
assert result == 42
# verify C-compatible types
assert obj.bool_field == True
assert obj.int8_field == 127
assert obj.int16_field == 32767
assert obj.int32_field == 2147483647
assert obj.int64_field == 9223372036854775807
assert obj.int_field == 1234567890
assert obj.uint8_field == 255
assert obj.uint16_field == 65535, f"Expected 65535, got {obj.uint16_field}"
assert obj.uint32_field == 4294967295, f"Expected 4294967295, got {obj.uint32_field}"
assert obj.uint64_field == 18446744073709551615, f"Expected 18446744073709551615, got {obj.uint64_field}"
assert obj.uint_field == 4294967295, f"Expected 4294967295, got {obj.uint_field}"
assert abs(obj.float32_field - 3.14) < 0.0001, f"Expected 3.14, got {obj.float32_field}"
assert abs(obj.float64_field - 3.14159265359) < 0.0000001, f"Expected 3.14159265359, got {obj.float64_field}"
assert abs(obj.complex64_field - (1.5 + 2.5j)) < 0.0001, f"Expected (1.5 + 2.5j), got {obj.complex64_field}"
assert abs(obj.complex128_field - (3.14 + 2.718j)) < 0.0000001, f"Expected (3.14 + 2.718j), got {obj.complex128_field}"
# verify non-C-compatible types
assert obj.string_field == "test string", f"Expected 'test string', got {obj.string_field}"
assert obj.slice_field == [1, 2, 3], f"Expected [1, 2, 3], got {obj.slice_field}"
assert obj.map_field["key"] == 42, f"Expected 42, got {obj.map_field['key']}"
assert obj.struct_field.x == 100, f"Expected 100, got {obj.struct_field.x}"
`
err := RunString(code)
if err != nil {
t.Fatalf("Test failed: %v", err)
}
}
type InitTestStruct struct {
Value int
}
func (i *InitTestStruct) Init(val int) {
i.Value = val
}
func TestAddTypeWithInit(t *testing.T) {
setupTest(t)
m := MainModule()
typ := m.AddType(InitTestStruct{}, (*InitTestStruct).Init, "InitTestStruct", "Test init struct")
if typ.Nil() {
t.Fatal("Failed to create type with init")
}
// test init function
code := `
# test init function
obj = InitTestStruct(42)
assert obj.value == 42
# test error handling without arguments
try:
obj2 = InitTestStruct()
assert False, "Should fail without arguments"
except TypeError as e:
pass
# test error handling with wrong argument type
try:
obj3 = InitTestStruct("wrong type")
assert False, "Should fail with wrong argument type"
except TypeError:
pass
`
err := RunString(code)
if err != nil {
t.Fatalf("Test failed: %v", err)
}
}
func TestCreateFunc(t *testing.T) {
setupTest(t)
tests := []struct {
name string
fn any
doc string
testCode string
}{
{
name: "simple",
fn: func(x int) int {
return x * 2
},
doc: "Doubles the input value",
testCode: `
result = simple_func(21)
assert result == 42, f"Expected 42, got {result}"
assert str(inspect.signature(simple_func)) == "(arg0, /)"
`,
},
{
name: "multi_args",
fn: func(x int, y string) (int, string) {
return x * 2, y + y
},
doc: "Returns doubled number and duplicated string",
testCode: `
num, text = multi_args_func(5, "hello")
assert num == 10, f"Expected 10, got {num}"
assert text == "hellohello", f"Expected 'hellohello', got {text}"
assert str(inspect.signature(multi_args_func)) == "(arg0, arg1, /)"
`,
},
{
name: "with_kwargs",
fn: func(name string, kwargs KwArgs) Object {
return None()
},
doc: "Function with kwargs",
testCode: `
result = with_kwargs_func("test", extra="value")
assert result is None
assert str(inspect.signature(with_kwargs_func)) == "(arg0, /, **kwargs)"
`,
},
{
name: "no_args",
fn: func() string {
return "hello"
},
doc: "Function with no arguments",
testCode: `
result = no_args_func()
assert result == "hello"
assert str(inspect.signature(no_args_func)) == "()"
`,
},
{
name: "only_kwargs",
fn: func(kwargs KwArgs) Object {
return None()
},
doc: "Function with only kwargs",
testCode: `
result = only_kwargs_func(x=1, y=2)
assert result is None
assert str(inspect.signature(only_kwargs_func)) == "(**kwargs)"
`,
},
}
code := `
import inspect
`
for _, tt := range tests {
funcName := tt.name + "_func"
f := CreateFunc(funcName, tt.fn, tt.doc)
if f.Nil() {
t.Fatalf("Failed to create function for test case: %s", tt.name)
}
code += tt.testCode + "\n"
}
t.Log(code)
err := RunString(code)
if err != nil {
t.Fatalf("Test failed: %v", err)
}
// Test failure cases
func() {
defer func() {
if r := recover(); r == nil {
t.Error("CreateFunc should panic with non-function argument")
}
}()
CreateFunc("invalid", 42, "should panic")
}()
func() {
defer func() {
if r := recover(); r == nil {
t.Error("CreateFunc should panic with nil function")
}
}()
CreateFunc("nil_func", nil, "should panic")
}()
}
func TestCreateFuncInvalid(t *testing.T) {
setupTest(t)
// Test invalid function type
defer func() {
if r := recover(); r == nil {
t.Error("CreateFunc should panic with non-function argument")
}
}()
CreateFunc("non_func", 42, "This should panic")
}
func explicitFunc(x int) int {
return x + 1
}
func TestModuleAddMethod(t *testing.T) {
setupTest(t)
m := MainModule()
tests := []struct {
name string
fn any
doc string
testCode string
}{
{
name: "explicit",
fn: explicitFunc,
doc: "adds one to input",
testCode: `
result = explicit_func(41)
assert result == 42, f"Expected 42, got {result}"
assert str(inspect.signature(explicit_func)) == "(arg0, /)"
`,
},
{
name: "with_kwargs",
fn: func(x int, kwargs KwArgs) int {
return x
},
doc: "function with kwargs",
testCode: `
result = with_kwargs_func(42, extra="value")
assert result == 42
assert str(inspect.signature(with_kwargs_func)) == "(arg0, /, **kwargs)"
`,
},
{
name: "multi_args",
fn: func(x, y int) int {
return x * y
},
doc: "multiplies two numbers",
testCode: `
result = multi_args_func(6, 7)
assert result == 42
assert str(inspect.signature(multi_args_func)) == "(arg0, arg1, /)"
`,
},
{
name: "no_args",
fn: func() string {
return "hello"
},
doc: "returns hello",
testCode: `
result = no_args_func()
assert result == "hello"
assert str(inspect.signature(no_args_func)) == "()"
`,
},
{
name: "only_kwargs",
fn: func(kwargs KwArgs) Object {
return None()
},
doc: "function with only kwargs",
testCode: `
result = only_kwargs_func(x=1, y=2)
assert result is None
assert str(inspect.signature(only_kwargs_func)) == "(**kwargs)"
`,
},
}
code := `
import inspect
`
for _, tt := range tests {
funcName := tt.name + "_func"
f := m.AddMethod(funcName, tt.fn, tt.doc)
if f.Nil() {
t.Fatalf("Failed to create function for test case: %s", tt.name)
}
code += tt.testCode + "\n"
}
t.Log(code)
err := RunString(code)
if err != nil {
t.Fatalf("Test failed: %v", err)
}
func() {
defer func() {
if r := recover(); r == nil {
t.Error("AddMethod should panic with non-function argument")
}
}()
m.AddMethod("invalid", 42, "should panic")
}()
func() {
defer func() {
if r := recover(); r == nil {
t.Error("AddMethod should panic with nil function")
}
}()
m.AddMethod("nil_func", nil, "should panic")
}()
func() {
defer func() {
if r := recover(); r == nil {
t.Error("AddMethod should panic with empty module")
}
}()
var emptyModule Module
emptyModule.AddMethod("empty", func() {}, "should panic")
}()
}
func TestAddTypeWithPtrReceiverInit(t *testing.T) {
setupTest(t)
m := MainModule()
type InitTestType struct {
Value int
Name string
Active bool
}
ptrInit := func(t *InitTestType, val int, name string, active bool) {
t.Value = val
t.Name = name
t.Active = active
}
typ := m.AddType(InitTestType{}, ptrInit, "InitTestType", "")
if typ.Nil() {
t.Fatal("Failed to create type with pointer receiver init")
}
code := `
# Test pointer receiver init with multiple args
obj = InitTestType(42, "hello", True)
assert obj.value == 42
assert obj.name == "hello"
assert obj.active == True
# Test error handling
try:
obj2 = InitTestType(42) # Missing arguments
assert False, "Should fail with wrong number of arguments"
except TypeError:
pass
try:
obj3 = InitTestType("wrong", "type", True) # Wrong argument type
assert False, "Should fail with wrong argument type"
except TypeError:
pass
`
err := RunString(code)
if err != nil {
t.Fatalf("Test failed: %v", err)
}
}
func TestAddTypeWithValueConstructor(t *testing.T) {
setupTest(t)
m := MainModule()
type InitTestType struct {
Value int
Name string
Active bool
}
constructorInit := func(val int, name string, active bool) InitTestType {
return InitTestType{
Value: val,
Name: name,
Active: active,
}
}
typ := m.AddType(InitTestType{}, constructorInit, "InitTestType", "")
if typ.Nil() {
t.Fatal("Failed to create type with value constructor")
}
code := `
# Test value constructor with multiple args
obj = InitTestType(43, "world", False)
assert obj.value == 43
assert obj.name == "world"
assert obj.active == False
`
err := RunString(code)
if err != nil {
t.Fatalf("Test failed: %v", err)
}
}
func TestAddTypeWithPtrConstructor(t *testing.T) {
setupTest(t)
m := MainModule()
type InitTestType struct {
Value int
Name string
Active bool
}
ptrConstructorInit := func(val int, name string) *InitTestType {
return &InitTestType{
Value: val,
Name: name,
}
}
typ := m.AddType(InitTestType{}, ptrConstructorInit, "InitTestType", "")
if typ.Nil() {
t.Fatal("Failed to create type with pointer constructor")
}
code := `
# Test pointer constructor with multiple args
obj = InitTestType(44, "python")
assert obj.value == 44
assert obj.name == "python"
assert obj.active == False # default value
`
err := RunString(code)
if err != nil {
t.Fatalf("Test failed: %v", err)
}
}
type Inner struct {
X int
Y string
}
func TestAddTypeRecursive(t *testing.T) {
setupTest(t)
m := MainModule()
type Outer struct {
Inner Inner
InnerPtr *Inner
Value int
InnerList []Inner
}
// Register Outer type - should automatically register Inner
obj := m.AddType(Outer{}, nil, "Outer", "")
if obj.Nil() {
t.Fatal("Failed to create Outer type")
}
code := `
# Test nested struct access
o = Outer()
o.inner.x = 42
o.inner.y = "hello"
assert o.inner.x == 42
assert o.inner.y == "hello"
# Test pointer to struct
o.inner_ptr = Inner()
o.inner_ptr.x = 43
o.inner_ptr.y = "world"
assert o.inner_ptr.x == 43
assert o.inner_ptr.y == "world"
# Test basic field
o.value = 100
assert o.value == 100
# Test slice of structs
o.inner_list = [Inner()]
o.inner_list[0].x = 44
o.inner_list[0].y = "python"
assert o.inner_list[0].x == 44
assert o.inner_list[0].y == "python"
`
err := RunString(code)
if err != nil {
t.Fatalf("Test failed: %v", err)
}
}
func TestSetterMethodEdgeCases(t *testing.T) {
setupTest(t)
type ChildStruct struct {
Value int
}
type ParentStruct struct {
unexported int
Value int
Child *ChildStruct
Nested ChildStruct
}
m := MainModule()
m.AddType(ChildStruct{}, nil, "ChildStruct", "")
m.AddType(ParentStruct{}, nil, "ParentStruct", "")
code := `
obj = ParentStruct()
try:
obj.value = "invalid" # Try to set int with string
assert False, "Should have raised TypeError"
except TypeError:
pass
try:
obj.child = 123 # Try to set struct pointer with int
assert False, "Should have raised TypeError"
except TypeError:
pass
try:
obj.nested = 123 # Try to set struct with int
assert False, "Should have raised TypeError"
except TypeError:
pass
`
err := RunString(code)
if err != nil {
t.Fatal(err)
}
}
func TestGetterMethodEdgeCases(t *testing.T) {
setupTest(t)
type ChildStruct struct {
Value int
}
type ParentStruct struct {
Value int
Child *ChildStruct
Nested ChildStruct
}
m := MainModule()
m.AddType(ChildStruct{}, nil, "ChildStruct", "")
m.AddType(ParentStruct{}, nil, "ParentStruct", "")
code := `
obj = ParentStruct()
obj.child = None # Set pointer to nil
val = obj.child # Should return None for nil pointer
assert val is None
obj.nested = ChildStruct() # Set nested struct
val = obj.nested # Should return wrapper for nested struct
assert isinstance(val, ChildStruct)
# Test accessing nested struct fields
obj.nested.value = 42
assert obj.nested.value == 42
`
err := RunString(code)
if err != nil {
t.Fatal(err)
}
}
func TestWrapperMethodEdgeCases(t *testing.T) {
setupTest(t)
type TestStruct struct {
Value int
}
m := MainModule()
// Test method with wrong number of arguments
m.AddMethod("test_func", func(x int, y int) int { return x + y }, "")
code := `
try:
test_func(1) # Missing argument
assert False, "Should have raised TypeError"
except TypeError:
pass
try:
test_func(1, 2, 3) # Too many arguments
assert False, "Should have raised TypeError"
except TypeError:
pass
try:
test_func("invalid", 2) # Invalid argument type
assert False, "Should have raised TypeError"
except TypeError:
pass
`
err := RunString(code)
if err != nil {
t.Fatal(err)
}
}
func TestAddTypeEdgeCases(t *testing.T) {
setupTest(t)
// Test adding non-struct type
defer func() {
if r := recover(); r == nil {
t.Error("Expected panic when adding non-struct type")
}
}()
m := MainModule()
m.AddType(123, nil, "NotAStruct", "")
}
func TestInitFunctionEdgeCases(t *testing.T) {
setupTest(t)
type TestStruct struct {
Value int
}
// Test init function with invalid signature
defer func() {
if r := recover(); r == nil {
t.Error("Expected panic when using invalid init function")
}
}()
m := MainModule()
invalidInit := func(x string) string { return x } // Wrong signature
m.AddType(TestStruct{}, invalidInit, "TestStruct", "")
}
func TestNestedStructRegistration(t *testing.T) {
setupTest(t)
type NestedStruct struct {
Value int
}
type ParentStruct struct {
Nested NestedStruct
NestedPtr *NestedStruct
}
m := MainModule()
m.AddType(ParentStruct{}, nil, "ParentStruct", "")
code := `
parent = ParentStruct()
assert hasattr(parent, "nested")
assert hasattr(parent, "nested_ptr")
# Test nested struct manipulation
parent.nested.value = 42
assert parent.nested.value == 42
parent.nested_ptr = None
assert parent.nested_ptr is None
# Create and assign new nested struct
parent.nested_ptr = NestedStruct()
parent.nested_ptr.value = 100
assert parent.nested_ptr.value == 100
`
err := RunString(code)
if err != nil {
t.Fatal(err)
}
}
func TestAddTypeWithPointerArg(t *testing.T) {
setupTest(t)
m := MainModule()
type TestStruct struct {
Value int
}
// Test adding type with pointer argument
typ1 := m.AddType(&TestStruct{}, nil, "TestStruct", "")
if typ1.Nil() {
t.Fatal("Failed to create type with pointer argument")
}
code := `
obj = TestStruct()
obj.value = 42
assert obj.value == 42
`
err := RunString(code)
if err != nil {
t.Fatal(err)
}
}
func TestAddTypeDuplicate(t *testing.T) {
setupTest(t)
m := MainModule()
type TestStruct struct {
Value int
}
// First registration
typ1 := m.AddType(TestStruct{}, nil, "TestStruct", "")
if typ1.Nil() {
t.Fatal("Failed to create type on first registration")
}
// Second registration should return the same type object
typ2 := m.AddType(TestStruct{}, nil, "TestStruct", "")
if typ2.Nil() {
t.Fatal("Failed to get type on second registration")
}
if typ1.cpyObj() != typ2.cpyObj() {
t.Fatal("Expected same type object on second registration")
}
// Both types should work with the same underlying Go type
code := `
obj1 = TestStruct()
obj1.value = 42
assert obj1.value == 42
`
err := RunString(code)
if err != nil {
t.Fatal(err)
}
// Also test with pointer argument
typ3 := m.AddType(&TestStruct{}, nil, "TestStruct3", "")
if typ3.Nil() {
t.Fatal("Failed to get type on registration with pointer")
}
if typ1.cpyObj() != typ3.cpyObj() {
t.Fatal("Expected same type object on second registration")
}
}
func TestStructPointerFieldDictAssignment(t *testing.T) {
setupTest(t)
m := MainModule()
type NestedStruct struct {
IntVal int
StringVal string
}
type ParentStruct struct {
PtrField *NestedStruct
}
m.AddType(ParentStruct{}, nil, "ParentStruct", "")
// Test assigning dict to nil pointer field
code := `
obj = ParentStruct()
# Initially the pointer should be nil
assert obj.ptr_field is None
# Assign dict to nil pointer field
obj.ptr_field = {"int_val": 42, "string_val": "hello"}
assert obj.ptr_field.int_val == 42
assert obj.ptr_field.string_val == "hello"
# Test invalid dict value type
try:
obj.ptr_field = {"int_val": "not an int", "string_val": "hello"}
assert False, "Should have raised TypeError for invalid int_val"
except TypeError:
pass
# Test completely wrong type
try:
obj.ptr_field = ["not", "a", "dict"]
assert False, "Should have raised TypeError for list"
except TypeError:
pass
# Test nested dict with wrong type
try:
obj.ptr_field = {"int_val": {"nested": "dict"}, "string_val": "hello"}
assert False, "Should have raised TypeError for nested dict"
except TypeError:
pass
`
err := RunString(code)
if err != nil {
t.Fatal(err)
}
}
func TestStructFieldDictAssignment(t *testing.T) {
setupTest(t)
m := MainModule()
type NestedStruct struct {
IntVal int
StringVal string
}
type ParentStruct struct {
Field NestedStruct
}
m.AddType(ParentStruct{}, nil, "ParentStruct", "")
// Test assigning dict to struct field
code := `
obj = ParentStruct()
# Assign valid dict
obj.field = {"int_val": 42, "string_val": "hello"}
assert obj.field.int_val == 42
assert obj.field.string_val == "hello"
# Test invalid value type
try:
obj.field = {"int_val": "not an int", "string_val": "hello"}
assert False, "Should have raised TypeError for invalid int_val"
except TypeError:
pass
# Test completely wrong type
try:
obj.field = ["not", "a", "dict"]
assert False, "Should have raised TypeError for list"
except TypeError:
pass
# Test nested dict with wrong type
try:
obj.field = {"int_val": {"nested": "dict"}, "string_val": "hello"}
assert False, "Should have raised TypeError for nested dict"
except TypeError:
pass
# Test with complex nested structure
obj.field = {
"int_val": 100,
"string_val": "test"
}
assert obj.field.int_val == 100
assert obj.field.string_val == "test"
`
err := RunString(code)
if err != nil {
t.Fatal(err)
}
}
func TestGenSig(t *testing.T) {
type CustomStruct struct {
Value int
}
tests := []struct {
name string
fn any
hasRecv bool
expectedSig string
}{
{
name: "function with return",
fn: func(x int) string { return "" },
hasRecv: false,
expectedSig: "(arg0, /)",
},
{
name: "multiple arguments",
fn: func(x string, y int) (int, string) { return 0, "" },
hasRecv: false,
expectedSig: "(arg0, arg1, /)",
},
{
name: "with kwargs",
fn: func(name string, kwargs KwArgs) Object { return None() },
hasRecv: false,
expectedSig: "(arg0, /, **kwargs)",
},
{
name: "method with receiver",
fn: func(r *CustomStruct, x int) float64 { return 0 },
hasRecv: true,
expectedSig: "(arg0, /)",
},
{
name: "no arguments",
fn: func() {},
hasRecv: false,
expectedSig: "()",
},
{
name: "only kwargs",
fn: func(kwargs KwArgs) {},
hasRecv: false,
expectedSig: "(**kwargs)",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := genSig(tt.fn, tt.hasRecv)
if got != tt.expectedSig {
t.Errorf("genSig() = %q, want %q", got, tt.expectedSig)