aboutsummaryrefslogtreecommitdiff
path: root/unit_tests/shared.h
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--unit_tests/shared.h116
1 files changed, 116 insertions, 0 deletions
diff --git a/unit_tests/shared.h b/unit_tests/shared.h
new file mode 100644
index 0000000..bf3a747
--- /dev/null
+++ b/unit_tests/shared.h
@@ -0,0 +1,116 @@
1#pragma once
2
3// #################################################################################################
4
5class LuaState
6{
7 public:
8
9 DECLARE_UNIQUE_TYPE(WithBaseLibs, bool);
10 DECLARE_UNIQUE_TYPE(WithFixture, bool);
11 lua_State* L{ luaL_newstate() };
12 bool finalizerWasCalled{};
13 STACK_CHECK_START_REL(L, 0);
14
15 ~LuaState()
16 {
17 close();
18 }
19 LuaState(WithBaseLibs withBaseLibs_, WithFixture withFixture_);
20
21 LuaState(LuaState&& rhs_) = default;
22
23 operator lua_State*() const { return L; }
24
25 void stackCheck(int delta_) { STACK_CHECK(L, delta_); }
26 void close();
27
28 // all these methods leave a single item on the stack: an error string on failure, or a single value that depends on what we do
29 [[nodiscard]]
30 LuaError doString(std::string_view const& str_) const;
31 std::string_view doStringAndRet(std::string_view const& str_) const;
32 [[nodiscard]]
33 LuaError doFile(std::filesystem::path const& root_, std::string_view const& str_) const;
34 [[nodiscard]]
35 LuaError loadString(std::string_view const& str_) const;
36 [[nodiscard]]
37 LuaError loadFile(std::filesystem::path const& root_, std::string_view const& str_) const;
38 [[nodiscard]]
39 LuaError runChunk() const;
40
41 friend std::ostream& operator<<(std::ostream& os_, LuaState const& s_)
42 {
43 os_ << luaG_tostring(s_.L, kIdxTop);
44 return os_;
45 }
46};
47
48#define LUA_EXPECT_SUCCESS(S_, WHAT_) { LuaState S{ std::move(S_) }; EXPECT_EQ(S.WHAT_, LuaError::OK) << S; }
49#define LUA_EXPECT_FAILURE(S_, WHAT_) { LuaState S{ std::move(S_) }; EXPECT_NE(S.WHAT_, LuaError::OK) << S; }
50
51// #################################################################################################
52
53enum class TestType
54{
55 AssertNoLuaError,
56 AssertNoThrow,
57 AssertThrows,
58};
59
60struct FileRunnerParam
61{
62 std::string_view script;
63 TestType test;
64};
65
66class FileRunner : public ::testing::TestWithParam<FileRunnerParam>
67{
68 protected:
69 LuaState L{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ true } };
70 std::string root;
71
72 ~FileRunner() override
73 {
74 lua_settop(L, 0);
75 }
76 void SetUp() override
77 {
78 lua_settop(L, 0);
79 }
80
81 void TearDown() override
82 {
83 lua_settop(L, 0);
84 }
85};
86
87// #################################################################################################
88
89class UnitTestRunner : public FileRunner
90{
91 public:
92 UnitTestRunner();
93};
94
95// #################################################################################################
96
97// Can't #ifdef stuff away inside a macro expansion
98#if LUA_VERSION_NUM == 501
99#define LUA51_ONLY(a) a
100#else
101#define LUA51_ONLY(a) ""
102#endif
103
104#if LUA_VERSION_NUM == 504
105#define LUA54_ONLY(a) a
106#else
107#define LUA54_ONLY(a) ""
108#endif
109
110#if LUAJIT_FLAVOR() == 0
111#define PUC_LUA_ONLY(a) a
112#define JIT_LUA_ONLY(a) ""
113#else
114#define PUC_LUA_ONLY(a) ""
115#define JIT_LUA_ONLY(a) a
116#endif