1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Add basic unittests for LBMManager

This commit is contained in:
sfan5 2025-03-23 20:13:24 +01:00
parent 7b746d21f9
commit f63436c8d3
2 changed files with 83 additions and 0 deletions

View file

@ -16,6 +16,7 @@ set (UNITTEST_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/test_irr_matrix4.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_irr_rotation.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_logging.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_lbmmanager.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_lua.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_map.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_mapblock.cpp

View file

@ -0,0 +1,82 @@
// Luanti
// SPDX-License-Identifier: LGPL-2.1-or-later
// Copyright (C) 2025 sfan5
#include "test.h"
#include <sstream>
#include "serverenvironment.h"
class TestLBMManager : public TestBase
{
public:
TestLBMManager() { TestManager::registerTestModule(this); }
const char *getName() { return "TestLBMManager"; }
void runTests(IGameDef *gamedef);
void testNew(IGameDef *gamedef);
void testExisting(IGameDef *gamedef);
void testDiscard(IGameDef *gamedef);
};
static TestLBMManager g_test_instance;
void TestLBMManager::runTests(IGameDef *gamedef)
{
TEST(testNew, gamedef);
TEST(testExisting, gamedef);
TEST(testDiscard, gamedef);
}
namespace {
struct FakeLBM : LoadingBlockModifierDef {
FakeLBM(const std::string &name, bool every_load) {
this->name = name;
this->run_at_every_load = every_load;
trigger_contents.emplace_back("air");
}
};
}
void TestLBMManager::testNew(IGameDef *gamedef)
{
LBMManager mgr;
mgr.addLBMDef(new FakeLBM(":foo:bar", false));
mgr.addLBMDef(new FakeLBM("not:this", true));
mgr.loadIntroductionTimes("", gamedef, 1234);
auto str = mgr.createIntroductionTimesString();
// name of first lbm should have been stripped
// the second should not appear at all
UASSERTEQ(auto, str, "foo:bar~1234;");
}
void TestLBMManager::testExisting(IGameDef *gamedef)
{
LBMManager mgr;
mgr.addLBMDef(new FakeLBM("foo:bar", false));
// colon should also be stripped when loading (due to old versions)
mgr.loadIntroductionTimes(":foo:bar~22;", gamedef, 1234);
auto str = mgr.createIntroductionTimesString();
UASSERTEQ(auto, str, "foo:bar~22;");
}
void TestLBMManager::testDiscard(IGameDef *gamedef)
{
LBMManager mgr;
// LBMs that no longer exist are dropped
mgr.loadIntroductionTimes("some:thing~2;", gamedef, 10);
auto str = mgr.createIntroductionTimesString();
UASSERTEQ(auto, str, "");
}
// We should also test LBMManager::applyLBMs in the future.