aboutsummaryrefslogtreecommitdiff
path: root/unit_tests/shared.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'unit_tests/shared.cpp')
-rw-r--r--unit_tests/shared.cpp57
1 files changed, 29 insertions, 28 deletions
diff --git a/unit_tests/shared.cpp b/unit_tests/shared.cpp
index 2e2af73..9f3b08e 100644
--- a/unit_tests/shared.cpp
+++ b/unit_tests/shared.cpp
@@ -18,9 +18,9 @@ namespace
18 { 18 {
19 STACK_CHECK_START_REL(L_, 0); 19 STACK_CHECK_START_REL(L_, 0);
20 lua_getglobal(L_, "package"); // L_: package 20 lua_getglobal(L_, "package"); // L_: package
21 std::ignore = luaG_getfield(L_, kIdxTop, "preload"); // L_: package package.preload 21 std::ignore = luaW_getfield(L_, kIdxTop, "preload"); // L_: package package.preload
22 lua_pushcfunction(L_, openf_); // L_: package package.preload openf_ 22 lua_pushcfunction(L_, openf_); // L_: package package.preload openf_
23 luaG_setfield(L_, StackIndex{ -2 }, name_); // L_: package package.preload 23 luaW_setfield(L_, StackIndex{ -2 }, name_); // L_: package package.preload
24 lua_pop(L_, 2); 24 lua_pop(L_, 2);
25 STACK_CHECK(L_, 0); 25 STACK_CHECK(L_, 0);
26 } 26 }
@@ -32,7 +32,7 @@ namespace
32 lua_CFunction sFreezingFinalizer = +[](lua_State* const L_) { 32 lua_CFunction sFreezingFinalizer = +[](lua_State* const L_) {
33 std::lock_guard _guard{ sCallCountsLock }; 33 std::lock_guard _guard{ sCallCountsLock };
34 sFinalizerHits[L_].test_and_set(); 34 sFinalizerHits[L_].test_and_set();
35 luaG_pushstring(L_, "freeze"); // just freeze the thread in place so that it can be debugged 35 luaW_pushstring(L_, "freeze"); // just freeze the thread in place so that it can be debugged
36 return 1; 36 return 1;
37 }; 37 };
38 38
@@ -47,22 +47,22 @@ namespace
47 }; 47 };
48 48
49 lua_CFunction sNewUserData = +[](lua_State* const L_) { 49 lua_CFunction sNewUserData = +[](lua_State* const L_) {
50 std::ignore = luaG_newuserdatauv<int>(L_, UserValueCount{ 0 }); 50 std::ignore = luaW_newuserdatauv<int>(L_, UserValueCount{ 0 });
51 return 1; 51 return 1;
52 }; 52 };
53 53
54 // a function that enables any lane to require "fixture" 54 // a function that enables any lane to require "fixture" and "deep_userdata_example"
55 lua_CFunction sOnStateCreate = +[](lua_State* const L_) { 55 lua_CFunction sOnStateCreate = +[](lua_State* const L_) {
56 PreloadModule(L_, "fixture", luaopen_fixture); 56 PreloadModule(L_, "fixture", luaopen_fixture);
57 PreloadModule(L_, "deep_userdata_example", luaopen_deep_userdata_example); 57 PreloadModule(L_, "deep_userdata_example", luaopen_deep_userdata_example);
58 return 0; 58 return 0;
59 }; 59 };
60 60
61 // a function that sleeps for the specified duration (in seconds) 61 // a function that blocks for the specified duration (in seconds) by putting the current thread to sleep
62 lua_CFunction sSleepFor = +[](lua_State* const L_) { 62 lua_CFunction sBlockFor = +[](lua_State* const L_) {
63 std::chrono::time_point<std::chrono::steady_clock> _until{ std::chrono::time_point<std::chrono::steady_clock>::max() }; 63 std::chrono::time_point<std::chrono::steady_clock> _until{ std::chrono::time_point<std::chrono::steady_clock>::max() };
64 lua_settop(L_, 1); 64 lua_settop(L_, 1);
65 if (luaG_type(L_, kIdxTop) == LuaType::NUMBER) { // we don't want to use lua_isnumber() because of autocoercion 65 if (luaW_type(L_, kIdxTop) == LuaType::NUMBER) { // we don't want to use lua_isnumber() because of autocoercion
66 lua_Duration const _duration{ lua_tonumber(L_, kIdxTop) }; 66 lua_Duration const _duration{ lua_tonumber(L_, kIdxTop) };
67 if (_duration.count() >= 0.0) { 67 if (_duration.count() >= 0.0) {
68 _until = std::chrono::steady_clock::now() + std::chrono::duration_cast<std::chrono::steady_clock::duration>(_duration); 68 _until = std::chrono::steady_clock::now() + std::chrono::duration_cast<std::chrono::steady_clock::duration>(_duration);
@@ -82,17 +82,17 @@ namespace
82 std::lock_guard _guard{ sCallCountsLock }; 82 std::lock_guard _guard{ sCallCountsLock };
83 sFinalizerHits[L_].test_and_set(); 83 sFinalizerHits[L_].test_and_set();
84 bool const _allLanesTerminated = lua_toboolean(L_, kIdxTop); 84 bool const _allLanesTerminated = lua_toboolean(L_, kIdxTop);
85 luaG_pushstring(L_, "Finalizer%s", _allLanesTerminated ? "" : ": Uncooperative lanes detected"); 85 luaW_pushstring(L_, "Finalizer%s", _allLanesTerminated ? "" : ": Uncooperative lanes detected");
86 return 1; 86 return 1;
87 }; 87 };
88 88
89 static luaL_Reg const sFixture[] = { 89 static luaL_Reg const sFixture[] = {
90 { "freezing_finalizer", sFreezingFinalizer }, 90 { "freezing_finalizer", sFreezingFinalizer },
91 { "give_me_back()", sGiveMeBack }, 91 { "give_me_back", sGiveMeBack },
92 { "newlightuserdata", sNewLightUserData }, 92 { "newlightuserdata", sNewLightUserData },
93 { "newuserdata", sNewUserData }, 93 { "newuserdata", sNewUserData },
94 { "on_state_create", sOnStateCreate }, 94 { "on_state_create", sOnStateCreate },
95 { "sleep_for", sSleepFor }, 95 { "block_for", sBlockFor },
96 { "throwing_finalizer", sThrowingFinalizer }, 96 { "throwing_finalizer", sThrowingFinalizer },
97 { nullptr, nullptr } 97 { nullptr, nullptr }
98 }; 98 };
@@ -103,7 +103,7 @@ namespace
103 int luaopen_fixture(lua_State* L_) 103 int luaopen_fixture(lua_State* L_)
104 { 104 {
105 STACK_CHECK_START_REL(L_, 0); 105 STACK_CHECK_START_REL(L_, 0);
106 luaG_newlib<std::size(local::sFixture)>(L_, local::sFixture); // M 106 luaW_newlib<std::size(local::sFixture)>(L_, local::sFixture); // M
107 STACK_CHECK(L_, 1); 107 STACK_CHECK(L_, 1);
108 return 1; 108 return 1;
109 } 109 }
@@ -246,7 +246,7 @@ LuaError LuaState::doString(std::string_view const& str_) const
246 return _loadErr; 246 return _loadErr;
247 } 247 }
248 LuaError const _callErr{ lua_pcall(L, 0, 1, 0) }; // L: "<msg>"? 248 LuaError const _callErr{ lua_pcall(L, 0, 1, 0) }; // L: "<msg>"?
249 [[maybe_unused]] std::string_view const _out{ luaG_tostring(L, kIdxTop) }; 249 [[maybe_unused]] std::string_view const _out{ luaW_tostring(L, kIdxTop) };
250 STACK_CHECK(L, 1); 250 STACK_CHECK(L, 1);
251 return _callErr; 251 return _callErr;
252} 252}
@@ -257,8 +257,8 @@ std::string_view LuaState::doStringAndRet(std::string_view const& str_) const
257{ 257{
258 lua_settop(L, 0); 258 lua_settop(L, 0);
259 if (str_.empty()) { 259 if (str_.empty()) {
260 luaG_pushstring(L, ""); 260 luaW_pushstring(L, "");
261 return luaG_tostring(L, kIdxTop); 261 return luaW_tostring(L, kIdxTop);
262 } 262 }
263 STACK_CHECK_START_REL(L, 0); 263 STACK_CHECK_START_REL(L, 0);
264 LuaError const _loadErr{ luaL_loadstring(L, str_.data()) }; // L: chunk() 264 LuaError const _loadErr{ luaL_loadstring(L, str_.data()) }; // L: chunk()
@@ -268,7 +268,7 @@ std::string_view LuaState::doStringAndRet(std::string_view const& str_) const
268 } 268 }
269 [[maybe_unused]] LuaError const _callErr{ lua_pcall(L, 0, 1, 0) }; // L: "<msg>"?|retstring 269 [[maybe_unused]] LuaError const _callErr{ lua_pcall(L, 0, 1, 0) }; // L: "<msg>"?|retstring
270 STACK_CHECK(L, 1); 270 STACK_CHECK(L, 1);
271 return luaG_tostring(L, kIdxTop); 271 return luaW_tostring(L, kIdxTop);
272} 272}
273 273
274// ################################################################################################# 274// #################################################################################################
@@ -336,7 +336,7 @@ void LuaState::requireFailure(std::string_view const& script_)
336{ 336{
337 auto const _result{ doString(script_) }; 337 auto const _result{ doString(script_) };
338 if (_result == LuaError::OK) { 338 if (_result == LuaError::OK) {
339 WARN(luaG_tostring(L, kIdxTop)); 339 WARN(luaW_tostring(L, kIdxTop));
340 } 340 }
341 REQUIRE(_result != LuaError::OK); 341 REQUIRE(_result != LuaError::OK);
342 lua_settop(L, 0); 342 lua_settop(L, 0);
@@ -372,7 +372,7 @@ void LuaState::requireSuccess(std::string_view const& script_)
372{ 372{
373 auto const _result{ doString(script_) }; 373 auto const _result{ doString(script_) };
374 if (_result != LuaError::OK) { 374 if (_result != LuaError::OK) {
375 WARN(luaG_tostring(L, kIdxTop)); 375 WARN(luaW_tostring(L, kIdxTop));
376 } 376 }
377 REQUIRE(_result == LuaError::OK); 377 REQUIRE(_result == LuaError::OK);
378 lua_settop(L, 0); 378 lua_settop(L, 0);
@@ -384,7 +384,7 @@ void LuaState::requireSuccess(std::filesystem::path const& root_, std::string_vi
384{ 384{
385 auto const _result{ doFile(root_, path_) }; 385 auto const _result{ doFile(root_, path_) };
386 if (_result != LuaError::OK) { 386 if (_result != LuaError::OK) {
387 WARN(luaG_tostring(L, kIdxTop)); 387 WARN(luaW_tostring(L, kIdxTop));
388 } 388 }
389 REQUIRE(_result == LuaError::OK); 389 REQUIRE(_result == LuaError::OK);
390 lua_settop(L, 0); 390 lua_settop(L, 0);
@@ -407,20 +407,20 @@ TEST_CASE("LuaState.doString")
407{ 407{
408 LuaState _L{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ false } }; 408 LuaState _L{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ false } };
409 // if the script fails to load, we should find the error message at the top of the stack 409 // if the script fails to load, we should find the error message at the top of the stack
410 REQUIRE([&L = _L]() { std::ignore = L.doString("function end"); return lua_gettop(L) == 1 && luaG_type(L, StackIndex{1}) == LuaType::STRING; }()); 410 REQUIRE([&L = _L]() { std::ignore = L.doString("function end"); return lua_gettop(L) == 1 && luaW_type(L, StackIndex{1}) == LuaType::STRING; }());
411 411
412 // if the script runs, the stack should contain its return value 412 // if the script runs, the stack should contain its return value
413 REQUIRE([&L = _L]() { std::ignore = L.doString("return true"); return lua_gettop(L) == 1 && luaG_type(L, StackIndex{1}) == LuaType::BOOLEAN; }()); 413 REQUIRE([&L = _L]() { std::ignore = L.doString("return true"); return lua_gettop(L) == 1 && luaW_type(L, StackIndex{1}) == LuaType::BOOLEAN; }());
414 REQUIRE([&L = _L]() { std::ignore = L.doString("return 'hello'"); return lua_gettop(L) == 1 && luaG_tostring(L, StackIndex{1}) == "hello"; }()); 414 REQUIRE([&L = _L]() { std::ignore = L.doString("return 'hello'"); return lua_gettop(L) == 1 && luaW_tostring(L, StackIndex{1}) == "hello"; }());
415 // or nil if it didn't return anything 415 // or nil if it didn't return anything
416 REQUIRE([&L = _L]() { std::ignore = L.doString("return"); return lua_gettop(L) == 1 && luaG_type(L, StackIndex{1}) == LuaType::NIL; }()); 416 REQUIRE([&L = _L]() { std::ignore = L.doString("return"); return lua_gettop(L) == 1 && luaW_type(L, StackIndex{1}) == LuaType::NIL; }());
417 417
418 // on failure, doStringAndRet returns "", and the error message is on the stack 418 // on failure, doStringAndRet returns "", and the error message is on the stack
419 REQUIRE([&L = _L]() { return L.doStringAndRet("function end") == "" && lua_gettop(L) == 1 && luaG_type(L, StackIndex{ 1 }) == LuaType::STRING && luaG_tostring(L, StackIndex{ 1 }) != ""; }()); 419 REQUIRE([&L = _L]() { return L.doStringAndRet("function end") == "" && lua_gettop(L) == 1 && luaW_type(L, StackIndex{ 1 }) == LuaType::STRING && luaW_tostring(L, StackIndex{ 1 }) != ""; }());
420 // on success doStringAndRet returns the string returned by the script, that is also at the top of the stack 420 // on success doStringAndRet returns the string returned by the script, that is also at the top of the stack
421 REQUIRE([&L = _L]() { return L.doStringAndRet("return 'hello'") == "hello" && lua_gettop(L) == 1 && luaG_type(L, StackIndex{ 1 }) == LuaType::STRING && luaG_tostring(L, StackIndex{ 1 }) == "hello"; }()); 421 REQUIRE([&L = _L]() { return L.doStringAndRet("return 'hello'") == "hello" && lua_gettop(L) == 1 && luaW_type(L, StackIndex{ 1 }) == LuaType::STRING && luaW_tostring(L, StackIndex{ 1 }) == "hello"; }());
422 // if the returned value is not (convertible to) a string, we should get an empty string out of doStringAndRet 422 // if the returned value is not (convertible to) a string, we should get an empty string out of doStringAndRet
423 REQUIRE([&L = _L]() { return L.doStringAndRet("return function() end") == "" && lua_gettop(L) == 1 && luaG_type(L, StackIndex{ 1 }) == LuaType::FUNCTION && luaG_tostring(L, StackIndex{ 1 }) == ""; }()); 423 REQUIRE([&L = _L]() { return L.doStringAndRet("return function() end") == "" && lua_gettop(L) == 1 && luaW_type(L, StackIndex{ 1 }) == LuaType::FUNCTION && luaW_tostring(L, StackIndex{ 1 }) == ""; }());
424} 424}
425 425
426// ################################################################################################# 426// #################################################################################################
@@ -436,9 +436,9 @@ FileRunner::FileRunner(std::string_view const& where_)
436 // because the VS Test Explorer doesn't appreciate the text output of some scripts, so absorb them 436 // because the VS Test Explorer doesn't appreciate the text output of some scripts, so absorb them
437 if constexpr (1) { 437 if constexpr (1) {
438 auto const _nullprint = +[](lua_State* const L_) { return 0; }; 438 auto const _nullprint = +[](lua_State* const L_) { return 0; };
439 luaG_pushglobaltable(L); 439 luaW_pushglobaltable(L);
440 lua_pushcfunction(L, _nullprint); 440 lua_pushcfunction(L, _nullprint);
441 luaG_setfield(L, StackIndex{ -2 }, std::string_view{ "print" }); 441 luaW_setfield(L, StackIndex{ -2 }, std::string_view{ "print" });
442 lua_pop(L, 1); 442 lua_pop(L, 1);
443 stackCheck(0); 443 stackCheck(0);
444 } 444 }
@@ -476,6 +476,7 @@ void FileRunner::performTest(FileRunnerParam const& testParam_)
476 INFO(testParam_.script); 476 INFO(testParam_.script);
477 switch (testParam_.test) { 477 switch (testParam_.test) {
478 case TestType::AssertNoLuaError: 478 case TestType::AssertNoLuaError:
479 lua_atpanic(L, _atPanic);
479 requireSuccess(root, testParam_.script); 480 requireSuccess(root, testParam_.script);
480 break; 481 break;
481 482