-
Notifications
You must be signed in to change notification settings - Fork 3
/
python_test.go
108 lines (86 loc) · 2.72 KB
/
python_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
package gopython
import (
"github.com/stretchr/testify/assert"
"os"
"testing"
)
func TestCompleteWorkFlow(t *testing.T) {
testFile, err := os.OpenFile("./hello.py", os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0666)
assert.Nil(t, err)
testFile.Write([]byte("def minus(x,y):\n"))
testFile.Write([]byte(" return x - y\n"))
testFile.Write([]byte("def add(x,y):\n"))
testFile.Write([]byte(" return x + y\n"))
testFile.Write([]byte("def return_self(self):\n"))
testFile.Write([]byte(" return self\n"))
testFile.Write([]byte("def zero_arg():\n"))
testFile.Write([]byte(" return 0\n"))
testFile.Close()
err = Initialize()
assert.Nil(t, err)
InsertPackagePath("./")
ret, err := CallFunc("hello", "minus", 1, 1)
callInt := GoInt(ret)
assert.True(t, callInt == 0)
assert.Nil(t, err)
ret, err = CallFunc("hello", "add", 1, 1)
callInt = GoInt(ret)
assert.True(t, callInt == 2)
assert.Nil(t, err)
self1, err := CallFunc("hello", "return_self", 1)
callSelf1 := GoInt(self1)
assert.True(t, callSelf1 == 1)
assert.Nil(t, err)
self2, err := CallFunc("hello", "return_self", "self")
callSelf2 := GoStr(self2)
assert.True(t, callSelf2 == "self")
assert.Nil(t, err)
zero, err := CallFunc("hello", "zero_arg")
callZero := GoInt(zero)
assert.True(t, callZero == 0)
assert.Nil(t, err)
Finalize()
defer os.Remove("./hello.py")
defer os.Remove("./hello.pyc")
}
func TestCompleteWorkFlowWithSubInterpreter(t *testing.T) {
testFile, err := os.OpenFile("./hello.py", os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0666)
assert.Nil(t, err)
testFile.Write([]byte("def minus(x,y):\n"))
testFile.Write([]byte(" return x - y\n"))
testFile.Write([]byte("def add(x,y):\n"))
testFile.Write([]byte(" return x + y\n"))
testFile.Write([]byte("def return_self(self):\n"))
testFile.Write([]byte(" return self\n"))
testFile.Write([]byte("def zero_arg():\n"))
testFile.Write([]byte(" return 0\n"))
testFile.Close()
err = Initialize()
assert.Nil(t, err)
subret, err := Py_NewInterpreter()
assert.Nil(t, err)
InsertPackagePath("./")
ret, err := CallFunc("hello", "minus", 1, 1)
callInt := GoInt(ret)
assert.True(t, callInt == 0)
assert.Nil(t, err)
ret, err = CallFunc("hello", "add", 1, 1)
callInt = GoInt(ret)
assert.True(t, callInt == 2)
assert.Nil(t, err)
self1, err := CallFunc("hello", "return_self", 1)
callSelf1 := GoInt(self1)
assert.True(t, callSelf1 == 1)
assert.Nil(t, err)
self2, err := CallFunc("hello", "return_self", "self")
callSelf2 := GoStr(self2)
assert.True(t, callSelf2 == "self")
assert.Nil(t, err)
zero, err := CallFunc("hello", "zero_arg")
callZero := GoInt(zero)
assert.True(t, callZero == 0)
assert.Nil(t, err)
Py_EndInterpreter(subret)
defer os.Remove("./hello.py")
defer os.Remove("./hello.pyc")
}