diff options
author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-12-13 17:22:17 +0100 |
---|---|---|
committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-12-13 17:22:17 +0100 |
commit | dc7a2bc3a9c8316e17902493b832ca117805d29f (patch) | |
tree | 3c1a03edf586869119ef0de03631f6f603650720 /unit_tests/embedded_tests.cpp | |
parent | 7500a80fc06c5311c46df8f1761f25ae67277fc4 (diff) | |
download | lanes-dc7a2bc3a9c8316e17902493b832ca117805d29f.tar.gz lanes-dc7a2bc3a9c8316e17902493b832ca117805d29f.tar.bz2 lanes-dc7a2bc3a9c8316e17902493b832ca117805d29f.zip |
Append all unit tests to depot
Diffstat (limited to 'unit_tests/embedded_tests.cpp')
-rw-r--r-- | unit_tests/embedded_tests.cpp | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/unit_tests/embedded_tests.cpp b/unit_tests/embedded_tests.cpp new file mode 100644 index 0000000..03905d7 --- /dev/null +++ b/unit_tests/embedded_tests.cpp | |||
@@ -0,0 +1,140 @@ | |||
1 | #include "_pch.hpp" | ||
2 | #include "shared.h" | ||
3 | #include "lanes/src/lanes.hpp" | ||
4 | |||
5 | #include <windows.h> | ||
6 | |||
7 | // ################################################################################################# | ||
8 | |||
9 | namespace | ||
10 | { | ||
11 | namespace local | ||
12 | { | ||
13 | static int load_lanes_lua(lua_State* const L_) | ||
14 | { | ||
15 | if (0 == luaL_dofile(L_, "lanes.lua")) { | ||
16 | return 1; | ||
17 | } else { | ||
18 | return 0; | ||
19 | } | ||
20 | } | ||
21 | } // namespace local | ||
22 | } | ||
23 | |||
24 | // ################################################################################################# | ||
25 | |||
26 | class EmbeddedTests : public ::testing::Test | ||
27 | { | ||
28 | private: | ||
29 | HMODULE hCore{}; | ||
30 | |||
31 | protected: | ||
32 | LuaState S{ LuaState::WithBaseLibs{ false }, LuaState::WithFixture{ false } }; | ||
33 | lua_CFunction lanes_register{}; | ||
34 | |||
35 | void SetUp() override | ||
36 | { | ||
37 | hCore = LoadLibraryW(L"lanes\\core"); | ||
38 | if (!hCore) { | ||
39 | throw std::logic_error("Could not load lanes.core"); | ||
40 | } | ||
41 | luaopen_lanes_embedded_t const _p_luaopen_lanes_embedded{ reinterpret_cast<luaopen_lanes_embedded_t>(GetProcAddress(hCore, "luaopen_lanes_embedded")) }; | ||
42 | if (!_p_luaopen_lanes_embedded) { | ||
43 | throw std::logic_error("Could not bind luaopen_lanes_embedded"); | ||
44 | } | ||
45 | |||
46 | lanes_register = reinterpret_cast<lua_CFunction>(GetProcAddress(hCore, "lanes_register")); | ||
47 | if (!lanes_register) { | ||
48 | throw std::logic_error("Could not bind lanes_register"); | ||
49 | } | ||
50 | // need base to make lanes happy | ||
51 | luaL_requiref(S, LUA_GNAME, luaopen_base, 1); | ||
52 | lua_pop(S, 1); | ||
53 | S.stackCheck(0); | ||
54 | |||
55 | // need package to be able to require lanes | ||
56 | luaL_requiref(S, LUA_LOADLIBNAME, luaopen_package, 1); | ||
57 | lua_pop(S, 1); | ||
58 | S.stackCheck(0); | ||
59 | |||
60 | // need table to make lanes happy | ||
61 | luaL_requiref(S, LUA_TABLIBNAME, luaopen_table, 1); | ||
62 | lua_pop(S, 1); | ||
63 | S.stackCheck(0); | ||
64 | |||
65 | // need string to make lanes happy | ||
66 | luaL_requiref(S, LUA_STRLIBNAME, luaopen_string, 1); | ||
67 | lua_pop(S, 1); | ||
68 | S.stackCheck(0); | ||
69 | |||
70 | _p_luaopen_lanes_embedded(S, local::load_lanes_lua); // S: lanes | ||
71 | lua_pop(S, 1); | ||
72 | S.stackCheck(0); | ||
73 | } | ||
74 | |||
75 | void TearDown() override | ||
76 | { | ||
77 | // close state manually before we unload lanes.core | ||
78 | S.close(); | ||
79 | FreeLibrary(hCore); | ||
80 | } | ||
81 | }; | ||
82 | |||
83 | TEST_F(EmbeddedTests, SingleState) | ||
84 | { | ||
85 | // this sends data in a linda. current contents: | ||
86 | // key: short string | ||
87 | // values: | ||
88 | // bool | ||
89 | // integer | ||
90 | // number | ||
91 | // long string | ||
92 | // table with array and hash parts | ||
93 | // function with an upvalue | ||
94 | std::string_view const script{ | ||
95 | " local lanes = require 'lanes'.configure{with_timers = false}" | ||
96 | " local l = lanes.linda'gleh'" | ||
97 | " local upvalue = 'oeauaoeuoeuaoeuaoeujaoefubycfjbycfybcfjybcfjybcfjbcf'" | ||
98 | " local upvalued = function()" | ||
99 | " return upvalue" | ||
100 | " end" | ||
101 | " local t = setmetatable({ true, true, true, a = true}, {__index = rawget })" | ||
102 | " l:set('yo', true, 10, 100.0, upvalue, t, upvalued)" // put a breakpoint in linda_set to have some data to explore with the NATVIS | ||
103 | " return 'SUCCESS'" | ||
104 | }; | ||
105 | std::string_view result = S.doStringAndRet(script); | ||
106 | ASSERT_EQ(result, "SUCCESS"); | ||
107 | } | ||
108 | |||
109 | // ################################################################################################# | ||
110 | |||
111 | TEST_F(EmbeddedTests, ManualRegister) | ||
112 | { | ||
113 | ASSERT_EQ(S.doString("require 'lanes'.configure{with_timers = false}"), LuaError::OK) << S; | ||
114 | lua_pop(S, 1); | ||
115 | |||
116 | // require 'io' library after Lanes is initialized: | ||
117 | luaL_requiref(S, LUA_IOLIBNAME, luaopen_io, 1); | ||
118 | lua_pop(S, 1); | ||
119 | S.stackCheck(0); | ||
120 | |||
121 | // try to send io.open into a linda | ||
122 | std::string_view const script{ | ||
123 | "local lanes = require 'lanes'.configure{with_timers = false}" | ||
124 | "local l = lanes.linda'gleh'" | ||
125 | "l:set('yo', io.open)" | ||
126 | "return 'SUCCESS'" | ||
127 | }; | ||
128 | std::string_view const result1{ S.doStringAndRet(script) }; | ||
129 | ASSERT_NE(result1, "SUCCESS") << S; // S: "msg" | ||
130 | lua_pop(S, 1); // S: | ||
131 | |||
132 | // try again after manual registration | ||
133 | lua_pushcfunction(S, lanes_register); // S: lanes_register | ||
134 | luaG_pushstring(S, LUA_IOLIBNAME); // S: lanes_register "io" | ||
135 | luaL_requiref(S, LUA_IOLIBNAME, luaopen_io, 1); // S: lanes_register "io" io | ||
136 | lua_call(S, 2, 0); // S: | ||
137 | S.stackCheck(0); | ||
138 | std::string_view const result2{ S.doStringAndRet(script) }; | ||
139 | ASSERT_EQ(result2, "SUCCESS") << S; | ||
140 | } | ||