functions.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "catch.hpp"
  2. #include <lua.hpp>
  3. #include <luwra.hpp>
  4. static
  5. int noret_environment = 0;
  6. static
  7. void test_function_noret_noparams() {
  8. noret_environment++;
  9. }
  10. TEST_CASE("wrap_function_noret_noparams") {
  11. lua_State* state = luaL_newstate();
  12. // Setup environment
  13. noret_environment = 1337;
  14. // Wrap function
  15. lua_CFunction cfun = luwra::wrap_function<void(), test_function_noret_noparams>;
  16. REQUIRE(cfun != nullptr);
  17. // Register function
  18. luwra::push(state, cfun);
  19. lua_setglobal(state, "test_function_noret_noparams");
  20. // Invoke function
  21. REQUIRE(luaL_dostring(state, "test_function_noret_noparams()") == 0);
  22. REQUIRE(lua_gettop(state) == 0);
  23. REQUIRE(noret_environment == 1338);
  24. lua_close(state);
  25. }
  26. static
  27. void test_function_noret(int a, int b) {
  28. noret_environment = a + b;
  29. }
  30. TEST_CASE("wrap_function_noret") {
  31. lua_State* state = luaL_newstate();
  32. // Test function beforehand
  33. test_function_noret(13, 37);
  34. int req_environemt = noret_environment;
  35. // Wrap function
  36. lua_CFunction cfun = luwra::wrap_function<void(int, int), test_function_noret>;
  37. REQUIRE(cfun != nullptr);
  38. // Register function
  39. luwra::push(state, cfun);
  40. lua_setglobal(state, "test_function_noret");
  41. // Invoke function
  42. REQUIRE(luaL_dostring(state, "test_function_noret(13, 37)") == 0);
  43. REQUIRE(lua_gettop(state) == 0);
  44. REQUIRE(noret_environment == req_environemt);
  45. lua_close(state);
  46. }
  47. static
  48. int test_function_noparams() {
  49. return 13 * 37;
  50. }
  51. TEST_CASE("wrap_function_noparams") {
  52. lua_State* state = luaL_newstate();
  53. // Wrap function
  54. lua_CFunction cfun = luwra::wrap_function<int(), test_function_noparams>;
  55. REQUIRE(cfun != nullptr);
  56. // Register function
  57. luwra::push(state, cfun);
  58. lua_setglobal(state, "test_function_noparams");
  59. // Invoke function
  60. REQUIRE(luaL_dostring(state, "return test_function_noparams()") == 0);
  61. REQUIRE(lua_gettop(state) == 1);
  62. REQUIRE(luwra::Value<int>::read(state, -1) == test_function_noparams());
  63. lua_close(state);
  64. }
  65. static
  66. int test_function(int a, int b) {
  67. return (a + b) * (a - b);
  68. }
  69. TEST_CASE("wrap_function") {
  70. lua_State* state = luaL_newstate();
  71. // Wrap function
  72. lua_CFunction cfun = luwra::wrap_function<int(int, int), test_function>;
  73. REQUIRE(cfun != nullptr);
  74. // Register function
  75. luwra::push(state, cfun);
  76. lua_setglobal(state, "test_function");
  77. // Invoke function
  78. REQUIRE(luaL_dostring(state, "return test_function(37, 13)") == 0);
  79. REQUIRE(lua_gettop(state) == 1);
  80. REQUIRE(luwra::Value<int>::read(state, -1) == test_function(37, 13));
  81. lua_close(state);
  82. }
  83. // TODO: Test whether type-checking works properly