-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert_test.go
381 lines (327 loc) · 8.86 KB
/
convert_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
package gp
import (
"reflect"
"testing"
)
func TestToValue(t *testing.T) {
setupTest(t)
tests := []struct {
name string
pyValue Object
goType interface{}
expected interface{}
}{
{"int8", From(42), int8(0), int8(42)},
{"int16", From(42), int16(0), int16(42)},
{"int32", From(42), int32(0), int32(42)},
{"int64", From(42), int64(0), int64(42)},
{"int", From(42), int(0), int(42)},
{"uint8", From(42), uint8(0), uint8(42)},
{"uint16", From(42), uint16(0), uint16(42)},
{"uint32", From(42), uint32(0), uint32(42)},
{"uint64", From(42), uint64(0), uint64(42)},
{"uint", From(42), uint(0), uint(42)},
{"float32", From(3.14), float32(0), float32(3.14)},
{"float64", From(3.14), float64(0), float64(3.14)},
{"complex64", From(complex(1, 2)), complex64(0), complex64(complex(1, 2))},
{"complex128", From(complex(1, 2)), complex128(0), complex128(complex(1, 2))},
}
for _, tt := range tests {
v := reflect.New(reflect.TypeOf(tt.goType)).Elem()
if !ToValue(tt.pyValue, v) {
t.Errorf("ToValue failed for %v", tt.name)
}
if v.Interface() != tt.expected {
t.Errorf("Expected %v, got %v", tt.expected, v.Interface())
}
}
func() {
v := reflect.New(reflect.TypeOf("")).Elem()
if !ToValue(From("hello"), v) {
t.Error("ToValue failed for string")
}
if v.String() != "hello" {
t.Errorf("Expected 'hello', got %v", v.String())
}
}()
func() {
v := reflect.New(reflect.TypeOf(true)).Elem()
if !ToValue(From(true), v) {
t.Error("ToValue failed for bool")
}
if !v.Bool() {
t.Error("Expected true, got false")
}
}()
func() {
expected := []byte("hello")
v := reflect.New(reflect.TypeOf([]byte{})).Elem()
if !ToValue(From(expected), v) {
t.Error("ToValue failed for []byte")
}
if !reflect.DeepEqual(v.Bytes(), expected) {
t.Errorf("Expected %v, got %v", expected, v.Bytes())
}
}()
func() {
expected := []int{1, 2, 3}
v := reflect.New(reflect.TypeOf([]int{})).Elem()
if !ToValue(From(expected), v) {
t.Error("ToValue failed for slice")
}
if !reflect.DeepEqual(v.Interface(), expected) {
t.Errorf("Expected %v, got %v", expected, v.Interface())
}
}()
func() {
expected := map[string]int{"one": 1, "two": 2}
v := reflect.New(reflect.TypeOf(map[string]int{})).Elem()
if !ToValue(From(expected), v) {
t.Error("ToValue failed for map")
}
if !reflect.DeepEqual(v.Interface(), expected) {
t.Errorf("Expected %v, got %v", expected, v.Interface())
}
}()
func() {
type TestStruct struct {
Name string
Age int
}
expected := TestStruct{Name: "Alice", Age: 30}
v := reflect.New(reflect.TypeOf(TestStruct{})).Elem()
if !ToValue(From(expected), v) {
t.Error("ToValue failed for struct")
}
if !reflect.DeepEqual(v.Interface(), expected) {
t.Errorf("Expected %v, got %v", expected, v.Interface())
}
}()
func() {
tests := []struct {
name string
pyValue Object
goType interface{}
}{
{"string to int", From("not a number"), int(0)},
{"int to bool", From(42), true},
{"float to string", From(3.14), ""},
{"list to map", From([]int{1, 2, 3}), map[string]int{}},
}
for _, tt := range tests {
v := reflect.New(reflect.TypeOf(tt.goType)).Elem()
if ToValue(tt.pyValue, v) {
t.Errorf("ToValue should have failed for %v", tt.name)
}
}
}()
func() {
defer func() {
if r := recover(); r == nil {
t.Error("ToValue should fail for nil reflect.Value")
}
}()
var nilValue reflect.Value
if ToValue(From(42), nilValue) {
t.Error("ToValue should fail for nil reflect.Value")
}
}()
func() {
defer func() {
if r := recover(); r == nil {
t.Error("ToValue should fail for non-settable value")
}
}()
v := reflect.ValueOf(42) // not settable
if ToValue(From(43), v) {
t.Error("ToValue should fail for non-settable value")
}
}()
}
func TestFromSpecialCases(t *testing.T) {
setupTest(t)
func() {
// Test From with uint values
tests := []struct {
input uint
expected uint64
}{
{0, 0},
{42, 42},
{^uint(0), ^uint64(0)}, // maximum uint value
}
for _, tt := range tests {
obj := From(tt.input)
if !obj.IsLong() {
t.Errorf("From(uint) did not create Long object")
}
if got := obj.AsLong().Uint64(); got != tt.expected {
t.Errorf("From(%d) = %d, want %d", tt.input, got, tt.expected)
}
}
}()
func() {
// Test From with Object.cpyObj()
original := From(42)
obj := From(original.cpyObj())
if !obj.IsLong() {
t.Error("From(Object.cpyObj()) did not create Long object")
}
if got := obj.AsLong().Int64(); got != 42 {
t.Errorf("From(Object.cpyObj()) = %d, want 42", got)
}
// Test that the new object is independent
original = From(100)
if got := obj.AsLong().Int64(); got != 42 {
t.Errorf("Object was not independent, got %d after modifying original", got)
}
}()
func() {
// Test From with functions
add := func(a, b int) int { return a + b }
obj := From(add)
// Verify it's a function type
if !obj.IsFunc() {
t.Error("From(func) did not create Function object")
}
fn := obj.AsFunc()
// Test function call
result := fn.Call(5, 3)
if !result.IsLong() {
t.Error("Function call result is not a Long")
}
if got := result.AsLong().Int64(); got != 8 {
t.Errorf("Function call = %d, want 8", got)
}
}()
func() {
// Test From with function that returns multiple values
divMod := func(a, b int) (int, int) {
return a / b, a % b
}
obj := From(divMod)
if !obj.IsFunc() {
t.Error("From(func) did not create Function object")
}
fn := obj.AsFunc()
result := fn.Call(7, 3)
// Result should be a tuple with two values
if !result.IsTuple() {
t.Error("Multiple return value function did not return a Tuple")
}
tuple := result.AsTuple()
if tuple.Len() != 2 {
t.Errorf("Expected tuple of length 2, got %d", tuple.Len())
}
quotient := tuple.Get(0).AsLong().Int64()
remainder := tuple.Get(1).AsLong().Int64()
if quotient != 2 || remainder != 1 {
t.Errorf("Got (%d, %d), want (2, 1)", quotient, remainder)
}
}()
}
func TestToValueWithCustomType(t *testing.T) {
setupTest(t)
// Define a custom Go type
type Point struct {
X int
Y int
}
// Add the type to Python
pointClass := MainModule().AddType(Point{}, nil, "Point", "Point class")
func() {
// Create a Point instance in Python and convert it back to Go
pyCode := `
p = Point()
p.x = 10
p.y = 20
`
locals := MakeDict(nil)
globals := MakeDict(nil)
builtins := ImportModule("builtins")
globals.Set(MakeStr("__builtins__"), builtins.Object)
globals.Set(MakeStr("Point"), pointClass)
code, err := CompileString(pyCode, "<string>", FileInput)
if err != nil {
t.Errorf("CompileString() error = %v", err)
}
EvalCode(code, globals, locals)
// Get the Python Point instance
pyPoint := locals.Get(MakeStr("p"))
// Convert back to Go Point struct
var point Point
v := reflect.ValueOf(&point).Elem()
if !ToValue(pyPoint, v) {
t.Error("ToValue failed for custom type Point")
}
// Verify the values
if point.X != 10 || point.Y != 20 {
t.Errorf("Expected Point{10, 20}, got Point{%d, %d}", point.X, point.Y)
}
}()
func() {
// Test converting a non-Point Python object to Point should fail
dict := MakeDict(nil)
dict.Set(MakeStr("x"), From(10))
dict.Set(MakeStr("y"), From(20))
var point Point
v := reflect.ValueOf(&point).Elem()
if !ToValue(dict.Object, v) {
t.Error("ToValue failed for custom type Point")
}
if point.X != 10 || point.Y != 20 {
t.Errorf("Expected Point{10, 20}, got Point{%d, %d}", point.X, point.Y)
}
}()
}
func TestFromWithCustomType(t *testing.T) {
setupTest(t)
type Point struct {
X int
Y int
}
// Add the type to Python
pointClass := MainModule().AddType(Point{}, nil, "Point", "Point class")
func() {
// Test From with struct instance
p := Point{X: 10, Y: 20}
obj := From(p)
// Verify the type
if obj.Type().cpyObj() != pointClass.cpyObj() {
t.Error("From(Point) created object with wrong type")
}
// Verify the values
if obj.AttrLong("x").Int64() != 10 {
t.Error("Wrong X value after From conversion")
}
if obj.AttrLong("y").Int64() != 20 {
t.Error("Wrong Y value after From conversion")
}
// Convert back to Go and verify
var p2 Point
v := reflect.ValueOf(&p2).Elem()
if !ToValue(obj, v) {
t.Error("ToValue failed for custom type Point")
}
if p2.X != p.X || p2.Y != p.Y {
t.Errorf("Round trip conversion failed: got Point{%d, %d}, want Point{%d, %d}",
p2.X, p2.Y, p.X, p.Y)
}
}()
func() {
// Test From with pointer to struct
p := &Point{X: 30, Y: 40}
obj := From(p)
// Verify the type
if obj.Type().cpyObj() != pointClass.cpyObj() {
t.Error("From(*Point) created object with wrong type")
}
// Verify the values
if obj.AttrLong("x").Int64() != 30 {
t.Error("Wrong X value after From pointer conversion")
}
if obj.AttrLong("y").Int64() != 40 {
t.Error("Wrong Y value after From pointer conversion")
}
}()
}