functions.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include <catch.hpp>
  2. #include <luwra.hpp>
  3. static
  4. int noret_environment = 0;
  5. static
  6. void test_function_noret_noparams() {
  7. noret_environment++;
  8. }
  9. static
  10. void test_function_noret(int a, int b) {
  11. noret_environment = a + b;
  12. }
  13. static
  14. int test_function_noparams() {
  15. return 13 * 37;
  16. }
  17. static
  18. int test_function(int a, int b) {
  19. return (a + b) * (a - b);
  20. }
  21. TEST_CASE("FunctionWrapper") {
  22. luwra::StateWrapper state;
  23. SECTION("without return value, without parameters") {
  24. // Setup environment
  25. noret_environment = 1337;
  26. // Wrap function
  27. lua_CFunction cfun = LUWRA_WRAP_FUNCTION(test_function_noret_noparams);
  28. REQUIRE(cfun != nullptr);
  29. // Register function
  30. luwra::setGlobal(state, "test_function_noret_noparams", cfun);
  31. // Invoke function
  32. REQUIRE(luaL_dostring(state, "test_function_noret_noparams()") == 0);
  33. REQUIRE(lua_gettop(state) == 0);
  34. REQUIRE(noret_environment == 1338);
  35. }
  36. SECTION("without return value, with parameters") {
  37. // Test function beforehand
  38. test_function_noret(13, 37);
  39. int req_environemt = noret_environment;
  40. // Wrap function
  41. lua_CFunction cfun = LUWRA_WRAP_FUNCTION(test_function_noret);
  42. REQUIRE(cfun != nullptr);
  43. // Register function
  44. luwra::setGlobal(state, "test_function_noret", cfun);
  45. // Invoke function
  46. REQUIRE(luaL_dostring(state, "test_function_noret(13, 37)") == 0);
  47. REQUIRE(lua_gettop(state) == 0);
  48. REQUIRE(noret_environment == req_environemt);
  49. }
  50. SECTION("with return value, without parameters") {
  51. // Wrap function
  52. lua_CFunction cfun = LUWRA_WRAP_FUNCTION(test_function_noparams);
  53. REQUIRE(cfun != nullptr);
  54. // Register function
  55. luwra::setGlobal(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. }
  61. SECTION("with return value, with parameters") {
  62. // Wrap function
  63. lua_CFunction cfun = LUWRA_WRAP_FUNCTION(test_function);
  64. REQUIRE(cfun != nullptr);
  65. // Register function
  66. luwra::setGlobal(state, "test_function", cfun);
  67. // Invoke function
  68. REQUIRE(luaL_dostring(state, "return test_function(37, 13)") == 0);
  69. REQUIRE(lua_gettop(state) == 1);
  70. REQUIRE(luwra::Value<int>::read(state, -1) == test_function(37, 13));
  71. }
  72. }
  73. TEST_CASE("NativeFunction") {
  74. luwra::StateWrapper state;
  75. SECTION("with return value") {
  76. REQUIRE(luaL_dostring(state, "return function (x, y) return x + y end") == LUA_OK);
  77. auto fun = luwra::read<luwra::NativeFunction<int>>(state, -1);
  78. REQUIRE(fun(13, 37) == 50);
  79. }
  80. SECTION("without return value") {
  81. REQUIRE(luaL_dostring(state, "return function (x, y) returnValue = x + y end") == LUA_OK);
  82. auto fun = luwra::read<luwra::NativeFunction<void>>(state, -1);
  83. fun(13, 37);
  84. int returnValue = state["returnValue"];
  85. REQUIRE(returnValue == 50);
  86. }
  87. }