|
| 1 | +#include <gtest/gtest.h> |
| 2 | + |
| 3 | +#include <memory> |
| 4 | +#include <filesystem> |
| 5 | + |
| 6 | +#include "Profile.h" |
| 7 | + |
| 8 | +namespace ProfileSettingTests |
| 9 | +{ |
| 10 | + class ProfileSettingEx : public ProfileSetting |
| 11 | + { |
| 12 | + public: |
| 13 | + ProfileSettingEx(const std::wstring &path) |
| 14 | + : ProfileSetting(path) |
| 15 | + { |
| 16 | + // any left over from previous run |
| 17 | + RemoveProfileFile(); |
| 18 | + } |
| 19 | + |
| 20 | + ~ProfileSettingEx() |
| 21 | + { |
| 22 | + RemoveProfileFile(); |
| 23 | + } |
| 24 | + |
| 25 | + bool ReadValue(const std::wstring §ion, const std::wstring &key, int &retVal, int defaultVal = 0) const |
| 26 | + { |
| 27 | + return Profile::ReadValue(section, key, retVal, defaultVal); |
| 28 | + } |
| 29 | + |
| 30 | + bool ReadValue(const std::wstring §ion, const std::wstring &key, std::wstring &retVal, const std::wstring &defaultVal = {}) const |
| 31 | + { |
| 32 | + return Profile::ReadValue(section, key, retVal, defaultVal); |
| 33 | + } |
| 34 | + |
| 35 | + bool WriteValue(const std::wstring §ion, const std::wstring &key, int value) const |
| 36 | + { |
| 37 | + return Profile::WriteValue(section, key, value); |
| 38 | + } |
| 39 | + |
| 40 | + bool WriteValue(const std::wstring §ion, const std::wstring &key, const std::wstring &value) const |
| 41 | + { |
| 42 | + return Profile::WriteValue(section, key, value); |
| 43 | + } |
| 44 | + |
| 45 | + private: |
| 46 | + void RemoveProfileFile() |
| 47 | + { |
| 48 | + std::error_code ec {}; |
| 49 | + std::filesystem::remove(m_ProfileFilePath, ec); |
| 50 | + } |
| 51 | + }; |
| 52 | + |
| 53 | + class ProfileTest : public ::testing::Test |
| 54 | + { |
| 55 | + protected: |
| 56 | + std::unique_ptr<ProfileSettingEx> m_pProfile; |
| 57 | + |
| 58 | + void SetUp() override |
| 59 | + { |
| 60 | + std::wstring exePath; |
| 61 | + wchar_t path[MAX_PATH]; |
| 62 | + DWORD size = GetModuleFileName(nullptr, path, MAX_PATH); |
| 63 | + if (size) |
| 64 | + { |
| 65 | + exePath = path; |
| 66 | + size_t pos = exePath.find_last_of(L"\\/"); |
| 67 | + if (pos != std::wstring::npos) |
| 68 | + { |
| 69 | + // Remove the executable name, leave only the directory |
| 70 | + exePath = exePath.substr(0, pos + 1); |
| 71 | + } |
| 72 | + } |
| 73 | + exePath += L"test_profile.ini"; |
| 74 | + |
| 75 | + m_pProfile = std::make_unique<ProfileSettingEx>(exePath); |
| 76 | + } |
| 77 | + |
| 78 | + void TearDown() override {} |
| 79 | + }; |
| 80 | + |
| 81 | + TEST_F(ProfileTest, ReadValueInt_Default) |
| 82 | + { |
| 83 | + int result = 0; |
| 84 | + EXPECT_TRUE(m_pProfile->ReadValue(L"Settings", L"SomeIntegerKey", result, 42)); |
| 85 | + EXPECT_EQ(result, 42); |
| 86 | + } |
| 87 | + |
| 88 | + TEST_F(ProfileTest, ReadValueInt_Positive) |
| 89 | + { |
| 90 | + int result = 0; |
| 91 | + EXPECT_TRUE(m_pProfile->WriteValue(L"Settings", L"SomeIntegerKey", 100)); |
| 92 | + EXPECT_TRUE(m_pProfile->ReadValue(L"Settings", L"SomeIntegerKey", result)); |
| 93 | + EXPECT_EQ(result, 100); |
| 94 | + } |
| 95 | + |
| 96 | + TEST_F(ProfileTest, ReadValueInt_Missing) |
| 97 | + { |
| 98 | + int result = 0; |
| 99 | + EXPECT_TRUE(m_pProfile->ReadValue(L"Settings", L"SomeIntegerKey", result)); |
| 100 | + EXPECT_EQ(result, 0); |
| 101 | + } |
| 102 | + |
| 103 | + TEST_F(ProfileTest, ReadValueString_Default) |
| 104 | + { |
| 105 | + std::wstring result; |
| 106 | + EXPECT_TRUE(m_pProfile->ReadValue(L"Settings", L"SomeStringKey", result, L"default")); |
| 107 | + EXPECT_EQ(result, L"default"); |
| 108 | + } |
| 109 | + |
| 110 | + TEST_F(ProfileTest, ReadValueString_Positive) |
| 111 | + { |
| 112 | + std::wstring result; |
| 113 | + EXPECT_TRUE(m_pProfile->WriteValue(L"Settings", L"SomeStringKey", L"It will be written there.")); |
| 114 | + EXPECT_TRUE(m_pProfile->ReadValue(L"Settings", L"SomeStringKey", result)); |
| 115 | + EXPECT_EQ(result, L"It will be written there."); |
| 116 | + } |
| 117 | + |
| 118 | + TEST_F(ProfileTest, ReadValueString_Missing) |
| 119 | + { |
| 120 | + std::wstring result; |
| 121 | + EXPECT_TRUE(m_pProfile->ReadValue(L"Settings", L"SomeStringKey", result)); |
| 122 | + EXPECT_EQ(result, L""); |
| 123 | + } |
| 124 | + |
| 125 | + TEST_F(ProfileTest, GetSettings_Defualt) |
| 126 | + { |
| 127 | + Setting setting {}; |
| 128 | + EXPECT_TRUE(m_pProfile->GetSettings(setting)); |
| 129 | + |
| 130 | + EXPECT_EQ(setting.lineEnding, LineEnding::AUTO); |
| 131 | + |
| 132 | + EXPECT_EQ(setting.lineFormat, LineFormat::DEFAULT); |
| 133 | + |
| 134 | + EXPECT_EQ(setting.indent.len, 4u); |
| 135 | + EXPECT_EQ(setting.indent.style, IndentStyle::AUTO); |
| 136 | + |
| 137 | + EXPECT_EQ(setting.bFollowCurrentTab, false); |
| 138 | + EXPECT_EQ(setting.bAutoFormat, false); |
| 139 | + EXPECT_EQ(setting.bUseJsonHighlight, true); |
| 140 | + |
| 141 | + EXPECT_EQ(setting.parseOptions.bIgnoreComment, true); |
| 142 | + EXPECT_EQ(setting.parseOptions.bIgnoreTrailingComma, true); |
| 143 | + EXPECT_EQ(setting.parseOptions.bReplaceUndefined, false); |
| 144 | + } |
| 145 | + |
| 146 | + TEST_F(ProfileTest, SetSettings_Positive) |
| 147 | + { |
| 148 | + Setting expected, actual; |
| 149 | + expected.lineEnding = LineEnding::WINDOWS; |
| 150 | + expected.lineFormat, LineFormat::SINGLELINE; |
| 151 | + expected.indent.len = 2u; |
| 152 | + expected.indent.style = IndentStyle::TAB; |
| 153 | + |
| 154 | + expected.bAutoFormat = true; |
| 155 | + expected.bFollowCurrentTab = true; |
| 156 | + expected.bAutoFormat = true; |
| 157 | + expected.bUseJsonHighlight = false; |
| 158 | + |
| 159 | + expected.parseOptions.bIgnoreComment = false; |
| 160 | + expected.parseOptions.bIgnoreTrailingComma = false; |
| 161 | + expected.parseOptions.bReplaceUndefined = true; |
| 162 | + |
| 163 | + EXPECT_TRUE(m_pProfile->SetSettings(expected)); |
| 164 | + EXPECT_TRUE(m_pProfile->GetSettings(actual)); |
| 165 | + |
| 166 | + EXPECT_EQ(actual.lineEnding, expected.lineEnding); |
| 167 | + |
| 168 | + EXPECT_EQ(actual.lineFormat, expected.lineFormat); |
| 169 | + |
| 170 | + EXPECT_EQ(actual.indent.len, expected.indent.len); |
| 171 | + EXPECT_EQ(actual.indent.style, expected.indent.style); |
| 172 | + |
| 173 | + EXPECT_EQ(actual.bFollowCurrentTab, expected.bFollowCurrentTab); |
| 174 | + EXPECT_EQ(actual.bAutoFormat, expected.bAutoFormat); |
| 175 | + EXPECT_EQ(actual.bUseJsonHighlight, expected.bUseJsonHighlight); |
| 176 | + |
| 177 | + EXPECT_EQ(actual.parseOptions.bIgnoreComment, expected.parseOptions.bIgnoreComment); |
| 178 | + EXPECT_EQ(actual.parseOptions.bIgnoreTrailingComma, expected.parseOptions.bIgnoreTrailingComma); |
| 179 | + EXPECT_EQ(actual.parseOptions.bReplaceUndefined, expected.parseOptions.bReplaceUndefined); |
| 180 | + } |
| 181 | +} // namespace ProfileSettingTests |
0 commit comments