-
Notifications
You must be signed in to change notification settings - Fork 0
/
StripWSTest.cpp
219 lines (184 loc) · 5.81 KB
/
StripWSTest.cpp
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#if !defined(CMDLINEUTIL_TEST_MODE)
#define CMDLINEUTIL_TEST_MODE
#endif
#include "Exceptions.h"
#include "StripWS.h"
#include "TestUtil.h"
#include "Utils.h"
#include <algorithm>
#include <boost/range/algorithm_ext/for_each.hpp>
#include <boost/range/algorithm/sort.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/test/data/test_case.hpp>
#include <sstream>
#include <vector>
namespace b = ::boost;
namespace fs = ::std::filesystem;
namespace ut = ::boost::unit_test;
namespace utd = ::boost::unit_test::data;
using ::std::begin;
using ::std::end;
using ::std::istringstream;
using ::std::ostringstream;
using ::std::string;
using PathList = ::std::vector<fs::path>;
BOOST_AUTO_TEST_SUITE(StripWSTestSuite)
BOOST_AUTO_TEST_SUITE(CmdLineParseFailTestSuite)
static char const*const k_args00[] = { "stripws" };
static char const*const k_args01[] = { "stripws", "-?" };
static char const*const k_args02[] = { "stripws", "-h" };
static char const*const k_args03[] = { "stripws", "-help" };
static char const*const k_args04[] = { "stripws", "-s" };
static CmdLineParseFailTestCase const k_testCases[] =
{
{ k_args00, ".*no files.*" },
{ k_args01, "^$" },
{ k_args02, "^$" },
{ k_args03, "^$" },
{ k_args04, ".*no files.*" },
};
BOOST_DATA_TEST_CASE(cmdLineParseFailTest, utd::make(k_testCases), tc)
{
BOOST_CHECK_EXCEPTION(StripWS(tc.m_args), CmdLineError,
[&tc](const CmdLineError& ex) { return tc.doesExMatch(ex); });
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE(CmdLineParseOkTestSuite)
struct CmdLineParseOkTestCase : public CmdLineParseTestCase
{
template<::std::size_t N, ::std::size_t M>
CmdLineParseOkTestCase(const char*const(&args)[N], bool isInQueryMode,
const char*const(&fileList)[M]) noexcept :
CmdLineParseTestCase(args),
m_isInQueryMode(isInQueryMode),
m_fileList(::std::span{fileList, M})
{}
bool m_isInQueryMode;
ArgSpan m_fileList;
};
static char const*const k_args00[] = { "stripws", "StripWS.cpp" };
static char const*const k_args01[] = { "stripws", "-s", "StripWS.cpp" };
static char const*const k_args02[] = { "stripws", "-s", "-s", "StripWS.cpp" };
static char const*const k_args03[] = { "stripws", "StripWS.cpp", "StripWS.h", "StripWSTest.cpp" };
static char const*const k_args04[] = { "stripws", "Strip*.h" };
static char const*const k_args05[] = { "stripws", "Strip*.h", "Strip*.cpp" };
static char const*const k_files00[] = { "StripWS.cpp" };
static char const*const k_files01[] = { "StripWS.cpp", "StripWS.h", "StripWSTest.cpp" };
static char const*const k_files02[] = { "Strip*.h" };
static char const*const k_files03[] = { "Strip*.h", "Strip*.cpp" };
static CmdLineParseOkTestCase const k_testCases[] =
{
{ k_args00, true, k_files00 },
{ k_args01, false, k_files00 },
{ k_args02, false, k_files00 },
{ k_args03, true, k_files01 },
{ k_args04, true, k_files02 },
{ k_args05, true, k_files03 },
};
static void checkEqual(const fs::path& tcPath, const fs::path& appPath)
{
BOOST_CHECK_EQUAL(tcPath.generic_string(), appPath.generic_string());
}
BOOST_DATA_TEST_CASE(cmdLineParseOkTest, utd::make(k_testCases), tc)
{
StripWS app(tc.m_args);
BOOST_CHECK_EQUAL(tc.m_isInQueryMode, app.m_isInQueryMode);
BOOST_CHECK_EQUAL(tc.m_fileList.size(), app.m_fileEnumerator.numFileSpecs());
PathList tcList(begin(tc.m_fileList), end(tc.m_fileList));
b::sort(tcList);
PathList appList;
app.m_fileEnumerator.getFileSpecList(appList);
b::sort(appList);
b::for_each(tcList, appList, checkEqual);
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE(ScanFileTestSuite)
struct ScanFileTestCase
{
char const* m_pInput;
char const* m_pOutput;
size_t m_numLinesAffected;
size_t m_numSpacesStripped;
size_t m_numTabsStripped;
};
static ScanFileTestCase const k_testCases[] =
{
// input, output, numLinesAffected, numSpacesStripped, numTabsStripped
{
"",
"",
0, 0, 0
},
{
"This one has no white space to strip at all",
"This one has no white space to strip at all",
0, 0, 0
},
{
"This one has spaces to strip at the end of the file ",
"This one has spaces to strip at the end of the file",
1, 1, 0
},
{
"This one has tabs to strip at the end of the file\t",
"This one has tabs to strip at the end of the file",
1, 0, 1
},
{
" \nHere we see\t\rthree lines to strip \r\n",
"\nHere we see\rthree lines to strip\r\n",
3, 5, 1
},
{
" \t \nHere \t\t \t\t we see\t\t\t\rthree more lines to strip \t\t \t\t \n",
"\nHere \t\t \t\t we see\rthree more lines to strip\n",
3, 8, 8
}
};
static ::std::ostream& operator<<(::std::ostream& ostrm, ScanFileTestCase const& tc)
{
return ostrm << "Test case with input \"" << tc.m_pInput << "\"";
}
static void dumpHex(string const& s)
{
if (false)
{
ostringstream strm;
strm << "Output: ";
for (auto ch : s)
{
strm << ' ' << static_cast<unsigned int>(static_cast<unsigned char>(ch));
}
BOOST_TEST_MESSAGE(strm.str());
}
}
BOOST_DATA_TEST_CASE(scanFileTest, utd::make(k_testCases), tc)
{
if (false)
{
BOOST_TEST_MESSAGE("Testing scanFile with input \"" << tc.m_pInput << "\"");
}
string input(tc.m_pInput);
size_t numLinesAffected;
size_t numSpacesStripped;
size_t numTabsStripped;
{
istringstream in(input);
StripWS::scanFile(in, numLinesAffected, numSpacesStripped, numTabsStripped);
BOOST_CHECK_EQUAL(tc.m_numLinesAffected, numLinesAffected);
BOOST_CHECK_EQUAL(tc.m_numSpacesStripped, numSpacesStripped);
BOOST_CHECK_EQUAL(tc.m_numTabsStripped, numTabsStripped);
}
{
istringstream in(input);
ostringstream out;
StripWS::scanFile(in, numLinesAffected, numSpacesStripped, numTabsStripped, &out);
BOOST_CHECK_EQUAL(tc.m_numLinesAffected, numLinesAffected);
BOOST_CHECK_EQUAL(tc.m_numSpacesStripped, numSpacesStripped);
BOOST_CHECK_EQUAL(tc.m_numTabsStripped, numTabsStripped);
dumpHex(out.str());
BOOST_CHECK_EQUAL(tc.m_pOutput, out.str());
}
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()