This repository has been archived by the owner on Jul 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrengo_test.go
132 lines (104 loc) · 2.64 KB
/
wrengo_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
package wrengo
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestCompileError(t *testing.T) {
vm := NewVM(NewConfiguration())
defer vm.FreeVM()
err := vm.Interpret(DefaultModule, `Hay, it's me, an error!`)
if err == nil {
t.FailNow()
}
}
func TestInterpret(t *testing.T) {
var out string
config := NewConfiguration()
config.WriteFunc = func(vm *VM, text string) {
out += text
}
config.ErrorFunc = CallbackError
vm := NewVM(config)
defer vm.FreeVM()
err := vm.Interpret(DefaultModule, `System.print("Hello there!")`)
if err != nil {
t.FailNow()
}
assert.Equal(t, "Hello there!\n", out)
}
func TestCallHandle(t *testing.T) {
config := NewConfiguration()
config.WriteFunc = CallbackWrite
config.ErrorFunc = CallbackError
vm := NewVM(config)
defer vm.FreeVM()
err := vm.Interpret(DefaultModule, `
class WrenMath {
static do_add(a, b) {
return a + b
}
}
`)
if err != nil {
t.FailNow()
}
vm.EnsureSlots(3)
vm.GetVariable(DefaultModule, "WrenMath", 0)
h := vm.NewCallHandle("do_add(_,_)")
vm.SetSlotDouble(1, 228)
vm.SetSlotDouble(2, 1337)
err = h.Call()
if err != nil {
t.FailNow()
}
res := vm.GetSlotDouble(0)
assert.Equal(t, float64(228+1337), res)
}
func TestSlots(t *testing.T) {
vm := NewVM(NewConfiguration())
defer vm.FreeVM()
vm.EnsureSlots(1)
assert.Equal(t, 1, vm.GetSlotCount())
vm.SetSlotBool(0, true)
assert.Equal(t, true, vm.GetSlotBool(0))
vm.SetSlotBytes(0, []byte("Hello"))
assert.Equal(t, []byte("Hello"), vm.GetSlotBytes(0, len([]byte("Hello"))))
vm.SetSlotDouble(0, 1.337)
assert.Equal(t, 1.337, vm.GetSlotDouble(0))
vm.SetSlotString(0, "Hi")
assert.Equal(t, "Hi", vm.GetSlotString(0))
}
type God struct {
msg string
}
// The constructor for a foreign type takes no arguments and returns
// an interface{} value representing the new object.
func NewGod() interface{} {
return &God{msg: "What are you doing? "}
}
func GetGodsMessage(vm *VM) {
god := vm.GetSlotForeign(0, God{}).(God)
name := vm.GetSlotString(1)
vm.SetSlotString(0, god.msg+name)
}
func TestForeign(t *testing.T) {
program := `
foreign class God {
construct new() {}
foreign getMessage(name)
}
var god = God.new()
System.print(god.getMessage("Damien"))
`
var out string
config := NewConfiguration()
config.WriteFunc = func(vm *VM, text string) {
out += text
}
config.ErrorFunc = CallbackError
vm := NewVM(config)
assert.NoError(t, vm.BindForeignClass("God", NewGod))
assert.NoError(t, vm.BindForeignMethod("God", false, "getMessage(_)", GetGodsMessage))
assert.NoError(t, vm.Interpret(DefaultModule, program))
assert.Equal(t, "What are you doing? Damien\n", out)
}