From 1fbf50fb133f50eefde07a5c4d0ee31db1a9a4fe Mon Sep 17 00:00:00 2001 From: elsid Date: Sun, 23 Apr 2023 14:46:30 +0200 Subject: [PATCH 1/2] Use PROJECT_SOURCE_DIR in tests --- apps/openmw_test_suite/CMakeLists.txt | 2 +- apps/openmw_test_suite/toutf8/toutf8.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/apps/openmw_test_suite/CMakeLists.txt b/apps/openmw_test_suite/CMakeLists.txt index 34892905c1..7af6520f35 100644 --- a/apps/openmw_test_suite/CMakeLists.txt +++ b/apps/openmw_test_suite/CMakeLists.txt @@ -115,7 +115,7 @@ file(DOWNLOAD target_compile_definitions(openmw_test_suite PRIVATE OPENMW_DATA_DIR=u8"${CMAKE_CURRENT_BINARY_DIR}/data" - OPENMW_TEST_SUITE_SOURCE_DIR=u8"${CMAKE_CURRENT_SOURCE_DIR}") + OPENMW_PROJECT_SOURCE_DIR=u8"${PROJECT_SOURCE_DIR}") if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.16 AND MSVC) target_precompile_headers(openmw_test_suite PRIVATE diff --git a/apps/openmw_test_suite/toutf8/toutf8.cpp b/apps/openmw_test_suite/toutf8/toutf8.cpp index 202c0e4890..f189294cf2 100644 --- a/apps/openmw_test_suite/toutf8/toutf8.cpp +++ b/apps/openmw_test_suite/toutf8/toutf8.cpp @@ -6,8 +6,8 @@ #include #include -#ifndef OPENMW_TEST_SUITE_SOURCE_DIR -#define OPENMW_TEST_SUITE_SOURCE_DIR "" +#ifndef OPENMW_PROJECT_SOURCE_DIR +#define OPENMW_PROJECT_SOURCE_DIR "." #endif namespace @@ -26,7 +26,7 @@ namespace { std::ifstream file; file.exceptions(std::ios::failbit | std::ios::badbit); - file.open(std::filesystem::path{ OPENMW_TEST_SUITE_SOURCE_DIR } / "toutf8" / "data" + file.open(std::filesystem::path{ OPENMW_PROJECT_SOURCE_DIR } / "apps" / "openmw_test_suite" / "toutf8" / "data" / Misc::StringUtils::stringToU8String(fileName)); std::stringstream buffer; buffer << file.rdbuf(); From 499d086cc117f9a1799fc3bc805a61d7fefa4d08 Mon Sep 17 00:00:00 2001 From: elsid Date: Sun, 23 Apr 2023 15:15:33 +0200 Subject: [PATCH 2/2] Add tests for Settings::Values --- apps/openmw_test_suite/CMakeLists.txt | 1 + .../openmw_test_suite/settings/testvalues.cpp | 85 +++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 apps/openmw_test_suite/settings/testvalues.cpp diff --git a/apps/openmw_test_suite/CMakeLists.txt b/apps/openmw_test_suite/CMakeLists.txt index 7af6520f35..9f4df61368 100644 --- a/apps/openmw_test_suite/CMakeLists.txt +++ b/apps/openmw_test_suite/CMakeLists.txt @@ -57,6 +57,7 @@ file(GLOB UNITTEST_SRC_FILES settings/parser.cpp settings/shadermanager.cpp + settings/testvalues.cpp shader/parsedefines.cpp shader/parsefors.cpp diff --git a/apps/openmw_test_suite/settings/testvalues.cpp b/apps/openmw_test_suite/settings/testvalues.cpp new file mode 100644 index 0000000000..38154ae039 --- /dev/null +++ b/apps/openmw_test_suite/settings/testvalues.cpp @@ -0,0 +1,85 @@ +#include "components/misc/strings/conversion.hpp" +#include "components/settings/parser.hpp" +#include "components/settings/values.hpp" + +#include +#include + +#ifndef OPENMW_PROJECT_SOURCE_DIR +#define OPENMW_PROJECT_SOURCE_DIR "." +#endif + +namespace Settings +{ + namespace + { + using namespace testing; + + struct SettingsValuesTest : Test + { + const std::filesystem::path mSettingsDefaultPath = std::filesystem::path{ OPENMW_PROJECT_SOURCE_DIR } + / "files" / Misc::StringUtils::stringToU8String("settings-default.cfg"); + + SettingsValuesTest() + { + Manager::mDefaultSettings.clear(); + Manager::mUserSettings.clear(); + Manager::mChangedSettings.clear(); + SettingsFileParser parser; + parser.loadSettingsFile(mSettingsDefaultPath, Manager::mDefaultSettings); + } + }; + + TEST_F(SettingsValuesTest, shouldLoadFromSettingsManager) + { + Values values; + EXPECT_EQ(values.mCamera.mFieldOfView.get(), 60); + } + + TEST_F(SettingsValuesTest, constructorShouldThrowExceptionOnMissingSetting) + { + Manager::mDefaultSettings.erase({ "Camera", "field of view" }); + EXPECT_THROW([] { Values values; }(), std::runtime_error); + } + + TEST_F(SettingsValuesTest, constructorShouldSanitize) + { + Manager::mUserSettings[std::make_pair("Camera", "field of view")] = "-1"; + Values values; + EXPECT_EQ(values.mCamera.mFieldOfView.get(), 1); + } + + TEST_F(SettingsValuesTest, moveConstructorShouldSetDefaults) + { + Values defaultValues; + Manager::mUserSettings.emplace(std::make_pair("Camera", "field of view"), "61"); + Values values(std::move(defaultValues)); + EXPECT_EQ(values.mCamera.mFieldOfView.get(), 61); + values.mCamera.mFieldOfView.reset(); + EXPECT_EQ(values.mCamera.mFieldOfView.get(), 60); + } + + TEST_F(SettingsValuesTest, moveConstructorShouldSanitize) + { + Values defaultValues; + Manager::mUserSettings[std::make_pair("Camera", "field of view")] = "-1"; + Values values(std::move(defaultValues)); + EXPECT_EQ(values.mCamera.mFieldOfView.get(), 1); + } + + TEST_F(SettingsValuesTest, setShouldChangeManagerUserSettings) + { + Values values; + values.mCamera.mFieldOfView.set(42); + EXPECT_EQ(Manager::mUserSettings.at({ "Camera", "field of view" }), "42"); + EXPECT_THAT(Manager::mChangedSettings, ElementsAre(std::make_pair("Camera", "field of view"))); + } + + TEST_F(SettingsValuesTest, setShouldNotChangeManagerChangedSettingsForNoChange) + { + Values values; + values.mCamera.mFieldOfView.set(values.mCamera.mFieldOfView.get()); + EXPECT_THAT(Manager::mChangedSettings, ElementsAre()); + } + } +}