?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. -- test lua script to be run with the luabridge test program
  2.  
  3. print("Running LuaBridge tests:");
  4.  
  5. -- enum from C++
  6. FN_CTOR = 0
  7. FN_DTOR = 1
  8. FN_STATIC = 2
  9. FN_VIRTUAL = 3
  10. FN_PROPGET = 4
  11. FN_PROPSET = 5
  12. FN_STATIC_PROPGET = 6
  13. FN_STATIC_PROPSET = 7
  14. FN_OPERATOR = 8
  15. NUM_FN_TYPES = 9
  16.  
  17. -- function to print contents of a table
  18. function printtable (t)
  19.   for k, v in pairs(t) do
  20.     if (type(v) == "table") then
  21.       print(k .. " =>", "(table)");
  22.     elseif (type(v) == "function") then
  23.       print(k .. " =>", "(function)");
  24.     elseif (type(v) == "userdata") then
  25.       print(k .. " =>", "(userdata)");
  26.     else
  27.       print(k .. " =>", v);
  28.     end
  29.   end
  30. end
  31.  
  32. function assert (expr)
  33.   if (not expr) then error("assert failed", 2) end
  34. end
  35.  
  36. -- test functions registered from C++
  37.  
  38. assert(testSucceeded());
  39. assert(testRetInt() == 47);
  40. assert(testRetFloat() == 47.0);
  41. assert(testRetConstCharPtr() == "Hello, world");
  42. assert(testRetStdString() == "Hello, world");
  43.  
  44. testParamInt(47);                       assert(testSucceeded());
  45. testParamBool(true);                    assert(testSucceeded());
  46. testParamFloat(47.0);                   assert(testSucceeded());
  47. testParamConstCharPtr("Hello, world");  assert(testSucceeded());
  48. testParamStdString("Hello, world");     assert(testSucceeded());
  49. testParamStdStringRef("Hello, world");  assert(testSucceeded());
  50.  
  51. -- test static methods of classes registered from C++
  52.  
  53. A.testStatic();             assert(testAFnCalled(FN_STATIC));
  54. B.testStatic();             assert(testAFnCalled(FN_STATIC));
  55. B.testStatic2();            assert(testBFnCalled(FN_STATIC));
  56.  
  57. -- test static properties of classes registered from C++
  58.  
  59. assert(A.testStaticProp == 47);
  60. assert(A.testStaticProp2 == 47);assert(testAFnCalled(FN_STATIC_PROPGET));
  61. A.testStaticProp = 48;          assert(A.testStaticProp == 48);
  62. A.testStaticProp2 = 49;         assert(testAFnCalled(FN_STATIC_PROPSET) and A.testStaticProp2 == 49);
  63.  
  64. -- test classes registered from C++
  65.  
  66. object1 = A("object1");          assert(testAFnCalled(FN_CTOR));
  67. object1:testVirtual();           assert(testAFnCalled(FN_VIRTUAL));
  68.  
  69. object2 = B("object2");         assert(testAFnCalled(FN_CTOR) and testBFnCalled(FN_CTOR));
  70. object2:testVirtual();          assert(testBFnCalled(FN_VIRTUAL) and not testAFnCalled(FN_VIRTUAL));
  71.  
  72. -- test functions taking and returning objects
  73.  
  74. testParamAPtr(object1);          assert(object1:testSucceeded());
  75. testParamAPtrConst(object1);     assert(object1:testSucceeded());
  76. testParamConstAPtr(object1);     assert(object1:testSucceeded());
  77. testParamSharedPtrA(object1);    assert(object1:testSucceeded());
  78.  
  79. testParamAPtr(object2);          assert(object2:testSucceeded());
  80. testParamAPtrConst(object2);     assert(object2:testSucceeded());
  81. testParamConstAPtr(object2);     assert(object2:testSucceeded());
  82. testParamSharedPtrA(object2);    assert(object2:testSucceeded());
  83.  
  84. result = testRetSharedPtrA();    assert(result:getName() == "from C");
  85.  
  86. -- test constness
  87.  
  88. constA = testRetSharedPtrConstA();    assert(constA:getName() == "const A");
  89. assert(constA.testVirtual == nil);
  90. testParamConstAPtr(constA);        assert(constA:testSucceeded());
  91. assert(pcall(testParamAPtr, constA) == false, "attempt to call nil value");
  92.  
  93. -- test properties
  94.  
  95. assert(object1.testProp == 47);
  96. assert(object1.testProp2 == 47);    assert(testAFnCalled(FN_PROPGET));
  97. assert(object2.testProp == 47);
  98. assert(object2.testProp2 == 47);    assert(testAFnCalled(FN_PROPGET));
  99.  
  100. object1.testProp = 48;          assert(object1.testProp == 48);
  101. object1.testProp2 = 49;          assert(testAFnCalled(FN_PROPSET) and object1.testProp2 == 49);
  102.  
  103. -- test operator overload
  104. object1a = object1 + object1;      assert(testAFnCalled(FN_OPERATOR));
  105. assert(object1a:getName() == "object1 + object1");
  106.  
  107. print("All tests succeeded.");
  108.