auxiliary.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include <catch.hpp>
  2. #include <lua.hpp>
  3. #include <luwra.hpp>
  4. TEST_CASE("equal") {
  5. luwra::StateWrapper state;
  6. REQUIRE(luwra::push(state, 1) == 1);
  7. REQUIRE(luwra::push(state, 2) == 1);
  8. REQUIRE(luwra::push(state, 1) == 1);
  9. REQUIRE(!luwra::equal(state, -1, -2));
  10. REQUIRE(!luwra::equal(state, -2, -1));
  11. REQUIRE(luwra::equal(state, -1, -3));
  12. REQUIRE(luwra::equal(state, -3, -1));
  13. REQUIRE(luwra::equal(state, -3, -3));
  14. REQUIRE(luwra::equal(state, -1, -1));
  15. }
  16. TEST_CASE("setGlobal") {
  17. luwra::StateWrapper state;
  18. luwra::setGlobal(state, "test", 1338);
  19. REQUIRE(luaL_dostring(state, "return test") == LUA_OK);
  20. REQUIRE(luwra::read<int>(state, -1) == 1338);
  21. }
  22. TEST_CASE("getGlobal") {
  23. luwra::StateWrapper state;
  24. REQUIRE(luaL_dostring(state, "test = 1337") == LUA_OK);
  25. REQUIRE(luwra::getGlobal<int>(state, "test") == 1337);
  26. }
  27. TEST_CASE("setFields") {
  28. luwra::StateWrapper state;
  29. lua_newtable(state);
  30. SECTION("templateVersion") {
  31. luwra::setFields(
  32. state, -1,
  33. "test", 1338,
  34. 123, 456
  35. );
  36. lua_setglobal(state, "test");
  37. REQUIRE(luaL_dostring(state, "return test.test, test[123]") == LUA_OK);
  38. REQUIRE(luwra::read<int>(state, -2) == 1338);
  39. REQUIRE(luwra::read<int>(state, -1) == 456);
  40. }
  41. SECTION("mapVersion") {
  42. luwra::setFields(
  43. state, -1,
  44. {
  45. {"test", 1338},
  46. {123, 456}
  47. }
  48. );
  49. lua_setglobal(state, "test");
  50. REQUIRE(luaL_dostring(state, "return test.test, test[123]") == LUA_OK);
  51. REQUIRE(luwra::read<int>(state, -2) == 1338);
  52. REQUIRE(luwra::read<int>(state, -1) == 456);
  53. }
  54. }
  55. TEST_CASE("getField") {
  56. luwra::StateWrapper state;
  57. REQUIRE(luaL_dostring(state, "return {hello = 123}") == LUA_OK);
  58. REQUIRE(luwra::getField<int>(state, -1, "hello") == 123);
  59. }
  60. TEST_CASE("FieldVector") {
  61. luwra::StateWrapper state;
  62. luwra::push<luwra::FieldVector>(state, {
  63. {"test", 7331},
  64. {7331, 123}
  65. });
  66. lua_setglobal(state, "test");
  67. REQUIRE(luaL_dostring(state, "return test.test, test[test.test]") == LUA_OK);
  68. REQUIRE(luwra::read<int>(state, -2) == 7331);
  69. REQUIRE(luwra::read<int>(state, -1) == 123);
  70. }