Debian Codegraph @

src/external/catch2/tests/SelfTest/IntrospectiveTests/CmdLine.tests.cpp

raw
1
2 // Copyright Catch2 Authors
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE.txt or copy at
5 // https://www.boost.org/LICENSE_1_0.txt)
6
7 // SPDX-License-Identifier: BSL-1.0
8
9 #include <catch2/catch_config.hpp>
10 #include <catch2/catch_approx.hpp>
11 #include <catch2/catch_test_macros.hpp>
12 #include <catch2/matchers/catch_matchers_string.hpp>
13 #include <catch2/internal/catch_test_spec_parser.hpp>
14 #include <catch2/catch_user_config.hpp>
15 #include <catch2/catch_test_case_info.hpp>
16 #include <catch2/internal/catch_commandline.hpp>
17 #include <catch2/generators/catch_generators.hpp>
18 #include <catch2/internal/catch_compiler_capabilities.hpp>
19
20
21 namespace {
22 auto fakeTestCase(const char* name, const char* desc = "") { return Catch::makeTestCaseInfo("", { name, desc }, CATCH_INTERNAL_LINEINFO); }
23 }
24
25 TEST_CASE( "Process can be configured on command line", "[config][command-line]" ) {
26
27 using namespace Catch::Matchers;
28
29 Catch::ConfigData config;
30 auto cli = Catch::makeCommandLineParser(config);
31
32 SECTION("empty args don't cause a crash") {
33 auto result = cli.parse({""});
34 CHECK(result);
35 CHECK(config.processName == "");
36 }
37
38 SECTION("default - no arguments") {
39 auto result = cli.parse({"test"});
40 CHECK(result);
41 CHECK(config.processName == "test");
42 CHECK(config.shouldDebugBreak == false);
43 CHECK(config.abortAfter == -1);
44 CHECK(config.noThrow == false);
45 CHECK( config.reporterSpecifications.empty() );
46
47 Catch::Config cfg(config);
48 CHECK_FALSE(cfg.hasTestFilters());
49
50 // The Config is responsible for mixing in the default reporter
51 auto expectedReporter =
52 #if defined( CATCH_CONFIG_DEFAULT_REPORTER )
53 CATCH_CONFIG_DEFAULT_REPORTER
54 #else
55 "console"
56 #endif
57 ;
58
59 CHECK( cfg.getReporterSpecs().size() == 1 );
60 CHECK( cfg.getReporterSpecs()[0] ==
61 Catch::ReporterSpec{ expectedReporter, {}, {}, {} } );
62 CHECK( cfg.getProcessedReporterSpecs().size() == 1 );
63 CHECK( cfg.getProcessedReporterSpecs()[0] ==
64 Catch::ProcessedReporterSpec{ expectedReporter,
65 std::string{},
66 Catch::ColourMode::PlatformDefault,
67 {} } );
68 }
69
70 SECTION("test lists") {
71 SECTION("Specify one test case using") {
72 auto result = cli.parse({"test", "test1"});
73 CHECK(result);
74
75 Catch::Config cfg(config);
76 REQUIRE(cfg.hasTestFilters());
77 REQUIRE(cfg.testSpec().matches(*fakeTestCase("notIncluded")) == false);
78 REQUIRE(cfg.testSpec().matches(*fakeTestCase("test1")));
79 }
80 SECTION("Specify one test case exclusion using exclude:") {
81 auto result = cli.parse({"test", "exclude:test1"});
82 CHECK(result);
83
84 Catch::Config cfg(config);
85 REQUIRE(cfg.hasTestFilters());
86 REQUIRE(cfg.testSpec().matches(*fakeTestCase("test1")) == false);
87 REQUIRE(cfg.testSpec().matches(*fakeTestCase("alwaysIncluded")));
88 }
89
90 SECTION("Specify one test case exclusion using ~") {
91 auto result = cli.parse({"test", "~test1"});
92 CHECK(result);
93
94 Catch::Config cfg(config);
95 REQUIRE(cfg.hasTestFilters());
96 REQUIRE(cfg.testSpec().matches(*fakeTestCase("test1")) == false);
97 REQUIRE(cfg.testSpec().matches(*fakeTestCase("alwaysIncluded")));
98 }
99
100 }
101
102 SECTION("reporter") {
103 using vec_Specs = std::vector<Catch::ReporterSpec>;
104 using namespace std::string_literals;
105 SECTION("-r/console") {
106 auto result = cli.parse({"test", "-r", "console"});
107 CAPTURE(result.errorMessage());
108 CHECK(result);
109
110 REQUIRE( config.reporterSpecifications ==
111 vec_Specs{ { "console", {}, {}, {} } } );
112 }
113 SECTION("-r/xml") {
114 auto result = cli.parse({"test", "-r", "xml"});
115 CAPTURE(result.errorMessage());
116 CHECK(result);
117
118 REQUIRE( config.reporterSpecifications ==
119 vec_Specs{ { "xml", {}, {}, {} } } );
120 }
121 SECTION("--reporter/junit") {
122 auto result = cli.parse({"test", "--reporter", "junit"});
123 CAPTURE(result.errorMessage());
124 CHECK(result);
125
126 REQUIRE( config.reporterSpecifications ==
127 vec_Specs{ { "junit", {}, {}, {} } } );
128 }
129 SECTION("must match one of the available ones") {
130 auto result = cli.parse({"test", "--reporter", "unsupported"});
131 CHECK(!result);
132
133 REQUIRE_THAT(result.errorMessage(), ContainsSubstring("Unrecognized reporter"));
134 }
135 SECTION("With output file") {
136 auto result = cli.parse({ "test", "-r", "console::out=out.txt" });
137 CAPTURE(result.errorMessage());
138 CHECK(result);
139 REQUIRE( config.reporterSpecifications ==
140 vec_Specs{ { "console", "out.txt"s, {}, {} } } );
141 }
142 SECTION("With Windows-like absolute path as output file") {
143 auto result = cli.parse({ "test", "-r", "console::out=C:\\Temp\\out.txt" });
144 CAPTURE(result.errorMessage());
145 CHECK(result);
146 REQUIRE( config.reporterSpecifications ==
147 vec_Specs{ { "console", "C:\\Temp\\out.txt"s, {}, {} } } );
148 }
149 SECTION("Multiple reporters") {
150 SECTION("All with output files") {
151 CHECK(cli.parse({ "test", "-r", "xml::out=output.xml", "-r", "junit::out=output-junit.xml" }));
152 REQUIRE( config.reporterSpecifications ==
153 vec_Specs{ { "xml", "output.xml"s, {}, {} },
154 { "junit", "output-junit.xml"s, {}, {} } } );
155 }
156 SECTION("Mixed output files and default output") {
157 CHECK(cli.parse({ "test", "-r", "xml::out=output.xml", "-r", "console" }));
158 REQUIRE( config.reporterSpecifications ==
159 vec_Specs{ { "xml", "output.xml"s, {}, {} },
160 { "console", {}, {}, {} } } );
161 }
162 SECTION("cannot have multiple reporters with default output") {
163 auto result = cli.parse({ "test", "-r", "console", "-r", "xml::out=output.xml", "-r", "junit" });
164 CHECK(!result);
165 REQUIRE_THAT(result.errorMessage(), ContainsSubstring("Only one reporter may have unspecified output file."));
166 }
167 }
168 }
169
170 SECTION("debugger") {
171 SECTION("-b") {
172 CHECK(cli.parse({"test", "-b"}));
173
174 REQUIRE(config.shouldDebugBreak == true);
175 }
176 SECTION("--break") {
177 CHECK(cli.parse({"test", "--break"}));
178
179 REQUIRE(config.shouldDebugBreak);
180 }
181 }
182
183
184 SECTION("abort") {
185 SECTION("-a aborts after first failure") {
186 CHECK(cli.parse({"test", "-a"}));
187
188 REQUIRE(config.abortAfter == 1);
189 }
190 SECTION("-x 2 aborts after two failures") {
191 CHECK(cli.parse({"test", "-x", "2"}));
192
193 REQUIRE(config.abortAfter == 2);
194 }
195 SECTION("-x must be numeric") {
196 auto result = cli.parse({"test", "-x", "oops"});
197 CHECK(!result);
198 REQUIRE_THAT(result.errorMessage(), ContainsSubstring("convert") && ContainsSubstring("oops"));
199 }
200
201 SECTION("wait-for-keypress") {
202 SECTION("Accepted options") {
203 using tuple_type = std::tuple<char const*, Catch::WaitForKeypress::When>;
204 auto input = GENERATE(table<char const*, Catch::WaitForKeypress::When>({
205 tuple_type{"never", Catch::WaitForKeypress::Never},
206 tuple_type{"start", Catch::WaitForKeypress::BeforeStart},
207 tuple_type{"exit", Catch::WaitForKeypress::BeforeExit},
208 tuple_type{"both", Catch::WaitForKeypress::BeforeStartAndExit},
209 }));
210 CHECK(cli.parse({"test", "--wait-for-keypress", std::get<0>(input)}));
211
212 REQUIRE(config.waitForKeypress == std::get<1>(input));
213 }
214
215 SECTION("invalid options are reported") {
216 auto result = cli.parse({"test", "--wait-for-keypress", "sometimes"});
217 CHECK(!result);
218
219 #ifndef CATCH_CONFIG_DISABLE_MATCHERS
220 REQUIRE_THAT(result.errorMessage(), ContainsSubstring("never") && ContainsSubstring("both"));
221 #endif
222 }
223 }
224 }
225
226 SECTION("nothrow") {
227 SECTION("-e") {
228 CHECK(cli.parse({"test", "-e"}));
229
230 REQUIRE(config.noThrow);
231 }
232 SECTION("--nothrow") {
233 CHECK(cli.parse({"test", "--nothrow"}));
234
235 REQUIRE(config.noThrow);
236 }
237 }
238
239 SECTION("output filename") {
240 SECTION("-o filename") {
241 CHECK(cli.parse({"test", "-o", "filename.ext"}));
242
243 REQUIRE(config.defaultOutputFilename == "filename.ext");
244 }
245 SECTION("--out") {
246 CHECK(cli.parse({"test", "--out", "filename.ext"}));
247
248 REQUIRE(config.defaultOutputFilename == "filename.ext");
249 }
250 }
251
252 SECTION("combinations") {
253 SECTION("Single character flags can be combined") {
254 CHECK(cli.parse({"test", "-abe"}));
255
256 CHECK(config.abortAfter == 1);
257 CHECK(config.shouldDebugBreak);
258 CHECK(config.noThrow == true);
259 }
260 }
261
262
263 SECTION( "use-colour") {
264
265 using Catch::ColourMode;
266
267 SECTION( "without option" ) {
268 CHECK(cli.parse({"test"}));
269
270 REQUIRE( config.defaultColourMode == ColourMode::PlatformDefault );
271 }
272
273 SECTION( "auto" ) {
274 CHECK( cli.parse( { "test", "--colour-mode", "default" } ) );
275
276 REQUIRE( config.defaultColourMode == ColourMode::PlatformDefault );
277 }
278
279 SECTION( "yes" ) {
280 CHECK(cli.parse({"test", "--colour-mode", "ansi"}));
281
282 REQUIRE( config.defaultColourMode == ColourMode::ANSI );
283 }
284
285 SECTION( "no" ) {
286 CHECK(cli.parse({"test", "--colour-mode", "none"}));
287
288 REQUIRE( config.defaultColourMode == ColourMode::None );
289 }
290
291 SECTION( "error" ) {
292 auto result = cli.parse({"test", "--colour-mode", "wrong"});
293 CHECK( !result );
294 CHECK_THAT( result.errorMessage(), ContainsSubstring( "colour mode must be one of" ) );
295 }
296 }
297
298 SECTION("Benchmark options") {
299 SECTION("samples") {
300 CHECK(cli.parse({ "test", "--benchmark-samples=200" }));
301
302 REQUIRE(config.benchmarkSamples == 200);
303 }
304
305 SECTION("resamples") {
306 CHECK(cli.parse({ "test", "--benchmark-resamples=20000" }));
307
308 REQUIRE(config.benchmarkResamples == 20000);
309 }
310
311 SECTION("confidence-interval") {
312 CHECK(cli.parse({ "test", "--benchmark-confidence-interval=0.99" }));
313
314 REQUIRE(config.benchmarkConfidenceInterval == Catch::Approx(0.99));
315 }
316
317 SECTION("no-analysis") {
318 CHECK(cli.parse({ "test", "--benchmark-no-analysis" }));
319
320 REQUIRE(config.benchmarkNoAnalysis);
321 }
322
323 SECTION("warmup-time") {
324 CHECK(cli.parse({ "test", "--benchmark-warmup-time=10" }));
325
326 REQUIRE(config.benchmarkWarmupTime == 10);
327 }
328 }
329 }
330
331 TEST_CASE("Parsing sharding-related cli flags", "[sharding]") {
332 using namespace Catch::Matchers;
333
334 Catch::ConfigData config;
335 auto cli = Catch::makeCommandLineParser(config);
336
337 SECTION("shard-count") {
338 CHECK(cli.parse({ "test", "--shard-count=8" }));
339
340 REQUIRE(config.shardCount == 8);
341 }
342
343 SECTION("Negative shard count reports error") {
344 auto result = cli.parse({ "test", "--shard-count=-1" });
345
346 CHECK_FALSE(result);
347 REQUIRE_THAT(
348 result.errorMessage(),
349 ContainsSubstring( "Could not parse '-1' as shard count" ) );
350 }
351
352 SECTION("Zero shard count reports error") {
353 auto result = cli.parse({ "test", "--shard-count=0" });
354
355 CHECK_FALSE(result);
356 REQUIRE_THAT(
357 result.errorMessage(),
358 ContainsSubstring( "Shard count must be positive" ) );
359 }
360
361 SECTION("shard-index") {
362 CHECK(cli.parse({ "test", "--shard-index=2" }));
363
364 REQUIRE(config.shardIndex == 2);
365 }
366
367 SECTION("Negative shard index reports error") {
368 auto result = cli.parse({ "test", "--shard-index=-12" });
369
370 CHECK_FALSE(result);
371 REQUIRE_THAT(
372 result.errorMessage(),
373 ContainsSubstring( "Could not parse '-12' as shard index" ) );
374 }
375
376 SECTION("Shard index 0 is accepted") {
377 CHECK(cli.parse({ "test", "--shard-index=0" }));
378
379 REQUIRE(config.shardIndex == 0);
380 }
381 }
382
383 TEST_CASE( "Parsing warnings", "[cli][warnings]" ) {
384 using Catch::WarnAbout;
385
386 Catch::ConfigData config;
387 auto cli = Catch::makeCommandLineParser( config );
388
389 SECTION( "NoAssertions" ) {
390 REQUIRE(cli.parse( { "test", "-w", "NoAssertions" } ));
391 REQUIRE( config.warnings == WarnAbout::NoAssertions );
392 }
393 SECTION( "NoTests is no longer supported" ) {
394 REQUIRE_FALSE(cli.parse( { "test", "-w", "NoTests" } ));
395 }
396 SECTION( "Combining multiple warnings" ) {
397 REQUIRE( cli.parse( { "test",
398 "--warn", "NoAssertions",
399 "--warn", "UnmatchedTestSpec" } ) );
400
401 REQUIRE( config.warnings == ( WarnAbout::NoAssertions | WarnAbout::UnmatchedTestSpec ) );
402 }
403 }
404
405 TEST_CASE("Test with special, characters \"in name", "[cli][regression]") {
406 // This test case succeeds if we can invoke it from the CLI
407 SUCCEED();
408 }
409
410 TEST_CASE("Various suspicious reporter specs are rejected",
411 "[cli][reporter-spec][approvals]") {
412 Catch::ConfigData config;
413 auto cli = Catch::makeCommandLineParser( config );
414
415 auto spec = GENERATE( as<std::string>{},
416 "",
417 "::console",
418 "console::",
419 "console::some-file::",
420 "::console::some-file::" );
421 CAPTURE( spec );
422
423 auto result = cli.parse( { "test", "--reporter", spec } );
424 REQUIRE_FALSE( result );
425 }
426
427 TEST_CASE("Win32 colour implementation is compile-time optional",
428 "[approvals][cli][colours]") {
429 Catch::ConfigData config;
430 auto cli = Catch::makeCommandLineParser( config );
431
432 auto result = cli.parse( { "test", "--colour-mode", "win32" } );
433
434 #if defined( CATCH_CONFIG_COLOUR_WIN32 )
435 REQUIRE( result );
436 #else
437 REQUIRE_FALSE( result );
438 #endif
439 }
440
441 TEST_CASE( "Parse rng seed in different formats", "[approvals][cli][rng-seed]" ) {
442 Catch::ConfigData config;
443 auto cli = Catch::makeCommandLineParser( config );
444
445 SECTION("well formed cases") {
446 char const* seed_string;
447 uint32_t seed_value;
448 // GCC-5 workaround
449 using gen_type = std::tuple<char const*, uint32_t>;
450 std::tie( seed_string, seed_value ) = GENERATE( table<char const*, uint32_t>({
451 gen_type{ "0xBEEF", 0xBEEF },
452 gen_type{ "12345678", 12345678 }
453 } ) );
454 CAPTURE( seed_string );
455
456 auto result = cli.parse( { "tests", "--rng-seed", seed_string } );
457
458 REQUIRE( result );
459 REQUIRE( config.rngSeed == seed_value );
460 }
461 SECTION( "Error cases" ) {
462 auto seed_string =
463 GENERATE( "0xSEED", "999999999999", "08888", "BEEF", "123 456" );
464 CAPTURE( seed_string );
465 REQUIRE_FALSE( cli.parse( { "tests", "--rng-seed", seed_string } ) );
466 }
467 }