catch_registry_hub.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright Catch2 Authors
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // https://www.boost.org/LICENSE_1_0.txt)
  5. // SPDX-License-Identifier: BSL-1.0
  6. #include <catch2/interfaces/catch_interfaces_registry_hub.hpp>
  7. #include <catch2/internal/catch_context.hpp>
  8. #include <catch2/internal/catch_enforce.hpp>
  9. #include <catch2/internal/catch_test_case_registry_impl.hpp>
  10. #include <catch2/internal/catch_reporter_registry.hpp>
  11. #include <catch2/internal/catch_exception_translator_registry.hpp>
  12. #include <catch2/internal/catch_tag_alias_registry.hpp>
  13. #include <catch2/internal/catch_startup_exception_registry.hpp>
  14. #include <catch2/internal/catch_singletons.hpp>
  15. #include <catch2/internal/catch_enum_values_registry.hpp>
  16. #include <catch2/catch_test_case_info.hpp>
  17. #include <catch2/internal/catch_noncopyable.hpp>
  18. #include <catch2/interfaces/catch_interfaces_reporter_factory.hpp>
  19. #include <catch2/internal/catch_move_and_forward.hpp>
  20. namespace Catch {
  21. namespace {
  22. class RegistryHub : public IRegistryHub,
  23. public IMutableRegistryHub,
  24. private Detail::NonCopyable {
  25. public: // IRegistryHub
  26. RegistryHub() = default;
  27. IReporterRegistry const& getReporterRegistry() const override {
  28. return m_reporterRegistry;
  29. }
  30. ITestCaseRegistry const& getTestCaseRegistry() const override {
  31. return m_testCaseRegistry;
  32. }
  33. IExceptionTranslatorRegistry const& getExceptionTranslatorRegistry() const override {
  34. return m_exceptionTranslatorRegistry;
  35. }
  36. ITagAliasRegistry const& getTagAliasRegistry() const override {
  37. return m_tagAliasRegistry;
  38. }
  39. StartupExceptionRegistry const& getStartupExceptionRegistry() const override {
  40. return m_exceptionRegistry;
  41. }
  42. public: // IMutableRegistryHub
  43. void registerReporter( std::string const& name, IReporterFactoryPtr factory ) override {
  44. m_reporterRegistry.registerReporter( name, CATCH_MOVE(factory) );
  45. }
  46. void registerListener( Detail::unique_ptr<EventListenerFactory> factory ) override {
  47. m_reporterRegistry.registerListener( CATCH_MOVE(factory) );
  48. }
  49. void registerTest( Detail::unique_ptr<TestCaseInfo>&& testInfo, Detail::unique_ptr<ITestInvoker>&& invoker ) override {
  50. m_testCaseRegistry.registerTest( CATCH_MOVE(testInfo), CATCH_MOVE(invoker) );
  51. }
  52. void registerTranslator( Detail::unique_ptr<IExceptionTranslator>&& translator ) override {
  53. m_exceptionTranslatorRegistry.registerTranslator( CATCH_MOVE(translator) );
  54. }
  55. void registerTagAlias( std::string const& alias, std::string const& tag, SourceLineInfo const& lineInfo ) override {
  56. m_tagAliasRegistry.add( alias, tag, lineInfo );
  57. }
  58. void registerStartupException() noexcept override {
  59. #if !defined(CATCH_CONFIG_DISABLE_EXCEPTIONS)
  60. m_exceptionRegistry.add(std::current_exception());
  61. #else
  62. CATCH_INTERNAL_ERROR("Attempted to register active exception under CATCH_CONFIG_DISABLE_EXCEPTIONS!");
  63. #endif
  64. }
  65. IMutableEnumValuesRegistry& getMutableEnumValuesRegistry() override {
  66. return m_enumValuesRegistry;
  67. }
  68. private:
  69. TestRegistry m_testCaseRegistry;
  70. ReporterRegistry m_reporterRegistry;
  71. ExceptionTranslatorRegistry m_exceptionTranslatorRegistry;
  72. TagAliasRegistry m_tagAliasRegistry;
  73. StartupExceptionRegistry m_exceptionRegistry;
  74. Detail::EnumValuesRegistry m_enumValuesRegistry;
  75. };
  76. }
  77. using RegistryHubSingleton = Singleton<RegistryHub, IRegistryHub, IMutableRegistryHub>;
  78. IRegistryHub const& getRegistryHub() {
  79. return RegistryHubSingleton::get();
  80. }
  81. IMutableRegistryHub& getMutableRegistryHub() {
  82. return RegistryHubSingleton::getMutable();
  83. }
  84. void cleanUp() {
  85. cleanupSingletons();
  86. cleanUpContext();
  87. }
  88. std::string translateActiveException() {
  89. return getRegistryHub().getExceptionTranslatorRegistry().translateActiveException();
  90. }
  91. } // end namespace Catch