Class: LuaInRuby_Test

Inherits:
Test::Unit::TestCase
  • Object
show all
Defined in:
tests/lua_in_ruby_test.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) assert_luastack_clean(lstate)

asserts that the state’s stack is empty incorrectly implemented Lua API blocks will trigger this



502
503
504
# File 'tests/lua_in_ruby_test.rb', line 502

def assert_luastack_clean( lstate )
    assert_equal 0, lstate.__top
end

- (Object) assert_pairs_match(key, val, expected_key, expected_val)

assert that a given key/value pair match an expected key/value pair



507
508
509
# File 'tests/lua_in_ruby_test.rb', line 507

def assert_pairs_match( key, val, expected_key, expected_val )
    assert_equal(key, expected_key) if val == expected_val
end

- (Object) test_basic_types

test the basic types



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
# File 'tests/lua_in_ruby_test.rb', line 40

def test_basic_types
     l = Lua::State.new

     # eval, and basic marshaling
     assert_nil l.eval('return') 
     assert_nil l.eval('return nil') 
     assert_equal 1,     l.eval('return 1') 
     assert_equal 1.1,   l.eval('return 1.1') 
     assert_equal 'abc', l.eval('return "abc"') 
     assert_equal true,  l.eval('return true') 
     assert_equal false, l.eval('return false')
     
     # multi-ret
     a = l.eval_mult( "return" )
     assert_instance_of Array, a
     assert_equal [], a
     assert_equal 0, a.length
     a = l.eval_mult( "return 1" )
     assert_instance_of Array, a
     assert_equal [1], a
     assert_equal 1, a.length
     a = l.eval_mult( "return 1, 2, 3, 4" )
     assert_instance_of Array, a
     assert_equal [1,2,3,4], a
     assert_equal 4, a.length

     # type id
     l.eval "            f = function() end\n            t = {}\n            u = io.output() -- userdata\n"
     assert_equal Lua::TFUNCTION, l['f'].__type
     assert_equal Lua::TTABLE,    l.t.__type
     assert_equal Lua::TUSERDATA, l.u.__type
     
    # access queries
     assert_equal true,   l.indexable?
     assert_equal true,   l.new_indexable?
     assert_equal false,  l.callable?
     assert_equal true,   l.t.indexable?
     assert_equal true,   l.t.new_indexable?
     assert_equal false,  l.t.callable?
     assert_equal false,  l['f'].indexable?
     assert_equal false,  l['f'].new_indexable?
     assert_equal true,   l['f'].callable?
     assert_equal true,   l['u'].indexable?
     assert_equal false,  l['u'].new_indexable?
     assert_equal false,  l['u'].callable?

     assert_luastack_clean l
end

- (Object) test_classes

test classes



446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'tests/lua_in_ruby_test.rb', line 446

def test_classes
    l = Lua::State.new

    l.eval "            Account = {\n                balance = 0,\n                deposit = function( self, v )\n                print \"testing testing testing\"\n                    self.balance = self.balance + v\n                end,\n            }\n"
    assert_equal Lua::Table, l..class
    assert_equal 0, l..balance
    assert_nothing_thrown { l..deposit! 100 }
    assert_equal 100, l..balance
    # DO WE WANT TO DO ANYTHING LIKE THIS?
    #assert_nothing_thrown { l.Account.!deposit 50 }
    #assert_equal 150, l.Account.balance
    #assert_nothing_thrown { l.Account.!deposit! 25 }
    #assert_equal 175, l.Account.balance

    assert_luastack_clean l
end

- (Object) test_construction

test construction of various entities



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'tests/lua_in_ruby_test.rb', line 20

def test_construction
    l = Lua::State.new

    assert_instance_of Lua::State, l
    assert_instance_of Lua::Table, l.__globals
    assert_instance_of Lua::Table, l.__registry

    assert_equal l, l.__state
    assert_equal l, l.__globals.__state
    assert_equal l, l.__registry.__state

    assert_luastack_clean l
    
    assert_raise TypeError do
        l_bad = Lua::State.new "aa"    
    end
end

- (Object) test_exceptions

test exceptions



474
475
476
477
478
479
480
481
482
483
484
485
486
# File 'tests/lua_in_ruby_test.rb', line 474

def test_exceptions
    l = Lua::State.new

    assert_raise SyntaxError do
        l.eval( 'if !exclamation_doesnt_work then return 0 end' )
    end
   
    assert_raise RuntimeError do
        l.eval( 'error("42")' )
    end

    assert_luastack_clean l
end

- (Object) test_garbage_collection

test garbage collection makes sure various C-based objects are properly cleaned



490
491
492
493
494
495
496
497
# File 'tests/lua_in_ruby_test.rb', line 490

def test_garbage_collection
    50.times do
        l = Lua::State.new
        g = l.__globals
        l = g = nil
        GC.start
    end
end

- (Object) test_libraries

test libraries



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
# File 'tests/lua_in_ruby_test.rb', line 111

def test_libraries
       # invoking libraries
    l = Lua::State.new
    assert_nothing_thrown do
        STDOUT << "you should see-> Hello from Lua!-> "
        l.eval( 'print "Hello from Lua!"' )
        STDOUT << "you should see-> Hello again from Lua!-> "
        l.print "Hello again from Lua!"
    end
    assert_luastack_clean l                

    # loading libraries
    #####################
    
    l = Lua::State.new   # all
    count = 0 ; l.__globals.each_key { |k| count += 1 }
    assert_equal 39, count 
    assert_instance_of Lua::RefObject, l['ipairs']  # base library check
    assert_instance_of Lua::Table, l.package
    assert_instance_of Lua::Table, l.table
    assert_instance_of Lua::Table, l.io
    assert_instance_of Lua::Table, l.os
    assert_instance_of Lua::Table, l.string
    assert_instance_of Lua::Table, l.math
    assert_instance_of Lua::Table, l.debug

    l = Lua::State.new( :loadlibs => :all )
    count = 0 ; l.__globals.each_key { |k| count += 1 }
    assert_equal 39, count 
    assert_instance_of Lua::RefObject, l['ipairs']  # base library check
    assert_instance_of Lua::Table, l.package
    assert_instance_of Lua::Table, l.table
    assert_instance_of Lua::Table, l.io
    assert_instance_of Lua::Table, l.os
    assert_instance_of Lua::Table, l.string
    assert_instance_of Lua::Table, l.math
    assert_instance_of Lua::Table, l.debug

    l = Lua::State.new( :loadlibs => :none )
    count = 0 ; l.__globals.each_key { |k| count += 1 }
    assert_equal 0, count 
    
    l = Lua::State.new( :loadlibs => :base )
    assert_instance_of Lua::RefObject, l['ipairs']  # base library check
    assert_equal nil, l.string
    l = Lua::State.new( :loadlibs => :package )
    assert_instance_of Lua::Table, l.package
    assert_equal nil, l.string
    l = Lua::State.new( :loadlibs => :table )
    assert_instance_of Lua::Table, l.table
    assert_equal nil, l.string
    l = Lua::State.new( :loadlibs => :io )
    assert_instance_of Lua::Table, l.io
    assert_equal nil, l.string
    l = Lua::State.new( :loadlibs => :os )
    assert_instance_of Lua::Table, l.os
    assert_equal nil, l.string
    l = Lua::State.new( :loadlibs => :string )
    assert_instance_of Lua::Table, l.string
    assert_equal nil, l.io
    l = Lua::State.new( :loadlibs => :math )
    assert_instance_of Lua::Table, l.math
    assert_equal nil, l.string
    l = Lua::State.new( :loadlibs => :debug )
    assert_instance_of Lua::Table, l.debug
     assert_equal nil, l.string
    
    l = Lua::State.new( :loadlibs => [:base, :package, :io] )
    assert_instance_of Lua::RefObject, l['ipairs']  # base library check
    assert_instance_of Lua::Table, l.package
    assert_instance_of Lua::Table, l.io
end

- (Object) test_method_dispatch

test method dispatch



392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'tests/lua_in_ruby_test.rb', line 392

def test_method_dispatch
    l = Lua::State.new        
    l.eval "            str = \"a\"\n            function uniret() return 1 end\n            function uniret2(a) return a end\n            function multiret() return 1, 2, 3 end\n            function multiret2(a,b) return 1, a, b end\n            \n            t = {}\n            t.str = \"a\"\n            t.uniret = uniret\n            t.uniret2 = uniret2\n            t.multiret = multiret\n            t.multiret2 = multiret2\n"

    assert_equal 'a', l.str
    assert_equal 'a', l.t.str
    assert_equal 'a', l.str()   # ugly, but Ruby 
    assert_equal 'a', l.t.str() # ugly, but Ruby
    assert_kind_of String, l.str
    assert_kind_of String, l.t.str
    assert_raise RuntimeError do l.str_   end
    assert_raise RuntimeError do l.str!   end
    assert_raise RuntimeError do l.str(2) end
    
    assert_kind_of Lua::RefObject, l['uniret']
    assert_kind_of Float, l.uniret
    assert_kind_of Float, l.uniret_
    assert_equal 1, l.uniret()
    assert_equal 1, l.uniret_
    assert_equal 1, l.t.uniret
    assert_equal 1, l.t.uniret()
    assert_equal 2, l.uniret2(2)
    assert_equal 2, l.t.uniret2(2)
    r = l.uniret2 2 ; assert_equal 2, r
    r = l.t.uniret2 2 ; assert_equal 2, r
                             
    assert_equal [1,2,3], l.multiret
    assert_equal [1,2,3], l.multiret()
    assert_equal [1,2,3], l.t.multiret
    assert_equal [1,2,3], l.t.multiret()
    assert_equal [1,5,6], l.multiret2( 5, 6 )
    assert_equal [1,5,6], l.t.multiret2( 5, 6 )
    r = l.multiret2 5, 6 ; assert_equal [1,5,6], r
    r = l.t.multiret2 5, 6 ; assert_equal [1,5,6], r

    assert_luastack_clean l
end

- (Object) test_module

test module-level entities



11
12
13
14
15
16
# File 'tests/lua_in_ruby_test.rb', line 11

def test_module
    assert_not_nil( Lua::BRIDGE_VERSION )
    assert_not_nil( Lua::BRIDGE_VERSION_NUM )
    assert_not_nil( Lua::LUA_VERSION )
    assert_not_nil( Lua::LUA_RELEASE )        
end

- (Object) test_setters

test setters



96
97
98
99
100
101
102
103
104
105
106
107
# File 'tests/lua_in_ruby_test.rb', line 96

def test_setters
    l = Lua::State.new
    
    l.v = 5   ; assert_equal 5, l.v
    l.a = {}  ; assert_instance_of Lua::Table, l.a
    l.a.v = 7 ; assert_equal 7, l.a.v

    l['b']   = 6 ; assert_equal 6, l.b               
    l.a['b'] = 7 ; assert_equal 7, l.a.b               
    
   assert_luastack_clean l                
end

- (Object) test_table_access

test table accesses



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
# File 'tests/lua_in_ruby_test.rb', line 236

def test_table_access
    l = Lua::State.new
    l.eval "            a = { 1, 2, 3, 4 }\n            h = { a='x', b='y',\n                  [true]  = 'a',\n                  [false] = 'b',\n                  [1.3]   = 'z', }\n            h2 = { a='x', b='y', }\n"

    assert_instance_of Lua::Table, l.a
    assert_instance_of Lua::Table, l.h
    assert_instance_of Lua::Table, l.h2
    assert_instance_of Lua::Table, l['a']
    assert_instance_of Lua::Table, l['h']
    assert_instance_of Lua::Table, l['h2']
             
    assert_nil      l.a[0]
    assert_equal 1, l.a[1]
    assert_equal 2, l.a[2]
    assert_equal 3, l.a[3]
    assert_equal 4, l.a[4]
    assert_equal 4, l.a.__length

    assert_equal 'x', l.h.a
    assert_equal 'y', l.h.b

    assert_nil        l.h[0]
    assert_equal 'x', l.h['a']
    assert_equal 'y', l.h['b']

    assert_equal 'z', l.h[1.3]
    assert_equal 'a', l.h[true]
    assert_equal 'b', l.h[false]
    assert_equal 0,   l.h.__length  # length only applies to Array

    temp = (l.a[10] = 'x')
    assert_equal 'x', temp
    assert_equal 'x', l.a[10]

    assert_instance_of Array,     l.a.to_array
    assert_instance_of Hash,      l.h2.to_hash
    assert_equal( [1,2,3,4],      l.a.to_array)
    assert_equal( {'a' => 'x','b' => 'y'}, l.h2.to_hash)

    assert_luastack_clean l
end

- (Object) test_table_creation_from_ruby

test table creation from ruby



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
# File 'tests/lua_in_ruby_test.rb', line 186

def test_table_creation_from_ruby
    l = Lua::State.new

    l.eval( "a = {}")
    assert_instance_of Lua::Table, l.a

    s = "return {1,2,3}"
    assert_instance_of Lua::Table, l.eval(s)
    assert_equal 3, l.eval(s).__length
    assert_equal [1,2,3], l.eval(s).to_array
    
    b = l.new_table_at 'b'
    assert_instance_of Lua::Table, b
    assert_instance_of Lua::Table, l.b

    b = l.a.new_table_at 'b'
    assert_instance_of Lua::Table, b
    assert_instance_of Lua::Table, l.a.b
    
    l.c = []
    assert_instance_of Lua::Table, l.c
    assert_equal 0, l.c.__length
    
    l.d = {}
    assert_instance_of Lua::Table, l.d
    assert_equal 0, l.d.__length
            
    e = [1,2,3,4]
    l.e = e
    assert_equal 1, l.e[1]
    assert_equal 2, l.e[2]
    assert_equal 3, l.e[3]
    assert_equal 4, l.e[4]
    assert_equal e, l.e.to_array
    assert_instance_of Lua::Table, l.e

    f = { 1=>1, 2=>2, 'a'=>3, 'b'=>4 }
    l.f = f
    assert_equal 1, l.f[1]
    assert_equal 2, l.f[2]
    assert_equal 3, l.f['a']
    assert_equal 4, l.f['b']
    assert_equal 2, l.f.__length
    assert_instance_of Lua::Table, l.f
    
    assert_luastack_clean l
end

- (Object) test_table_iteration

test table iteration



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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'tests/lua_in_ruby_test.rb', line 288

def test_table_iteration
    l = Lua::State.new

    # array integer iteration
    l.eval( "array = { 100, 200, 300, 400 }" )
    assert_instance_of Lua::Table, l.array                
    n = 0 ; l.array.each_ipair { |k,v| n += 1
        assert_pairs_match( k, v, 1, 100 )
        assert_pairs_match( k, v, 2, 200 )
        assert_pairs_match( k, v, 3, 300 )
        assert_pairs_match( k, v, 4, 400 )
    }
    assert_equal(4, n)         

    n = 0 ; l.array.each_ikey { |k| n += 1
        assert_pairs_match( n, k, 1, 1 )
        assert_pairs_match( n, k, 2, 2 )
        assert_pairs_match( n, k, 3, 3 )
        assert_pairs_match( n, k, 4, 4 )
        assert( n <= 4 )
    }
    assert_equal(4, n)

    sumv = n = 0 ; l.array.each_ivalue { |v| n += 1
        sumv += v
        assert_equal(v, n*100)
    }
    assert_equal(4, n) ; assert_equal(1000, sumv)         

    # hash integer iteration
    l.eval( "hsh = { [1]=100, [2]=200, a=300, b=400, }" )
    assert_instance_of Lua::Table, l.hsh
    n = 0 ; l.hsh.each_ipair { |k,v| n += 1
        assert_pairs_match( k, v, 1, 100 )
        assert_pairs_match( k, v, 2, 200 )
        assert_pairs_match( k, v, 'a', 300 )
        assert_pairs_match( k, v, 'b', 400 )
    }
    assert_equal(2, n)         

    sumk = n = 0 ; l.hsh.each_ikey { |k| n += 1
        sumk += k
    }
    assert_equal(2, n) ; assert_equal(3, sumk)         

    sumv = n = 0 ; l.hsh.each_ivalue { |v| n += 1
        sumv += v
        assert_equal(v, n*100)
    }
    assert_equal(2, n) ; assert_equal(300, sumv)         
            
    # array assoc iteration
    l.eval( "array = { 100, 200, 300, 400 }" )
    assert_instance_of Lua::Table, l.array                
    n = 0 ; l.array.each_pair { |k,v| n += 1
        assert_pairs_match( k, v, 1, 100 )
        assert_pairs_match( k, v, 2, 200 )
        assert_pairs_match( k, v, 3, 300 )
        assert_pairs_match( k, v, 4, 400 )
    }
    assert_equal(4, n)         

    sumk = n = 0 ; l.array.each_key { |k| n += 1
        sumk += k
        assert_equal(k, n)
    }
    assert_equal(4, n) ; assert_equal(10, sumk)         

    sumv = n = 0 ; l.array.each_ivalue { |v| n += 1
        sumv += v
        assert_equal(v, n*100)
    }
    assert_equal(4, n) ; assert_equal(1000, sumv)         

    # hash assoc iteration
    l.eval( "hsh = { [1]=100, [2]=200, a=300, b=400, }" )
    assert_instance_of Lua::Table, l.array                
    n = 0 ; l.hsh.each_pair { |k,v| n += 1
        assert_pairs_match( k, v, 1, 100 )
        assert_pairs_match( k, v, 2, 200 )
        assert_pairs_match( k, v, 'a', 300 )
        assert_pairs_match( k, v, 'b', 400 )
    }
    assert_equal( n, 4)

    n = 0 ; l.hsh.each_key { |k| n += 1 
        assert_pairs_match( n, k, 1, 100 )
        assert_pairs_match( n, k, 2, 200 )
        assert_pairs_match( n, k, 3, 'a' )
        assert_pairs_match( n, k, 4, 'b' )
    }
    assert_equal( n, 4)
    
    n = sumv = 0 ; l.hsh.each_value { |v| n += 1
        sumv += v
    }
    assert_equal(4, n) ; assert_equal( sumv, 1000)

    # done iteration tests
    assert_luastack_clean l
end