functions.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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(test_function_noret_noparams);
  28. REQUIRE(cfun != nullptr);
  29. // Register function
  30. state.set("test_function_noret_noparams", cfun);
  31. // Invoke function
  32. REQUIRE(state.runString("test_function_noret_noparams()") == LUA_OK);
  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. noret_environment = 0;
  41. // Wrap function
  42. lua_CFunction cfun = LUWRA_WRAP(test_function_noret);
  43. REQUIRE(cfun != nullptr);
  44. // Register function
  45. state.set("test_function_noret", cfun);
  46. // Invoke function
  47. REQUIRE(state.runString("test_function_noret(13, 37)") == LUA_OK);
  48. REQUIRE(lua_gettop(state) == 0);
  49. REQUIRE(noret_environment == req_environemt);
  50. }
  51. SECTION("with return value, without parameters") {
  52. // Wrap function
  53. lua_CFunction cfun = LUWRA_WRAP(test_function_noparams);
  54. REQUIRE(cfun != nullptr);
  55. // Register function
  56. state.set("test_function_noparams", cfun);
  57. // Invoke function
  58. REQUIRE(state.runString("return test_function_noparams()") == LUA_OK);
  59. REQUIRE(lua_gettop(state) == 1);
  60. REQUIRE(luwra::read<int>(state, -1) == test_function_noparams());
  61. }
  62. SECTION("with return value, with parameters") {
  63. // Wrap function
  64. lua_CFunction cfun = LUWRA_WRAP(test_function);
  65. REQUIRE(cfun != nullptr);
  66. // Register function
  67. state.set("test_function", cfun);
  68. // Invoke function
  69. REQUIRE(state.runString("return test_function(37, 13)") == LUA_OK);
  70. REQUIRE(lua_gettop(state) == 1);
  71. REQUIRE(luwra::read<int>(state, -1) == test_function(37, 13));
  72. }
  73. }
  74. TEST_CASE("NativeFunction") {
  75. luwra::StateWrapper state;
  76. SECTION("with return value") {
  77. REQUIRE(state.runString("return function (x, y) return x + y end") == LUA_OK);
  78. auto fun = luwra::read<luwra::NativeFunction<int>>(state, -1);
  79. REQUIRE(fun(13, 37) == 50);
  80. }
  81. SECTION("without return value") {
  82. REQUIRE(state.runString("return function (x, y) returnValue = x + y end") == LUA_OK);
  83. auto fun = luwra::read<luwra::NativeFunction<void>>(state, -1);
  84. fun(13, 37);
  85. int returnValue = state.get<int>("returnValue");
  86. REQUIRE(returnValue == 50);
  87. }
  88. }