# class LuaInRuby_Test

### Public Instance Methods

assert_luastack_clean( lstate ) click to toggle source

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

        # File tests/lua_in_ruby_test.rb
    498 def assert_luastack_clean( lstate )
    499     assert_equal 0, lstate.__top
    500 end

assert_pairs_match( key, val, expected_key, expected_val ) click to toggle source

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

        # File tests/lua_in_ruby_test.rb
    503 def assert_pairs_match( key, val, expected_key, expected_val )
    504     assert_equal(key, expected_key) if val == expected_val
    505 end

test_basic_types() click to toggle source

test the basic types

       # File tests/lua_in_ruby_test.rb
    40     def test_basic_types
    41         l = Lua::State.new
    42
    43         # eval, and basic marshaling
    44         assert_nil l.eval('return')
    45         assert_nil l.eval('return nil')
    46         assert_equal 1,     l.eval('return 1')
    47         assert_equal 1.1,   l.eval('return 1.1')
    48         assert_equal 'abc', l.eval('return "abc"')
    49         assert_equal true,  l.eval('return true')
    50         assert_equal false, l.eval('return false')
    51
    52         # multi-ret
    53         a = l.eval_mult( "return" )
    54         assert_instance_of Array, a
    55         assert_equal [], a
    56         assert_equal 0, a.length
    57         a = l.eval_mult( "return 1" )
    58         assert_instance_of Array, a
    59         assert_equal [1], a
    60         assert_equal 1, a.length
    61         a = l.eval_mult( "return 1, 2, 3, 4" )
    62         assert_instance_of Array, a
    63         assert_equal [1,2,3,4], a
    64         assert_equal 4, a.length
    65
    66         # type id
    67         l.eval <<LUA_END
    68             f = function() end
    69             t = {}
    70             u = io.output() -- userdata
    71 LUA_END
    72         assert_equal Lua::TFUNCTION, l['f'].__type
    73         assert_equal Lua::TTABLE,    l.t.__type
    74         assert_equal Lua::TUSERDATA, l.u.__type
    75
    76        # access queries
    77         assert_equal true,   l.indexable?
    78         assert_equal true,   l.new_indexable?
    79         assert_equal false,  l.callable?
    80         assert_equal true,   l.t.indexable?
    81         assert_equal true,   l.t.new_indexable?
    82         assert_equal false,  l.t.callable?
    83         assert_equal false,  l['f'].indexable?
    84         assert_equal false,  l['f'].new_indexable?
    85         assert_equal true,   l['f'].callable?
    86         assert_equal true,   l['u'].indexable?
    87         assert_equal false,  l['u'].new_indexable?
    88         assert_equal false,  l['u'].callable?
    89
    90         assert_luastack_clean l
    91    end

test_classes() click to toggle source

test classes

        # File tests/lua_in_ruby_test.rb
    443     def test_classes
    444         l = Lua::State.new
    445
    446         l.eval <<END_LUA
    447             Account = {
    448                 balance = 0,
    449                 deposit = function( self, v )
    450                 print "testing testing testing"
    451                     self.balance = self.balance + v
    452                 end,
    453             }
    454 END_LUA
    455         assert_equal Lua::Table, l.Account.class
    456         assert_equal 0, l.Account.balance
    457         assert_nothing_thrown { l.Account.deposit! 100 }
    458         assert_equal 100, l.Account.balance
    459         # DO WE WANT TO DO ANYTHING LIKE THIS?
    460         #assert_nothing_thrown { l.Account.!deposit 50 }
    461         #assert_equal 150, l.Account.balance
    462         #assert_nothing_thrown { l.Account.!deposit! 25 }
    463         #assert_equal 175, l.Account.balance
    464
    465         assert_luastack_clean l
    466     end

test_construction() click to toggle source

test construction of various entities

       # File tests/lua_in_ruby_test.rb
    20 def test_construction
    21     l = Lua::State.new
    22
    23     assert_instance_of Lua::State, l
    24     assert_instance_of Lua::Table, l.__globals
    25     assert_instance_of Lua::Table, l.__registry
    26
    27     assert_equal l, l.__state
    28     assert_equal l, l.__globals.__state
    29     assert_equal l, l.__registry.__state
    30
    31     assert_luastack_clean l
    32
    33     assert_raise TypeError do
    34         l_bad = Lua::State.new "aa"
    35     end
    36 end

test_exceptions() click to toggle source

test exceptions

        # File tests/lua_in_ruby_test.rb
    470 def test_exceptions
    471     l = Lua::State.new
    472
    473     assert_raise SyntaxError do
    474         l.eval( 'if !exclamation_doesnt_work then return 0 end' )
    475     end
    476
    477     assert_raise RuntimeError do
    478         l.eval( 'error("42")' )
    479     end
    480
    481     assert_luastack_clean l
    482 end

test_garbage_collection() click to toggle source

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

        # File tests/lua_in_ruby_test.rb
    486 def test_garbage_collection
    487     50.times do
    488         l = Lua::State.new
    489         g = l.__globals
    490         l = g = nil
    491         GC.start
    492     end
    493 end

test_libraries() click to toggle source

test libraries

        # File tests/lua_in_ruby_test.rb
    110 def test_libraries
    111         # invoking libraries
    112      l = Lua::State.new
    113      assert_nothing_thrown do
    114          STDOUT << "you should see-> Hello from Lua!-> "
    115          l.eval( 'print "Hello from Lua!"' )
    116          STDOUT << "you should see-> Hello again from Lua!-> "
    117          l.print "Hello again from Lua!"
    118      end
    119      assert_luastack_clean l
    120
    121      # loading libraries
    122      #####################
    123
    124      l = Lua::State.new   # all
    125      count = 0 ; l.__globals.each_key { |k| count += 1 }
    126      assert_equal 39, count
    127      assert_instance_of Lua::RefObject, l['ipairs']  # base library check
    128      assert_instance_of Lua::Table, l.package
    129      assert_instance_of Lua::Table, l.table
    130      assert_instance_of Lua::Table, l.io
    131      assert_instance_of Lua::Table, l.os
    132      assert_instance_of Lua::Table, l.string
    133      assert_instance_of Lua::Table, l.math
    134      assert_instance_of Lua::Table, l.debug
    135
    136      l = Lua::State.new( :loadlibs => :all )
    137      count = 0 ; l.__globals.each_key { |k| count += 1 }
    138      assert_equal 39, count
    139      assert_instance_of Lua::RefObject, l['ipairs']  # base library check
    140      assert_instance_of Lua::Table, l.package
    141      assert_instance_of Lua::Table, l.table
    142      assert_instance_of Lua::Table, l.io
    143      assert_instance_of Lua::Table, l.os
    144      assert_instance_of Lua::Table, l.string
    145      assert_instance_of Lua::Table, l.math
    146      assert_instance_of Lua::Table, l.debug
    147
    148      l = Lua::State.new( :loadlibs => :none )
    149      count = 0 ; l.__globals.each_key { |k| count += 1 }
    150      assert_equal 0, count
    151
    152      l = Lua::State.new( :loadlibs => :base )
    153      assert_instance_of Lua::RefObject, l['ipairs']  # base library check
    154      assert_equal nil, l.string
    155      l = Lua::State.new( :loadlibs => :package )
    156      assert_instance_of Lua::Table, l.package
    157      assert_equal nil, l.string
    158      l = Lua::State.new( :loadlibs => :table )
    159      assert_instance_of Lua::Table, l.table
    160      assert_equal nil, l.string
    161      l = Lua::State.new( :loadlibs => :io )
    162      assert_instance_of Lua::Table, l.io
    163      assert_equal nil, l.string
    164      l = Lua::State.new( :loadlibs => :os )
    165      assert_instance_of Lua::Table, l.os
    166      assert_equal nil, l.string
    167      l = Lua::State.new( :loadlibs => :string )
    168      assert_instance_of Lua::Table, l.string
    169      assert_equal nil, l.io
    170      l = Lua::State.new( :loadlibs => :math )
    171      assert_instance_of Lua::Table, l.math
    172      assert_equal nil, l.string
    173      l = Lua::State.new( :loadlibs => :debug )
    174      assert_instance_of Lua::Table, l.debug
    175       assert_equal nil, l.string
    176
    177      l = Lua::State.new( :loadlibs => [:base, :package, :io] )
    178      assert_instance_of Lua::RefObject, l['ipairs']  # base library check
    179      assert_instance_of Lua::Table, l.package
    180      assert_instance_of Lua::Table, l.io
    181  end

test_method_dispatch() click to toggle source

test method dispatch

        # File tests/lua_in_ruby_test.rb
    390     def test_method_dispatch
    391         l = Lua::State.new
    392         l.eval <<END_OF_LUA
    393             str = "a"
    394             function uniret() return 1 end
    395             function uniret2(a) return a end
    396             function multiret() return 1, 2, 3 end
    397             function multiret2(a,b) return 1, a, b end
    398
    399             t = {}
    400             t.str = "a"
    401             t.uniret = uniret
    402             t.uniret2 = uniret2
    403             t.multiret = multiret
    404             t.multiret2 = multiret2
    405 END_OF_LUA
    406
    407         assert_equal 'a', l.str
    408         assert_equal 'a', l.t.str
    409         assert_equal 'a', l.str()   # ugly, but Ruby
    410         assert_equal 'a', l.t.str() # ugly, but Ruby
    411         assert_kind_of String, l.str
    412         assert_kind_of String, l.t.str
    413         assert_raise RuntimeError do l.str_   end
    414         assert_raise RuntimeError do l.str!   end
    415         assert_raise RuntimeError do l.str(2) end
    416
    417         assert_kind_of Lua::RefObject, l['uniret']
    418         assert_kind_of Float, l.uniret
    419         assert_kind_of Float, l.uniret_
    420         assert_equal 1, l.uniret()
    421         assert_equal 1, l.uniret_
    422         assert_equal 1, l.t.uniret
    423         assert_equal 1, l.t.uniret()
    424         assert_equal 2, l.uniret2(2)
    425         assert_equal 2, l.t.uniret2(2)
    426         r = l.uniret2 2 ; assert_equal 2, r
    427         r = l.t.uniret2 2 ; assert_equal 2, r
    428
    429         assert_equal [1,2,3], l.multiret
    430         assert_equal [1,2,3], l.multiret()
    431         assert_equal [1,2,3], l.t.multiret
    432         assert_equal [1,2,3], l.t.multiret()
    433         assert_equal [1,5,6], l.multiret2( 5, 6 )
    434         assert_equal [1,5,6], l.t.multiret2( 5, 6 )
    435         r = l.multiret2 5, 6 ; assert_equal [1,5,6], r
    436         r = l.t.multiret2 5, 6 ; assert_equal [1,5,6], r
    437
    438         assert_luastack_clean l
    439     end

test_module() click to toggle source

test module-level entities

       # File tests/lua_in_ruby_test.rb
    11 def test_module
    12     assert_not_nil( Lua::BRIDGE_VERSION )
    13     assert_not_nil( Lua::BRIDGE_VERSION_NUM )
    14     assert_not_nil( Lua::LUA_VERSION )
    15     assert_not_nil( Lua::LUA_RELEASE )
    16 end

test_setters() click to toggle source

test setters

        # File tests/lua_in_ruby_test.rb
     95 def test_setters
     96     l = Lua::State.new
     97
     98     l.v = 5   ; assert_equal 5, l.v
     99     l.a = {}  ; assert_instance_of Lua::Table, l.a
    100     l.a.v = 7 ; assert_equal 7, l.a.v
    101
    102     l['b']   = 6 ; assert_equal 6, l.b
    103     l.a['b'] = 7 ; assert_equal 7, l.a.b
    104
    105    assert_luastack_clean l
    106 end

test_table_access() click to toggle source

test table accesses

        # File tests/lua_in_ruby_test.rb
    235     def test_table_access
    236         l = Lua::State.new
    237         l.eval <<LUA_END
    238             a = { 1, 2, 3, 4 }
    239             h = { a='x', b='y',
    240                   [true]  = 'a',
    241                   [false] = 'b',
    242                   [1.3]   = 'z', }
    243             h2 = { a='x', b='y', }
    244 LUA_END
    245
    246         assert_instance_of Lua::Table, l.a
    247         assert_instance_of Lua::Table, l.h
    248         assert_instance_of Lua::Table, l.h2
    249         assert_instance_of Lua::Table, l['a']
    250         assert_instance_of Lua::Table, l['h']
    251         assert_instance_of Lua::Table, l['h2']
    252
    253         assert_nil      l.a[0]
    254         assert_equal 1, l.a[1]
    255         assert_equal 2, l.a[2]
    256         assert_equal 3, l.a[3]
    257         assert_equal 4, l.a[4]
    258         assert_equal 4, l.a.__length
    259
    260         assert_equal 'x', l.h.a
    261         assert_equal 'y', l.h.b
    262
    263         assert_nil        l.h[0]
    264         assert_equal 'x', l.h['a']
    265         assert_equal 'y', l.h['b']
    266
    267         assert_equal 'z', l.h[1.3]
    268         assert_equal 'a', l.h[true]
    269         assert_equal 'b', l.h[false]
    270         assert_equal 0,   l.h.__length  # length only applies to Array
    271
    272         temp = (l.a[10] = 'x')
    273         assert_equal 'x', temp
    274         assert_equal 'x', l.a[10]
    275
    276         assert_instance_of Array,     l.a.to_array
    277         assert_instance_of Hash,      l.h2.to_hash
    278         assert_equal( [1,2,3,4],      l.a.to_array)
    279         assert_equal( {'a' => 'x','b' => 'y'}, l.h2.to_hash)
    280
    281         assert_luastack_clean l
    282     end

test_table_creation_from_ruby() click to toggle source

test table creation from ruby

        # File tests/lua_in_ruby_test.rb
    185 def test_table_creation_from_ruby
    186     l = Lua::State.new
    187
    188     l.eval( "a = {}")
    189     assert_instance_of Lua::Table, l.a
    190
    191     s = "return {1,2,3}"
    192     assert_instance_of Lua::Table, l.eval(s)
    193     assert_equal 3, l.eval(s).__length
    194     assert_equal [1,2,3], l.eval(s).to_array
    195
    196     b = l.new_table_at 'b'
    197     assert_instance_of Lua::Table, b
    198     assert_instance_of Lua::Table, l.b
    199
    200     b = l.a.new_table_at 'b'
    201     assert_instance_of Lua::Table, b
    202     assert_instance_of Lua::Table, l.a.b
    203
    204     l.c = []
    205     assert_instance_of Lua::Table, l.c
    206     assert_equal 0, l.c.__length
    207
    208     l.d = {}
    209     assert_instance_of Lua::Table, l.d
    210     assert_equal 0, l.d.__length
    211
    212     e = [1,2,3,4]
    213     l.e = e
    214     assert_equal 1, l.e[1]
    215     assert_equal 2, l.e[2]
    216     assert_equal 3, l.e[3]
    217     assert_equal 4, l.e[4]
    218     assert_equal e, l.e.to_array
    219     assert_instance_of Lua::Table, l.e
    220
    221     f = { 1=>1, 2=>2, 'a'=>3, 'b'=>4 }
    222     l.f = f
    223     assert_equal 1, l.f[1]
    224     assert_equal 2, l.f[2]
    225     assert_equal 3, l.f['a']
    226     assert_equal 4, l.f['b']
    227     assert_equal 2, l.f.__length
    228     assert_instance_of Lua::Table, l.f
    229
    230     assert_luastack_clean l
    231 end

test_table_iteration() click to toggle source

test table iteration

        # File tests/lua_in_ruby_test.rb
    286 def test_table_iteration
    287     l = Lua::State.new
    288
    289     # array integer iteration
    290     l.eval( "array = { 100, 200, 300, 400 }" )
    291     assert_instance_of Lua::Table, l.array
    292     n = 0 ; l.array.each_ipair { |k,v| n += 1
    293         assert_pairs_match( k, v, 1, 100 )
    294         assert_pairs_match( k, v, 2, 200 )
    295         assert_pairs_match( k, v, 3, 300 )
    296         assert_pairs_match( k, v, 4, 400 )
    297     }
    298     assert_equal(4, n)
    299
    300     n = 0 ; l.array.each_ikey { |k| n += 1
    301         assert_pairs_match( n, k, 1, 1 )
    302         assert_pairs_match( n, k, 2, 2 )
    303         assert_pairs_match( n, k, 3, 3 )
    304         assert_pairs_match( n, k, 4, 4 )
    305         assert( n <= 4 )
    306     }
    307     assert_equal(4, n)
    308
    309     sumv = n = 0 ; l.array.each_ivalue { |v| n += 1
    310         sumv += v
    311         assert_equal(v, n*100)
    312     }
    313     assert_equal(4, n) ; assert_equal(1000, sumv)
    314
    315     # hash integer iteration
    316     l.eval( "hsh = { [1]=100, [2]=200, a=300, b=400, }" )
    317     assert_instance_of Lua::Table, l.hsh
    318     n = 0 ; l.hsh.each_ipair { |k,v| n += 1
    319         assert_pairs_match( k, v, 1, 100 )
    320         assert_pairs_match( k, v, 2, 200 )
    321         assert_pairs_match( k, v, 'a', 300 )
    322         assert_pairs_match( k, v, 'b', 400 )
    323     }
    324     assert_equal(2, n)
    325
    326     sumk = n = 0 ; l.hsh.each_ikey { |k| n += 1
    327         sumk += k
    328     }
    329     assert_equal(2, n) ; assert_equal(3, sumk)
    330
    331     sumv = n = 0 ; l.hsh.each_ivalue { |v| n += 1
    332         sumv += v
    333         assert_equal(v, n*100)
    334     }
    335     assert_equal(2, n) ; assert_equal(300, sumv)
    336
    337     # array assoc iteration
    338     l.eval( "array = { 100, 200, 300, 400 }" )
    339     assert_instance_of Lua::Table, l.array
    340     n = 0 ; l.array.each_pair { |k,v| n += 1
    341         assert_pairs_match( k, v, 1, 100 )
    342         assert_pairs_match( k, v, 2, 200 )
    343         assert_pairs_match( k, v, 3, 300 )
    344         assert_pairs_match( k, v, 4, 400 )
    345     }
    346     assert_equal(4, n)
    347
    348     sumk = n = 0 ; l.array.each_key { |k| n += 1
    349         sumk += k
    350         assert_equal(k, n)
    351     }
    352     assert_equal(4, n) ; assert_equal(10, sumk)
    353
    354     sumv = n = 0 ; l.array.each_ivalue { |v| n += 1
    355         sumv += v
    356         assert_equal(v, n*100)
    357     }
    358     assert_equal(4, n) ; assert_equal(1000, sumv)
    359
    360     # hash assoc iteration
    361     l.eval( "hsh = { [1]=100, [2]=200, a=300, b=400, }" )
    362     assert_instance_of Lua::Table, l.array
    363     n = 0 ; l.hsh.each_pair { |k,v| n += 1
    364         assert_pairs_match( k, v, 1, 100 )
    365         assert_pairs_match( k, v, 2, 200 )
    366         assert_pairs_match( k, v, 'a', 300 )
    367         assert_pairs_match( k, v, 'b', 400 )
    368     }
    369     assert_equal( n, 4)
    370
    371     n = 0 ; l.hsh.each_key { |k| n += 1
    372         assert_pairs_match( n, k, 1, 100 )
    373         assert_pairs_match( n, k, 2, 200 )
    374         assert_pairs_match( n, k, 3, 'a' )
    375         assert_pairs_match( n, k, 4, 'b' )
    376     }
    377     assert_equal( n, 4)
    378
    379     n = sumv = 0 ; l.hsh.each_value { |v| n += 1
    380         sumv += v
    381     }
    382     assert_equal(4, n) ; assert_equal( sumv, 1000)
    383
    384     # done iteration tests
    385     assert_luastack_clean l
    386 end