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.cpp108
1 files changed, 84 insertions, 24 deletions
diff --git a/unit_tests/shared.cpp b/unit_tests/shared.cpp
index 14b2b13..0825227 100644
--- a/unit_tests/shared.cpp
+++ b/unit_tests/shared.cpp
@@ -98,14 +98,14 @@ namespace
98// ################################################################################################# 98// #################################################################################################
99// ################################################################################################# 99// #################################################################################################
100 100
101TEST(Internals, StackChecker) 101TEST_CASE("stack checker")
102{ 102{
103 LuaState _L{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ false } }; 103 LuaState _L{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ false } };
104 StackChecker::CallsCassert = false; 104 StackChecker::CallsCassert = false;
105 105
106 auto _doStackCheckerTest = [&_L](lua_CFunction const _f, LuaError const _expected) { 106 auto _doStackCheckerTest = [&_L](lua_CFunction const _f, LuaError const _expected) {
107 lua_pushcfunction(_L, _f); 107 lua_pushcfunction(_L, _f);
108 ASSERT_EQ(ToLuaError(lua_pcall(_L, 0, 0, 0)), _expected); 108 REQUIRE(ToLuaError(lua_pcall(_L, 0, 0, 0)) == _expected);
109 }; 109 };
110 110
111 // function where the StackChecker detects something wrong with the stack 111 // function where the StackChecker detects something wrong with the stack
@@ -289,6 +289,66 @@ LuaError LuaState::loadFile(std::filesystem::path const& root_, std::string_view
289 289
290// ################################################################################################# 290// #################################################################################################
291 291
292void LuaState::requireFailure(std::string_view const& script_)
293{
294 auto const _result{ doString(script_) };
295 REQUIRE(_result != LuaError::OK);
296 if (_result == LuaError::OK) {
297 INFO(luaG_tostring(L, kIdxTop));
298 }
299 lua_settop(L, 0);
300}
301
302// #################################################################################################
303
304void LuaState::requireNotReturnedString(std::string_view const& script_, std::string_view const& unexpected_)
305{
306 auto const _result{ doStringAndRet(script_) };
307 REQUIRE(_result != unexpected_);
308 if (_result == unexpected_) {
309 INFO(_result);
310 }
311 lua_settop(L, 0);
312}
313
314// #################################################################################################
315
316void LuaState::requireReturnedString(std::string_view const& script_, std::string_view const& expected_)
317{
318 auto const _result{ doStringAndRet(script_) };
319 REQUIRE(_result == expected_);
320 if (_result != expected_) {
321 INFO(_result);
322 }
323 lua_settop(L, 0);
324}
325
326// #################################################################################################
327
328void LuaState::requireSuccess(std::string_view const& script_)
329{
330 auto const _result{ doString(script_) };
331 REQUIRE(_result == LuaError::OK);
332 if (_result != LuaError::OK) {
333 INFO(luaG_tostring(L, kIdxTop));
334 }
335 lua_settop(L, 0);
336}
337
338// #################################################################################################
339
340void LuaState::requireSuccess(std::filesystem::path const& root_, std::string_view const& path_)
341{
342 auto const _result{ doFile(root_, path_) };
343 REQUIRE(_result == LuaError::OK);
344 if (_result != LuaError::OK) {
345 INFO(luaG_tostring(L, kIdxTop));
346 }
347 lua_settop(L, 0);
348}
349
350// #################################################################################################
351
292LuaError LuaState::runChunk() const 352LuaError LuaState::runChunk() const
293{ 353{
294 STACK_CHECK_START_ABS(L, 1); // we must start with the chunk on the stack (or an error string if it failed to load) 354 STACK_CHECK_START_ABS(L, 1); // we must start with the chunk on the stack (or an error string if it failed to load)
@@ -300,61 +360,61 @@ LuaError LuaState::runChunk() const
300// ################################################################################################# 360// #################################################################################################
301// ################################################################################################# 361// #################################################################################################
302 362
303TEST(LuaState, DoString) 363TEST_CASE("LuaState::doString")
304{ 364{
305 LuaState _L{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ false } }; 365 LuaState _L{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ false } };
306 // if the script fails to load, we should find the error message at the top of the stack 366 // if the script fails to load, we should find the error message at the top of the stack
307 ASSERT_TRUE([&L = _L]() { std::ignore = L.doString("function end"); return lua_gettop(L) == 1 && luaG_type(L, StackIndex{1}) == LuaType::STRING; }()); 367 REQUIRE([&L = _L]() { std::ignore = L.doString("function end"); return lua_gettop(L) == 1 && luaG_type(L, StackIndex{1}) == LuaType::STRING; }());
308 368
309 // if the script runs, the stack should contain its return value 369 // if the script runs, the stack should contain its return value
310 ASSERT_TRUE([&L = _L]() { std::ignore = L.doString("return true"); return lua_gettop(L) == 1 && luaG_type(L, StackIndex{1}) == LuaType::BOOLEAN; }()); 370 REQUIRE([&L = _L]() { std::ignore = L.doString("return true"); return lua_gettop(L) == 1 && luaG_type(L, StackIndex{1}) == LuaType::BOOLEAN; }());
311 ASSERT_TRUE([&L = _L]() { std::ignore = L.doString("return 'hello'"); return lua_gettop(L) == 1 && luaG_tostring(L, StackIndex{1}) == "hello"; }()); 371 REQUIRE([&L = _L]() { std::ignore = L.doString("return 'hello'"); return lua_gettop(L) == 1 && luaG_tostring(L, StackIndex{1}) == "hello"; }());
312 // or nil if it didn't return anything 372 // or nil if it didn't return anything
313 ASSERT_TRUE([&L = _L]() { std::ignore = L.doString("return"); return lua_gettop(L) == 1 && luaG_type(L, StackIndex{1}) == LuaType::NIL; }()); 373 REQUIRE([&L = _L]() { std::ignore = L.doString("return"); return lua_gettop(L) == 1 && luaG_type(L, StackIndex{1}) == LuaType::NIL; }());
314 374
315 // on failure, doStringAndRet returns "", and the error message is on the stack 375 // on failure, doStringAndRet returns "", and the error message is on the stack
316 ASSERT_TRUE([&L = _L]() { return L.doStringAndRet("function end") == "" && lua_gettop(L) == 1 && luaG_type(L, StackIndex{1}) == LuaType::STRING && luaG_tostring(L, StackIndex{1}) != ""; }()); 376 REQUIRE([&L = _L]() { return L.doStringAndRet("function end") == "" && lua_gettop(L) == 1 && luaG_type(L, StackIndex{ 1 }) == LuaType::STRING && luaG_tostring(L, StackIndex{ 1 }) != ""; }());
317 // on success doStringAndRet returns the string returned by the script, that is also at the top of the stack 377 // on success doStringAndRet returns the string returned by the script, that is also at the top of the stack
318 ASSERT_TRUE([&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"; }()); 378 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"; }());
319 // if the returned value is not (convertible to) a string, we should get an empty string out of doStringAndRet 379 // if the returned value is not (convertible to) a string, we should get an empty string out of doStringAndRet
320 ASSERT_TRUE([&L = _L]() { return L.doStringAndRet("return function() end") == "" && lua_gettop(L) == 1 && luaG_type(L, StackIndex{1}) == LuaType::FUNCTION && luaG_tostring(L, StackIndex{1}) == ""; }()); 380 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 }) == ""; }());
321} 381}
322 382
323// ################################################################################################# 383// #################################################################################################
324// ################################################################################################# 384// #################################################################################################
325// UnitTestRunner 385// FileRunner
326// ################################################################################################# 386// #################################################################################################
327// ################################################################################################# 387// #################################################################################################
328 388
329UnitTestRunner::UnitTestRunner() 389FileRunner::FileRunner(std::string_view const& where_)
390: LuaState{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ true } }
330{ 391{
331 [[maybe_unused]] std::filesystem::path const _current{ std::filesystem::current_path() }; 392 [[maybe_unused]] std::filesystem::path const _current{ std::filesystem::current_path() };
332 std::filesystem::path _assertPath{ R"(.\lanes\unit_tests\scripts)" }; 393 std::filesystem::path _path{ where_ };
394 root = std::filesystem::canonical(_path).generic_string();
333 // I need to append that path to the list of locations where modules can be required 395 // I need to append that path to the list of locations where modules can be required
334 // so that the scripts can require "_assert" and find _assert.lua (same with "_utils.lua") 396 // so that the legacy scripts can require"assert" and find assert.lua
335 std::string _script{ "package.path = package.path.." }; 397 std::string _script{ "package.path = package.path.." };
336 _script += "';"; 398 _script += "';";
337 _script += std::filesystem::canonical(_assertPath).generic_string(); 399 _script += root;
338 _script += "/?.lua'"; 400 _script += "/?.lua'";
339 std::ignore = L.doString(_script.c_str()); 401 std::ignore = doString(_script.c_str());
340
341 root = std::filesystem::canonical(R"(.\lanes\unit_tests\scripts)").generic_string();
342} 402}
343 403
344// ################################################################################################# 404// #################################################################################################
345 405
346TEST_P(UnitTestRunner, ScriptedTest) 406void FileRunner::performTest(FileRunnerParam const& testParam_)
347{ 407{
348 FileRunnerParam const& _param = GetParam(); 408 INFO(testParam_.script);
349 switch (_param.test) { 409 switch (testParam_.test) {
350 case TestType::AssertNoLuaError: 410 case TestType::AssertNoLuaError:
351 ASSERT_EQ(L.doFile(root, _param.script), LuaError::OK) << L; 411 requireSuccess(root, testParam_.script);
352 break; 412 break;
353 case TestType::AssertNoThrow: 413 case TestType::AssertNoThrow:
354 ASSERT_NO_THROW((std::ignore = L.doFile(root, _param.script), L.close())) << L; 414 REQUIRE_NOTHROW((std::ignore = doFile(root, testParam_.script), close()));
355 break; 415 break;
356 case TestType::AssertThrows: 416 case TestType::AssertThrows:
357 ASSERT_THROW((std::ignore = L.doFile(root, _param.script), L.close()), std::logic_error) << L; 417 REQUIRE_THROWS_AS((std::ignore = doFile(root, testParam_.script), close()), std::logic_error);
358 break; 418 break;
359 } 419 }
360} 420}