-
Notifications
You must be signed in to change notification settings - Fork 0
/
IndentClassifierTest.cpp
270 lines (231 loc) · 6.7 KB
/
IndentClassifierTest.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#if !defined(CMDLINEUTIL_TEST_MODE)
#define CMDLINEUTIL_TEST_MODE
#endif
#include "Exceptions.h"
#include "IndentClassifier.h"
#include "PathDeleter.h"
#include "TestUtil.h"
#include "Utils.h"
#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 <numeric>
#include <string>
#include <sstream>
#include <vector>
namespace b = ::boost;
namespace fs = ::std::filesystem;
namespace utd = ::boost::unit_test::data;
using ::std::begin;
using ::std::end;
using ::std::istringstream;
using ::std::string;
using PathList = ::std::vector<fs::path>;
BOOST_AUTO_TEST_SUITE(IndentClassifierTestSuite)
BOOST_AUTO_TEST_SUITE(CmdLineParseFailTestSuite)
static char const*const k_args00[] = { "indents" };
static char const*const k_args01[] = { "indents", "-?" };
static char const*const k_args02[] = { "indents", "-h" };
static char const*const k_args03[] = { "indents", "-help" };
static char const*const k_args04[] = { "indents", "-r" };
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(IndentClassifier(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 isRecursive,
const char*const(&fileList)[M]) noexcept :
CmdLineParseTestCase(args),
m_isRecursive(isRecursive),
m_fileList(::std::span{fileList, M})
{}
bool m_isRecursive;
ArgSpan m_fileList;
};
static char const*const k_args00[] = { "indents", "IndentClassifier.cpp" };
static char const*const k_args01[] = { "indents", "-r", "IndentClassifier*.h", "IndentClassifier*.cpp" };
static char const*const k_files00[] = { "IndentClassifier.cpp" };
static char const*const k_files01[] = { "IndentClassifier*.h", "IndentClassifier*.cpp" };
static CmdLineParseOkTestCase const k_testCases[] =
{
{ k_args00, false, k_files00 },
{ k_args01, true, k_files01 },
};
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)
{
IndentClassifier app(tc.m_args);
BOOST_CHECK_EQUAL(tc.m_isRecursive, app.m_fileEnumerator.isRecursive());
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*const m_pInput;
bool m_isJavaFile;
size_t m_numSpaceLines;
size_t m_numTabLines;
size_t m_numJavadocTabLines;
size_t m_numJavadocLeftLines;
size_t m_numMixedLines;
size_t m_numIndLines;
IndentType m_fileType;
};
static ScanFileTestCase const k_testCases[] =
{
// input, numSpaceLines, numTabLines, numMixedLines, numIndLines, fileType
{
"",
false, 0, 0, 0, 0, 0, 1, IndentType::indeterminate
},
{
"This one has no whitespace at the beginning of the only line.",
false, 0, 0, 0, 0, 0, 1, IndentType::indeterminate
},
{
"This one has no whitespace\n"
"at the beginning\n"
"of any of the lines.\n",
false, 0, 0, 0, 0, 0, 4, IndentType::indeterminate
},
{
" \n"
" This one has spaces\n"
" at the beginning\n"
" of all of the lines.\n",
false, 4, 0, 0, 0, 0, 1, IndentType::space
},
{
"foo\n"
" This one has spaces\n"
" at the beginning\n"
" of all of the lines but one.\n",
false, 3, 0, 0, 0, 0, 2, IndentType::space
},
{
"\tThis one has tabs\n"
"\t\t\tat the beginning\n"
"\tof all of the lines.\n",
false, 0, 3, 0, 0, 0, 1, IndentType::tab
},
{
"foo\n"
"\tThis one has tabs\n"
"\t\t\tat the beginning\n"
"\tof all of the lines.\n",
false, 0, 3, 0, 0, 0, 2, IndentType::tab
},
{
"\tThis one has tabs\n"
" on some lines\n"
"\t\tand spaces on others.\n",
false, 1, 2, 0, 0, 0, 1, IndentType::mixed
},
{
" \tThis one has tabs\n"
" \t\t and spaces\n"
"\t \ton each line.\n",
false, 0, 0, 0, 0, 3, 1, IndentType::mixed
},
{
"This one has no whitespace\n"
"at the beginning\n"
"of any of the lines.\n"
"\tThis one has tabs\n"
" on some lines\n"
"\t\tand spaces on others.\n"
" \tThis one has tabs\n"
" \t\t and spaces\n"
"\t \ton each line.\n",
false, 1, 2, 0, 0, 3, 4, IndentType::mixed
},
{
"/**\n"
" * A comment\n"
" */\n"
"public interface Foo {\n"
"}",
false, 2, 0, 0, 0, 0, 3, IndentType::space
},
{
"/**\n"
" * A comment\n"
" */\n"
"public interface Foo {\n"
"}",
true, 0, 0, 0, 2, 0, 3, IndentType::javadocTab
},
{
"/**\n"
" * A comment\n"
" */\n"
"public interface Foo {\n"
"\tvoid aMethod();\n"
"}",
true, 0, 1, 0, 2, 0, 3, IndentType::javadocTab
},
{
"/**\n"
" * A comment\n"
" */\n"
"public interface Foo {\n"
"\t/**\n"
"\t * Another comment\n"
"\t */\n"
"\tvoid aMethod();\n"
"}",
true, 0, 2, 2, 2, 0, 3, IndentType::javadocTab
}
};
static ::std::ostream& operator<<(::std::ostream& ostrm, const ScanFileTestCase& tc)
{
return ostrm << "Test case with input \"" << tc.m_pInput << "\"";
}
BOOST_DATA_TEST_CASE(scanFileTest, utd::make(k_testCases), tc)
{
string input(tc.m_pInput);
if (false)
{
BOOST_TEST_MESSAGE(input);
}
istringstream in(input);
auto lineTypeCounts{IndentClassifier::scanFile(in, tc.m_isJavaFile)};
auto fileType{IndentClassifier::classifyFile(lineTypeCounts)};
BOOST_CHECK_EQUAL(tc.m_numSpaceLines, get(lineTypeCounts, IndentType::space));
BOOST_CHECK_EQUAL(tc.m_numTabLines, get(lineTypeCounts, IndentType::tab));
BOOST_CHECK_EQUAL(tc.m_numMixedLines, get(lineTypeCounts, IndentType::mixed));
BOOST_CHECK_EQUAL(tc.m_numIndLines, get(lineTypeCounts, IndentType::indeterminate));
auto totalLines{::std::accumulate(begin(lineTypeCounts), end(lineTypeCounts), size_t{0},
[](size_t value, const LineTypeCounts::value_type& mapEntry){ return value + mapEntry.second; })};
BOOST_CHECK_EQUAL(
tc.m_numSpaceLines + tc.m_numTabLines + tc.m_numJavadocTabLines
+ tc.m_numJavadocLeftLines + tc.m_numMixedLines + tc.m_numIndLines,
totalLines);
BOOST_CHECK_EQUAL(static_cast<int>(tc.m_fileType), static_cast<int>(fileType));
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()