Skip to content

Commit 6e896b6

Browse files
authored
Merge pull request #10 from cpunion/tests
Tests
2 parents 581c896 + a005c44 commit 6e896b6

19 files changed

+898
-250
lines changed

adap_go.go

-10
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,6 @@ func AllocCStrDontFree(s string) *C.char {
2222
return C.CString(s)
2323
}
2424

25-
func AllocWCStr(s string) *C.wchar_t {
26-
runes := []rune(s)
27-
wchars := make([]uint16, len(runes)+1)
28-
for i, r := range runes {
29-
wchars[i] = uint16(r)
30-
}
31-
wchars[len(runes)] = 0
32-
return (*C.wchar_t)(unsafe.Pointer(&wchars[0]))
33-
}
34-
3525
func GoString(s *C.char) string {
3626
return C.GoString((*C.char)(s))
3727
}

adap_go_test.go

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package gp
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestAllocCStr(t *testing.T) {
8+
setupTest(t)
9+
tests := []struct {
10+
name string
11+
input string
12+
want string
13+
}{
14+
{"empty string", "", ""},
15+
{"ascii string", "hello", "hello"},
16+
{"unicode string", "hello 世界", "hello 世界"},
17+
}
18+
19+
for _, tt := range tests {
20+
cstr := AllocCStr(tt.input)
21+
got := GoString(cstr)
22+
if got != tt.want {
23+
t.Errorf("AllocCStr() = %v, want %v", got, tt.want)
24+
}
25+
}
26+
}
27+
28+
func TestGoStringN(t *testing.T) {
29+
setupTest(t)
30+
tests := []struct {
31+
name string
32+
input string
33+
n int
34+
want string
35+
}{
36+
{"empty string", "", 0, ""},
37+
{"partial string", "hello", 3, "hel"},
38+
{"full string", "hello", 5, "hello"},
39+
{"unicode partial", "hello 世界", 6, "hello "},
40+
{"unicode full", "hello 世界", 12, "hello 世界"},
41+
}
42+
43+
for _, tt := range tests {
44+
cstr := AllocCStr(tt.input)
45+
got := GoStringN(cstr, tt.n)
46+
if got != tt.want {
47+
t.Errorf("GoStringN() = %v, want %v", got, tt.want)
48+
}
49+
}
50+
}
51+
52+
func TestAllocCStrDontFree(t *testing.T) {
53+
setupTest(t)
54+
input := "test string"
55+
cstr := AllocCStrDontFree(input)
56+
got := GoString(cstr)
57+
if got != input {
58+
t.Errorf("AllocCStrDontFree() = %v, want %v", got, input)
59+
}
60+
}

bool_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
)
66

77
func TestBool(t *testing.T) {
8+
setupTest(t)
89
// Test MakeBool
910
b1 := MakeBool(true)
1011
if !b1.Bool() {

bytes_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
)
77

88
func TestBytesCreation(t *testing.T) {
9+
setupTest(t)
910
// Test BytesFromStr
1011
b1 := BytesFromStr("hello")
1112
if string(b1.Bytes()) != "hello" {
@@ -21,6 +22,7 @@ func TestBytesCreation(t *testing.T) {
2122
}
2223

2324
func TestBytesDecode(t *testing.T) {
25+
setupTest(t)
2426
// Test UTF-8 decode
2527
b := BytesFromStr("你好")
2628
if !bytes.Equal(b.Bytes(), []byte("你好")) {
@@ -40,6 +42,7 @@ func TestBytesDecode(t *testing.T) {
4042
}
4143

4244
func TestBytesConversion(t *testing.T) {
45+
setupTest(t)
4346
original := []byte{0x48, 0x65, 0x6c, 0x6c, 0x6f} // "Hello" in hex
4447
b := MakeBytes(original)
4548

complex_test.go

+16-15
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
)
66

77
func TestComplex(t *testing.T) {
8+
setupTest(t)
89
tests := []struct {
910
name string
1011
input complex128
@@ -38,28 +39,27 @@ func TestComplex(t *testing.T) {
3839
}
3940

4041
for _, tt := range tests {
41-
t.Run(tt.name, func(t *testing.T) {
42-
c := MakeComplex(tt.input)
42+
c := MakeComplex(tt.input)
4343

44-
// Test Real() method
45-
if got := c.Real(); got != tt.wantReal {
46-
t.Errorf("Complex.Real() = %v, want %v", got, tt.wantReal)
47-
}
44+
// Test Real() method
45+
if got := c.Real(); got != tt.wantReal {
46+
t.Errorf("Complex.Real() = %v, want %v", got, tt.wantReal)
47+
}
4848

49-
// Test Imag() method
50-
if got := c.Imag(); got != tt.wantImag {
51-
t.Errorf("Complex.Imag() = %v, want %v", got, tt.wantImag)
52-
}
49+
// Test Imag() method
50+
if got := c.Imag(); got != tt.wantImag {
51+
t.Errorf("Complex.Imag() = %v, want %v", got, tt.wantImag)
52+
}
5353

54-
// Test Complex128() method
55-
if got := c.Complex128(); got != tt.input {
56-
t.Errorf("Complex.Complex128() = %v, want %v", got, tt.input)
57-
}
58-
})
54+
// Test Complex128() method
55+
if got := c.Complex128(); got != tt.input {
56+
t.Errorf("Complex.Complex128() = %v, want %v", got, tt.input)
57+
}
5958
}
6059
}
6160

6261
func TestComplexZeroValue(t *testing.T) {
62+
setupTest(t)
6363
// Create a proper zero complex number instead of using zero-value struct
6464
c := MakeComplex(complex(0, 0))
6565

@@ -76,6 +76,7 @@ func TestComplexZeroValue(t *testing.T) {
7676
}
7777

7878
func TestComplexNilHandling(t *testing.T) {
79+
setupTest(t)
7980
var c Complex // zero-value struct with nil pointer
8081
defer func() {
8182
if r := recover(); r == nil {

dict.go

+5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ func MakeDict(m map[any]any) Dict {
4040
return dict
4141
}
4242

43+
func (d Dict) Has(key any) bool {
44+
keyObj := From(key)
45+
return C.PyDict_Contains(d.obj, keyObj.obj) != 0
46+
}
47+
4348
func (d Dict) Get(key Objecter) Object {
4449
v := C.PyDict_GetItem(d.obj, key.Obj())
4550
C.Py_IncRef(v)

dict_test.go

+27-25
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import (
55
)
66

77
func TestDictFromPairs(t *testing.T) {
8+
setupTest(t)
89
// Add panic test case
9-
t.Run("odd number of arguments", func(t *testing.T) {
10+
func() {
1011
defer func() {
1112
if r := recover(); r == nil {
1213
t.Errorf("DictFromPairs() with odd number of arguments should panic")
@@ -16,7 +17,7 @@ func TestDictFromPairs(t *testing.T) {
1617
}()
1718

1819
DictFromPairs("key1", "value1", "key2") // Should panic
19-
})
20+
}()
2021

2122
tests := []struct {
2223
name string
@@ -39,23 +40,22 @@ func TestDictFromPairs(t *testing.T) {
3940
}
4041

4142
for _, tt := range tests {
42-
t.Run(tt.name, func(t *testing.T) {
43-
dict := DictFromPairs(tt.pairs...)
44-
45-
// Verify each key-value pair
46-
for i := 0; i < len(tt.wantKeys); i++ {
47-
key := From(tt.wantKeys[i])
48-
val := dict.Get(key)
49-
if !ObjectsAreEqual(val, From(tt.wantVals[i])) {
50-
t.Errorf("DictFromPairs() got value %v for key %v, want %v",
51-
val, tt.wantKeys[i], tt.wantVals[i])
52-
}
43+
dict := DictFromPairs(tt.pairs...)
44+
45+
// Verify each key-value pair
46+
for i := 0; i < len(tt.wantKeys); i++ {
47+
key := From(tt.wantKeys[i])
48+
val := dict.Get(key)
49+
if !ObjectsAreEqual(val, From(tt.wantVals[i])) {
50+
t.Errorf("DictFromPairs() got value %v for key %v, want %v",
51+
val, tt.wantKeys[i], tt.wantVals[i])
5352
}
54-
})
53+
}
5554
}
5655
}
5756

5857
func TestMakeDict(t *testing.T) {
58+
setupTest(t)
5959
tests := []struct {
6060
name string
6161
m map[any]any
@@ -78,22 +78,21 @@ func TestMakeDict(t *testing.T) {
7878
}
7979

8080
for _, tt := range tests {
81-
t.Run(tt.name, func(t *testing.T) {
82-
dict := MakeDict(tt.m)
83-
84-
// Verify each key-value pair
85-
for k, v := range tt.m {
86-
key := From(k)
87-
got := dict.Get(key)
88-
if !ObjectsAreEqual(got, From(v)) {
89-
t.Errorf("MakeDict() got value %v for key %v, want %v", got, k, v)
90-
}
81+
dict := MakeDict(tt.m)
82+
83+
// Verify each key-value pair
84+
for k, v := range tt.m {
85+
key := From(k)
86+
got := dict.Get(key)
87+
if !ObjectsAreEqual(got, From(v)) {
88+
t.Errorf("MakeDict() got value %v for key %v, want %v", got, k, v)
9189
}
92-
})
90+
}
9391
}
9492
}
9593

9694
func TestDictSetGet(t *testing.T) {
95+
setupTest(t)
9796
dict := DictFromPairs()
9897

9998
// Test Set and Get
@@ -108,6 +107,7 @@ func TestDictSetGet(t *testing.T) {
108107
}
109108

110109
func TestDictSetGetString(t *testing.T) {
110+
setupTest(t)
111111
dict := DictFromPairs()
112112

113113
// Test SetString and GetString
@@ -121,6 +121,7 @@ func TestDictSetGetString(t *testing.T) {
121121
}
122122

123123
func TestDictDel(t *testing.T) {
124+
setupTest(t)
124125
dict := DictFromPairs("test_key", "test_value")
125126
key := From("test_key")
126127

@@ -141,6 +142,7 @@ func TestDictDel(t *testing.T) {
141142
}
142143

143144
func TestDictForEach(t *testing.T) {
145+
setupTest(t)
144146
dict := DictFromPairs(
145147
"key1", "value1",
146148
"key2", "value2",

float_test.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import (
55
)
66

77
func TestFloat(t *testing.T) {
8-
t.Run("MakeFloat and conversions", func(t *testing.T) {
8+
setupTest(t)
9+
func() {
910
// Test creating float and converting back
1011
f := MakeFloat(3.14159)
1112

@@ -18,9 +19,9 @@ func TestFloat(t *testing.T) {
1819
if got := f.Float32(); float64(got) != float64(float32(3.14159)) {
1920
t.Errorf("Float32() = %v, want %v", got, float32(3.14159))
2021
}
21-
})
22+
}()
2223

23-
t.Run("IsInteger", func(t *testing.T) {
24+
func() {
2425
// Test integer float
2526
intFloat := MakeFloat(5.0)
2627

@@ -34,9 +35,9 @@ func TestFloat(t *testing.T) {
3435
if fracFloat.IsInteger().Bool() {
3536
t.Errorf("IsInteger() for 5.5 = true, want false")
3637
}
37-
})
38+
}()
3839

39-
t.Run("Zero and special values", func(t *testing.T) {
40+
func() {
4041
// Test zero
4142
zero := MakeFloat(0.0)
4243

@@ -50,5 +51,5 @@ func TestFloat(t *testing.T) {
5051
if got := large.Float64(); got != 1e308 {
5152
t.Errorf("Float64() = %v, want 1e308", got)
5253
}
53-
})
54+
}()
5455
}

function_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func (t *TestStruct) TestMethod() int {
3535
}
3636

3737
func TestAddType(t *testing.T) {
38+
setupTest(t)
3839
m := MainModule()
3940

4041
// test add type
@@ -121,6 +122,7 @@ func (i *InitTestStruct) Init(val int) {
121122
}
122123

123124
func TestAddTypeWithInit(t *testing.T) {
125+
setupTest(t)
124126
m := MainModule()
125127

126128
typ := AddType[InitTestStruct](m, (*InitTestStruct).Init, "InitTestStruct", "Test init struct")

kw_test.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
)
77

88
func TestSplitArgs(t *testing.T) {
9+
setupTest(t)
910
tests := []struct {
1011
name string
1112
args []any
@@ -39,21 +40,20 @@ func TestSplitArgs(t *testing.T) {
3940
}
4041

4142
for _, tt := range tests {
42-
t.Run(tt.name, func(t *testing.T) {
43-
gotTup, gotKw := splitArgs(tt.args...)
43+
gotTup, gotKw := splitArgs(tt.args...)
4444

45-
if !reflect.DeepEqual(gotTup, tt.wantTup) {
46-
t.Errorf("splitArgs() tuple = %v, want %v", gotTup, tt.wantTup)
47-
}
45+
if !reflect.DeepEqual(gotTup, tt.wantTup) {
46+
t.Errorf("splitArgs() tuple = %v, want %v", gotTup, tt.wantTup)
47+
}
4848

49-
if !reflect.DeepEqual(gotKw, tt.wantKw) {
50-
t.Errorf("splitArgs() kwargs = %v, want %v", gotKw, tt.wantKw)
51-
}
52-
})
49+
if !reflect.DeepEqual(gotKw, tt.wantKw) {
50+
t.Errorf("splitArgs() kwargs = %v, want %v", gotKw, tt.wantKw)
51+
}
5352
}
5453
}
5554

5655
func TestKwArgs(t *testing.T) {
56+
setupTest(t)
5757
kw := KwArgs{
5858
"name": "test",
5959
"age": 42,

0 commit comments

Comments
 (0)