/branches/swig-2.0/Examples/lua/import/runme.lua

# · Lua · 103 lines · 76 code · 22 blank · 5 comment · 6 complexity · c1c25f9404ed034107bc9905fe384d84 MD5 · raw file

  1. # Test various properties of classes defined in separate modules
  2. print("Testing the %import directive")
  3. if string.sub(_VERSION,1,7)=='Lua 5.0' then
  4. -- lua5.0 doesnt have a nice way to do this
  5. function loadit(a)
  6. lib=loadlib(a..'.dll','luaopen_'..a) or loadlib(a..'.so','luaopen_'..a)
  7. assert(lib)()
  8. end
  9. loadit('base')
  10. loadit('foo')
  11. loadit('bar')
  12. loadit('spam')
  13. else
  14. -- lua 5.1 does
  15. require 'base'
  16. require 'foo'
  17. require 'bar'
  18. require 'spam'
  19. end
  20. -- Create some objects
  21. print("Creating some objects")
  22. a = base.Base()
  23. b = foo.Foo()
  24. c = bar.Bar()
  25. d = spam.Spam()
  26. -- Try calling some methods
  27. print("Testing some methods")
  28. print("Should see 'Base::A' ---> ",a:A())
  29. print("Should see 'Base::B' ---> ",a:B())
  30. print("Should see 'Foo::A' ---> ",b:A())
  31. print("Should see 'Foo::B' ---> ",b:B())
  32. print("Should see 'Bar::A' ---> ",c:A())
  33. print("Should see 'Bar::B' ---> ",c:B())
  34. print("Should see 'Spam::A' ---> ",d:A())
  35. print("Should see 'Spam::B' ---> ",d:B())
  36. -- Try some casts
  37. print("\nTesting some casts")
  38. x = a:toBase()
  39. print("Should see 'Base::A' ---> ",x:A())
  40. print("Should see 'Base::B' ---> ",x:B())
  41. x = b:toBase()
  42. print("Should see 'Foo::A' ---> ",x:A())
  43. print("Should see 'Base::B' ---> ",x:B())
  44. x = c:toBase()
  45. print("Should see 'Bar::A' ---> ",x:A())
  46. print("Should see 'Base::B' ---> ",x:B())
  47. x = d:toBase()
  48. print("Should see 'Spam::A' ---> ",x:A())
  49. print("Should see 'Base::B' ---> ",x:B())
  50. x = d:toBar()
  51. print("Should see 'Bar::B' ---> ",x:B())
  52. print "\nTesting some dynamic casts\n"
  53. x = d:toBase()
  54. print " Spam -> Base -> Foo : "
  55. y = foo.Foo_fromBase(x)
  56. if y then
  57. print "bad swig"
  58. else
  59. print "good swig"
  60. end
  61. print " Spam -> Base -> Bar : "
  62. y = bar.Bar_fromBase(x)
  63. if y then
  64. print "good swig"
  65. else
  66. print "bad swig"
  67. end
  68. print " Spam -> Base -> Spam : "
  69. y = spam.Spam_fromBase(x)
  70. if y then
  71. print "good swig"
  72. else
  73. print "bad swig"
  74. end
  75. print " Foo -> Spam : "
  76. y = spam.Spam_fromBase(b)
  77. if y then
  78. print "bad swig"
  79. else
  80. print "good swig"
  81. end