mirror of
https://github.com/catchorg/Catch2.git
synced 2024-12-22 11:23:29 +01:00
Refactor how the RNG seed is parsed when handling cmdline
This commit is contained in:
parent
d1e7544e9f
commit
f1361ef624
@ -13,6 +13,7 @@
|
||||
#include <catch2/interfaces/catch_interfaces_registry_hub.hpp>
|
||||
#include <catch2/interfaces/catch_interfaces_reporter_registry.hpp>
|
||||
#include <catch2/internal/catch_console_colour.hpp>
|
||||
#include <catch2/internal/catch_parse_numbers.hpp>
|
||||
#include <catch2/internal/catch_reporter_spec_parser.hpp>
|
||||
|
||||
#include <fstream>
|
||||
@ -77,23 +78,14 @@ namespace Catch {
|
||||
return ParserResult::ok(ParseResultType::Matched);
|
||||
}
|
||||
|
||||
CATCH_TRY {
|
||||
std::size_t parsedTo = 0;
|
||||
unsigned long parsedSeed = std::stoul(seed, &parsedTo, 0);
|
||||
if (parsedTo != seed.size()) {
|
||||
return ParserResult::runtimeError("Could not parse '" + seed + "' as seed");
|
||||
}
|
||||
|
||||
// TODO: Ideally we could parse unsigned int directly,
|
||||
// but the stdlib doesn't provide helper for that
|
||||
// type. After this is refactored to use fixed size
|
||||
// type, we should check the parsed value is in range
|
||||
// of the underlying type.
|
||||
config.rngSeed = static_cast<unsigned int>(parsedSeed);
|
||||
return ParserResult::ok(ParseResultType::Matched);
|
||||
} CATCH_CATCH_ANON(std::exception const&) {
|
||||
return ParserResult::runtimeError("Could not parse '" + seed + "' as seed");
|
||||
// TODO: ideally we should be parsing uint32_t directly
|
||||
// fix this later when we add new parse overload
|
||||
auto parsedSeed = parseUInt( seed, 0 );
|
||||
if ( !parsedSeed ) {
|
||||
return ParserResult::runtimeError( "Could not parse '" + seed + "' as seed" );
|
||||
}
|
||||
config.rngSeed = *parsedSeed;
|
||||
return ParserResult::ok( ParseResultType::Matched );
|
||||
};
|
||||
auto const setDefaultColourMode = [&]( std::string const& colourMode ) {
|
||||
Optional<ColourMode> maybeMode = Catch::Detail::stringToColourMode(toLower( colourMode ));
|
||||
|
@ -737,3 +737,31 @@ TEST_CASE("Win32 colour implementation is compile-time optional",
|
||||
REQUIRE_FALSE( result );
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST_CASE( "Parse rng seed in different formats", "[approvals][cli][rng-seed]" ) {
|
||||
Catch::ConfigData config;
|
||||
auto cli = Catch::makeCommandLineParser( config );
|
||||
|
||||
SECTION("well formed cases") {
|
||||
char const* seed_string;
|
||||
uint32_t seed_value;
|
||||
// GCC-5 workaround
|
||||
using gen_type = std::tuple<char const*, uint32_t>;
|
||||
std::tie( seed_string, seed_value ) = GENERATE( table<char const*, uint32_t>({
|
||||
gen_type{ "0xBEEF", 0xBEEF },
|
||||
gen_type{ "12345678", 12345678 }
|
||||
} ) );
|
||||
CAPTURE( seed_string );
|
||||
|
||||
auto result = cli.parse( { "tests", "--rng-seed", seed_string } );
|
||||
|
||||
REQUIRE( result );
|
||||
REQUIRE( config.rngSeed == seed_value );
|
||||
}
|
||||
SECTION( "Error cases" ) {
|
||||
auto seed_string =
|
||||
GENERATE( "0xSEED", "999999999999", "08888", "BEEF", "123 456" );
|
||||
CAPTURE( seed_string );
|
||||
REQUIRE_FALSE( cli.parse( { "tests", "--rng-seed", seed_string } ) );
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user