diff --git a/apps/openmw_tests/CMakeLists.txt b/apps/openmw_tests/CMakeLists.txt index 0c6b0d84df..9b57113110 100644 --- a/apps/openmw_tests/CMakeLists.txt +++ b/apps/openmw_tests/CMakeLists.txt @@ -9,6 +9,7 @@ file(GLOB UNITTEST_SRC_FILES mwworld/test_store.cpp mwworld/testduration.cpp mwworld/testtimestamp.cpp + mwworld/testptr.cpp mwdialogue/test_keywordsearch.cpp diff --git a/apps/openmw_tests/main.cpp b/apps/openmw_tests/main.cpp index fd7d4900c8..6b7298596a 100644 --- a/apps/openmw_tests/main.cpp +++ b/apps/openmw_tests/main.cpp @@ -1,11 +1,28 @@ #include +#include +#include +#include #include +#include + int main(int argc, char* argv[]) { Log::sMinDebugLevel = Debug::getDebugLevel(); + const std::filesystem::path settingsDefaultPath = std::filesystem::path{ OPENMW_PROJECT_SOURCE_DIR } / "files" + / Misc::StringUtils::stringToU8String("settings-default.cfg"); + + Settings::SettingsFileParser parser; + parser.loadSettingsFile(settingsDefaultPath, Settings::Manager::mDefaultSettings); + + Settings::StaticValues::initDefaults(); + + Settings::Manager::mUserSettings = Settings::Manager::mDefaultSettings; + + Settings::StaticValues::init(); + testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } diff --git a/apps/openmw_tests/mwworld/testptr.cpp b/apps/openmw_tests/mwworld/testptr.cpp new file mode 100644 index 0000000000..7bc0bebcec --- /dev/null +++ b/apps/openmw_tests/mwworld/testptr.cpp @@ -0,0 +1,82 @@ +#include "apps/openmw/mwclass/npc.hpp" +#include "apps/openmw/mwworld/esmstore.hpp" +#include "apps/openmw/mwworld/livecellref.hpp" +#include "apps/openmw/mwworld/ptr.hpp" +#include "apps/openmw/mwworld/worldmodel.hpp" + +#include +#include + +#include + +namespace MWWorld +{ + namespace + { + using namespace testing; + + TEST(MWWorldPtrTest, toStringShouldReturnHumanReadableTextRepresentationOfPtrWithNullRef) + { + Ptr ptr; + EXPECT_EQ(ptr.toString(), "null object"); + } + + TEST(MWWorldPtrTest, toStringShouldReturnHumanReadableTextRepresentationOfPtrWithDeletedRef) + { + MWClass::Npc::registerSelf(); + ESM::NPC npc; + npc.blank(); + npc.mId = ESM::RefId::stringRefId("Player"); + ESMStore store; + store.insert(npc); + ESM::CellRef cellRef; + cellRef.blank(); + cellRef.mRefID = npc.mId; + cellRef.mRefNum = ESM::RefNum{ .mIndex = 0x2a, .mContentFile = 0xd }; + LiveCellRef liveCellRef(cellRef, &npc); + liveCellRef.mData.setDeletedByContentFile(true); + Ptr ptr(&liveCellRef); + EXPECT_EQ(ptr.toString(), "deleted object0xd00002a (NPC, \"player\")"); + } + + TEST(MWWorldPtrTest, toStringShouldReturnHumanReadableTextRepresentationOfPtr) + { + MWClass::Npc::registerSelf(); + ESM::NPC npc; + npc.blank(); + npc.mId = ESM::RefId::stringRefId("Player"); + ESMStore store; + store.insert(npc); + ESM::CellRef cellRef; + cellRef.blank(); + cellRef.mRefID = npc.mId; + cellRef.mRefNum = ESM::RefNum{ .mIndex = 0x2a, .mContentFile = 0xd }; + LiveCellRef liveCellRef(cellRef, &npc); + Ptr ptr(&liveCellRef); + EXPECT_EQ(ptr.toString(), "object0xd00002a (NPC, \"player\")"); + } + + TEST(MWWorldPtrTest, underlyingLiveCellRefShouldBeDeregisteredOnDestruction) + { + MWClass::Npc::registerSelf(); + ESM::NPC npc; + npc.blank(); + npc.mId = ESM::RefId::stringRefId("Player"); + ESMStore store; + store.insert(npc); + ESM::ReadersCache readersCache; + WorldModel worldModel(store, readersCache); + ESM::CellRef cellRef; + cellRef.blank(); + cellRef.mRefID = npc.mId; + cellRef.mRefNum = ESM::FormId{ .mIndex = 0x2a, .mContentFile = 0xd }; + { + LiveCellRef liveCellRef(cellRef, &npc); + Ptr ptr(&liveCellRef); + worldModel.registerPtr(ptr); + ASSERT_EQ(worldModel.getPtr(cellRef.mRefNum), ptr); + } + EXPECT_EQ(worldModel.getPtr(cellRef.mRefNum), Ptr()); + } + } +}