functions.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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::register_global(state, "test_function_noret_noparams", cfun);
  19. // Invoke function
  20. REQUIRE(luaL_dostring(state, "test_function_noret_noparams()") == 0);
  21. REQUIRE(lua_gettop(state) == 0);
  22. REQUIRE(noret_environment == 1338);
  23. lua_close(state);
  24. }
  25. static
  26. void test_function_noret(int a, int b) {
  27. noret_environment = a + b;
  28. }
  29. TEST_CASE("wrap_function_noret") {
  30. lua_State* state = luaL_newstate();
  31. // Test function beforehand
  32. test_function_noret(13, 37);
  33. int req_environemt = noret_environment;
  34. // Wrap function
  35. lua_CFunction cfun = luwra::wrap_function<void(int, int), test_function_noret>;
  36. REQUIRE(cfun != nullptr);
  37. // Register function
  38. luwra::register_global(state, "test_function_noret", cfun);
  39. // Invoke function
  40. REQUIRE(luaL_dostring(state, "test_function_noret(13, 37)") == 0);
  41. REQUIRE(lua_gettop(state) == 0);
  42. REQUIRE(noret_environment == req_environemt);
  43. lua_close(state);
  44. }
  45. static
  46. int test_function_noparams() {
  47. return 13 * 37;
  48. }
  49. TEST_CASE("wrap_function_noparams") {
  50. lua_State* state = luaL_newstate();
  51. // Wrap function
  52. lua_CFunction cfun = luwra::wrap_function<int(), test_function_noparams>;
  53. REQUIRE(cfun != nullptr);
  54. // Register function
  55. luwra::register_global(state, "test_function_noparams", cfun);
  56. // Invoke function
  57. REQUIRE(luaL_dostring(state, "return test_function_noparams()") == 0);
  58. REQUIRE(lua_gettop(state) == 1);
  59. REQUIRE(luwra::Value<int>::read(state, -1) == test_function_noparams());
  60. lua_close(state);
  61. }
  62. static
  63. int test_function(int a, int b) {
  64. return (a + b) * (a - b);
  65. }
  66. TEST_CASE("wrap_function") {
  67. lua_State* state = luaL_newstate();
  68. // Wrap function
  69. lua_CFunction cfun = luwra::wrap_function<int(int, int), test_function>;
  70. REQUIRE(cfun != nullptr);
  71. // Register function
  72. luwra::register_global(state, "test_function", cfun);
  73. // Invoke function
  74. REQUIRE(luaL_dostring(state, "return test_function(37, 13)") == 0);
  75. REQUIRE(lua_gettop(state) == 1);
  76. REQUIRE(luwra::Value<int>::read(state, -1) == test_function(37, 13));
  77. lua_close(state);
  78. }
  79. // TODO: Test whether type-checking works properly