-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindFileExtTest.cpp
211 lines (169 loc) · 6.58 KB
/
FindFileExtTest.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
#if !defined(CMDLINEUTIL_TEST_MODE)
#define CMDLINEUTIL_TEST_MODE
#endif
#include "FindFileExt.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 <string>
#include <vector>
namespace b = ::boost;
namespace fs = ::std::filesystem;
namespace utd = ::boost::unit_test::data;
using ::std::begin;
using ::std::end;
using ::std::string;
using PathList = ::std::vector<fs::path>;
BOOST_AUTO_TEST_SUITE(FindFileExtTestSuite)
BOOST_AUTO_TEST_SUITE(CmdLineParseFailTestSuite)
static char const*const k_args00[] = { "findext" };
static char const*const k_args01[] = { "findext", "-?" };
static char const*const k_args02[] = { "findext", "-h" };
static char const*const k_args03[] = { "findext", "-help" };
static char const*const k_args04[] = { "findext", "-r" };
static char const*const k_args05[] = { "findext", "-d" };
static char const*const k_args06[] = { "findext", "fooble" };
static char const*const k_args07[] = { "findext", "FindFileExt.cpp" };
static CmdLineParseFailTestCase const k_testCases[] =
{
{ k_args00, "^No directories specified$" },
{ k_args01, "^$" },
{ k_args02, "^$" },
{ k_args03, "^$" },
{ k_args04, "^No directories specified$" },
{ k_args05, "^.* does not exist" },
{ k_args06, "^.* does not exist$" },
{ k_args07, "^.* is not a directory$" },
};
BOOST_DATA_TEST_CASE(cmdLineParseFailTest, utd::make(k_testCases), tc)
{
BOOST_CHECK_EXCEPTION(FindFileExt(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,
bool includeCounts, bool outputAsWildcards,
const char*const(&fileList)[M]) noexcept :
CmdLineParseTestCase(args),
m_isRecursive(isRecursive),
m_includeCounts(includeCounts),
m_outputAsWildcards(outputAsWildcards),
m_fileList(::std::span{fileList, M})
{}
bool m_isRecursive;
bool m_includeCounts;
bool m_outputAsWildcards;
ArgSpan m_fileList;
};
static char const*const k_args00[] = { "findext", "." };
static char const*const k_args01[] = { "findext", "-r", "." };
static char const*const k_args02[] = { "findext", "-c", "." };
static char const*const k_args03[] = { "findext", "-w", "." };
static char const*const k_args04[] = { "findext", "-r", "-r", "." };
static char const*const k_args05[] = { "findext", "-r", "-c", "." };
static char const*const k_args06[] = { "findext", "-c", "-r", ".", "bb-bin" };
static char const*const k_args07[] = { "findext", "-r", "-w", "." };
static char const*const k_args08[] = { "findext", "-c", "-w", "." };
static char const*const k_args09[] = { "findext", "-c", "-r", "-w", "." };
static char const*const k_args10[] = { "findext", "-r", ".", "bb-bin" };
static char const*const k_files00[] = { "./*" };
static char const*const k_files01[] = { "./*", "bb-bin/*" };
static CmdLineParseOkTestCase const k_testCases[] =
{
{ k_args00, false, false, false, k_files00 },
{ k_args01, true, false, false, k_files00 },
{ k_args02, false, true, false, k_files00 },
{ k_args03, false, false, true, k_files00 },
{ k_args04, true, false, false, k_files00 },
{ k_args05, true, true, false, k_files00 },
{ k_args06, true, true, false, k_files01 },
{ k_args07, true, false, true, k_files00 },
{ k_args08, false, true, true, k_files00 },
{ k_args09, true, true, true, k_files00 },
{ k_args10, true, false, false, 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)
{
FindFileExt app(tc.m_args);
BOOST_CHECK_EQUAL(tc.m_isRecursive, app.m_fileEnumerator.isRecursive());
BOOST_CHECK_EQUAL(tc.m_includeCounts, app.m_includeCounts);
BOOST_CHECK_EQUAL(tc.m_outputAsWildcards, app.m_outputAsWildcards);
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(CountFilesTestSuite)
struct CountFilesTestCase : public CmdLineParseTestCase
{
template<::std::size_t N>
CountFilesTestCase(const char*const(&args)[N], bool isRecursive,
::std::size_t hCount, ::std::size_t cppCount) noexcept :
CmdLineParseTestCase(args),
m_isRecursive(isRecursive),
m_hCount(hCount),
m_cppCount(cppCount)
{}
bool m_isRecursive;
::std::size_t m_hCount;
::std::size_t m_cppCount;
};
static char const*const k_args00[] = { "findext", "TempTestDir" };
static char const*const k_args01[] = { "findext", "-r", "TempTestDir" };
static CountFilesTestCase const k_testCases[] =
{
{ k_args00, false, 1, 2 },
{ k_args01, true, 2, 4 },
};
static void copyFile(const fs::path& fromDir, const fs::path& toDir, const char* pFileName)
{
copy_file(fromDir / pFileName, toDir / pFileName);
}
static void checkEqual(const FindFileExt::StrToCountMap::value_type& expMapEntry,
const FindFileExt::StrToCountMap::value_type& obsMapEntry)
{
BOOST_CHECK_EQUAL(expMapEntry.first, obsMapEntry.first);
BOOST_CHECK_EQUAL(expMapEntry.second, obsMapEntry.second);
}
BOOST_DATA_TEST_CASE(countFilesTest, utd::make(k_testCases), tc)
{
// Setup:
fs::path cwDir(".");
fs::path testDir = cwDir / "TempTestDir";
fs::path testDir2 = testDir / "dir2";
PathDeleter testPathDeleter(testDir);
create_directories(testDir2);
copyFile(cwDir, testDir, "FileEnumerator.cpp");
copyFile(cwDir, testDir, "FileEnumerator.h");
copyFile(cwDir, testDir, "FileEnumeratorTest.cpp");
copyFile(cwDir, testDir2, "FileEnumerator.cpp");
copyFile(cwDir, testDir2, "FileEnumerator.h");
copyFile(cwDir, testDir2, "FileEnumeratorTest.cpp");
testDir.make_preferred();
FindFileExt app(tc.m_args);
BOOST_CHECK_EQUAL(tc.m_isRecursive, app.m_fileEnumerator.isRecursive());
app.countFiles();
FindFileExt::StrToCountMap expExtToCountMap; // expected result
expExtToCountMap.insert(make_pair(string(".cpp"), tc.m_cppCount));
expExtToCountMap.insert(make_pair(string(".h"), tc.m_hCount));
BOOST_CHECK_EQUAL(expExtToCountMap.size(), app.m_extToCountMap.size());
b::for_each(expExtToCountMap, app.m_extToCountMap, checkEqual);
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()