forked from mitchellh/go-mruby
-
Notifications
You must be signed in to change notification settings - Fork 2
/
value_test.go
269 lines (220 loc) · 5.02 KB
/
value_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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package gruby_test
import (
"errors"
"testing"
. "github.com/onsi/gomega"
"github.com/zhulik/gruby"
)
func TestExceptionString_afterClose(t *testing.T) {
t.Parallel()
g := NewG(t)
grb := gruby.Must(gruby.New())
_, err := grb.LoadString(`clearly a syntax error`)
grb.Close()
// This panics before the bug fix that this test tests
g.Expect(err.Error()).To(Equal("undefined method 'error'"))
}
func TestExceptionBacktrace(t *testing.T) {
t.Parallel()
g := NewG(t)
grb := gruby.Must(gruby.New())
defer grb.Close()
parser := gruby.NewParser(grb)
defer parser.Close()
context := gruby.NewCompileContext(grb)
context.SetFilename("hello.rb")
defer context.Close()
_, err := parser.Parse(`
def do_error
raise "Exception"
end
def hop1
do_error
end
def hop2
hop1
end
hop2
`, context)
g.Expect(err).ToNot(HaveOccurred())
proc := parser.GenerateCode()
_, err = grb.Run(proc, nil)
g.Expect(err).To(HaveOccurred())
var exc *gruby.ExceptionError
errors.As(err, &exc)
g.Expect(exc.Message).To(Equal("Exception"))
g.Expect(exc.File).To(Equal("hello.rb"))
g.Expect(exc.Line).To(Equal(3))
g.Expect(exc.Backtrace).To(HaveLen(4))
}
func TestValueCall(t *testing.T) {
t.Parallel()
g := NewG(t)
grb := gruby.Must(gruby.New())
defer grb.Close()
value, err := grb.LoadString(`"foo"`)
g.Expect(err).ToNot(HaveOccurred())
_, err = value.Call("some_function_that_doesnt_exist")
g.Expect(err).To(HaveOccurred())
result, err := value.Call("==", gruby.MustToRuby(grb, "foo"))
g.Expect(err).ToNot(HaveOccurred())
g.Expect(result.Type()).To(Equal(gruby.TypeTrue))
}
func TestValueCallBlock(t *testing.T) {
t.Parallel()
g := NewG(t)
grb := gruby.Must(gruby.New())
defer grb.Close()
value, err := grb.LoadString(`"foo"`)
g.Expect(err).ToNot(HaveOccurred())
block, err := grb.LoadString(`Proc.new { |_| "bar" }`)
g.Expect(err).ToNot(HaveOccurred())
result, err := value.CallBlock("gsub", gruby.MustToRuby(grb, "foo"), block)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(result.Type()).To(Equal(gruby.TypeString))
g.Expect(result.String()).To(Equal("bar"))
}
func TestValueFixnum(t *testing.T) {
t.Parallel()
g := NewG(t)
grb := gruby.Must(gruby.New())
defer grb.Close()
value, err := grb.LoadString("42")
g.Expect(err).ToNot(HaveOccurred())
g.Expect(gruby.MustToGo[int](value)).To(Equal(42))
}
func TestValueString(t *testing.T) {
t.Parallel()
g := NewG(t)
grb := gruby.Must(gruby.New())
defer grb.Close()
value, err := grb.LoadString(`"foo"`)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(value.String()).To(Equal("foo"))
}
func TestValueType(t *testing.T) {
t.Parallel()
g := NewG(t)
grb := gruby.Must(gruby.New())
defer grb.Close()
cases := []struct {
Input string
Expected gruby.ValueType
}{
{
`false`,
gruby.TypeFalse,
},
// TypeFree - Type of value after GC collection
{
`true`,
gruby.TypeTrue,
},
{
`1`,
gruby.TypeFixnum,
},
{
`:test`,
gruby.TypeSymbol,
},
// TypeUndef - Internal value used by mruby for undefined things (instance vars etc)
// These all seem to get converted to exceptions before hitting userland
{
`1.1`,
gruby.TypeFloat,
},
// TypeCptr
{
`Object.new`,
gruby.TypeObject,
},
{
`Object`,
gruby.TypeClass,
},
{
`module T; end; T`,
gruby.TypeModule,
},
// TypeIClass
// TypeSClass
{
`Proc.new { 1 }`,
gruby.TypeProc,
},
{
`[]`,
gruby.TypeArray,
},
{
`{}`,
gruby.TypeHash,
},
{
`"string"`,
gruby.TypeString,
},
{
`1..2`,
gruby.TypeRange,
},
{
`Exception.new`,
gruby.TypeException,
},
// TypeFile
// TypeEnv
// TypeData
// TypeFiber
// TypeMaxDefine
{
`nil`,
gruby.TypeNil,
},
}
for _, tcase := range cases {
r, err := grb.LoadString(tcase.Input)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(r.Type()).To(Equal(tcase.Expected))
}
}
func TestIntGValue(t *testing.T) {
t.Parallel()
g := NewG(t)
grb := gruby.Must(gruby.New())
defer grb.Close()
value := gruby.MustToRuby(grb, 42)
g.Expect(gruby.MustToGo[int](value)).To(Equal(42))
}
func TestStringGValue(t *testing.T) {
t.Parallel()
g := NewG(t)
grb := gruby.Must(gruby.New())
defer grb.Close()
value := gruby.MustToRuby(grb, "foo")
g.Expect(value.String()).To(Equal("foo"))
}
func TestValueClass(t *testing.T) {
t.Parallel()
g := NewG(t)
grb := gruby.Must(gruby.New())
defer grb.Close()
val, err := grb.ObjectClass().New()
g.Expect(err).ToNot(HaveOccurred())
g.Expect(val.Class()).To(Equal(grb.ObjectClass()))
}
func TestValueSingletonClass(t *testing.T) {
t.Parallel()
g := NewG(t)
grb := gruby.Must(gruby.New())
defer grb.Close()
fn := func(grb *gruby.GRuby, self gruby.Value) (gruby.Value, gruby.Value) {
args := grb.GetArgs()
return gruby.MustToRuby(grb, gruby.MustToGo[int](args[0])+gruby.MustToGo[int](args[1])), nil
}
grb.TopSelf().SingletonClass().DefineMethod("add", fn, gruby.ArgsReq(2))
result, err := grb.LoadString(`add(46, 2)`)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(result.String()).To(Equal("48"))
}