mirror of
				https://github.com/catchorg/Catch2.git
				synced 2025-10-31 20:27:11 +01:00 
			
		
		
		
	Exception message testing now supports wildcards
- extracted WildcardPattern from TestSpec::NamePattern
This commit is contained in:
		| @@ -77,6 +77,7 @@ namespace Catch { | ||||
|     FreeFunctionTestCase::~FreeFunctionTestCase() {} | ||||
|     IGeneratorInfo::~IGeneratorInfo() {} | ||||
|     IGeneratorsForTest::~IGeneratorsForTest() {} | ||||
|     WildcardPattern::~WildcardPattern() {} | ||||
|     TestSpec::Pattern::~Pattern() {} | ||||
|     TestSpec::NamePattern::~NamePattern() {} | ||||
|     TestSpec::TagPattern::~TagPattern() {} | ||||
|   | ||||
| @@ -14,7 +14,7 @@ | ||||
| #include "catch_interfaces_runner.h" | ||||
| #include "catch_interfaces_capture.h" | ||||
| #include "catch_interfaces_registry_hub.h" | ||||
|  | ||||
| #include "catch_wildcard_pattern.hpp" | ||||
|  | ||||
| namespace Catch { | ||||
|  | ||||
| @@ -78,7 +78,8 @@ namespace Catch { | ||||
|         if( expectedMessage != "" ) { | ||||
|              | ||||
|             std::string actualMessage = Catch::translateActiveException(); | ||||
|             if( expectedMessage != actualMessage ) { | ||||
|             WildcardPattern pattern( expectedMessage, WildcardPattern::CaseInsensitive ); | ||||
|             if( !pattern.matches( actualMessage ) ) { | ||||
|                 data.resultType = ResultWas::ExpressionFailed; | ||||
|                 data.reconstructedExpression = actualMessage; | ||||
|             } | ||||
|   | ||||
| @@ -13,63 +13,32 @@ | ||||
| #pragma clang diagnostic ignored "-Wpadded" | ||||
| #endif | ||||
|  | ||||
| #include "catch_wildcard_pattern.hpp" | ||||
| #include "catch_test_case_info.h" | ||||
|  | ||||
| #include <string> | ||||
| #include <vector> | ||||
|  | ||||
| namespace Catch { | ||||
|  | ||||
|      | ||||
|     class TestSpec { | ||||
|         struct Pattern : SharedImpl<> { | ||||
|             virtual ~Pattern(); | ||||
|             virtual bool matches( TestCaseInfo const& testCase ) const = 0; | ||||
|         }; | ||||
|         class NamePattern : public Pattern { | ||||
|             enum WildcardPosition { | ||||
|                 NoWildcard = 0, | ||||
|                 WildcardAtStart = 1, | ||||
|                 WildcardAtEnd = 2, | ||||
|                 WildcardAtBothEnds = WildcardAtStart | WildcardAtEnd | ||||
|             }; | ||||
|  | ||||
|         public: | ||||
|             NamePattern( std::string const& name ) : m_name( toLower( name ) ), m_wildcard( NoWildcard ) { | ||||
|                 if( startsWith( m_name, "*" ) ) { | ||||
|                     m_name = m_name.substr( 1 ); | ||||
|                     m_wildcard = WildcardAtStart; | ||||
|                 } | ||||
|                 if( endsWith( m_name, "*" ) ) { | ||||
|                     m_name = m_name.substr( 0, m_name.size()-1 ); | ||||
|                     m_wildcard = static_cast<WildcardPosition>( m_wildcard | WildcardAtEnd ); | ||||
|                 } | ||||
|             } | ||||
|             NamePattern( std::string const& name ) | ||||
|             : m_wildcardPattern( toLower( name ), WildcardPattern::CaseInsensitive ) | ||||
|             {} | ||||
|             virtual ~NamePattern(); | ||||
|             virtual bool matches( TestCaseInfo const& testCase ) const { | ||||
|                 switch( m_wildcard ) { | ||||
|                     case NoWildcard: | ||||
|                         return m_name == toLower( testCase.name ); | ||||
|                     case WildcardAtStart: | ||||
|                         return endsWith( toLower( testCase.name ), m_name ); | ||||
|                     case WildcardAtEnd: | ||||
|                         return startsWith( toLower( testCase.name ), m_name ); | ||||
|                     case WildcardAtBothEnds: | ||||
|                         return contains( toLower( testCase.name ), m_name ); | ||||
|                 } | ||||
|  | ||||
| #ifdef __clang__ | ||||
| #pragma clang diagnostic push | ||||
| #pragma clang diagnostic ignored "-Wunreachable-code" | ||||
| #endif | ||||
|                 throw std::logic_error( "Unknown enum" ); | ||||
| #ifdef __clang__ | ||||
| #pragma clang diagnostic pop | ||||
| #endif | ||||
|                 return m_wildcardPattern.matches( toLower( testCase.name ) ); | ||||
|             } | ||||
|         private: | ||||
|             std::string m_name; | ||||
|             WildcardPosition m_wildcard; | ||||
|             WildcardPattern m_wildcardPattern; | ||||
|         }; | ||||
|          | ||||
|         class TagPattern : public Pattern { | ||||
|         public: | ||||
|             TagPattern( std::string const& tag ) : m_tag( toLower( tag ) ) {} | ||||
| @@ -80,6 +49,7 @@ namespace Catch { | ||||
|         private: | ||||
|             std::string m_tag; | ||||
|         }; | ||||
|          | ||||
|         class ExcludedPattern : public Pattern { | ||||
|         public: | ||||
|             ExcludedPattern( Ptr<Pattern> const& underlyingPattern ) : m_underlyingPattern( underlyingPattern ) {} | ||||
|   | ||||
							
								
								
									
										75
									
								
								include/internal/catch_wildcard_pattern.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										75
									
								
								include/internal/catch_wildcard_pattern.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,75 @@ | ||||
| /* | ||||
|  *  Created by Phil on 13/7/2015. | ||||
|  *  Copyright 2015 Two Blue Cubes Ltd. All rights reserved. | ||||
|  * | ||||
|  *  Distributed under the Boost Software License, Version 1.0. (See accompanying | ||||
|  *  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||||
|  */ | ||||
| #ifndef TWOBLUECUBES_CATCH_WILDCARD_PATTERN_HPP_INCLUDED | ||||
| #define TWOBLUECUBES_CATCH_WILDCARD_PATTERN_HPP_INCLUDED | ||||
|  | ||||
| #include "catch_common.h" | ||||
|  | ||||
| namespace Catch | ||||
| { | ||||
|     class WildcardPattern { | ||||
|         enum WildcardPosition { | ||||
|             NoWildcard = 0, | ||||
|             WildcardAtStart = 1, | ||||
|             WildcardAtEnd = 2, | ||||
|             WildcardAtBothEnds = WildcardAtStart | WildcardAtEnd | ||||
|         }; | ||||
|          | ||||
|     public: | ||||
|          | ||||
|         enum CaseSensitivity { | ||||
|             CaseSensitive, | ||||
|             CaseInsensitive | ||||
|         }; | ||||
|         WildcardPattern( std::string const& pattern, CaseSensitivity caseSensitivity ) | ||||
|         :   m_caseSensitivity( caseSensitivity ), | ||||
|             m_wildcard( NoWildcard ), | ||||
|             m_pattern( adjustCase( pattern ) ) | ||||
|         { | ||||
|             if( startsWith( m_pattern, "*" ) ) { | ||||
|                 m_pattern = m_pattern.substr( 1 ); | ||||
|                 m_wildcard = WildcardAtStart; | ||||
|             } | ||||
|             if( endsWith( m_pattern, "*" ) ) { | ||||
|                 m_pattern = m_pattern.substr( 0, m_pattern.size()-1 ); | ||||
|                 m_wildcard = static_cast<WildcardPosition>( m_wildcard | WildcardAtEnd ); | ||||
|             } | ||||
|         } | ||||
|         virtual ~WildcardPattern(); | ||||
|         virtual bool matches( std::string const& str ) const { | ||||
|             switch( m_wildcard ) { | ||||
|                 case NoWildcard: | ||||
|                     return m_pattern == adjustCase( str ); | ||||
|                 case WildcardAtStart: | ||||
|                     return endsWith( adjustCase( str ), m_pattern ); | ||||
|                 case WildcardAtEnd: | ||||
|                     return startsWith( adjustCase( str ), m_pattern ); | ||||
|                 case WildcardAtBothEnds: | ||||
|                     return contains( adjustCase( str ), m_pattern ); | ||||
|             } | ||||
|              | ||||
| #ifdef __clang__ | ||||
| #pragma clang diagnostic push | ||||
| #pragma clang diagnostic ignored "-Wunreachable-code" | ||||
| #endif | ||||
|             throw std::logic_error( "Unknown enum" ); | ||||
| #ifdef __clang__ | ||||
| #pragma clang diagnostic pop | ||||
| #endif | ||||
|         } | ||||
|     private: | ||||
|         std::string adjustCase( std::string const& str ) const { | ||||
|             return m_caseSensitivity == CaseInsensitive ? toLower( str ) : str; | ||||
|         } | ||||
|         CaseSensitivity m_caseSensitivity; | ||||
|         WildcardPosition m_wildcard; | ||||
|         std::string m_pattern; | ||||
|     }; | ||||
| } | ||||
|  | ||||
| #endif // TWOBLUECUBES_CATCH_WILDCARD_PATTERN_HPP_INCLUDED | ||||
		Reference in New Issue
	
	Block a user
	 Phil Nash
					Phil Nash