auxiliary.cpp 2.0 KB

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