From cf4d84a349e664307060e1721d6b3fe4439cf32f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Tue, 14 Mar 2023 21:15:13 +0100 Subject: [PATCH] Unify ITagAliasRegistry and TagAliasRegistry --- src/CMakeLists.txt | 1 - src/catch2/catch_config.cpp | 4 +-- src/catch2/catch_registry_hub.cpp | 2 +- .../interfaces/catch_interfaces_all.hpp | 1 - .../catch_interfaces_registry_hub.hpp | 4 +-- .../catch_interfaces_tag_alias_registry.hpp | 29 ------------------- .../internal/catch_tag_alias_registry.cpp | 24 +++++++++------ .../internal/catch_tag_alias_registry.hpp | 20 ++++++++----- .../internal/catch_test_spec_parser.cpp | 4 +-- .../internal/catch_test_spec_parser.hpp | 6 ++-- src/catch2/meson.build | 1 - tests/SelfTest/helpers/parse_test_spec.cpp | 4 +-- 12 files changed, 40 insertions(+), 60 deletions(-) delete mode 100644 src/catch2/interfaces/catch_interfaces_tag_alias_registry.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9f750031..9bef6cbb 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -216,7 +216,6 @@ set(INTERFACE_HEADERS ${SOURCES_DIR}/interfaces/catch_interfaces_registry_hub.hpp ${SOURCES_DIR}/interfaces/catch_interfaces_reporter.hpp ${SOURCES_DIR}/interfaces/catch_interfaces_reporter_factory.hpp - ${SOURCES_DIR}/interfaces/catch_interfaces_tag_alias_registry.hpp ${SOURCES_DIR}/interfaces/catch_interfaces_testcase.hpp ) set(INTERFACE_SOURCES diff --git a/src/catch2/catch_config.cpp b/src/catch2/catch_config.cpp index eb4f5ad3..ceedb008 100644 --- a/src/catch2/catch_config.cpp +++ b/src/catch2/catch_config.cpp @@ -13,7 +13,7 @@ #include #include #include -#include +#include #include #include @@ -123,7 +123,7 @@ namespace Catch { // Bazel support can modify the test specs, so parsing has to happen // after reading Bazel env vars. - TestSpecParser parser( ITagAliasRegistry::get() ); + TestSpecParser parser( TagAliasRegistry::get() ); if ( !m_data.testsOrTags.empty() ) { m_hasTestFilters = true; for ( auto const& testOrTags : m_data.testsOrTags ) { diff --git a/src/catch2/catch_registry_hub.cpp b/src/catch2/catch_registry_hub.cpp index d12c7ba7..4db9c7e0 100644 --- a/src/catch2/catch_registry_hub.cpp +++ b/src/catch2/catch_registry_hub.cpp @@ -41,7 +41,7 @@ namespace Catch { ExceptionTranslatorRegistry const& getExceptionTranslatorRegistry() const override { return m_exceptionTranslatorRegistry; } - ITagAliasRegistry const& getTagAliasRegistry() const override { + TagAliasRegistry const& getTagAliasRegistry() const override { return m_tagAliasRegistry; } StartupExceptionRegistry const& getStartupExceptionRegistry() const override { diff --git a/src/catch2/interfaces/catch_interfaces_all.hpp b/src/catch2/interfaces/catch_interfaces_all.hpp index df9e53e6..b1b8e48a 100644 --- a/src/catch2/interfaces/catch_interfaces_all.hpp +++ b/src/catch2/interfaces/catch_interfaces_all.hpp @@ -29,7 +29,6 @@ #include #include #include -#include #include #endif // CATCH_INTERFACES_ALL_HPP_INCLUDED diff --git a/src/catch2/interfaces/catch_interfaces_registry_hub.hpp b/src/catch2/interfaces/catch_interfaces_registry_hub.hpp index d225e48a..ba9c34f7 100644 --- a/src/catch2/interfaces/catch_interfaces_registry_hub.hpp +++ b/src/catch2/interfaces/catch_interfaces_registry_hub.hpp @@ -21,7 +21,7 @@ namespace Catch { class IExceptionTranslator; class ReporterRegistry; class IReporterFactory; - class ITagAliasRegistry; + class TagAliasRegistry; class ITestInvoker; class EnumValuesRegistry; struct SourceLineInfo; @@ -37,7 +37,7 @@ namespace Catch { virtual ReporterRegistry const& getReporterRegistry() const = 0; virtual ITestCaseRegistry const& getTestCaseRegistry() const = 0; - virtual ITagAliasRegistry const& getTagAliasRegistry() const = 0; + virtual TagAliasRegistry const& getTagAliasRegistry() const = 0; virtual ExceptionTranslatorRegistry const& getExceptionTranslatorRegistry() const = 0; diff --git a/src/catch2/interfaces/catch_interfaces_tag_alias_registry.hpp b/src/catch2/interfaces/catch_interfaces_tag_alias_registry.hpp deleted file mode 100644 index 5da0f8d1..00000000 --- a/src/catch2/interfaces/catch_interfaces_tag_alias_registry.hpp +++ /dev/null @@ -1,29 +0,0 @@ - -// Copyright Catch2 Authors -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE.txt or copy at -// https://www.boost.org/LICENSE_1_0.txt) - -// SPDX-License-Identifier: BSL-1.0 -#ifndef CATCH_INTERFACES_TAG_ALIAS_REGISTRY_HPP_INCLUDED -#define CATCH_INTERFACES_TAG_ALIAS_REGISTRY_HPP_INCLUDED - -#include - -namespace Catch { - - struct TagAlias; - - class ITagAliasRegistry { - public: - virtual ~ITagAliasRegistry(); // = default - // Nullptr if not present - virtual TagAlias const* find( std::string const& alias ) const = 0; - virtual std::string expandAliases( std::string const& unexpandedTestSpec ) const = 0; - - static ITagAliasRegistry const& get(); - }; - -} // end namespace Catch - -#endif // CATCH_INTERFACES_TAG_ALIAS_REGISTRY_HPP_INCLUDED diff --git a/src/catch2/internal/catch_tag_alias_registry.cpp b/src/catch2/internal/catch_tag_alias_registry.cpp index b7c6b9ec..f73f4799 100644 --- a/src/catch2/internal/catch_tag_alias_registry.cpp +++ b/src/catch2/internal/catch_tag_alias_registry.cpp @@ -11,13 +11,21 @@ #include #include -namespace Catch { +#include - TagAliasRegistry::~TagAliasRegistry() {} +namespace Catch { + struct TagAliasRegistry::TagAliasRegistryImpl { + std::map registry; + }; + + + TagAliasRegistry::TagAliasRegistry(): + m_impl( Detail::make_unique() ){} + TagAliasRegistry::~TagAliasRegistry() = default; TagAlias const* TagAliasRegistry::find( std::string const& alias ) const { - auto it = m_registry.find( alias ); - if( it != m_registry.end() ) + auto it = m_impl->registry.find( alias ); + if( it != m_impl->registry.end() ) return &(it->second); else return nullptr; @@ -25,7 +33,7 @@ namespace Catch { std::string TagAliasRegistry::expandAliases( std::string const& unexpandedTestSpec ) const { std::string expandedTestSpec = unexpandedTestSpec; - for( auto const& registryKvp : m_registry ) { + for( auto const& registryKvp : m_impl->registry ) { std::size_t pos = expandedTestSpec.find( registryKvp.first ); if( pos != std::string::npos ) { expandedTestSpec = expandedTestSpec.substr( 0, pos ) + @@ -40,15 +48,13 @@ namespace Catch { CATCH_ENFORCE( startsWith(alias, "[@") && endsWith(alias, ']'), "error: tag alias, '" << alias << "' is not of the form [@alias name].\n" << lineInfo ); - CATCH_ENFORCE( m_registry.insert(std::make_pair(alias, TagAlias(tag, lineInfo))).second, + CATCH_ENFORCE( m_impl->registry.insert(std::make_pair(alias, TagAlias(tag, lineInfo))).second, "error: tag alias, '" << alias << "' already registered.\n" << "\tFirst seen at: " << find(alias)->lineInfo << "\n" << "\tRedefined at: " << lineInfo ); } - ITagAliasRegistry::~ITagAliasRegistry() = default; - - ITagAliasRegistry const& ITagAliasRegistry::get() { + TagAliasRegistry const& TagAliasRegistry::get() { return getRegistryHub().getTagAliasRegistry(); } diff --git a/src/catch2/internal/catch_tag_alias_registry.hpp b/src/catch2/internal/catch_tag_alias_registry.hpp index 64c0f8f3..93571711 100644 --- a/src/catch2/internal/catch_tag_alias_registry.hpp +++ b/src/catch2/internal/catch_tag_alias_registry.hpp @@ -8,24 +8,30 @@ #ifndef CATCH_TAG_ALIAS_REGISTRY_HPP_INCLUDED #define CATCH_TAG_ALIAS_REGISTRY_HPP_INCLUDED -#include #include +#include -#include #include namespace Catch { struct SourceLineInfo; - class TagAliasRegistry : public ITagAliasRegistry { + class TagAliasRegistry { + struct TagAliasRegistryImpl; + Detail::unique_ptr m_impl; public: - ~TagAliasRegistry() override; - TagAlias const* find( std::string const& alias ) const override; - std::string expandAliases( std::string const& unexpandedTestSpec ) const override; + TagAliasRegistry(); + ~TagAliasRegistry(); // = default; + + //! Nullptr if not present + TagAlias const* find( std::string const& alias ) const; + //! Returns the test spec but with expanded aliases + std::string expandAliases( std::string const& unexpandedTestSpec ) const; void add( std::string const& alias, std::string const& tag, SourceLineInfo const& lineInfo ); + static TagAliasRegistry const& get(); + private: - std::map m_registry; }; } // end namespace Catch diff --git a/src/catch2/internal/catch_test_spec_parser.cpp b/src/catch2/internal/catch_test_spec_parser.cpp index d6e4cb58..9c1b812c 100644 --- a/src/catch2/internal/catch_test_spec_parser.cpp +++ b/src/catch2/internal/catch_test_spec_parser.cpp @@ -8,13 +8,13 @@ #include #include -#include +#include #include namespace Catch { - TestSpecParser::TestSpecParser( ITagAliasRegistry const& tagAliases ) : m_tagAliases( &tagAliases ) {} + TestSpecParser::TestSpecParser( TagAliasRegistry const& tagAliases ) : m_tagAliases( &tagAliases ) {} TestSpecParser& TestSpecParser::parse( std::string const& arg ) { m_mode = None; diff --git a/src/catch2/internal/catch_test_spec_parser.hpp b/src/catch2/internal/catch_test_spec_parser.hpp index aa2917db..13996e63 100644 --- a/src/catch2/internal/catch_test_spec_parser.hpp +++ b/src/catch2/internal/catch_test_spec_parser.hpp @@ -20,7 +20,7 @@ namespace Catch { - class ITagAliasRegistry; + class TagAliasRegistry; class TestSpecParser { enum Mode{ None, Name, QuotedName, Tag, EscapedName }; @@ -35,10 +35,10 @@ namespace Catch { std::vector m_escapeChars; TestSpec::Filter m_currentFilter; TestSpec m_testSpec; - ITagAliasRegistry const* m_tagAliases = nullptr; + TagAliasRegistry const* m_tagAliases = nullptr; public: - TestSpecParser( ITagAliasRegistry const& tagAliases ); + TestSpecParser( TagAliasRegistry const& tagAliases ); TestSpecParser& parse( std::string const& arg ); TestSpec testSpec(); diff --git a/src/catch2/meson.build b/src/catch2/meson.build index 123c6717..c3505d10 100644 --- a/src/catch2/meson.build +++ b/src/catch2/meson.build @@ -63,7 +63,6 @@ internal_headers = [ 'interfaces/catch_interfaces_registry_hub.hpp', 'interfaces/catch_interfaces_reporter.hpp', 'interfaces/catch_interfaces_reporter_factory.hpp', - 'interfaces/catch_interfaces_tag_alias_registry.hpp', 'interfaces/catch_interfaces_testcase.hpp', 'internal/catch_assertion_handler.hpp', 'internal/catch_case_insensitive_comparisons.hpp', diff --git a/tests/SelfTest/helpers/parse_test_spec.cpp b/tests/SelfTest/helpers/parse_test_spec.cpp index aa64404a..0830048a 100644 --- a/tests/SelfTest/helpers/parse_test_spec.cpp +++ b/tests/SelfTest/helpers/parse_test_spec.cpp @@ -9,12 +9,12 @@ #include #include -#include +#include namespace Catch { TestSpec parseTestSpec( std::string const& arg ) { - return TestSpecParser( ITagAliasRegistry::get() ) + return TestSpecParser( TagAliasRegistry::get() ) .parse( arg ) .testSpec(); }