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,222 +0,0 @@
1
-
2
- #
3
- # Specifying rufus-lua
4
- #
5
- # Tue Jul 22 21:07:10 JST 2014
6
- #
7
-
8
- require 'spec_base'
9
-
10
-
11
- describe 'State and error handler' do
12
-
13
- before do
14
- @s = Rufus::Lua::State.new
15
- end
16
- after do
17
- @s.close
18
- end
19
-
20
- describe '#set_error_handler(lua_code)' do
21
-
22
- it 'registers a function as error handler' do
23
-
24
- le = nil
25
-
26
- @s.set_error_handler(%{
27
- function (e)
28
- return e .. '\\n' .. debug.traceback()
29
- end
30
- })
31
-
32
- @s.eval(%{
33
- function f ()
34
- error("in f")
35
- end
36
- }, nil, 'mystuff.lua', 77)
37
- begin
38
- @s.eval('f()', nil, 'mymain.lua', 88)
39
- rescue Rufus::Lua::LuaError => le
40
- end
41
-
42
- expect(le.message).to eq(%{
43
- eval:pcall : '[string "mystuff.lua:77"]:3: in f
44
- stack traceback:
45
- [string "line"]:2: in function <[string "line"]:1>
46
- [C]: in function 'error'
47
- [string "mystuff.lua:77"]:3: in function 'f'
48
- [string "mymain.lua:88"]:1: in main chunk' (2 LUA_ERRRUN)
49
- }.strip)
50
-
51
- expect(@s.send(:stack_top)).to eq(1)
52
- end
53
-
54
- it 'sets the error handler in a permanent way' do
55
-
56
- le = nil
57
-
58
- @s.set_error_handler(%{
59
- function (e)
60
- return 'something went wrong: ' .. string.gmatch(e, ": (.+)$")()
61
- end
62
- })
63
-
64
- begin
65
- @s.eval('error("a")')
66
- rescue Rufus::Lua::LuaError => le
67
- end
68
-
69
- expect(le.msg).to eq('something went wrong: a')
70
-
71
- begin
72
- @s.eval('error("b")')
73
- rescue Rufus::Lua::LuaError => le
74
- end
75
-
76
- expect(le.msg).to eq('something went wrong: b')
77
- end
78
-
79
- context 'CallbackState' do
80
-
81
- # Setting an error handler on the calling state or even directly
82
- # on the CallbackState doesn't have any effect.
83
- # Any error handler seems bypassed.
84
-
85
- it 'has no effect' do
86
-
87
- e = nil
88
-
89
- @s.set_error_handler(%{
90
- function (e) return 'bad: ' .. string.gmatch(e, ": (.+)$")() end
91
- })
92
- f = @s.function(:do_fail) { fail('in style') }
93
- begin
94
- @s.eval('do_fail()')
95
- rescue Exception => e
96
- end
97
-
98
- expect(e.class).to eq(RuntimeError)
99
- end
100
- end
101
- end
102
-
103
- describe '#set_error_handler(:traceback)' do
104
-
105
- it 'sets a vanilla debug.traceback() error handler' do
106
-
107
- le = nil
108
-
109
- @s.set_error_handler(:traceback)
110
-
111
- @s.eval(%{
112
- function f ()
113
- error("in f")
114
- end
115
- }, nil, 'mystuff.lua', 77)
116
- begin
117
- @s.eval('f()', nil, 'mymain.lua', 88)
118
- rescue Rufus::Lua::LuaError => le
119
- end
120
-
121
- expect(le.message).to eq(%{
122
- eval:pcall : '[string "mystuff.lua:77"]:3: in f
123
- stack traceback:
124
- [C]: in function 'error'
125
- [string "mystuff.lua:77"]:3: in function 'f'
126
- [string "mymain.lua:88"]:1: in main chunk' (2 LUA_ERRRUN)
127
- }.strip)
128
- end
129
- end
130
-
131
- describe '#set_error_handler(:backtrace)' do
132
-
133
- it 'sets a special handler that provides a merged Ruby then Lua backtrace'
134
- # does it make any sense?
135
- end
136
-
137
- describe '#set_error_handler(&ruby_block)' do
138
-
139
- it 'sets a Ruby callback as handler' do
140
-
141
- le = nil
142
-
143
- @s.set_error_handler do |msg|
144
- ([ msg.split.last ] * 3).join(' ')
145
- end
146
-
147
- begin
148
- @s.eval('error("tora")')
149
- rescue Rufus::Lua::LuaError => le
150
- end
151
-
152
- expect(le.msg).to eq('tora tora tora')
153
- end
154
- end
155
-
156
- describe '#set_error_handler(nil)' do
157
-
158
- it 'unsets the current error handler' do
159
-
160
- le = nil
161
-
162
- # set
163
-
164
- @s.set_error_handler(%{
165
- function (e)
166
- return 'something went wrong: ' .. string.gmatch(e, ": (.+)$")()
167
- end
168
- })
169
-
170
- begin
171
- @s.eval('error("a")')
172
- rescue Rufus::Lua::LuaError => le
173
- end
174
-
175
- expect(le.msg).to eq('something went wrong: a')
176
-
177
- # unset
178
-
179
- @s.set_error_handler(nil)
180
-
181
- begin
182
- @s.eval('error("b")')
183
- rescue Rufus::Lua::LuaError => le
184
- end
185
-
186
- expect(le.msg).to eq('[string "line"]:1: b')
187
- expect(@s.send(:stack_top)).to eq(0)
188
- end
189
- end
190
-
191
- describe '#set_error_handler(x)' do
192
-
193
- it 'pops the previous handler from the stack' do
194
-
195
- le = nil
196
-
197
- expect(@s.send(:stack_top)).to eq(0)
198
-
199
- @s.set_error_handler(%{ function (e) return "a" end })
200
- #@s.set_error_handler(:traceback)
201
- #@s.set_error_handler do |msg| return msg * 2; end
202
-
203
- #@s.send(:print_stack)
204
-
205
- expect(@s.send(:stack_top)).to eq(1)
206
-
207
- @s.set_error_handler(%{ function (e) return "b" end })
208
-
209
- #@s.send(:print_stack)
210
-
211
- expect(@s.send(:stack_top)).to eq(1)
212
-
213
- begin
214
- @s.eval('error("Z")')
215
- rescue Rufus::Lua::LuaError => le
216
- end
217
-
218
- expect(le.msg).to eq('b')
219
- end
220
- end
221
- end
222
-
@@ -1,165 +0,0 @@
1
-
2
- #
3
- # Specifying rufus-lua
4
- #
5
- # Wed Mar 11 17:09:17 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 '#[]' do
21
-
22
- it 'returns nil for an unknown value/binding' do
23
-
24
- expect(@s['unknown']).to eq nil
25
- end
26
- end
27
-
28
- describe '#eval' do
29
-
30
- it 'evals true' do
31
-
32
- @s.eval('a = true')
33
- expect(@s['a']).to eq true
34
- end
35
-
36
- it 'evals false' do
37
-
38
- @s.eval('a = false')
39
- expect(@s['a']).to eq false
40
- end
41
-
42
- it 'evals strings' do
43
-
44
- @s.eval('a = "black adder"')
45
- expect(@s['a']).to eq 'black adder'
46
- end
47
-
48
- it 'evals additions' do
49
-
50
- @s.eval('a = 1 + 1')
51
- expect(@s['a']).to eq 2.0
52
- end
53
-
54
- it 'evals nested lookups' do
55
-
56
- @s.eval('a = { b = { c = 0 } }')
57
- @s.eval('_ = a.b.c')
58
- expect(@s['_']).to eq 0
59
- end
60
-
61
- #it 'returns the global environment' do
62
- # @s['_G']).to eq {}
63
- #end
64
-
65
- it 'returns numbers' do
66
-
67
- expect(@s.eval('return 7')).to eq 7.0
68
- end
69
-
70
- it 'returns multiple values' do
71
-
72
- expect(@s.eval('return 1, 2')).to eq [ 1.0, 2.0 ]
73
- end
74
-
75
- it 'returns multiple values (tables included)' do
76
-
77
- r = @s.eval('return 1, 2, {}')
78
-
79
- expect(r.class).to eq Array
80
- expect(r[0, 2]).to eq [ 1.0, 2.0 ]
81
- expect(r[2].class).to eq Rufus::Lua::Table
82
- expect(r[2].size).to eq 0
83
- end
84
-
85
- it 'returns multiple values (tables included) (take 2)' do
86
-
87
- r = @s.eval('return { "hello", "world" }, 2, { 3 }')
88
-
89
- expect(r[0].class).to eq Rufus::Lua::Table
90
- expect(r[0][1]).to eq 'hello'
91
- expect(r[0][2]).to eq 'world'
92
- expect(r[1]).to eq 2.0
93
- expect(r[2].class).to eq Rufus::Lua::Table
94
- expect(r[2][1]).to eq 3.0
95
- end
96
-
97
- it 'returns false' do
98
-
99
- expect(@s.eval('return false')).to eq false
100
- end
101
-
102
- it 'returns true' do
103
-
104
- expect(@s.eval('return true')).to eq true
105
- end
106
-
107
- it 'returns tables' do
108
-
109
- r = @s.eval('return { "hello", "world", 2 }')
110
-
111
- expect(r.class).to eq Rufus::Lua::Table
112
- expect(r[0]).to eq nil
113
- expect(r[1]).to eq 'hello'
114
- expect(r[2]).to eq 'world'
115
- expect(r[3]).to eq 2.0
116
- end
117
-
118
- it 'accepts a binding optional argument'
119
-
120
- it 'accepts a filename and a lineno optional arguments' do
121
-
122
- le = nil
123
- begin
124
- @s.eval('error(77)', nil, '/nada/virtual.lua', 63)
125
- rescue Rufus::Lua::LuaError => le
126
- end
127
-
128
- expect(le.kind).to eq('eval:pcall')
129
- expect(le.msg).to eq('[string "/nada/virtual.lua:63"]:1: 77')
130
- expect(le.errcode).to eq(2)
131
-
132
- expect(le.filename).to eq('/nada/virtual.lua')
133
- expect(le.lineno).to eq(63)
134
-
135
- expect(le.original_backtrace.first).to match(/\/lua\/state\.rb:/)
136
- expect(le.backtrace.first).to eq('/nada/virtual.lua:63:')
137
- end
138
-
139
- context 'and errors' do
140
-
141
- it 'makes the file and line available' do
142
-
143
- le = nil
144
- begin
145
- @s.eval('error(77)')
146
- rescue Rufus::Lua::LuaError => le
147
- end
148
-
149
- expect(le.kind).to eq('eval:pcall')
150
- expect(le.msg).to eq('[string "line"]:1: 77')
151
- expect(le.errcode).to eq(2)
152
-
153
- expect(le.message).to eq(
154
- "eval:pcall : '[string \"line\"]:1: 77' (2 LUA_ERRRUN)")
155
-
156
- expect(le.filename).to eq(__FILE__)
157
- expect(le.lineno).to eq(__LINE__ - 12)
158
-
159
- expect(le.original_backtrace.first).to match(/\/lua\/state\.rb:/)
160
- expect(le.backtrace.first).to match(/\/eval_spec\.rb:/)
161
- end
162
- end
163
- end
164
- end
165
-
@@ -1,119 +0,0 @@
1
-
2
- #
3
- # Specifying rufus-lua
4
- #
5
- # Wed Mar 11 17:09:17 JST 2009
6
- #
7
-
8
- require 'spec_base'
9
-
10
-
11
- describe Rufus::Lua::State do
12
-
13
- describe 'Lua functions' do
14
-
15
- before do
16
- @s = Rufus::Lua::State.new
17
- end
18
- after do
19
- @s.close
20
- end
21
-
22
- it 'are returned as Rufus::Lua::Function instances' do
23
-
24
- expect(@s.eval('return function () end').class).to eq Rufus::Lua::Function
25
- end
26
-
27
- it 'are callable from Ruby' do
28
-
29
- f = @s.eval(%{
30
- f = function ()
31
- return 77
32
- end
33
- return f
34
- })
35
-
36
- expect(f.call()).to eq 77.0
37
- end
38
-
39
- it 'are callable even when they return multiple values' do
40
-
41
- f = @s.eval(%{
42
- f = function ()
43
- return 77, 44
44
- end
45
- return f
46
- })
47
-
48
- expect(f.call()).to eq [ 77.0, 44.0 ]
49
- end
50
-
51
- it 'are callable with arguments' do
52
-
53
- f = @s.eval(%{
54
- f = function (x)
55
- return x * x
56
- end
57
- return f
58
- })
59
-
60
- expect(f.call(2)).to eq 4.0
61
- end
62
-
63
- it 'are callable with boolean arguments' do
64
-
65
- f = @s.eval(%{
66
- f = function (x)
67
- return x
68
- end
69
- return f
70
- })
71
-
72
- expect(f.call(true)).to eq true
73
- expect(f.call(false)).to eq false
74
- end
75
-
76
- it 'are callable with array arguments' do
77
-
78
- f = @s.eval(%{
79
- f = function (x)
80
- return x
81
- end
82
- return f
83
- })
84
-
85
- expect(f.call(%w[ one two three ]).to_a).to eq %w[ one two three ]
86
- end
87
-
88
- it 'are callable with multiple arguments' do
89
-
90
- f = @s.eval(%{
91
- f = function (x, y)
92
- return x + y
93
- end
94
- return f
95
- })
96
-
97
- expect(f.call(1, 2)).to eq 3.0
98
- end
99
-
100
- it 'are called with #to_lua\'ed Ruby arguments' do
101
-
102
- f = @s.eval(%{
103
- f = function (x)
104
- return x
105
- end
106
- return f
107
- })
108
-
109
- t = Time.now
110
-
111
- def t.to_lua
112
- "lua:#{to_s}"
113
- end
114
-
115
- expect(f.call(t)).to eq t.to_lua
116
- end
117
- end
118
- end
119
-