functions.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include <catch.hpp>
  2. #include <luwra.hpp>
  3. TEST_CASE("NativeFunction<R>") {
  4. luwra::StateWrapper state;
  5. REQUIRE(state.runString("return function (x, y) return x + y end") == LUA_OK);
  6. auto fun = luwra::read<luwra::NativeFunction<int>>(state, -1);
  7. REQUIRE(fun(13, 37) == 50);
  8. luwra::NativeFunction<double> fun2 = fun;
  9. REQUIRE(fun2(37.13, 13.37) == 50.5);
  10. }
  11. TEST_CASE("NativeFunction<void>") {
  12. luwra::StateWrapper state;
  13. REQUIRE(state.runString("return function (x, y) returnValue = x + y end") == LUA_OK);
  14. auto fun = luwra::read<luwra::NativeFunction<void>>(state, -1);
  15. fun(13, 37);
  16. int returnValue = state.get<int>("returnValue");
  17. REQUIRE(returnValue == 50);
  18. }
  19. TEST_CASE("function<R(A...)>") {
  20. luwra::StateWrapper state;
  21. REQUIRE(state.runString("return function (x, y) return x + y end") == LUA_OK);
  22. auto fun = luwra::read<std::function<int(int, int)>>(state, -1);
  23. REQUIRE(fun(13, 37) == 50);
  24. }
  25. TEST_CASE("function<void(A...)>") {
  26. luwra::StateWrapper state;
  27. REQUIRE(state.runString("return function (x, y) returnValue = x + y end") == LUA_OK);
  28. auto fun = luwra::read<std::function<void(int, int)>>(state, -1);
  29. fun(13, 37);
  30. int returnValue = state.get<int>("returnValue");
  31. REQUIRE(returnValue == 50);
  32. }