Cleanup string matchers

Removed nested `StdString` namespace and added Doxygen comments.
Also renamed some matchers to avoid colisions now that there are
less separate namespaces for matchers to go to. Since this is a
breaking release anyway, it shouldn't matter, and the factory
functions that the users should use remain the same anyway.
This commit is contained in:
Martin Hořeňovský 2020-03-28 13:01:48 +01:00
parent afc8b28c07
commit 904c47a634
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
2 changed files with 137 additions and 145 deletions

View File

@ -12,8 +12,6 @@
namespace Catch { namespace Catch {
namespace Matchers { namespace Matchers {
namespace StdString {
CasedString::CasedString( std::string const& str, CaseSensitive::Choice caseSensitivity ) CasedString::CasedString( std::string const& str, CaseSensitive::Choice caseSensitivity )
: m_caseSensitivity( caseSensitivity ), : m_caseSensitivity( caseSensitivity ),
m_str( adjustString( str ) ) m_str( adjustString( str ) )
@ -47,16 +45,16 @@ namespace Matchers {
return description; return description;
} }
EqualsMatcher::EqualsMatcher( CasedString const& comparator ) : StringMatcherBase( "equals", comparator ) {} StringEqualsMatcher::StringEqualsMatcher( CasedString const& comparator ) : StringMatcherBase( "equals", comparator ) {}
bool EqualsMatcher::match( std::string const& source ) const { bool StringEqualsMatcher::match( std::string const& source ) const {
return m_comparator.adjustString( source ) == m_comparator.m_str; return m_comparator.adjustString( source ) == m_comparator.m_str;
} }
ContainsMatcher::ContainsMatcher( CasedString const& comparator ) : StringMatcherBase( "contains", comparator ) {} StringContainsMatcher::StringContainsMatcher( CasedString const& comparator ) : StringMatcherBase( "contains", comparator ) {}
bool ContainsMatcher::match( std::string const& source ) const { bool StringContainsMatcher::match( std::string const& source ) const {
return contains( m_comparator.adjustString( source ), m_comparator.m_str ); return contains( m_comparator.adjustString( source ), m_comparator.m_str );
} }
@ -91,24 +89,22 @@ namespace Matchers {
return "matches " + ::Catch::Detail::stringify(m_regex) + ((m_caseSensitivity == CaseSensitive::Choice::Yes)? " case sensitively" : " case insensitively"); return "matches " + ::Catch::Detail::stringify(m_regex) + ((m_caseSensitivity == CaseSensitive::Choice::Yes)? " case sensitively" : " case insensitively");
} }
} // namespace StdString
StringEqualsMatcher Equals( std::string const& str, CaseSensitive::Choice caseSensitivity ) {
StdString::EqualsMatcher Equals( std::string const& str, CaseSensitive::Choice caseSensitivity ) { return StringEqualsMatcher( CasedString( str, caseSensitivity) );
return StdString::EqualsMatcher( StdString::CasedString( str, caseSensitivity) );
} }
StdString::ContainsMatcher Contains( std::string const& str, CaseSensitive::Choice caseSensitivity ) { StringContainsMatcher Contains( std::string const& str, CaseSensitive::Choice caseSensitivity ) {
return StdString::ContainsMatcher( StdString::CasedString( str, caseSensitivity) ); return StringContainsMatcher( CasedString( str, caseSensitivity) );
} }
StdString::EndsWithMatcher EndsWith( std::string const& str, CaseSensitive::Choice caseSensitivity ) { EndsWithMatcher EndsWith( std::string const& str, CaseSensitive::Choice caseSensitivity ) {
return StdString::EndsWithMatcher( StdString::CasedString( str, caseSensitivity) ); return EndsWithMatcher( CasedString( str, caseSensitivity) );
} }
StdString::StartsWithMatcher StartsWith( std::string const& str, CaseSensitive::Choice caseSensitivity ) { StartsWithMatcher StartsWith( std::string const& str, CaseSensitive::Choice caseSensitivity ) {
return StdString::StartsWithMatcher( StdString::CasedString( str, caseSensitivity) ); return StartsWithMatcher( CasedString( str, caseSensitivity) );
} }
StdString::RegexMatcher Matches(std::string const& regex, CaseSensitive::Choice caseSensitivity) { RegexMatcher Matches(std::string const& regex, CaseSensitive::Choice caseSensitivity) {
return StdString::RegexMatcher(regex, caseSensitivity); return RegexMatcher(regex, caseSensitivity);
} }
} // namespace Matchers } // namespace Matchers

View File

@ -15,10 +15,7 @@
namespace Catch { namespace Catch {
namespace Matchers { namespace Matchers {
namespace StdString { struct CasedString {
struct CasedString
{
CasedString( std::string const& str, CaseSensitive::Choice caseSensitivity ); CasedString( std::string const& str, CaseSensitive::Choice caseSensitivity );
std::string adjustString( std::string const& str ) const; std::string adjustString( std::string const& str ) const;
std::string caseSensitivitySuffix() const; std::string caseSensitivitySuffix() const;
@ -35,12 +32,12 @@ namespace Matchers {
std::string m_operation; std::string m_operation;
}; };
struct EqualsMatcher final : StringMatcherBase { struct StringEqualsMatcher final : StringMatcherBase {
EqualsMatcher( CasedString const& comparator ); StringEqualsMatcher( CasedString const& comparator );
bool match( std::string const& source ) const override; bool match( std::string const& source ) const override;
}; };
struct ContainsMatcher final : StringMatcherBase { struct StringContainsMatcher final : StringMatcherBase {
ContainsMatcher( CasedString const& comparator ); StringContainsMatcher( CasedString const& comparator );
bool match( std::string const& source ) const override; bool match( std::string const& source ) const override;
}; };
struct StartsWithMatcher final : StringMatcherBase { struct StartsWithMatcher final : StringMatcherBase {
@ -62,17 +59,16 @@ namespace Matchers {
CaseSensitive::Choice m_caseSensitivity; CaseSensitive::Choice m_caseSensitivity;
}; };
} // namespace StdString //! Creates matcher that accepts strings that are exactly equal to `str`
StringEqualsMatcher Equals( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes );
//! Creates matcher that accepts strings that contain `str`
// The following functions create the actual matcher objects. StringContainsMatcher Contains( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes );
// This allows the types to be inferred //! Creates matcher that accepts strings that _end_ with `str`
EndsWithMatcher EndsWith( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes );
StdString::EqualsMatcher Equals( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ); //! Creates matcher that accepts strings that _start_ with `str`
StdString::ContainsMatcher Contains( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ); StartsWithMatcher StartsWith( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes );
StdString::EndsWithMatcher EndsWith( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ); //! Creates matcher that accepts strings matching `regex`
StdString::StartsWithMatcher StartsWith( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ); RegexMatcher Matches( std::string const& regex, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes );
StdString::RegexMatcher Matches( std::string const& regex, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes );
} // namespace Matchers } // namespace Matchers
} // namespace Catch } // namespace Catch