| 
									
										
										
										
											2017-02-08 14:17:17 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  *  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)
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-06 22:28:42 +02:00
										 |  |  | #include "catch_matchers_string.h"
 | 
					
						
							| 
									
										
										
										
											2017-07-25 21:57:35 +02:00
										 |  |  | #include "catch_string_manip.h"
 | 
					
						
							| 
									
										
										
										
											2017-11-13 15:35:31 +01:00
										 |  |  | #include "catch_tostring.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <regex>
 | 
					
						
							| 
									
										
										
										
											2017-02-08 14:17:17 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Catch { | 
					
						
							|  |  |  | namespace Matchers { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     namespace StdString { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-08 15:14:51 +00:00
										 |  |  |         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(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-02-08 14:17:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-08 15:14:51 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-06 13:16:43 +01:00
										 |  |  |         StringMatcherBase::StringMatcherBase( std::string const& operation, CasedString const& comparator ) | 
					
						
							| 
									
										
										
										
											2017-02-08 15:14:51 +00:00
										 |  |  |         : m_comparator( comparator ), | 
					
						
							|  |  |  |           m_operation( operation ) { | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-14 09:04:26 +00:00
										 |  |  |         std::string StringMatcherBase::describe() const { | 
					
						
							| 
									
										
										
										
											2017-02-08 15:14:51 +00:00
										 |  |  |             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; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         EqualsMatcher::EqualsMatcher( CasedString const& comparator ) : StringMatcherBase( "equals", comparator ) {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         bool EqualsMatcher::match( std::string const& source ) const { | 
					
						
							|  |  |  |             return m_comparator.adjustString( source ) == m_comparator.m_str; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ContainsMatcher::ContainsMatcher( CasedString const& comparator ) : StringMatcherBase( "contains", comparator ) {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         bool ContainsMatcher::match( std::string const& source ) const { | 
					
						
							|  |  |  |             return contains( m_comparator.adjustString( source ), m_comparator.m_str ); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-02-08 14:17:17 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-08 15:14:51 +00:00
										 |  |  |         StartsWithMatcher::StartsWithMatcher( CasedString const& comparator ) : StringMatcherBase( "starts with", comparator ) {} | 
					
						
							| 
									
										
										
										
											2017-02-08 14:17:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-08 15:14:51 +00:00
										 |  |  |         bool StartsWithMatcher::match( std::string const& source ) const { | 
					
						
							|  |  |  |             return startsWith( m_comparator.adjustString( source ), m_comparator.m_str ); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-02-08 14:17:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-08 15:14:51 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         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 ); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-13 15:35:31 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         RegexMatcher::RegexMatcher(std::string regex, CaseSensitive::Choice caseSensitivity): m_regex(std::move(regex)), m_caseSensitivity(caseSensitivity) {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         bool RegexMatcher::match(std::string const& matchee) const { | 
					
						
							|  |  |  |             auto flags = std::regex::ECMAScript; // ECMAScript is the default syntax option anyway
 | 
					
						
							|  |  |  |             if (m_caseSensitivity == CaseSensitive::Choice::No) { | 
					
						
							|  |  |  |                 flags |= std::regex::icase; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             auto reg = std::regex(m_regex, flags); | 
					
						
							|  |  |  |             return std::regex_match(matchee, reg); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         std::string RegexMatcher::describe() const { | 
					
						
							|  |  |  |             return "matches " + ::Catch::Detail::stringify(m_regex) + ((m_caseSensitivity == CaseSensitive::Choice::Yes)? " case sensitively" : " case insensitively"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-08 15:14:51 +00:00
										 |  |  |     } // namespace StdString
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     StdString::EqualsMatcher Equals( std::string const& str, CaseSensitive::Choice caseSensitivity ) { | 
					
						
							| 
									
										
										
										
											2017-02-08 14:17:17 +00:00
										 |  |  |         return StdString::EqualsMatcher( StdString::CasedString( str, caseSensitivity) ); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-02-08 15:14:51 +00:00
										 |  |  |     StdString::ContainsMatcher Contains( std::string const& str, CaseSensitive::Choice caseSensitivity ) { | 
					
						
							| 
									
										
										
										
											2017-02-08 14:17:17 +00:00
										 |  |  |         return StdString::ContainsMatcher( StdString::CasedString( str, caseSensitivity) ); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-02-08 15:14:51 +00:00
										 |  |  |     StdString::EndsWithMatcher EndsWith( std::string const& str, CaseSensitive::Choice caseSensitivity ) { | 
					
						
							| 
									
										
										
										
											2017-02-08 14:17:17 +00:00
										 |  |  |         return StdString::EndsWithMatcher( StdString::CasedString( str, caseSensitivity) ); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-02-08 15:14:51 +00:00
										 |  |  |     StdString::StartsWithMatcher StartsWith( std::string const& str, CaseSensitive::Choice caseSensitivity ) { | 
					
						
							| 
									
										
										
										
											2017-02-08 14:17:17 +00:00
										 |  |  |         return StdString::StartsWithMatcher( StdString::CasedString( str, caseSensitivity) ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-13 15:35:31 +01:00
										 |  |  |     StdString::RegexMatcher Matches(std::string const& regex, CaseSensitive::Choice caseSensitivity) { | 
					
						
							|  |  |  |         return StdString::RegexMatcher(regex, caseSensitivity); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-08 14:17:17 +00:00
										 |  |  | } // namespace Matchers
 | 
					
						
							|  |  |  | } // namespace Catch
 |