-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathUtilityTest.cpp
More file actions
201 lines (173 loc) · 5.53 KB
/
UtilityTest.cpp
File metadata and controls
201 lines (173 loc) · 5.53 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <gtest/gtest.h>
#include <windows.h>
#include <optional>
#include <filesystem>
#include <fstream>
#include "Utility.h"
namespace UtilityTest
{
// Test GetXFromLPARAM with sample LPARAM
TEST(CUtilityTest, GetXFromLPARAM)
{
CUtility util;
LPARAM lp = MAKELPARAM(50, 100);
short x = util.GetXFromLPARAM(lp);
EXPECT_EQ(x, 50);
}
// Test GetYFromLPARAM with sample LPARAM
TEST(CUtilityTest, GetYFromLPARAM)
{
CUtility util;
LPARAM lp = MAKELPARAM(50, 100);
short y = util.GetYFromLPARAM(lp);
EXPECT_EQ(y, 100);
}
// Test GetEditCtrlText with nullptr
TEST(CUtilityTest, GetEditCtrlText_NullHandle)
{
auto text = CUtility::GetEditCtrlText(nullptr);
EXPECT_EQ(text, L"");
}
// Test GetCheckboxStatus with nullptr
TEST(CUtilityTest, GetCheckboxStatus_NullHandle)
{
bool status = CUtility::GetCheckboxStatus(nullptr);
EXPECT_FALSE(status);
}
// Test CreateDir functionality
TEST(CUtilityTest, CreateDir_Success)
{
std::wstring testDir = std::filesystem::temp_directory_path().wstring() + L"\\TestDir_Utility";
bool created = CUtility::CreateDir(testDir);
EXPECT_TRUE(created);
// Cleanup
if (std::filesystem::exists(testDir))
{
std::filesystem::remove(testDir);
}
}
// Test Copy functionality
TEST(CUtilityTest, Copy_Success)
{
std::filesystem::path srcFile = std::filesystem::temp_directory_path() / "test_utility.tmp";
auto dstFile = std::filesystem::temp_directory_path() / "test_copy.tmp";
std::ofstream testFile(srcFile);
testFile << "test content";
testFile.close();
if (!std::filesystem::exists(srcFile))
{
// Something bad, failed to create file, hence skip the test
GTEST_SKIP() << "Failed to create source file for copy test.";
}
bool copied = CUtility::Copy(srcFile.wstring(), dstFile.wstring());
EXPECT_TRUE(copied);
EXPECT_TRUE(std::filesystem::exists(dstFile));
// Cleanup
if (std::filesystem::exists(srcFile))
{
std::filesystem::remove(srcFile);
}
if (std::filesystem::exists(dstFile))
{
std::filesystem::remove(dstFile);
}
}
// Test DirExist with a valid directory
TEST(CUtilityTest, DirExist_ValidDir)
{
CUtility util;
bool exists = util.DirExist(L"C:\\Windows");
EXPECT_TRUE(exists);
}
// Test DirExist with an invalid directory
TEST(CUtilityTest, DirExist_InvalidDir)
{
CUtility util;
bool exists = util.DirExist(L"Z:\\InvalidDir");
EXPECT_FALSE(exists);
}
// Test FileExist with a valid file
TEST(CUtilityTest, FileExist_ValidFile)
{
CUtility util;
bool exists = util.FileExist(L"C:\\Windows\\notepad.exe");
EXPECT_TRUE(exists);
}
// Test FileExist with an invalid file
TEST(CUtilityTest, FileExist_InvalidFile)
{
CUtility util;
bool exists = util.FileExist(L"C:\\InvalidPath\\file.exe");
EXPECT_FALSE(exists);
}
// Test FileSize with a valid file
TEST(CUtilityTest, FileSize_ValidFile)
{
CUtility util;
long size = util.FileSize(L"C:\\Windows\\notepad.exe");
EXPECT_GT(size, 0);
}
// Test FileSize with an invalid file
TEST(CUtilityTest, FileSize_InvalidFile)
{
CUtility util;
EXPECT_THROW(util.FileSize(L"C:\\InvalidPath\\file.exe"), std::filesystem::filesystem_error);
}
// Test GetFileName with extension
TEST(CUtilityTest, GetFileName_WithExtension)
{
CUtility util;
std::wstring fileName = util.GetFileName(L"C:\\path\\file.txt", true);
EXPECT_EQ(fileName, L"file.txt");
}
// Test GetFileName without extension
TEST(CUtilityTest, GetFileName_WithoutExtension)
{
CUtility util;
std::wstring fileName = util.GetFileName(L"C:\\path\\file.txt", false);
EXPECT_EQ(fileName, L"file");
}
// Test GetFileExtension with a valid file name
TEST(CUtilityTest, GetFileExtension_ValidFileName)
{
CUtility util;
std::wstring ext = util.GetFileExtension(L"file.txt");
EXPECT_EQ(ext, L".txt");
}
// Test GetTempFilePath
TEST(CUtilityTest, GetTempFilePath)
{
CUtility util;
std::wstring tempPath = util.GetTempFilePath();
EXPECT_FALSE(tempPath.empty());
}
// Test IsNumber with a valid number string
TEST(CUtilityTest, IsNumber_ValidNumber)
{
CUtility util;
bool isNumber = util.IsNumber(L"12345");
EXPECT_TRUE(isNumber);
}
// Test IsNumber with an invalid number string
TEST(CUtilityTest, IsNumber_InvalidNumber)
{
CUtility util;
bool isNumber = util.IsNumber(L"123a45");
EXPECT_FALSE(isNumber);
}
// Test GetNumber with valid number string
TEST(CUtilityTest, GetNumber_ValidNumber)
{
CUtility util;
auto number = util.GetNumber(L"12345");
ASSERT_TRUE(number.has_value());
EXPECT_EQ(number.value(), 12345);
}
// Test GetNumber with invalid number string
TEST(CUtilityTest, GetNumber_InvalidNumber)
{
CUtility util;
auto number = util.GetNumber(L"123a45");
EXPECT_FALSE(number.has_value());
}
} // namespace UtilityTest