Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/src/catch2/internal/catch_reporter_spec_parser.cpp
diff options
context:
space:
mode:
authorMartin Hořeňovský2022-04-04 17:06:47 +0200
committerMartin Hořeňovský2022-04-05 15:45:54 +0200
commita51fd07bd006101c90662317c98de0ebe9e97312 (patch)
tree7c732c3a6e8df9ab6593dbd551aecc09a782080b /src/catch2/internal/catch_reporter_spec_parser.cpp
parent8ac86495de5bf249fd50775335f62eedb77815d6 (diff)
downloadcatch3-a51fd07bd006101c90662317c98de0ebe9e97312.tar.gz
Add helper for parsing the new reporter specs
The new reporter spec generalizes key-value options that can be passed to the reporter, looking like this `reporterName[::key=value]*`. A key can be either Catch2-recognized, which currently means either `out` or `colour`, or reporter-specific which is anything prefixed with `X`, e.g. `Xfoo`.
Diffstat (limited to 'src/catch2/internal/catch_reporter_spec_parser.cpp')
-rw-r--r--src/catch2/internal/catch_reporter_spec_parser.cpp93
1 files changed, 93 insertions, 0 deletions
diff --git a/src/catch2/internal/catch_reporter_spec_parser.cpp b/src/catch2/internal/catch_reporter_spec_parser.cpp
index 9f896ae2..a4f7105e 100644
--- a/src/catch2/internal/catch_reporter_spec_parser.cpp
+++ b/src/catch2/internal/catch_reporter_spec_parser.cpp
@@ -9,9 +9,28 @@
#include <catch2/internal/catch_reporter_spec_parser.hpp>
#include <catch2/interfaces/catch_interfaces_config.hpp>
+#include <catch2/internal/catch_move_and_forward.hpp>
+
+#include <algorithm>
+#include <ostream>
namespace Catch {
+ namespace {
+ struct kvPair {
+ StringRef key, value;
+ };
+
+ kvPair splitKVPair(StringRef kvString) {
+ auto splitPos = static_cast<size_t>( std::distance(
+ kvString.begin(),
+ std::find( kvString.begin(), kvString.end(), '=' ) ) );
+
+ return { kvString.substr( 0, splitPos ),
+ kvString.substr( splitPos + 1, kvString.size() ) };
+ }
+ }
+
namespace Detail {
std::vector<std::string> splitReporterSpec( StringRef reporterSpec ) {
static constexpr auto separator = "::";
@@ -78,4 +97,78 @@ namespace Catch {
} // namespace Detail
+ bool operator==( ReporterSpec const& lhs, ReporterSpec const& rhs ) {
+ return lhs.m_name == rhs.m_name &&
+ lhs.m_outputFileName == rhs.m_outputFileName &&
+ lhs.m_colourMode == rhs.m_colourMode &&
+ lhs.m_customOptions == rhs.m_customOptions;
+ }
+
+ Optional<ReporterSpec> parseReporterSpec( StringRef reporterSpec ) {
+ auto parts = Detail::splitReporterSpec( reporterSpec );
+
+ assert( parts.size() > 0 && "Split should never return empty vector" );
+
+ std::map<std::string, std::string> kvPairs;
+ Optional<std::string> outputFileName;
+ Optional<ColourMode> colourMode;
+
+ // First part is always reporter name, so we skip it
+ for ( size_t i = 1; i < parts.size(); ++i ) {
+ auto kv = splitKVPair( parts[i] );
+ auto key = kv.key, value = kv.value;
+
+ if ( key.empty() || value.empty() ) {
+ return {};
+ } else if ( key[0] == 'X' ) {
+ // This is a reporter-specific option, we don't check these
+ // apart from basic sanity checks
+ if ( key.size() == 1 ) {
+ return {};
+ }
+
+ auto ret = kvPairs.emplace( kv.key, kv.value );
+ if ( !ret.second ) {
+ // Duplicated key. We might want to handle this differently,
+ // e.g. by overwriting the existing value?
+ return {};
+ }
+ } else if ( key == "out" ) {
+ // Duplicated key
+ if ( outputFileName ) {
+ return {};
+ }
+ outputFileName = static_cast<std::string>( value );
+ } else if ( key == "colour" ) {
+ // Duplicated key
+ if ( colourMode ) {
+ return {};
+ }
+ colourMode = Detail::stringToColourMode( value );
+ // Parsing failed
+ if ( !colourMode ) {
+ return {};
+ }
+ } else {
+ // Unrecognized option
+ return {};
+ }
+ }
+
+ return ReporterSpec{ CATCH_MOVE( parts[0] ),
+ CATCH_MOVE( outputFileName ),
+ CATCH_MOVE( colourMode ),
+ CATCH_MOVE( kvPairs ) };
+ }
+
+ReporterSpec::ReporterSpec(
+ std::string name,
+ Optional<std::string> outputFileName,
+ Optional<ColourMode> colourMode,
+ std::map<std::string, std::string> customOptions ):
+ m_name( CATCH_MOVE( name ) ),
+ m_outputFileName( CATCH_MOVE( outputFileName ) ),
+ m_colourMode( CATCH_MOVE( colourMode ) ),
+ m_customOptions( CATCH_MOVE( customOptions ) ) {}
+
} // namespace Catch