forked from wapc/wapc-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule_test.go
62 lines (49 loc) · 1.42 KB
/
module_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
package wapc_test
import (
"context"
"io/ioutil"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/wapc/wapc-go"
)
func TestModule(t *testing.T) {
ctx := context.Background()
code, err := ioutil.ReadFile("testdata/hello.wasm")
require.NoError(t, err)
consoleLogInvoked := false
hostCallInvoked := false
consoleLog := func(msg string) {
assert.Equal(t, "logging something", msg)
consoleLogInvoked = true
}
hostCall := func(ctx context.Context, binding, namespace, operation string, payload []byte) ([]byte, error) {
assert.Equal(t, "myBinding", binding)
assert.Equal(t, "sample", namespace)
assert.Equal(t, "hello", operation)
assert.Equal(t, "Simon", string(payload))
hostCallInvoked = true
return []byte("test"), nil
}
module, err := wapc.New(code, hostCall)
module.SetLogger(consoleLog)
require.NoError(t, err)
defer module.Close()
instance, err := module.Instantiate()
require.NoError(t, err)
defer instance.Close()
result, err := instance.Invoke(ctx, "hello", []byte("waPC"))
require.NoError(t, err)
assert.Equal(t, "Hello, waPC", string(result))
assert.True(t, consoleLogInvoked)
assert.True(t, hostCallInvoked)
result, err = instance.Invoke(ctx, "error", []byte("waPC"))
require.Error(t, err)
msg := err.Error()
index := strings.IndexByte(msg, ';')
if index != -1 {
msg = msg[:index]
}
assert.Equal(t, "error occurred", msg)
}