-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfunction_test.go
99 lines (84 loc) · 3.32 KB
/
function_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
package wazergo_test
import (
"context"
"errors"
"reflect"
"strconv"
"testing"
. "github.com/stealthrocket/wazergo"
"github.com/stealthrocket/wazergo/internal/wasmtest"
. "github.com/stealthrocket/wazergo/types"
"github.com/stealthrocket/wazergo/wasm"
"github.com/tetratelabs/wazero/api"
)
type value[T any] ParamResult[T]
type instance struct{}
func (*instance) Close(context.Context) error { return nil }
type plugin struct{}
func (plugin) Name() string { return "test" }
func (plugin) Functions() Functions[*instance] { return nil }
func (plugin) Instantiate(...Option[*instance]) *instance { return nil }
func TestFunc0(t *testing.T) {
oops := errors.New("oops")
testFunc0(t, 1, func(*instance, context.Context) Int32 { return 1 })
testFunc0(t, 2, func(*instance, context.Context) Int64 { return 2 })
testFunc0(t, 3, func(*instance, context.Context) Uint32 { return 3 })
testFunc0(t, 4, func(*instance, context.Context) Uint64 { return 4 })
testFunc0(t, 0.1, func(*instance, context.Context) Float32 { return 0.1 })
testFunc0(t, 0.5, func(*instance, context.Context) Float64 { return 0.5 })
testFunc0(t, OK, func(*instance, context.Context) Error { return OK })
testFunc0(t, Fail(^Errno(0)), func(*instance, context.Context) Error { return Fail(oops) })
}
func TestFunc1(t *testing.T) {
testFunc1(t, 42, 42, func(this *instance, ctx context.Context, v Int32) Int32 {
return v
})
testFunc1(t, Res(Int32(42)), wasmtest.Bytes("42"),
func(this *instance, ctx context.Context, v wasmtest.Bytes) Optional[Int32] {
i, err := strconv.Atoi(string(v))
return Opt(Int32(i), err)
},
)
}
func TestFunc2(t *testing.T) {
testFunc2(t, Res(Int32(41)), wasmtest.Bytes("42"), wasmtest.Bytes("-1"),
func(this *instance, ctx context.Context, v1, v2 wasmtest.Bytes) Optional[Int32] {
i1, _ := strconv.Atoi(string(v1))
i2, _ := strconv.Atoi(string(v2))
return Res(Int32(i1 + i2))
},
)
}
func testFunc(t *testing.T, opts []Option[*instance], test func(*instance, context.Context, api.Module)) {
t.Helper()
memory := wasm.NewFixedSizeMemory(wasm.PageSize)
module := wasmtest.NewModule("test", wasmtest.Memory(memory))
test(new(instance), context.Background(), module)
}
func testFunc0[R value[R]](t *testing.T, want R, f func(*instance, context.Context) R, opts ...Option[*instance]) {
t.Helper()
testFunc(t, opts, func(this *instance, ctx context.Context, module api.Module) {
t.Helper()
assertEqual(t, want, wasmtest.Call[R](F0(f), ctx, module, this))
})
}
func testFunc1[R value[R], T value[T]](t *testing.T, want R, arg T, f func(*instance, context.Context, T) R, opts ...Option[*instance]) {
t.Helper()
testFunc(t, opts, func(this *instance, ctx context.Context, module api.Module) {
t.Helper()
assertEqual(t, want, wasmtest.Call[R](F1(f), ctx, module, this, arg))
})
}
func testFunc2[R value[R], T1 value[T1], T2 value[T2]](t *testing.T, want R, arg1 T1, arg2 T2, f func(*instance, context.Context, T1, T2) R, opts ...Option[*instance]) {
t.Helper()
testFunc(t, opts, func(this *instance, ctx context.Context, module api.Module) {
t.Helper()
assertEqual(t, want, wasmtest.Call[R](F2(f), ctx, module, this, arg1, arg2))
})
}
func assertEqual(t *testing.T, want, got any) {
t.Helper()
if !reflect.DeepEqual(want, got) {
t.Errorf("result mismatch: want=%+v got=%+v", want, got)
}
}