forked from httprunner/hrp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin_test.go
80 lines (67 loc) · 1.52 KB
/
plugin_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
// +build linux freebsd darwin
// go plugin doesn't support windows
package hrp
import (
"fmt"
"os"
"os/exec"
"testing"
"github.com/stretchr/testify/assert"
)
func TestMain(m *testing.M) {
fmt.Println("[TestMain] build go plugin")
// flag -race is necessary in order to be consistent with go test
cmd := exec.Command("go", "build", "-buildmode=plugin", `-race`, "-o=examples/debugtalk.so", "examples/plugin/debugtalk.go")
if err := cmd.Run(); err != nil {
panic(err)
}
os.Exit(m.Run())
}
func TestLocatePlugin(t *testing.T) {
cwd, _ := os.Getwd()
_, err := locatePlugin(cwd)
if !assert.Error(t, err) {
t.Fail()
}
_, err = locatePlugin("")
if !assert.Error(t, err) {
t.Fail()
}
startPath := "examples/debugtalk.so"
_, err = locatePlugin(startPath)
if !assert.Nil(t, err) {
t.Fail()
}
startPath = "examples/demo.json"
_, err = locatePlugin(startPath)
if !assert.Nil(t, err) {
t.Fail()
}
startPath = "examples/"
_, err = locatePlugin(startPath)
if !assert.Nil(t, err) {
t.Fail()
}
startPath = "examples/plugin/debugtalk.go"
_, err = locatePlugin(startPath)
if !assert.Nil(t, err) {
t.Fail()
}
startPath = "/abc"
_, err = locatePlugin(startPath)
if !assert.Error(t, err) {
t.Fail()
}
}
func TestCallPluginFunction(t *testing.T) {
parser := newParser()
parser.loadPlugin("examples/debugtalk.so")
// call function without arguments
result, err := parser.callFunc("Concatenate", 1, "2", 3.14)
if !assert.NoError(t, err) {
t.Fail()
}
if !assert.Equal(t, result, "1_2_3.14") {
t.Fail()
}
}