libshit

Just some random shit
git clone https://git.neptards.moe/neptards/libshit.git
Log | Files | Refs | Submodules | README | LICENSE

function_call.cpp (2266B)


      1 #include "libshit/lua/function_call.hpp"
      2 
      3 #include "libshit/doctest.hpp"
      4 
      5 namespace Libshit::Lua::Test
      6 {
      7   TEST_SUITE_BEGIN("Libshit::Lua::FunctionCall");
      8   static int global;
      9   static void voidfun() { global = 1; }
     10 
     11   TEST_CASE("void function")
     12   {
     13     State vm;
     14     vm.PushFunction<voidfun>();
     15 
     16     global = 0;
     17     lua_call(vm, 0, 0);
     18     CHECK(global == 1);
     19   }
     20 
     21   static int intfun(int a, int b) { return a+b+3; }
     22   TEST_CASE("int function")
     23   {
     24     State vm;
     25     vm.PushFunction<intfun>();
     26 
     27     vm.Push(5);
     28     vm.Push(7);
     29     lua_call(vm, 2, 1);
     30     CHECK(vm.Get<int>() == 5+7+3);
     31     lua_pop(vm, 1);
     32 
     33     CHECK(lua_gettop(vm) == 0);
     34   }
     35 
     36   static std::string strfun(size_t a, const std::string& b)
     37   {
     38     std::string ret;
     39     for (size_t i = 0; i < a; ++i)
     40       ret += b;
     41     return ret;
     42   }
     43   TEST_CASE("string function")
     44   {
     45     State vm;
     46     vm.PushFunction<strfun>();
     47 
     48     vm.Push(5);
     49     vm.Push("hello");
     50     lua_call(vm, 2, 1);
     51     CHECK(vm.Get<std::string>() == "hellohellohellohellohello");
     52     lua_pop(vm, 1);
     53 
     54     CHECK(lua_gettop(vm) == 0);
     55   }
     56 
     57   static std::tuple<bool, int, std::string> tuplefun(bool a, int n)
     58   {
     59     std::stringstream ss;
     60     ss << a << n;
     61     return std::make_tuple(a, n, ss.str());
     62   }
     63   TEST_CASE("tuple function")
     64   {
     65     State vm;
     66     vm.PushFunction<tuplefun>();
     67 
     68     vm.Push(false);
     69     vm.Push(5);
     70     lua_call(vm, 2, 3);
     71     CHECK(vm.Get<bool>(1)        == false);
     72     CHECK(vm.Get<int>(2)         == 5);
     73     CHECK(vm.Get<std::string>(3) == "05");
     74     lua_pop(vm, 3);
     75 
     76     CHECK(lua_gettop(vm) == 0);
     77   }
     78 
     79   static int called;
     80   static void overload_int(int x) { called = x; }
     81   static void overload_str(const std::string& str) { called = str.size(); }
     82   TEST_CASE("overload function")
     83   {
     84     State vm;
     85     vm.TranslateException([&]()
     86     {
     87       vm.PushFunction<overload_int, overload_str>();
     88 
     89       lua_pushvalue(vm, -1);
     90       vm.Push(42);
     91       lua_call(vm, 1, 0);
     92       CHECK(called == 42);
     93 
     94       lua_pushvalue(vm, -1);
     95       vm.Push("Hello");
     96       lua_call(vm, 1, 0);
     97       CHECK(called == 5);
     98 
     99       lua_pushvalue(vm, -1);
    100       vm.Push(false);
    101     });
    102     LIBSHIT_CHECK_LUA_THROWS(
    103       vm, 2, lua_call(vm, 1, 0),
    104       "Invalid arguments (boolean) to overloaded function");
    105   }
    106 
    107   TEST_SUITE_END();
    108 }