mirror of
				https://github.com/catchorg/Catch2.git
				synced 2025-10-31 04:07:10 +01:00 
			
		
		
		
	Extracted string matchers impl into cpp that is only compiled into main TU
This commit is contained in:
		| @@ -36,6 +36,7 @@ | ||||
| #include "catch_result_builder.hpp" | ||||
| #include "catch_tag_alias_registry.hpp" | ||||
| #include "catch_test_case_tracker.hpp" | ||||
| #include "catch_matchers_string.hpp" | ||||
|  | ||||
| #include "../reporters/catch_reporter_multi.hpp" | ||||
| #include "../reporters/catch_reporter_xml.hpp" | ||||
|   | ||||
							
								
								
									
										67
									
								
								include/internal/catch_matchers_string.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								include/internal/catch_matchers_string.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,67 @@ | ||||
| /* | ||||
|  *  Created by Phil Nash on 08/02/2017. | ||||
|  *  Copyright (c) 2017 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_MATCHERS_STRING_HPP_INCLUDED | ||||
| #define TWOBLUECUBES_CATCH_MATCHERS_STRING_HPP_INCLUDED | ||||
|  | ||||
| #include "catch_matchers.hpp" | ||||
|  | ||||
| namespace Catch { | ||||
| namespace Matchers { | ||||
|  | ||||
|     namespace StdString { | ||||
|  | ||||
|         struct CasedString | ||||
|         { | ||||
|             CasedString( std::string const& str, CaseSensitive::Choice caseSensitivity ); | ||||
|             std::string adjustString( std::string const& str ) const; | ||||
|             std::string caseSensitivitySuffix() const; | ||||
|  | ||||
|             CaseSensitive::Choice m_caseSensitivity; | ||||
|             std::string m_str; | ||||
|         }; | ||||
|  | ||||
|         struct StringMatcherBase : Impl::MatcherBase<std::string> { | ||||
|             StringMatcherBase( std::string operation, CasedString const& comparator ); | ||||
|             virtual std::string toStringUncached() const CATCH_OVERRIDE; | ||||
|  | ||||
|             CasedString m_comparator; | ||||
|             std::string m_operation; | ||||
|         }; | ||||
|  | ||||
|         struct EqualsMatcher : StringMatcherBase { | ||||
|             EqualsMatcher( CasedString const& comparator ); | ||||
|             virtual bool match( std::string const& source ) const CATCH_OVERRIDE; | ||||
|         }; | ||||
|         struct ContainsMatcher : StringMatcherBase { | ||||
|             ContainsMatcher( CasedString const& comparator ); | ||||
|             virtual bool match( std::string const& source ) const CATCH_OVERRIDE; | ||||
|         }; | ||||
|         struct StartsWithMatcher : StringMatcherBase { | ||||
|             StartsWithMatcher( CasedString const& comparator ); | ||||
|             virtual bool match( std::string const& source ) const CATCH_OVERRIDE; | ||||
|         }; | ||||
|         struct EndsWithMatcher : StringMatcherBase { | ||||
|             EndsWithMatcher( CasedString const& comparator ); | ||||
|             virtual bool match( std::string const& source ) const CATCH_OVERRIDE; | ||||
|         }; | ||||
|  | ||||
|     } // namespace StdString | ||||
|  | ||||
|  | ||||
|     // The following functions create the actual matcher objects. | ||||
|     // This allows the types to be inferred | ||||
|  | ||||
|     StdString::EqualsMatcher Equals( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ); | ||||
|     StdString::ContainsMatcher Contains( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ); | ||||
|     StdString::EndsWithMatcher EndsWith( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ); | ||||
|     StdString::StartsWithMatcher StartsWith( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ); | ||||
|  | ||||
| } // namespace Matchers | ||||
| } // namespace Catch | ||||
|  | ||||
| #endif // TWOBLUECUBES_CATCH_MATCHERS_STRING_HPP_INCLUDED | ||||
| @@ -5,8 +5,6 @@ | ||||
|  * 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_MATCHERS_STRING_HPP_INCLUDED | ||||
| #define TWOBLUECUBES_CATCH_MATCHERS_STRING_HPP_INCLUDED | ||||
|  | ||||
| #include "catch_matchers.hpp" | ||||
|  | ||||
| @@ -15,100 +13,81 @@ namespace Matchers { | ||||
|  | ||||
|     namespace StdString { | ||||
|  | ||||
|         struct CasedString | ||||
|         { | ||||
|             CasedString( std::string const& str, CaseSensitive::Choice caseSensitivity ) | ||||
|             :   m_caseSensitivity( caseSensitivity ), | ||||
|                 m_str( adjustString( str ) ) | ||||
|             {} | ||||
|             std::string adjustString( std::string const& str ) const { | ||||
|                 return m_caseSensitivity == CaseSensitive::No | ||||
|                        ? toLower( str ) | ||||
|                        : str; | ||||
|             } | ||||
|             std::string caseSensitivitySuffix() const { | ||||
|                 return m_caseSensitivity == CaseSensitive::No | ||||
|                        ? " (case insensitive)" | ||||
|                        : std::string(); | ||||
|             } | ||||
|             CaseSensitive::Choice m_caseSensitivity; | ||||
|             std::string m_str; | ||||
|         }; | ||||
|         CasedString::CasedString( std::string const& str, CaseSensitive::Choice caseSensitivity ) | ||||
|         :   m_caseSensitivity( caseSensitivity ), | ||||
|             m_str( adjustString( str ) ) | ||||
|         {} | ||||
|         std::string CasedString::adjustString( std::string const& str ) const { | ||||
|             return m_caseSensitivity == CaseSensitive::No | ||||
|                    ? toLower( str ) | ||||
|                    : str; | ||||
|         } | ||||
|         std::string CasedString::caseSensitivitySuffix() const { | ||||
|             return m_caseSensitivity == CaseSensitive::No | ||||
|                    ? " (case insensitive)" | ||||
|                    : std::string(); | ||||
|         } | ||||
|  | ||||
|         // !TBD Move impl | ||||
|         struct StringMatcherBase : Impl::MatcherBase<std::string> { | ||||
|             StringMatcherBase( std::string operation, CasedString const& comparator ) | ||||
|             : m_comparator( comparator ), | ||||
|               m_operation( operation ) { | ||||
|             } | ||||
|  | ||||
|             virtual std::string toStringUncached() const CATCH_OVERRIDE { | ||||
|                 std::string description; | ||||
|                 description.reserve(5 + m_operation.size() + m_comparator.m_str.size() + | ||||
|                                             m_comparator.caseSensitivitySuffix().size()); | ||||
|                 description += m_operation; | ||||
|                 description += ": \""; | ||||
|                 description += m_comparator.m_str; | ||||
|                 description += "\""; | ||||
|                 description += m_comparator.caseSensitivitySuffix(); | ||||
|                 return description; | ||||
|             } | ||||
|         StringMatcherBase::StringMatcherBase( std::string operation, CasedString const& comparator ) | ||||
|         : m_comparator( comparator ), | ||||
|           m_operation( operation ) { | ||||
|         } | ||||
|  | ||||
|             CasedString m_comparator; | ||||
|             std::string m_operation; | ||||
|         }; | ||||
|         std::string StringMatcherBase::toStringUncached() const { | ||||
|             std::string description; | ||||
|             description.reserve(5 + m_operation.size() + m_comparator.m_str.size() + | ||||
|                                         m_comparator.caseSensitivitySuffix().size()); | ||||
|             description += m_operation; | ||||
|             description += ": \""; | ||||
|             description += m_comparator.m_str; | ||||
|             description += "\""; | ||||
|             description += m_comparator.caseSensitivitySuffix(); | ||||
|             return description; | ||||
|         } | ||||
|  | ||||
|         // !TBD Move impl | ||||
|         struct EqualsMatcher : StringMatcherBase { | ||||
|             EqualsMatcher( CasedString const& comparator ) : StringMatcherBase( "equals", comparator ) {} | ||||
|         EqualsMatcher::EqualsMatcher( CasedString const& comparator ) : StringMatcherBase( "equals", comparator ) {} | ||||
|  | ||||
|             virtual bool match( std::string const& source ) const CATCH_OVERRIDE { | ||||
|                 return m_comparator.adjustString( source ) == m_comparator.m_str; | ||||
|             } | ||||
|         }; | ||||
|         struct ContainsMatcher : StringMatcherBase { | ||||
|             ContainsMatcher( CasedString const& comparator ) : StringMatcherBase( "contains", comparator ) {} | ||||
|         bool EqualsMatcher::match( std::string const& source ) const { | ||||
|             return m_comparator.adjustString( source ) == m_comparator.m_str; | ||||
|         } | ||||
|  | ||||
|             virtual bool match( std::string const& source ) const CATCH_OVERRIDE { | ||||
|                 return contains( m_comparator.adjustString( source ), m_comparator.m_str ); | ||||
|             } | ||||
|         }; | ||||
|         struct StartsWithMatcher : StringMatcherBase { | ||||
|             StartsWithMatcher( CasedString const& comparator ) : StringMatcherBase( "starts with", comparator ) {} | ||||
|  | ||||
|             virtual bool match( std::string const& source ) const CATCH_OVERRIDE { | ||||
|                 return startsWith( m_comparator.adjustString( source ), m_comparator.m_str ); | ||||
|             } | ||||
|         }; | ||||
|         struct EndsWithMatcher : StringMatcherBase { | ||||
|             EndsWithMatcher( CasedString const& comparator ) : StringMatcherBase( "ends with", comparator ) {} | ||||
|         ContainsMatcher::ContainsMatcher( CasedString const& comparator ) : StringMatcherBase( "contains", comparator ) {} | ||||
|  | ||||
|             virtual bool match( std::string const& source ) const CATCH_OVERRIDE { | ||||
|                 return endsWith( m_comparator.adjustString( source ), m_comparator.m_str ); | ||||
|             } | ||||
|         }; | ||||
|         bool ContainsMatcher::match( std::string const& source ) const { | ||||
|             return contains( m_comparator.adjustString( source ), m_comparator.m_str ); | ||||
|         } | ||||
|  | ||||
|  | ||||
|         StartsWithMatcher::StartsWithMatcher( CasedString const& comparator ) : StringMatcherBase( "starts with", comparator ) {} | ||||
|  | ||||
|         bool StartsWithMatcher::match( std::string const& source ) const { | ||||
|             return startsWith( m_comparator.adjustString( source ), m_comparator.m_str ); | ||||
|         } | ||||
|  | ||||
|  | ||||
|         EndsWithMatcher::EndsWithMatcher( CasedString const& comparator ) : StringMatcherBase( "ends with", comparator ) {} | ||||
|  | ||||
|         bool EndsWithMatcher::match( std::string const& source ) const { | ||||
|             return endsWith( m_comparator.adjustString( source ), m_comparator.m_str ); | ||||
|         } | ||||
|  | ||||
|     } // namespace StdString | ||||
|  | ||||
|  | ||||
|  | ||||
|     // The following functions create the actual matcher objects. | ||||
|     // This allows the types to be inferred | ||||
|  | ||||
|     inline StdString::EqualsMatcher Equals( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ) { | ||||
|     StdString::EqualsMatcher Equals( std::string const& str, CaseSensitive::Choice caseSensitivity ) { | ||||
|         return StdString::EqualsMatcher( StdString::CasedString( str, caseSensitivity) ); | ||||
|     } | ||||
|     inline StdString::ContainsMatcher Contains( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ) { | ||||
|     StdString::ContainsMatcher Contains( std::string const& str, CaseSensitive::Choice caseSensitivity ) { | ||||
|         return StdString::ContainsMatcher( StdString::CasedString( str, caseSensitivity) ); | ||||
|     } | ||||
|     inline StdString::EndsWithMatcher EndsWith( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ) { | ||||
|     StdString::EndsWithMatcher EndsWith( std::string const& str, CaseSensitive::Choice caseSensitivity ) { | ||||
|         return StdString::EndsWithMatcher( StdString::CasedString( str, caseSensitivity) ); | ||||
|     } | ||||
|     inline StdString::StartsWithMatcher StartsWith( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ) { | ||||
|     StdString::StartsWithMatcher StartsWith( std::string const& str, CaseSensitive::Choice caseSensitivity ) { | ||||
|         return StdString::StartsWithMatcher( StdString::CasedString( str, caseSensitivity) ); | ||||
|     } | ||||
|  | ||||
| } // namespace Matchers | ||||
| } // namespace Catch | ||||
|  | ||||
| #endif // TWOBLUECUBES_CATCH_MATCHERS_STRING_HPP_INCLUDED | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Phil Nash
					Phil Nash