Settings manager test

This commit is contained in:
Dan Paulat 2021-10-24 21:57:01 -05:00
parent 1c0140fc98
commit fd0a148d3e
4 changed files with 113 additions and 4 deletions

View file

@ -93,6 +93,11 @@ GeneralSettings::Load(const boost::json::value* json, bool& jsonDirty)
return generalSettings;
}
bool operator==(const GeneralSettings& lhs, const GeneralSettings& rhs)
{
return lhs.p->defaultRadarSite_ == rhs.p->defaultRadarSite_;
}
} // namespace settings
} // namespace qt
} // namespace scwx

View file

@ -34,6 +34,9 @@ public:
static std::shared_ptr<GeneralSettings> Load(const boost::json::value* json,
bool& jsonDirty);
friend bool operator==(const GeneralSettings& lhs,
const GeneralSettings& rhs);
private:
std::unique_ptr<GeneralSettingsImpl> p;
};

View file

@ -0,0 +1,97 @@
#include <scwx/qt/manager/settings_manager.hpp>
#include <filesystem>
#include <fstream>
#include <gtest/gtest.h>
namespace scwx
{
namespace qt
{
namespace manager
{
static const std::string DEFAULT_SETTINGS_FILE =
std::string(SCWX_TEST_DATA_DIR) + "/json/settings/settings-default.json";
static const std::string TEMP_SETTINGS_FILE =
std::string(SCWX_TEST_DATA_DIR) + "/json/settings/settings-temp.json";
class DefaultSettingsTest : public testing::TestWithParam<std::string>
{
};
void VerifyDefaults()
{
std::shared_ptr<settings::GeneralSettings> defaultGeneralSettings =
settings::GeneralSettings::Create();
EXPECT_EQ(*defaultGeneralSettings, *SettingsManager::general_settings());
}
void CompareFiles(const std::string& file1, const std::string& file2)
{
std::ifstream ifs1 {file1};
std::stringstream buffer1;
buffer1 << buffer1.rdbuf();
std::ifstream ifs2 {file2};
std::stringstream buffer2;
buffer2 << buffer2.rdbuf();
EXPECT_EQ(buffer1.str(), buffer2.str());
}
TEST(SettingsManager, CreateJson)
{
std::string filename {TEMP_SETTINGS_FILE};
// Verify file doesn't exist prior to test start
EXPECT_EQ(std::filesystem::exists(filename), false);
SettingsManager::ReadSettings(filename);
EXPECT_EQ(std::filesystem::exists(filename), true);
VerifyDefaults();
CompareFiles(filename, DEFAULT_SETTINGS_FILE);
std::filesystem::remove(filename);
EXPECT_EQ(std::filesystem::exists(filename), false);
}
TEST(SettingsManager, SettingsKeax)
{
std::string filename(std::string(SCWX_TEST_DATA_DIR) +
"/json/settings/settings-keax.json");
SettingsManager::ReadSettings(filename);
EXPECT_EQ(SettingsManager::general_settings()->default_radar_site(), "KEAX");
}
TEST_P(DefaultSettingsTest, DefaultSettings)
{
std::string sourceFile(std::string(SCWX_TEST_DATA_DIR) + "/json/settings/" +
GetParam());
std::string filename {TEMP_SETTINGS_FILE};
std::filesystem::copy_file(sourceFile, filename);
SettingsManager::ReadSettings(filename);
VerifyDefaults();
CompareFiles(filename, DEFAULT_SETTINGS_FILE);
std::filesystem::remove(filename);
}
INSTANTIATE_TEST_SUITE_P(SettingsManager,
DefaultSettingsTest,
testing::Values("settings-bad-types.json",
"settings-empty-groups.json",
"settings-empty-object.json"));
} // namespace manager
} // namespace qt
} // namespace scwx

View file

@ -9,17 +9,20 @@ find_package(GTest)
set(SRC_MAIN source/scwx/wxtest.cpp)
set(SRC_COMMON_TESTS source/scwx/common/color_table.test.cpp)
set(SRC_QT_MANAGER_TESTS source/scwx/qt/manager/settings_manager.test.cpp)
set(SRC_UTIL_TESTS source/scwx/util/rangebuf.test.cpp
source/scwx/util/vectorbuf.test.cpp)
set(SRC_WSR88D_TESTS source/scwx/wsr88d/ar2v_file.test.cpp)
add_executable(wxtest ${SRC_MAIN}
${SRC_COMMON_TESTS}
${SRC_QT_MANAGER_TESTS}
${SRC_UTIL_TESTS}
${SRC_WSR88D_TESTS})
source_group("Source Files\\main" FILES ${SRC_MAIN})
source_group("Source Files\\common" FILES ${SRC_COMMON_TESTS})
source_group("Source Files\\qt\\manager" FILES ${SRC_QT_MANAGER_TESTS})
source_group("Source Files\\util" FILES ${SRC_UTIL_TESTS})
source_group("Source Files\\wsr88d" FILES ${SRC_WSR88D_TESTS})
@ -38,4 +41,5 @@ target_compile_definitions(wxtest PRIVATE SCWX_TEST_DATA_DIR="${SCWX_DIR}/test/d
gtest_discover_tests(wxtest)
target_link_libraries(wxtest GTest::gtest
scwx-qt
wxdata)