forked from varoudis/depthmapX
-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathtestcommandlineparser.cpp
More file actions
283 lines (237 loc) · 10.5 KB
/
testcommandlineparser.cpp
File metadata and controls
283 lines (237 loc) · 10.5 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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
// Copyright (C) 2017 Christian Sailer
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "catch.hpp"
#include "Catch/fakeit.hpp"
#include "../depthmapXcli/commandlineparser.h"
#include "../depthmapXcli/imodeparser.h"
#include "../depthmapXcli/imodeparserfactory.h"
#include <cstring>
#include <sstream>
#include "argumentholder.h"
using namespace fakeit;
class TestParser: public IModeParser
{
public:
TestParser(const std::string &modeName) : _parseCalled(false), _runCalled(false), _modeName(modeName)
{
}
virtual std::string getModeName()const
{
return _modeName;
}
virtual std::string getHelp() const
{
return formatTestHelpString(_runCalled, _parseCalled);
}
virtual void parse(int , char ** )
{
_parseCalled = true;
}
virtual void run(const CommandLineParser &, IPerformanceSink &) const
{
_runCalled = true;
}
static std::string formatTestHelpString(bool runCalled, bool parseCalled)
{
std::stringstream buf;
buf << "runCalled " << (runCalled ? "yes" : "no")
<< " : parseCalled " << (parseCalled? "yes": "no")
<< std::flush;
return buf.str();
}
private:
bool _parseCalled;
mutable bool _runCalled;
std::string _modeName;
};
TEST_CASE("Invalid Parser","Constructor"){
std::vector<std::unique_ptr<IModeParser> > parsers;
parsers.push_back(std::unique_ptr<IModeParser>(new TestParser("TEST1")));
parsers.push_back(std::unique_ptr<IModeParser>(new TestParser("TEST2")));
Mock<IModeParserFactory> factoryMock;
When(Method(factoryMock,getModeParsers)).AlwaysReturn(parsers);
{
CommandLineParser cmdP(factoryMock.get());
REQUIRE_THROWS_WITH(cmdP.parse(0, 0), Catch::Contains("No commandline parameters provided - don't know what to do"));
}
{
CommandLineParser cmdP(factoryMock.get());
ArgumentHolder ah{"prog", "-m"};
REQUIRE_THROWS_WITH(cmdP.parse(ah.argc(), ah.argv()), "-m requires an argument");
}
{
CommandLineParser cmdP(factoryMock.get());
ArgumentHolder ah{"prog", "-f"};
REQUIRE_THROWS_WITH(cmdP.parse(ah.argc(), ah.argv()), "-f requires an argument");
}
{
CommandLineParser cmdP(factoryMock.get());
ArgumentHolder ah{"prog", "-o"};
REQUIRE_THROWS_WITH(cmdP.parse(ah.argc(), ah.argv()), "-o requires an argument");
}
{
CommandLineParser cmdP(factoryMock.get());
ArgumentHolder ah{"prog", "-t", "-o"};
REQUIRE_THROWS_WITH(cmdP.parse(ah.argc(), ah.argv()), "-t requires an argument");
}
{
CommandLineParser cmdP(factoryMock.get());
ArgumentHolder ah{"prog", "-m", "-f"};
REQUIRE_THROWS_WITH(cmdP.parse(ah.argc(), ah.argv()), "-m requires an argument");
}
{
CommandLineParser cmdP(factoryMock.get());
ArgumentHolder ah{"prog", "-m", "LaLaLa"};
REQUIRE_THROWS_WITH(cmdP.parse(ah.argc(), ah.argv()), "Invalid mode: LaLaLa");
}
{
CommandLineParser cmdP(factoryMock.get());
ArgumentHolder ah{"prog", "-f", "inputfile.graph", "-o", "outputfile.graph"};
REQUIRE_THROWS_WITH(cmdP.parse(ah.argc(), ah.argv()), Catch::Contains("-m for mode is required"));
}
{
CommandLineParser cmdP(factoryMock.get());
ArgumentHolder ah{"prog", "-m", "TEST1", "-m", "TEST2","-f", "inputfile.graph"};
REQUIRE_THROWS_WITH(cmdP.parse(ah.argc(), ah.argv()), Catch::Contains("-m can only be used once"));
}
{
CommandLineParser cmdP(factoryMock.get());
ArgumentHolder ah{"prog", "-m", "TEST1", "-o", "outputfile.graph"};
REQUIRE_THROWS_WITH(cmdP.parse(ah.argc(), ah.argv()), Catch::Contains("-f for input file is required"));
}
{
CommandLineParser cmdP(factoryMock.get());
ArgumentHolder ah{"prog", "-m", "TEST1", "-f", "inputfile.graph"};
REQUIRE_THROWS_WITH(cmdP.parse(ah.argc(), ah.argv()), Catch::Contains("-o for output file is required"));
}
}
TEST_CASE("Valid Parser","CheckValues"){
std::vector<std::unique_ptr<IModeParser> > parsers;
parsers.push_back(std::unique_ptr<IModeParser>(new TestParser("TEST1")));
parsers.push_back(std::unique_ptr<IModeParser>(new TestParser("TEST2")));
Mock<IModeParserFactory> factoryMock;
When(Method(factoryMock,getModeParsers)).AlwaysReturn(parsers);
SECTION("Parser test2 used")
{
CommandLineParser cmdP(factoryMock.get());
ArgumentHolder ah{"prog", "-m", "TEST2", "-f", "inputfile.graph", "-o", "outputfile.graph", "-lnk", "1.2,3.4,5.6,7.8"};
cmdP.parse(ah.argc(), ah.argv());
REQUIRE(cmdP.isValid());
REQUIRE(cmdP.getTimingFile().empty());
REQUIRE(parsers[0]->getHelp() == TestParser::formatTestHelpString(false, false));
REQUIRE(parsers[1]->getHelp() == TestParser::formatTestHelpString(false, true));
}
SECTION("Parser test1 used")
{
CommandLineParser cmdP(factoryMock.get());
ArgumentHolder ah{"prog", "-m", "TEST1", "-f", "inputfile.graph", "-o", "outputfile.graph"};
cmdP.parse(ah.argc(), ah.argv());
REQUIRE(cmdP.isValid());
REQUIRE_FALSE(cmdP.simpleMode());
REQUIRE(cmdP.getTimingFile().empty());
REQUIRE(cmdP.modeOptions().getModeName() == "TEST1");
REQUIRE(parsers[0]->getHelp() == TestParser::formatTestHelpString(false, true));
REQUIRE(parsers[1]->getHelp() == TestParser::formatTestHelpString(false, false));
}
SECTION("Parser test1 used, timings file, simple mode")
{
CommandLineParser cmdP(factoryMock.get());
ArgumentHolder ah{"prog", "-m", "TEST1", "-f", "inputfile.graph", "-o", "outputfile.graph", "-s", "-t", "timings.csv"};
cmdP.parse(ah.argc(), ah.argv());
REQUIRE(cmdP.isValid());
REQUIRE(cmdP.simpleMode());
REQUIRE(cmdP.getTimingFile() == "timings.csv");
REQUIRE(parsers[0]->getHelp() == TestParser::formatTestHelpString(false, true));
REQUIRE(parsers[1]->getHelp() == TestParser::formatTestHelpString(false, false));
}
}
TEST_CASE("Run Tests","Check we only run if it's appropriate"){
std::vector<std::unique_ptr<IModeParser> > parsers;
parsers.push_back(std::unique_ptr<IModeParser>(new TestParser("TEST1")));
parsers.push_back(std::unique_ptr<IModeParser>(new TestParser("TEST2")));
Mock<IModeParserFactory> factoryMock;
When(Method(factoryMock,getModeParsers)).AlwaysReturn(parsers);
Mock<IPerformanceSink> perfSink;
SECTION("Fail - run without parsing")
{
CommandLineParser cmdP(factoryMock.get());
REQUIRE_THROWS_WITH(cmdP.run(perfSink.get()), Catch::Contains("Trying to run with invalid command line parameters"));
}
SECTION("Fail run with help on the command line")
{
CommandLineParser cmdP(factoryMock.get());
ArgumentHolder ah{"prog", "-h", "-m", "TEST1", "-f", "inputfile.graph", "-o", "outputfile.graph"};
cmdP.parse(ah.argc(), ah.argv());
REQUIRE_FALSE(cmdP.isValid());
REQUIRE_THROWS_WITH(cmdP.run(perfSink.get()), Catch::Contains("Trying to run with invalid command line parameters"));
}
SECTION("Fail run with version on the command line")
{
CommandLineParser cmdP(factoryMock.get());
ArgumentHolder ah{"prog", "-v", "-m", "TEST1", "-f", "inputfile.graph", "-o", "outputfile.graph"};
cmdP.parse(ah.argc(), ah.argv());
REQUIRE_FALSE(cmdP.isValid());
REQUIRE_THROWS_WITH(cmdP.run(perfSink.get()), Catch::Contains("Trying to run with invalid command line parameters"));
}
SECTION("Fail run with version on the command line as last parameter")
{
CommandLineParser cmdP(factoryMock.get());
ArgumentHolder ah{"prog", "-m", "TEST1", "-f", "inputfile.graph", "-o", "outputfile.graph", "-v"};
cmdP.parse(ah.argc(), ah.argv());
REQUIRE_FALSE(cmdP.isValid());
REQUIRE_THROWS_WITH(cmdP.run(perfSink.get()), Catch::Contains("Trying to run with invalid command line parameters"));
}
SECTION("Fail run without sub parser")
{
CommandLineParser cmdP(factoryMock.get());
ArgumentHolder ah{"prog" "-f", "inputfile.graph", "-o", "outputfile.graph"};
REQUIRE_THROWS(cmdP.parse(ah.argc(), ah.argv()));
REQUIRE_FALSE(cmdP.isValid());
REQUIRE_THROWS_WITH(cmdP.run(perfSink.get()), Catch::Contains("Trying to run with invalid command line parameters"));
}
SECTION("Parser test1 used, timings file, simple mode")
{
CommandLineParser cmdP(factoryMock.get());
ArgumentHolder ah{"prog", "-m", "TEST1", "-f", "inputfile.graph", "-o", "outputfile.graph", "-s", "-t", "timings.csv"};
cmdP.parse(ah.argc(), ah.argv());
REQUIRE(cmdP.isValid());
REQUIRE(cmdP.simpleMode());
REQUIRE(cmdP.getTimingFile() == "timings.csv");
REQUIRE(parsers[0]->getHelp() == TestParser::formatTestHelpString(false, true));
REQUIRE(parsers[1]->getHelp() == TestParser::formatTestHelpString(false, false));
cmdP.run(perfSink.get());
REQUIRE(parsers[0]->getHelp() == TestParser::formatTestHelpString(true, true));
REQUIRE(parsers[1]->getHelp() == TestParser::formatTestHelpString(false, false));
}
}
TEST_CASE("Invalid Parser Need Help", "CheckForHelp")
{
std::vector<std::unique_ptr<IModeParser> > parsers;
parsers.push_back(std::unique_ptr<IModeParser>(new TestParser("TEST1")));
parsers.push_back(std::unique_ptr<IModeParser>(new TestParser("TEST2")));
Mock<IModeParserFactory> factoryMock;
When(Method(factoryMock,getModeParsers)).AlwaysReturn(parsers);
SECTION("Help requested")
{
CommandLineParser cmdP(factoryMock.get());
ArgumentHolder ah{ "prog", "-h"};
cmdP.parse(ah.argc(), ah.argv());
REQUIRE_FALSE(cmdP.isValid());
}
SECTION("Version requested")
{
CommandLineParser cmdP(factoryMock.get());
ArgumentHolder ah{ "prog", "-v"};
cmdP.parse(ah.argc(), ah.argv());
REQUIRE_FALSE(cmdP.isValid());
}
}