rufus-lua 1.1.2 → 1.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,41 +0,0 @@
1
-
2
- #
3
- # Specifying rufus-lua
4
- #
5
- # Mon Mar 16 23:07:49 JST 2009
6
- #
7
-
8
- require 'spec_base'
9
-
10
-
11
- describe Rufus::Lua::State do
12
-
13
- before do
14
- @s = Rufus::Lua::State.new
15
- end
16
- after do
17
- @s.close
18
- end
19
-
20
- describe '#gc_collect!' do
21
-
22
- it 'raises when called on a closed Rufus::Lua::State instance' do
23
-
24
- s = Rufus::Lua::State.new
25
- s.close
26
- expect(lambda { s.gc_collect! }).to raise_error(RuntimeError)
27
- end
28
- end
29
-
30
- describe '#gc_count' do
31
-
32
- it 'returns an indication about the Lua interpreter memory usage' do
33
-
34
- before_usage = @s.gc_count
35
- @s.eval("return table.concat({ 'hello', 'from', 'Lua' }, ' ')")
36
- after_usage = @s.gc_count
37
-
38
- expect(after_usage).to be > before_usage
39
- end
40
- end
41
- end
@@ -1,32 +0,0 @@
1
-
2
- #
3
- # Specifying rufus-lua
4
- #
5
- # Thu Jun 19 20:29:06 JST 2014
6
- #
7
-
8
- require 'spec_base'
9
-
10
-
11
- describe Rufus::Lua::Lib do
12
-
13
- describe '.path' do
14
-
15
- it 'returns the Lua lib being used' do
16
-
17
- path =
18
- Array(
19
- Dir.glob('/usr/lib/liblua*.so') +
20
- Dir.glob('/usr/lib/*/liblua*.so') +
21
- Dir.glob('/usr/local/lib/liblua*.so') +
22
- Dir.glob('/opt/local/lib/liblua*.so') +
23
- Dir.glob('/usr/lib/liblua*.dylib') +
24
- Dir.glob('/usr/local/lib/liblua*.dylib') +
25
- Dir.glob('/opt/local/lib/liblua*.dylib')
26
- ).first
27
-
28
- expect(Rufus::Lua::Lib.path).to eq path
29
- end
30
- end
31
- end
32
-
@@ -1,295 +0,0 @@
1
-
2
- #
3
- # Specifying rufus-lua
4
- #
5
- # Wed Mar 18 17:53:06 JST 2009
6
- #
7
-
8
- require 'spec_base'
9
-
10
-
11
- describe Rufus::Lua::State do
12
-
13
- before do
14
- @s = Rufus::Lua::State.new
15
- end
16
- after do
17
- @s.close
18
- end
19
-
20
- describe '#function' do
21
-
22
- it 'raises when no block is given' do
23
-
24
- expect(lambda {
25
- @s.function 'no_block'
26
- }).to raise_error(RuntimeError)
27
- end
28
-
29
- it 'works without arguments' do
30
-
31
- blinked = false
32
-
33
- @s.function 'blink' do
34
- blinked = true
35
- end
36
-
37
- @s.eval('blink()')
38
-
39
- expect(blinked).to eq true
40
- end
41
-
42
- it 'works with arguments' do
43
-
44
- message = nil
45
-
46
- @s.function :greet do |msg|
47
- message = msg
48
- end
49
-
50
- @s.eval("greet('obandegozaimasu')")
51
-
52
- expect(message).to eq 'obandegozaimasu'
53
- end
54
-
55
- it 'works with function arguments' do
56
-
57
- message = nil
58
-
59
- @s.function :exec do |fun|
60
- message = fun.call()
61
- end
62
-
63
- @s.eval("exec(function() return 'foobar' end)")
64
-
65
- expect(message).to eq 'foobar'
66
- end
67
-
68
- it 'binds functions inside of Lua tables' do
69
-
70
- @s.eval('lib = {}')
71
- @s.function 'lib.myfunc' do |x|
72
- x + 2
73
- end
74
-
75
- expect(@s.eval("return lib.myfunc(3)")).to eq 5.0
76
- end
77
-
78
- it 'creates the top Lua table if not present' do
79
-
80
- @s.function 'lib.myfunc' do |x|
81
- x + 2
82
- end
83
-
84
- expect(@s.eval("return lib.myfunc(3)")).to eq 5.0
85
- end
86
-
87
- it 'only creates the top table (not intermediary tables)' do
88
-
89
- expect(lambda {
90
- @s.function('lib.toto.myfunc') { |x| x + 2 }
91
- }).to raise_error(ArgumentError)
92
- end
93
- end
94
-
95
- describe 'calling a Ruby function from Lua' do
96
-
97
- it 'may return a single value' do
98
-
99
- @s.function :greet do
100
- 'hello !'
101
- end
102
-
103
- expect(@s.eval('return greet()')).to eq 'hello !'
104
- end
105
-
106
- it 'may return multiple values' do
107
-
108
- @s.function :compute do
109
- [ 'a', true, 1 ]
110
- end
111
-
112
- expect(@s.eval('return compute()').to_a).to eq [ 'a', true, 1.0 ]
113
- end
114
-
115
- it 'may return tables' do
116
-
117
- @s.function :compute do
118
- { 'a' => 'alpha', 'z' => 'zebra' }
119
- end
120
-
121
- expect(@s.eval('return compute()').to_h).to eq(
122
- { 'a' => 'alpha', 'z' => 'zebra' })
123
- end
124
-
125
- it 'may return tables (with nested arrays)' do
126
-
127
- @s.function :compute do
128
- { 'a' => 'alpha', 'z' => [ 1, 2, 3 ] }
129
- end
130
-
131
- expect(@s.eval('return compute()').to_h['z'].to_a).to eq [ 1.0, 2.0, 3.0 ]
132
- end
133
-
134
- it 'accepts hashes as arguments' do
135
-
136
- @s.function :to_json do |h|
137
- "{" + h.collect { |k, v| "#{k}:\"#{v}\"" }.join(",") + "}"
138
- end
139
-
140
- expect(@s.eval(
141
- "return to_json({ a = 'ALPHA', b = 'BRAVO' })"
142
- )).to eq(
143
- '{a:"ALPHA",b:"BRAVO"}'
144
- )
145
- end
146
-
147
- it 'accepts arrays as arguments' do
148
-
149
- @s.function :do_join do |a|
150
- a.to_a.join(', ')
151
- end
152
-
153
- expect(@s.eval(
154
- "return do_join({ 'alice', 'bob', 'charly' })"
155
- )).to eq(
156
- 'alice, bob, charly'
157
- )
158
- end
159
-
160
- it 'raise exceptions (Ruby -> Lua -> Ruby and back)' do
161
-
162
- @s.function :do_fail do
163
- raise 'fail!'
164
- end
165
-
166
- expect(lambda {
167
- @s.eval('return do_fail()')
168
- }).to raise_error(RuntimeError)
169
- end
170
-
171
- it 'counts the animals correctly' do
172
-
173
- @s.function 'key_up' do |table|
174
- table.inject({}) do |h, (k, v)|
175
- h[k.to_s.upcase] = v; h
176
- end
177
- end
178
-
179
- expect(@s.eval(%{
180
- local table = {}
181
- table['CoW'] = 2
182
- table['pigs'] = 3
183
- table['DUCKS'] = 'none'
184
- return key_up(table)
185
- }).to_h).to eq(
186
- { 'COW' => 2.0, 'DUCKS' => 'none', 'PIGS' => 3.0 }
187
- )
188
- end
189
-
190
- it 'returns Ruby arrays as Lua tables' do
191
-
192
- @s.function :get_data do |msg|
193
- %w[ one two three ]
194
- end
195
-
196
- @s.eval('data = get_data()')
197
-
198
- expect(@s['data'].to_a).to eq %w[ one two three ]
199
- expect(@s.eval('return type(data)')).to eq('table')
200
- end
201
-
202
- it 'return properly indexed Lua tables' do
203
-
204
- @s.function :get_data do |msg|
205
- %w[ one two three ]
206
- end
207
-
208
- @s.eval('data = get_data()')
209
-
210
- expect(@s.eval('return data[0]')).to eq nil
211
- expect(@s.eval('return data[1]')).to eq 'one'
212
- end
213
-
214
- it 'accepts more than 1 arg and order the args correctly' do
215
-
216
- @s.function 'myfunc' do |a, b, c|
217
- "#{a}_#{b}_#{c}"
218
- end
219
-
220
- expect(@s.eval("return myfunc(1, 2, 3)")).to eq '1.0_2.0_3.0'
221
- end
222
-
223
- it 'accepts optional arguments' do
224
-
225
- @s.function 'myfunc' do |a, b, c|
226
- "#{a}_#{b}_#{c}"
227
- end
228
-
229
- expect(@s.eval("return myfunc(1)")).to eq '1.0__'
230
- end
231
-
232
- it 'is ok when there are too many args' do
233
-
234
- @s.function 'myfunc' do |a, b|
235
- "#{a}_#{b}"
236
- end
237
-
238
- expect(@s.eval("return myfunc(1, 2, 3)")).to eq '1.0_2.0'
239
- end
240
-
241
- it 'passes Float arguments correctly' do
242
-
243
- @s.function 'myfunc' do |a|
244
- "#{a.class} #{a}"
245
- end
246
-
247
- expect(@s.eval("return myfunc(3.14)")).to eq 'Float 3.14'
248
- end
249
-
250
- it 'preserves arguments' do
251
-
252
- @s.function 'check_types' do |t, s, f, h, a|
253
-
254
- #p [ t, s, f, h, a ]
255
-
256
- (t.is_a?(TrueClass) &&
257
- s.is_a?(String) &&
258
- f.is_a?(Float) &&
259
- h.is_a?(Rufus::Lua::Table) &&
260
- a.is_a?(Rufus::Lua::Table))
261
- end
262
-
263
- expect(@s.eval(
264
- "return check_types(true, 'foobar', 3.13, {a='ay',b='bee'}, {'one','two','three'})"
265
- )).to eq true
266
- end
267
-
268
- it 'honours to_ruby=true' do
269
-
270
- @s.function 'check_types', :to_ruby => true do |t, s, f, h, a|
271
-
272
- #p [ t, s, f, h, a ]
273
-
274
- (t.is_a?(TrueClass) &&
275
- s.is_a?(String) &&
276
- f.is_a?(Float) &&
277
- h.is_a?(Hash) &&
278
- a.is_a?(Array))
279
- end
280
-
281
- expect(@s.eval(
282
- "return check_types(true, 'foobar', 3.13, {a='ay',b='bee'}, {'one','two','three'})"
283
- )).to eq true
284
- end
285
-
286
- it 'protects callbacks from GC' do
287
-
288
- @s.function 'myfunc' do |a|
289
- end
290
-
291
- expect(@s.instance_variable_get(:@callbacks).size).to eq 1
292
- end
293
- end
294
- end
295
-
@@ -1,9 +0,0 @@
1
-
2
- #
3
- # specifying rufus-lua
4
- #
5
- # Wed Mar 11 16:09:11 JST 2009
6
- #
7
-
8
- Dir["#{File.dirname(__FILE__)}/*_spec.rb"].each { |path| load(path) }
9
-
@@ -1,10 +0,0 @@
1
-
2
- #
3
- # Specifying rufus-lua
4
- #
5
- # Wed Mar 11 16:09:31 JST 2009
6
- #
7
-
8
- require 'fileutils'
9
- require 'rufus-lua'
10
-
@@ -1,106 +0,0 @@
1
-
2
- #
3
- # Specifying rufus-lua
4
- #
5
- # Mon Mar 16 23:38:00 JST 2009
6
- #
7
-
8
- require 'spec_base'
9
-
10
-
11
- describe Rufus::Lua::State do
12
-
13
- describe '#new' do
14
-
15
- it 'loads all libs by default' do
16
-
17
- @s = Rufus::Lua::State.new
18
- expect(@s.eval('return os;')).not_to be nil
19
- @s.close
20
- end
21
-
22
- it 'loads no libs when told so' do
23
-
24
- @s = Rufus::Lua::State.new(false)
25
- expect(@s.eval('return os;')).to be nil
26
- @s.close
27
- end
28
-
29
- it 'loads only specific libs when told so' do
30
-
31
- @s = Rufus::Lua::State.new([ :os, :math ])
32
- expect(@s.eval('return io;')).to be nil
33
- expect(@s.eval('return os;')).not_to be nil
34
- @s.close
35
- end
36
- end
37
-
38
- describe '#close' do
39
-
40
- it 'does not crash when closing an already closed State' do
41
-
42
- @s = Rufus::Lua::State.new
43
- @s.close
44
-
45
- expect(lambda { @s.close }).to raise_error(RuntimeError)
46
- end
47
- end
48
-
49
- describe '#[]' do
50
-
51
- before do
52
- @s = Rufus::Lua::State.new
53
- end
54
- after do
55
- @s.close
56
- end
57
-
58
- it 'return nils for unbound variables' do
59
-
60
- expect(@s['a']).to be nil
61
- end
62
-
63
- it 'accepts setting values directly' do
64
-
65
- @s['a'] = 1
66
- @s['a'] == 1
67
- end
68
-
69
- it 'accepts setting array values directly' do
70
-
71
- @s['a'] = [ true, false, %w[ alpha bravo charly ] ]
72
-
73
- expect(@s['a'].to_a[0]).to be true
74
- expect(@s['a'].to_a[2].to_a).to eq %w[ alpha bravo charly ]
75
- end
76
-
77
- it 'accepts setting hash values directly' do
78
-
79
- @s['a'] = { 'a' => 'alpha', 'b' => 'bravo' }
80
-
81
- expect(@s['a'].to_h).to eq({ 'a' => 'alpha', 'b' => 'bravo' })
82
- end
83
- end
84
-
85
- context 'gh-6 panic: unprotected error' do
86
-
87
- it 'does not happen when loading io alone' do
88
-
89
- state = Rufus::Lua::State.new(%w[ io ])
90
-
91
- expect(state.eval('return io;')).not_to be nil
92
- expect(state.eval('return math;')).to be nil
93
- state.close
94
- end
95
-
96
- it 'does not happen' do
97
-
98
- state = Rufus::Lua::State.new(%w[ base table string math package ])
99
-
100
- expect(state.eval('return table;')).not_to be nil
101
- expect(state.eval('return math;')).not_to be nil
102
- state.close
103
- end
104
- end
105
- end
106
-