From a3b6bc7263946a988bacc4f1534997194d9da147 Mon Sep 17 00:00:00 2001 From: elsid Date: Wed, 20 Oct 2021 16:28:18 +0200 Subject: [PATCH] Fix logic expression for not found value --- apps/openmw_test_suite/CMakeLists.txt | 1 + apps/openmw_test_suite/esmloader/esmdata.cpp | 101 +++++++++++++++++++ components/esmloader/esmdata.cpp | 2 +- 3 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 apps/openmw_test_suite/esmloader/esmdata.cpp diff --git a/apps/openmw_test_suite/CMakeLists.txt b/apps/openmw_test_suite/CMakeLists.txt index 3d19834066..9947f3e233 100644 --- a/apps/openmw_test_suite/CMakeLists.txt +++ b/apps/openmw_test_suite/CMakeLists.txt @@ -52,6 +52,7 @@ if (GTEST_FOUND AND GMOCK_FOUND) sqlite3/transaction.cpp esmloader/load.cpp + esmloader/esmdata.cpp ) source_group(apps\\openmw_test_suite FILES openmw_test_suite.cpp ${UNITTEST_SRC_FILES}) diff --git a/apps/openmw_test_suite/esmloader/esmdata.cpp b/apps/openmw_test_suite/esmloader/esmdata.cpp new file mode 100644 index 0000000000..da78aaa3a2 --- /dev/null +++ b/apps/openmw_test_suite/esmloader/esmdata.cpp @@ -0,0 +1,101 @@ +#include + +#include + +#include +#include +#include +#include + +namespace +{ + using namespace testing; + using namespace EsmLoader; + + struct Params + { + std::string mRefId; + ESM::RecNameInts mType; + std::string mResult; + std::function mPushBack; + }; + + struct EsmLoaderGetModelTest : TestWithParam {}; + + TEST_P(EsmLoaderGetModelTest, shouldReturnFoundModelName) + { + EsmData data; + GetParam().mPushBack(data); + EXPECT_EQ(EsmLoader::getModel(data, GetParam().mRefId, GetParam().mType), GetParam().mResult); + } + + void pushBack(ESM::Activator&& value, EsmData& esmData) + { + esmData.mActivators.push_back(std::move(value)); + } + + void pushBack(ESM::Container&& value, EsmData& esmData) + { + esmData.mContainers.push_back(std::move(value)); + } + + void pushBack(ESM::Door&& value, EsmData& esmData) + { + esmData.mDoors.push_back(std::move(value)); + } + + void pushBack(ESM::Static&& value, EsmData& esmData) + { + esmData.mStatics.push_back(std::move(value)); + } + + template + struct PushBack + { + std::string mId; + std::string mModel; + + void operator()(EsmData& esmData) const + { + T value; + value.mId = mId; + value.mModel = mModel; + pushBack(std::move(value), esmData); + } + }; + + const std::array params = { + Params {"acti_ref_id", ESM::REC_ACTI, "acti_model", PushBack {"acti_ref_id", "acti_model"}}, + Params {"cont_ref_id", ESM::REC_CONT, "cont_model", PushBack {"cont_ref_id", "cont_model"}}, + Params {"door_ref_id", ESM::REC_DOOR, "door_model", PushBack {"door_ref_id", "door_model"}}, + Params {"static_ref_id", ESM::REC_STAT, "static_model", PushBack {"static_ref_id", "static_model"}}, + Params {"acti_ref_id_a", ESM::REC_ACTI, "", PushBack {"acti_ref_id_z", "acti_model"}}, + Params {"cont_ref_id_a", ESM::REC_CONT, "", PushBack {"cont_ref_id_z", "cont_model"}}, + Params {"door_ref_id_a", ESM::REC_DOOR, "", PushBack {"door_ref_id_z", "door_model"}}, + Params {"static_ref_id_a", ESM::REC_STAT, "", PushBack {"static_ref_id_z", "static_model"}}, + Params {"acti_ref_id_z", ESM::REC_ACTI, "", PushBack {"acti_ref_id_a", "acti_model"}}, + Params {"cont_ref_id_z", ESM::REC_CONT, "", PushBack {"cont_ref_id_a", "cont_model"}}, + Params {"door_ref_id_z", ESM::REC_DOOR, "", PushBack {"door_ref_id_a", "door_model"}}, + Params {"static_ref_id_z", ESM::REC_STAT, "", PushBack {"static_ref_id_a", "static_model"}}, + Params {"ref_id", ESM::REC_STAT, "", [] (EsmData&) {}}, + Params {"ref_id", ESM::REC_BOOK, "", [] (EsmData&) {}}, + }; + + INSTANTIATE_TEST_SUITE_P(Params, EsmLoaderGetModelTest, ValuesIn(params)); + + TEST(EsmLoaderGetGameSettingTest, shouldReturnFoundValue) + { + std::vector settings; + ESM::GameSetting setting; + setting.mId = "setting"; + setting.mValue = ESM::Variant(42); + settings.push_back(setting); + EXPECT_EQ(EsmLoader::getGameSetting(settings, "setting"), ESM::Variant(42)); + } + + TEST(EsmLoaderGetGameSettingTest, shouldThrowExceptionWhenNotFound) + { + const std::vector settings; + EXPECT_THROW(EsmLoader::getGameSetting(settings, "setting"), std::runtime_error); + } +} diff --git a/components/esmloader/esmdata.cpp b/components/esmloader/esmdata.cpp index 06f358ea0f..bf2a8675d2 100644 --- a/components/esmloader/esmdata.cpp +++ b/components/esmloader/esmdata.cpp @@ -37,7 +37,7 @@ namespace EsmLoader { const auto it = std::lower_bound(values.begin(), values.end(), refId, LessById {}); - if (it == values.end() && it->mId != refId) + if (it == values.end() || it->mId != refId) return returnAs(std::forward(f)); return std::forward(f)(*it);