This repository has been archived by the owner on Mar 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathrfunctions_spec.rb
323 lines (226 loc) · 6.37 KB
/
rfunctions_spec.rb
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#
# Specifying rufus-lua
#
# Wed Mar 18 17:53:06 JST 2009
#
require 'spec_base'
describe Rufus::Lua::State do
before do
@s = Rufus::Lua::State.new
end
after do
@s.close
end
describe '#function' do
it 'raises when no block is given' do
expect(lambda {
@s.function 'no_block'
}).to raise_error(RuntimeError)
end
it 'works without arguments' do
blinked = false
@s.function 'blink' do
blinked = true
end
@s.eval('blink()')
expect(blinked).to eq true
end
it 'works with arguments' do
message = nil
@s.function :greet do |msg|
message = msg
end
@s.eval("greet('obandegozaimasu')")
expect(message).to eq 'obandegozaimasu'
end
it 'works with function arguments' do
message = nil
@s.function :exec do |fun|
message = fun.call()
end
@s.eval("exec(function() return 'foobar' end)")
expect(message).to eq 'foobar'
end
it 'binds functions inside of Lua tables' do
@s.eval('lib = {}')
@s.function 'lib.myfunc' do |x|
x + 2
end
expect(@s.eval("return lib.myfunc(3)")).to eq 5.0
end
it 'creates the top Lua table if not present' do
@s.function 'lib.myfunc' do |x|
x + 2
end
expect(@s.eval("return lib.myfunc(3)")).to eq 5.0
end
it 'only creates the top table (not intermediary tables)' do
expect(lambda {
@s.function('lib.toto.myfunc') { |x| x + 2 }
}).to raise_error(ArgumentError)
end
end
context 'calling a Ruby function from Lua' do
it 'may return a single value' do
@s.function :greet do
'hello !'
end
expect(@s.eval('return greet()')).to eq 'hello !'
end
it 'may return multiple values' do
@s.function :compute do
[ 'a', true, 1 ]
end
expect(@s.eval('return compute()').to_a).to eq [ 'a', true, 1.0 ]
end
it 'may return tables' do
@s.function :compute do
{ 'a' => 'alpha', 'z' => 'zebra' }
end
expect(@s.eval('return compute()').to_h).to eq(
{ 'a' => 'alpha', 'z' => 'zebra' })
end
it 'may return tables (with nested arrays)' do
@s.function :compute do
{ 'a' => 'alpha', 'z' => [ 1, 2, 3 ] }
end
expect(@s.eval('return compute()').to_h['z'].to_a).to eq [ 1.0, 2.0, 3.0 ]
end
it 'accepts hashes as arguments' do
@s.function :to_json do |h|
"{" + h.collect { |k, v| "#{k}:\"#{v}\"" }.join(",") + "}"
end
expect(@s.eval(
"return to_json({ a = 'ALPHA', b = 'BRAVO' })"
)).to eq(
'{a:"ALPHA",b:"BRAVO"}'
)
end
it 'accepts arrays as arguments' do
@s.function :do_join do |a|
a.to_a.join(', ')
end
expect(@s.eval(
"return do_join({ 'alice', 'bob', 'charly' })"
)).to eq(
'alice, bob, charly'
)
end
it 'counts the animals correctly' do
@s.function 'key_up' do |table|
table.inject({}) do |h, (k, v)|
h[k.to_s.upcase] = v; h
end
end
expect(@s.eval(%{
local table = {}
table['CoW'] = 2
table['pigs'] = 3
table['DUCKS'] = 'none'
return key_up(table)
}).to_h).to eq(
{ 'COW' => 2.0, 'DUCKS' => 'none', 'PIGS' => 3.0 }
)
end
it 'returns Ruby arrays as Lua tables' do
@s.function :get_data do |msg|
%w[ one two three ]
end
@s.eval('data = get_data()')
expect(@s['data'].to_a).to eq %w[ one two three ]
expect(@s.eval('return type(data)')).to eq('table')
end
it 'return properly indexed Lua tables' do
@s.function :get_data do |msg|
%w[ one two three ]
end
@s.eval('data = get_data()')
expect(@s.eval('return data[0]')).to eq nil
expect(@s.eval('return data[1]')).to eq 'one'
end
it 'accepts more than 1 arg and order the args correctly' do
@s.function 'myfunc' do |a, b, c|
"#{a}_#{b}_#{c}"
end
expect(@s.eval("return myfunc(1, 2, 3)")).to eq '1.0_2.0_3.0'
end
it 'accepts optional arguments' do
@s.function 'myfunc' do |a, b, c|
"#{a}_#{b}_#{c}"
end
expect(@s.eval("return myfunc(1)")).to eq '1.0__'
end
it 'is ok when there are too many args' do
@s.function 'myfunc' do |a, b|
"#{a}_#{b}"
end
expect(@s.eval("return myfunc(1, 2, 3)")).to eq '1.0_2.0'
end
it 'passes Float arguments correctly' do
@s.function 'myfunc' do |a|
"#{a.class} #{a}"
end
expect(@s.eval("return myfunc(3.14)")).to eq 'Float 3.14'
end
it 'preserves arguments' do
@s.function 'check_types' do |t, s, f, h, a|
#p [ t, s, f, h, a ]
(t.is_a?(TrueClass) &&
s.is_a?(String) &&
f.is_a?(Float) &&
h.is_a?(Rufus::Lua::Table) &&
a.is_a?(Rufus::Lua::Table))
end
expect(@s.eval(
"return check_types(true, 'foobar', 3.13, {a='ay',b='bee'}, {'one','two','three'})"
)).to eq true
end
it 'honours to_ruby=true' do
@s.function 'check_types', :to_ruby => true do |t, s, f, h, a|
#p [ t, s, f, h, a ]
(t.is_a?(TrueClass) &&
s.is_a?(String) &&
f.is_a?(Float) &&
h.is_a?(Hash) &&
a.is_a?(Array))
end
expect(@s.eval(
"return check_types(true, 'foobar', 3.13, {a='ay',b='bee'}, {'one','two','three'})"
)).to eq true
end
it 'protects callbacks from GC' do
@s.function 'myfunc' do |a|
end
expect(@s.instance_variable_get(:@callbacks).size).to eq 1
end
end
context 'Ruby functions and exceptions' do
it 'raises exceptions (Ruby -> Lua -> Ruby and back)' do
@s.function :do_fail do
raise 'fail!'
end
expect {
@s.eval('return do_fail()')
}.to raise_error(RuntimeError)
end
it 'raises exceptions (Ruby -> Lua -> Ruby and back)' do
@s.function :do_fail do
raise 'fail!'
end
expect {
@s.eval('do_fail()')
}.to raise_error(RuntimeError)
end
it 'raises exceptions (gh-42)' do
@s.function :no_fail do
1
end
@s.function :do_fail do
raise 'fail!'
end
expect {
@s.eval('do_fail(); no_fail()')
}.to raise_error(RuntimeError)
end
end
end