Debian Codegraph @

tests/CppUTest/CommandLineArgumentsTest.cpp

raw symbols SCIP
1 /*
2 * Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the <organization> nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "CppUTest/TestHarness.h"
29 #include "CppUTest/CommandLineArguments.h"
30 #include "CppUTest/TestRegistry.h"
31
32 class OptionsPlugin: public TestPlugin
33 {
34 public:
35 OptionsPlugin(const SimpleString& name) :
36 TestPlugin(name)
37 {
38 }
39 ~OptionsPlugin() _destructor_override
40 {
41 }
42 bool parseArguments(int /*ac*/, const char *const * /*av*/, int /*index*/) _override
43 {
44 return true;
45 }
46 };
47
48 TEST_GROUP(CommandLineArguments)
49 {
50 CommandLineArguments* args;
51 OptionsPlugin* plugin;
52
53 void setup()
54 {
55 plugin = new OptionsPlugin("options");
56 args = NULLPTR;
57 }
58 void teardown()
59 {
60 delete args;
61 delete plugin;
62 }
63
64 bool newArgumentParser(int argc, const char *const *argv)
65 {
66 args = new CommandLineArguments(argc, argv);
67 return args->parse(plugin);
68 }
69 };
70
71 TEST(CommandLineArguments, Create)
72 {
73 }
74
75 TEST(CommandLineArguments, verboseSetMultipleParameters)
76 {
77 const char* argv[] = { "tests.exe", "-v" };
78 CHECK(newArgumentParser(2, argv));
79 CHECK(args->isVerbose());
80 }
81
82 TEST(CommandLineArguments, veryVerbose)
83 {
84 const char* argv[] = { "tests.exe", "-vv" };
85 CHECK(newArgumentParser(2, argv));
86 CHECK(args->isVeryVerbose());
87 }
88
89 TEST(CommandLineArguments, setColor)
90 {
91 const char* argv[] = { "tests.exe", "-c" };
92 CHECK(newArgumentParser(2, argv));
93 CHECK(args->isColor());
94 }
95
96 TEST(CommandLineArguments, repeatSet)
97 {
98 int argc = 2;
99 const char* argv[] = { "tests.exe", "-r3" };
100 CHECK(newArgumentParser(argc, argv));
101 LONGS_EQUAL(3, args->getRepeatCount());
102 }
103
104 TEST(CommandLineArguments, repeatSetDifferentParameter)
105 {
106 int argc = 3;
107 const char* argv[] = { "tests.exe", "-r", "4" };
108 CHECK(newArgumentParser(argc, argv));
109 LONGS_EQUAL(4, args->getRepeatCount());
110 }
111
112 TEST(CommandLineArguments, repeatSetDefaultsToTwoAndShuffleDisabled)
113 {
114 int argc = 2;
115 const char* argv[] = { "tests.exe", "-r" };
116 CHECK(newArgumentParser(argc, argv));
117 LONGS_EQUAL(2, args->getRepeatCount());
118 }
119
120 TEST(CommandLineArguments, reverseEnabled)
121 {
122 int argc = 2;
123 const char* argv[] = { "tests.exe", "-b" };
124 CHECK(newArgumentParser(argc, argv));
125 CHECK_TRUE(args->isReversing());
126 }
127
128 TEST(CommandLineArguments, shuffleDisabledByDefault)
129 {
130 int argc = 1;
131 const char* argv[] = { "tests.exe" };
132 CHECK(newArgumentParser(argc, argv));
133 CHECK_FALSE(args->isShuffling());
134 }
135
136 TEST(CommandLineArguments, shuffleEnabled)
137 {
138 int argc = 2;
139 const char* argv[] = { "tests.exe", "-s" };
140 CHECK(newArgumentParser(argc, argv));
141 CHECK_TRUE(args->isShuffling());
142 }
143
144 TEST(CommandLineArguments, shuffleWithSeedZeroIsOk)
145 {
146 int argc = 2;
147 const char* argv[] = { "tests.exe", "-s0" };
148 CHECK_FALSE(newArgumentParser(argc, argv));
149 CHECK_EQUAL(0, args->getShuffleSeed());
150 }
151
152 TEST(CommandLineArguments, shuffleEnabledSpecificSeedCase1)
153 {
154 int argc = 2;
155 const char* argv[] = { "tests.exe", "-s999"};
156 CHECK(newArgumentParser(argc, argv));
157 CHECK_EQUAL(999, args->getShuffleSeed());
158 }
159
160 TEST(CommandLineArguments, shuffleEnabledSpecificSeedCase2)
161 {
162 int argc = 2;
163 const char* argv[] = { "tests.exe", "-s 888"};
164 CHECK(newArgumentParser(argc, argv));
165 CHECK_EQUAL(888, args->getShuffleSeed());
166 }
167
168 TEST(CommandLineArguments, shuffleEnabledSpecificSeedCase3)
169 {
170 int argc = 3;
171 const char* argv[] = { "tests.exe", "-s", "777"};
172 CHECK(newArgumentParser(argc, argv));
173 CHECK_EQUAL(777, args->getShuffleSeed());
174 }
175
176 TEST(CommandLineArguments, shuffleBeforeDoesNotDisturbOtherSwitch)
177 {
178 int argc = 4;
179 const char* argv[] = { "tests.exe", "-s", "-sg", "group" };
180 CHECK(newArgumentParser(argc, argv));
181 TestFilter groupFilter("group");
182 groupFilter.strictMatching();
183 CHECK_EQUAL(groupFilter, *args->getGroupFilters());
184 CHECK_TRUE(args->isShuffling());
185 }
186
187 TEST(CommandLineArguments, runningTestsInSeperateProcesses)
188 {
189 int argc = 2;
190 const char* argv[] = { "tests.exe", "-p" };
191 CHECK(newArgumentParser(argc, argv));
192 CHECK(args->runTestsInSeperateProcess());
193 }
194
195 TEST(CommandLineArguments, setGroupFilter)
196 {
197 int argc = 3;
198 const char* argv[] = { "tests.exe", "-g", "group" };
199 CHECK(newArgumentParser(argc, argv));
200 CHECK_EQUAL(TestFilter("group"), *args->getGroupFilters());
201 }
202
203 TEST(CommandLineArguments, setCompleteGroupDotNameFilterInvalidArgument)
204 {
205 int argc = 3;
206 const char* argv[] = { "tests.exe", "-t", "groupname" };
207 CHECK_FALSE(newArgumentParser(argc, argv));
208 }
209 TEST(CommandLineArguments, setCompleteGroupDotNameFilter)
210 {
211 int argc = 3;
212 const char* argv[] = { "tests.exe", "-t", "group.name" };
213 CHECK(newArgumentParser(argc, argv));
214 CHECK_EQUAL(TestFilter("group"), *args->getGroupFilters());
215 CHECK_EQUAL(TestFilter("name"), *args->getNameFilters());
216 }
217
218
219 TEST(CommandLineArguments, setGroupFilterSameParameter)
220 {
221 int argc = 2;
222 const char* argv[] = { "tests.exe", "-ggroup" };
223 CHECK(newArgumentParser(argc, argv));
224 CHECK_EQUAL(TestFilter("group"), *args->getGroupFilters());
225 }
226
227 TEST(CommandLineArguments, setStrictGroupFilter)
228 {
229 int argc = 3;
230 const char* argv[] = { "tests.exe", "-sg", "group" };
231 CHECK(newArgumentParser(argc, argv));
232 TestFilter groupFilter("group");
233 groupFilter.strictMatching();
234 CHECK_EQUAL(groupFilter, *args->getGroupFilters());
235 }
236
237 TEST(CommandLineArguments, setStrictGroupFilterSameParameter)
238 {
239 int argc = 2;
240 const char* argv[] = { "tests.exe", "-sggroup" };
241 CHECK(newArgumentParser(argc, argv));
242 TestFilter groupFilter("group");
243 groupFilter.strictMatching();
244 CHECK_EQUAL(groupFilter, *args->getGroupFilters());
245 }
246
247 TEST(CommandLineArguments, setExcludeGroupFilter)
248 {
249 int argc = 3;
250 const char* argv[] = { "tests.exe", "-xg", "group" };
251 CHECK(newArgumentParser(argc, argv));
252 TestFilter groupFilter("group");
253 groupFilter.invertMatching();
254 CHECK_EQUAL(groupFilter, *args->getGroupFilters());
255 }
256
257 TEST(CommandLineArguments, setExcludeGroupFilterSameParameter)
258 {
259 int argc = 2;
260 const char* argv[] = { "tests.exe", "-xggroup" };
261 CHECK(newArgumentParser(argc, argv));
262 TestFilter groupFilter("group");
263 groupFilter.invertMatching();
264 CHECK_EQUAL(groupFilter, *args->getGroupFilters());
265 }
266
267 TEST(CommandLineArguments, setExcludeStrictGroupFilter)
268 {
269 int argc = 3;
270 const char* argv[] = { "tests.exe", "-xsg", "group" };
271 CHECK(newArgumentParser(argc, argv));
272 TestFilter groupFilter("group");
273 groupFilter.invertMatching();
274 groupFilter.strictMatching();
275 CHECK_EQUAL(groupFilter, *args->getGroupFilters());
276 }
277
278 TEST(CommandLineArguments, setExcludeStrictGroupFilterSameParameter)
279 {
280 int argc = 2;
281 const char* argv[] = { "tests.exe", "-xsggroup" };
282 CHECK(newArgumentParser(argc, argv));
283 TestFilter groupFilter("group");
284 groupFilter.invertMatching();
285 groupFilter.strictMatching();
286 CHECK_EQUAL(groupFilter, *args->getGroupFilters());
287 }
288
289 TEST(CommandLineArguments, setNameFilter)
290 {
291 int argc = 3;
292 const char* argv[] = { "tests.exe", "-n", "name" };
293 CHECK(newArgumentParser(argc, argv));
294 CHECK_EQUAL(TestFilter("name"), *args->getNameFilters());
295 }
296
297 TEST(CommandLineArguments, setNameFilterSameParameter)
298 {
299 int argc = 2;
300 const char* argv[] = { "tests.exe", "-nname" };
301 CHECK(newArgumentParser(argc, argv));
302 CHECK_EQUAL(TestFilter("name"), *args->getNameFilters());
303 }
304
305 TEST(CommandLineArguments, setStrictNameFilter)
306 {
307 int argc = 3;
308 const char* argv[] = { "tests.exe", "-sn", "name" };
309 CHECK(newArgumentParser(argc, argv));
310 TestFilter nameFilter("name");
311 nameFilter.strictMatching();
312 CHECK_EQUAL(nameFilter, *args->getNameFilters());
313 }
314
315 TEST(CommandLineArguments, setStrictNameFilterSameParameter)
316 {
317 int argc = 2;
318 const char* argv[] = { "tests.exe", "-snname" };
319 CHECK(newArgumentParser(argc, argv));
320 TestFilter nameFilter("name");
321 nameFilter.strictMatching();
322 CHECK_EQUAL(nameFilter, *args->getNameFilters());
323 }
324
325 TEST(CommandLineArguments, setExcludeNameFilter)
326 {
327 int argc = 3;
328 const char* argv[] = { "tests.exe", "-xn", "name" };
329 CHECK(newArgumentParser(argc, argv));
330 TestFilter nameFilter("name");
331 nameFilter.invertMatching();
332 CHECK_EQUAL(nameFilter, *args->getNameFilters());
333 }
334
335 TEST(CommandLineArguments, setExcludeNameFilterSameParameter)
336 {
337 int argc = 2;
338 const char* argv[] = { "tests.exe", "-xnname" };
339 CHECK(newArgumentParser(argc, argv));
340 TestFilter nameFilter("name");
341 nameFilter.invertMatching();
342 CHECK_EQUAL(nameFilter, *args->getNameFilters());
343 }
344
345 TEST(CommandLineArguments, setExcludeStrictNameFilter)
346 {
347 int argc = 3;
348 const char* argv[] = { "tests.exe", "-xsn", "name" };
349 CHECK(newArgumentParser(argc, argv));
350 TestFilter nameFilter("name");
351 nameFilter.invertMatching();
352 nameFilter.strictMatching();
353 CHECK_EQUAL(nameFilter, *args->getNameFilters());
354 }
355
356 TEST(CommandLineArguments, setExcludeStrictNameFilterSameParameter)
357 {
358 int argc = 2;
359 const char* argv[] = { "tests.exe", "-xsnname" };
360 CHECK(newArgumentParser(argc, argv));
361 TestFilter nameFilter("name");
362 nameFilter.invertMatching();
363 nameFilter.strictMatching();
364 CHECK_EQUAL(nameFilter, *args->getNameFilters());
365 }
366
367 TEST(CommandLineArguments, setTestToRunUsingVerboseOutput)
368 {
369 int argc = 2;
370 const char* argv[] = { "tests.exe", "TEST(testgroup, testname) - stuff" };
371 CHECK(newArgumentParser(argc, argv));
372
373 TestFilter nameFilter("testname");
374 TestFilter groupFilter("testgroup");
375 nameFilter.strictMatching();
376 groupFilter.strictMatching();
377 CHECK_EQUAL(nameFilter, *args->getNameFilters());
378 CHECK_EQUAL(groupFilter, *args->getGroupFilters());
379 }
380
381 TEST(CommandLineArguments, setTestToRunUsingVerboseOutputOfIgnoreTest)
382 {
383 int argc = 2;
384 const char* argv[] = { "tests.exe", "IGNORE_TEST(testgroup, testname) - stuff" };
385 CHECK(newArgumentParser(argc, argv));
386
387 TestFilter nameFilter("testname");
388 TestFilter groupFilter("testgroup");
389 nameFilter.strictMatching();
390 groupFilter.strictMatching();
391 CHECK_EQUAL(nameFilter, *args->getNameFilters());
392 CHECK_EQUAL(groupFilter, *args->getGroupFilters());
393 }
394
395 TEST(CommandLineArguments, setNormalOutput)
396 {
397 int argc = 2;
398 const char* argv[] = { "tests.exe", "-onormal" };
399 CHECK(newArgumentParser(argc, argv));
400 CHECK(args->isEclipseOutput());
401 }
402
403 TEST(CommandLineArguments, setEclipseOutput)
404 {
405 int argc = 2;
406 const char* argv[] = { "tests.exe", "-oeclipse" };
407 CHECK(newArgumentParser(argc, argv));
408 CHECK(args->isEclipseOutput());
409 }
410
411 TEST(CommandLineArguments, setNormalOutputDifferentParameter)
412 {
413 int argc = 3;
414 const char* argv[] = { "tests.exe", "-o", "normal" };
415 CHECK(newArgumentParser(argc, argv));
416 CHECK(args->isEclipseOutput());
417 }
418
419 TEST(CommandLineArguments, setJUnitOutputDifferentParameter)
420 {
421 int argc = 3;
422 const char* argv[] = { "tests.exe", "-o", "junit" };
423 CHECK(newArgumentParser(argc, argv));
424 CHECK(args->isJUnitOutput());
425 }
426
427 TEST(CommandLineArguments, setTeamCityOutputDifferentParameter)
428 {
429 int argc = 3;
430 const char* argv[] = { "tests.exe", "-o", "teamcity" };
431 CHECK(newArgumentParser(argc, argv));
432 CHECK(args->isTeamCityOutput());
433 }
434
435 TEST(CommandLineArguments, setOutputToGarbage)
436 {
437 int argc = 3;
438 const char* argv[] = { "tests.exe", "-o", "garbage" };
439 CHECK(!newArgumentParser(argc, argv));
440 }
441
442 TEST(CommandLineArguments, setPrintGroups)
443 {
444 int argc = 2;
445 const char* argv[] = { "tests.exe", "-lg" };
446 CHECK(newArgumentParser(argc, argv));
447 CHECK(args->isListingTestGroupNames());
448 }
449
450 TEST(CommandLineArguments, setPrintGroupsAndNames)
451 {
452 int argc = 2;
453 const char* argv[] = { "tests.exe", "-ln" };
454 CHECK(newArgumentParser(argc, argv));
455 CHECK(args->isListingTestGroupAndCaseNames());
456 }
457
458 TEST(CommandLineArguments, weirdParamatersReturnsFalse)
459 {
460 int argc = 2;
461 const char* argv[] = { "tests.exe", "-SomethingWeird" };
462 CHECK(!newArgumentParser(argc, argv));
463 }
464
465 TEST(CommandLineArguments, printUsage)
466 {
467 STRCMP_EQUAL("use -h for more extensive help\nusage [-h] [-v] [-vv] [-c] [-p] [-lg] [-ln] [-ri] [-r#]\n"
468 " [-g|sg|xg|xsg groupName]... [-n|sn|xn|xsn testName]... [-t groupName.testName]...\n"
469 " [-b] [-s [randomizerSeed>0]] [\"TEST(groupName, testName)\"]... [-o{normal, junit, teamcity}] [-k packageName]\n",
470 args->usage());
471 }
472
473 TEST(CommandLineArguments, helpPrintsTheHelp)
474 {
475 int argc = 2;
476 const char* argv[] = { "tests.exe", "-h" };
477 CHECK(!newArgumentParser(argc, argv));
478 CHECK(args->needHelp());
479 }
480
481
482 TEST(CommandLineArguments, pluginKnowsOption)
483 {
484 int argc = 2;
485 const char* argv[] = { "tests.exe", "-pPluginOption" };
486 TestRegistry::getCurrentRegistry()->installPlugin(plugin);
487 CHECK(newArgumentParser(argc, argv));
488 TestRegistry::getCurrentRegistry()->removePluginByName("options");
489 }
490
491
492 TEST(CommandLineArguments, checkDefaultArguments)
493 {
494 int argc = 1;
495 const char* argv[] = { "tests.exe" };
496 CHECK(newArgumentParser(argc, argv));
497 CHECK(!args->isVerbose());
498 LONGS_EQUAL(1, args->getRepeatCount());
499 CHECK(NULLPTR == args->getGroupFilters());
500 CHECK(NULLPTR == args->getNameFilters());
501 CHECK(args->isEclipseOutput());
502 CHECK(SimpleString("") == args->getPackageName());
503 }
504
505 TEST(CommandLineArguments, setPackageName)
506 {
507 int argc = 3;
508 const char* argv[] = { "tests.exe", "-k", "package" };
509 CHECK(newArgumentParser(argc, argv));
510 CHECK_EQUAL(SimpleString("package"), args->getPackageName());
511 }
512
513 TEST(CommandLineArguments, lotsOfGroupsAndTests)
514 {
515 int argc = 10;
516 const char* argv[] = { "tests.exe", "-sggroup1", "-xntest1", "-sggroup2", "-sntest2", "-sntest3", "-sggroup3", "-sntest4", "-sggroup4", "-sntest5" };
517 CHECK(newArgumentParser(argc, argv));
518 TestFilter nameFilter("test1");
519 nameFilter.invertMatching();
520 TestFilter groupFilter("group1");
521 groupFilter.strictMatching();
522 CHECK_EQUAL(nameFilter, *args->getNameFilters()->getNext()->getNext()->getNext()->getNext());
523 CHECK_EQUAL(groupFilter, *args->getGroupFilters()->getNext()->getNext()->getNext());
524 }
525
526 TEST(CommandLineArguments, lastParameterFieldMissing)
527 {
528 int argc = 2;
529 const char* argv[] = { "tests.exe", "-k"};
530 CHECK(newArgumentParser(argc, argv));
531 CHECK_EQUAL(SimpleString(""), args->getPackageName());
532 }
533
534 TEST(CommandLineArguments, setOptRun)
535 {
536 int argc = 2;
537 const char* argv[] = { "tests.exe", "-ri"};
538 CHECK(newArgumentParser(argc, argv));
539 CHECK(args->isRunIgnored());
540 }
541