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