-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.cpp
More file actions
185 lines (141 loc) · 6.07 KB
/
tests.cpp
File metadata and controls
185 lines (141 loc) · 6.07 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
#include <gtest/gtest.h>
#include <iostream>
#include <string>
#include <vector>
#include <windows.h>
// Include the actual implementation code
#include "cli_args_display.hpp"
// For running tests outside the main application
std::string wstring_to_string(const std::wstring& wstr)
{
if (wstr.empty())
return std::string();
int size_needed =
WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), static_cast<int>(wstr.size()), nullptr, 0, nullptr, nullptr);
std::string strTo(size_needed, 0);
WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), static_cast<int>(wstr.size()), &strTo[0], size_needed, nullptr,
nullptr);
return strTo;
}
// Test fixture for CLI argument formatting tests
class CliArgsTest : public ::testing::Test
{
protected:
// Helper for better error messages with wide strings
void ExpectWideStringEq(const std::wstring& expected, const std::wstring& actual)
{
EXPECT_EQ(wstring_to_string(expected), wstring_to_string(actual))
<< "Expected: " << wstring_to_string(expected) << std::endl
<< "Actual: " << wstring_to_string(actual);
}
};
// Test for empty arguments
TEST_F(CliArgsTest, EmptyArguments)
{
std::vector<std::wstring> args = {};
std::wstring expectedHeader = L"No arguments were received.";
std::wstring expectedArgsLine = L"";
std::wstring actualHeader = BuildCliHeaderText(args);
std::wstring actualArgsLine = BuildCliArgsText(args);
ExpectWideStringEq(expectedHeader, actualHeader);
ExpectWideStringEq(expectedArgsLine, actualArgsLine);
}
// Test for a single argument
TEST_F(CliArgsTest, SingleArgument)
{
std::vector<std::wstring> args = {L"first"};
std::wstring expectedHeader = L"Received the following arguments:";
std::wstring expectedArgsLine = L"first";
std::wstring actualHeader = BuildCliHeaderText(args);
std::wstring actualArgsLine = BuildCliArgsText(args);
ExpectWideStringEq(expectedHeader, actualHeader);
ExpectWideStringEq(expectedArgsLine, actualArgsLine);
}
// Test for multiple arguments
TEST_F(CliArgsTest, MultipleArguments)
{
std::vector<std::wstring> args = {L"one", L"two", L"three"};
std::wstring expectedHeader = L"Received the following arguments:";
std::wstring expectedArgsLine = L"one two three";
std::wstring actualHeader = BuildCliHeaderText(args);
std::wstring actualArgsLine = BuildCliArgsText(args);
ExpectWideStringEq(expectedHeader, actualHeader);
ExpectWideStringEq(expectedArgsLine, actualArgsLine);
}
// Test for arguments with spaces
TEST_F(CliArgsTest, ArgumentsWithSpaces)
{
std::vector<std::wstring> args = {L"hello world", L"arg"};
std::wstring expectedHeader = L"Received the following arguments:";
std::wstring expectedArgsLine = L"\"hello world\" arg";
std::wstring actualHeader = BuildCliHeaderText(args);
std::wstring actualArgsLine = BuildCliArgsText(args);
ExpectWideStringEq(expectedHeader, actualHeader);
ExpectWideStringEq(expectedArgsLine, actualArgsLine);
}
// Test for non-ASCII arguments
TEST_F(CliArgsTest, NonAsciiArguments)
{
std::vector<std::wstring> args = {L"こんにちは", L"世界"};
std::wstring expectedHeader = L"Received the following arguments:";
std::wstring expectedArgsLine = L"こんにちは 世界";
std::wstring actualHeader = BuildCliHeaderText(args);
std::wstring actualArgsLine = BuildCliArgsText(args);
ExpectWideStringEq(expectedHeader, actualHeader);
ExpectWideStringEq(expectedArgsLine, actualArgsLine);
}
// Test for paths with spaces
TEST_F(CliArgsTest, PathWithSpaces)
{
std::vector<std::wstring> args = {L"C:\\Program Files\\App", L"-f"};
std::wstring expectedHeader = L"Received the following arguments:";
std::wstring expectedArgsLine = L"\"C:\\Program Files\\App\" -f";
std::wstring actualHeader = BuildCliHeaderText(args);
std::wstring actualArgsLine = BuildCliArgsText(args);
ExpectWideStringEq(expectedHeader, actualHeader);
ExpectWideStringEq(expectedArgsLine, actualArgsLine);
}
// Test for empty string in arguments
TEST_F(CliArgsTest, EmptyStringArgument)
{
std::vector<std::wstring> args = {L"", L"empty-arg"};
std::wstring expectedHeader = L"Received the following arguments:";
std::wstring expectedArgsLine = L"\"\" empty-arg";
std::wstring actualHeader = BuildCliHeaderText(args);
std::wstring actualArgsLine = BuildCliArgsText(args);
ExpectWideStringEq(expectedHeader, actualHeader);
ExpectWideStringEq(expectedArgsLine, actualArgsLine);
}
// Test for argument with quotes
TEST_F(CliArgsTest, ArgumentWithQuotes)
{
std::vector<std::wstring> args = {L"argument with \"quotes\""};
std::wstring expectedHeader = L"Received the following arguments:";
std::wstring expectedArgsLine = L"\"argument with \"quotes\"\"";
std::wstring actualHeader = BuildCliHeaderText(args);
std::wstring actualArgsLine = BuildCliArgsText(args);
ExpectWideStringEq(expectedHeader, actualHeader);
ExpectWideStringEq(expectedArgsLine, actualArgsLine);
}
// Test for single argument with space - no trailing space
TEST_F(CliArgsTest, SingleArgumentWithSpaceNoTrailingSpace)
{
std::vector<std::wstring> args = {L"single arg with space"};
std::wstring expectedHeader = L"Received the following arguments:";
std::wstring expectedArgsLine = L"\"single arg with space\"";
std::wstring actualHeader = BuildCliHeaderText(args);
std::wstring actualArgsLine = BuildCliArgsText(args);
ExpectWideStringEq(expectedHeader, actualHeader);
ExpectWideStringEq(expectedArgsLine, actualArgsLine);
}
// Test for arguments containing tabs or newlines
TEST_F(CliArgsTest, ArgumentWithWhitespaceCharacters)
{
std::vector<std::wstring> args = {L"tab\tnewline\n", L"arg"};
std::wstring expectedHeader = L"Received the following arguments:";
std::wstring expectedArgsLine = L"\"tab\tnewline\n\" arg";
std::wstring actualHeader = BuildCliHeaderText(args);
std::wstring actualArgsLine = BuildCliArgsText(args);
ExpectWideStringEq(expectedHeader, actualHeader);
ExpectWideStringEq(expectedArgsLine, actualArgsLine);
}