mirror of
https://github.com/catchorg/Catch2.git
synced 2024-11-22 21:36:11 +01:00
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:
parent
afc8b28c07
commit
904c47a634
@ -12,103 +12,99 @@
|
|||||||
namespace Catch {
|
namespace Catch {
|
||||||
namespace Matchers {
|
namespace Matchers {
|
||||||
|
|
||||||
namespace StdString {
|
CasedString::CasedString( std::string const& str, CaseSensitive::Choice caseSensitivity )
|
||||||
|
: m_caseSensitivity( caseSensitivity ),
|
||||||
CasedString::CasedString( std::string const& str, CaseSensitive::Choice caseSensitivity )
|
m_str( adjustString( str ) )
|
||||||
: m_caseSensitivity( caseSensitivity ),
|
{}
|
||||||
m_str( adjustString( str ) )
|
std::string CasedString::adjustString( std::string const& str ) const {
|
||||||
{}
|
return m_caseSensitivity == CaseSensitive::No
|
||||||
std::string CasedString::adjustString( std::string const& str ) const {
|
? toLower( str )
|
||||||
return m_caseSensitivity == CaseSensitive::No
|
: str;
|
||||||
? toLower( str )
|
|
||||||
: str;
|
|
||||||
}
|
|
||||||
std::string CasedString::caseSensitivitySuffix() const {
|
|
||||||
return m_caseSensitivity == CaseSensitive::No
|
|
||||||
? " (case insensitive)"
|
|
||||||
: std::string();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
StringMatcherBase::StringMatcherBase( std::string const& operation, CasedString const& comparator )
|
|
||||||
: m_comparator( comparator ),
|
|
||||||
m_operation( operation ) {
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string StringMatcherBase::describe() 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
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 );
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
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 );
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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");
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace StdString
|
|
||||||
|
|
||||||
|
|
||||||
StdString::EqualsMatcher Equals( std::string const& str, CaseSensitive::Choice caseSensitivity ) {
|
|
||||||
return StdString::EqualsMatcher( StdString::CasedString( str, caseSensitivity) );
|
|
||||||
}
|
}
|
||||||
StdString::ContainsMatcher Contains( std::string const& str, CaseSensitive::Choice caseSensitivity ) {
|
std::string CasedString::caseSensitivitySuffix() const {
|
||||||
return StdString::ContainsMatcher( StdString::CasedString( str, caseSensitivity) );
|
return m_caseSensitivity == CaseSensitive::No
|
||||||
}
|
? " (case insensitive)"
|
||||||
StdString::EndsWithMatcher EndsWith( std::string const& str, CaseSensitive::Choice caseSensitivity ) {
|
: std::string();
|
||||||
return StdString::EndsWithMatcher( StdString::CasedString( str, caseSensitivity) );
|
|
||||||
}
|
|
||||||
StdString::StartsWithMatcher StartsWith( std::string const& str, CaseSensitive::Choice caseSensitivity ) {
|
|
||||||
return StdString::StartsWithMatcher( StdString::CasedString( str, caseSensitivity) );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
StdString::RegexMatcher Matches(std::string const& regex, CaseSensitive::Choice caseSensitivity) {
|
|
||||||
return StdString::RegexMatcher(regex, caseSensitivity);
|
StringMatcherBase::StringMatcherBase( std::string const& operation, CasedString const& comparator )
|
||||||
|
: m_comparator( comparator ),
|
||||||
|
m_operation( operation ) {
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string StringMatcherBase::describe() 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
StringEqualsMatcher::StringEqualsMatcher( CasedString const& comparator ) : StringMatcherBase( "equals", comparator ) {}
|
||||||
|
|
||||||
|
bool StringEqualsMatcher::match( std::string const& source ) const {
|
||||||
|
return m_comparator.adjustString( source ) == m_comparator.m_str;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
StringContainsMatcher::StringContainsMatcher( CasedString const& comparator ) : StringMatcherBase( "contains", comparator ) {}
|
||||||
|
|
||||||
|
bool StringContainsMatcher::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 );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
StringEqualsMatcher Equals( std::string const& str, CaseSensitive::Choice caseSensitivity ) {
|
||||||
|
return StringEqualsMatcher( CasedString( str, caseSensitivity) );
|
||||||
|
}
|
||||||
|
StringContainsMatcher Contains( std::string const& str, CaseSensitive::Choice caseSensitivity ) {
|
||||||
|
return StringContainsMatcher( CasedString( str, caseSensitivity) );
|
||||||
|
}
|
||||||
|
EndsWithMatcher EndsWith( std::string const& str, CaseSensitive::Choice caseSensitivity ) {
|
||||||
|
return EndsWithMatcher( CasedString( str, caseSensitivity) );
|
||||||
|
}
|
||||||
|
StartsWithMatcher StartsWith( std::string const& str, CaseSensitive::Choice caseSensitivity ) {
|
||||||
|
return StartsWithMatcher( CasedString( str, caseSensitivity) );
|
||||||
|
}
|
||||||
|
|
||||||
|
RegexMatcher Matches(std::string const& regex, CaseSensitive::Choice caseSensitivity) {
|
||||||
|
return RegexMatcher(regex, caseSensitivity);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace Matchers
|
} // namespace Matchers
|
||||||
|
@ -15,64 +15,60 @@
|
|||||||
namespace Catch {
|
namespace Catch {
|
||||||
namespace Matchers {
|
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;
|
||||||
|
|
||||||
struct CasedString
|
CaseSensitive::Choice m_caseSensitivity;
|
||||||
{
|
std::string m_str;
|
||||||
CasedString( std::string const& str, CaseSensitive::Choice caseSensitivity );
|
};
|
||||||
std::string adjustString( std::string const& str ) const;
|
|
||||||
std::string caseSensitivitySuffix() const;
|
|
||||||
|
|
||||||
CaseSensitive::Choice m_caseSensitivity;
|
struct StringMatcherBase : MatcherBase<std::string> {
|
||||||
std::string m_str;
|
StringMatcherBase( std::string const& operation, CasedString const& comparator );
|
||||||
};
|
std::string describe() const override;
|
||||||
|
|
||||||
struct StringMatcherBase : MatcherBase<std::string> {
|
CasedString m_comparator;
|
||||||
StringMatcherBase( std::string const& operation, CasedString const& comparator );
|
std::string m_operation;
|
||||||
std::string describe() const override;
|
};
|
||||||
|
|
||||||
CasedString m_comparator;
|
struct StringEqualsMatcher final : StringMatcherBase {
|
||||||
std::string m_operation;
|
StringEqualsMatcher( CasedString const& comparator );
|
||||||
};
|
bool match( std::string const& source ) const override;
|
||||||
|
};
|
||||||
|
struct StringContainsMatcher final : StringMatcherBase {
|
||||||
|
StringContainsMatcher( CasedString const& comparator );
|
||||||
|
bool match( std::string const& source ) const override;
|
||||||
|
};
|
||||||
|
struct StartsWithMatcher final : StringMatcherBase {
|
||||||
|
StartsWithMatcher( CasedString const& comparator );
|
||||||
|
bool match( std::string const& source ) const override;
|
||||||
|
};
|
||||||
|
struct EndsWithMatcher final : StringMatcherBase {
|
||||||
|
EndsWithMatcher( CasedString const& comparator );
|
||||||
|
bool match( std::string const& source ) const override;
|
||||||
|
};
|
||||||
|
|
||||||
struct EqualsMatcher final : StringMatcherBase {
|
struct RegexMatcher final : MatcherBase<std::string> {
|
||||||
EqualsMatcher( CasedString const& comparator );
|
RegexMatcher( std::string regex, CaseSensitive::Choice caseSensitivity );
|
||||||
bool match( std::string const& source ) const override;
|
bool match( std::string const& matchee ) const override;
|
||||||
};
|
std::string describe() const override;
|
||||||
struct ContainsMatcher final : StringMatcherBase {
|
|
||||||
ContainsMatcher( CasedString const& comparator );
|
|
||||||
bool match( std::string const& source ) const override;
|
|
||||||
};
|
|
||||||
struct StartsWithMatcher final : StringMatcherBase {
|
|
||||||
StartsWithMatcher( CasedString const& comparator );
|
|
||||||
bool match( std::string const& source ) const override;
|
|
||||||
};
|
|
||||||
struct EndsWithMatcher final : StringMatcherBase {
|
|
||||||
EndsWithMatcher( CasedString const& comparator );
|
|
||||||
bool match( std::string const& source ) const override;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct RegexMatcher final : MatcherBase<std::string> {
|
private:
|
||||||
RegexMatcher( std::string regex, CaseSensitive::Choice caseSensitivity );
|
std::string m_regex;
|
||||||
bool match( std::string const& matchee ) const override;
|
CaseSensitive::Choice m_caseSensitivity;
|
||||||
std::string describe() const override;
|
};
|
||||||
|
|
||||||
private:
|
//! Creates matcher that accepts strings that are exactly equal to `str`
|
||||||
std::string m_regex;
|
StringEqualsMatcher Equals( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes );
|
||||||
CaseSensitive::Choice m_caseSensitivity;
|
//! Creates matcher that accepts strings that contain `str`
|
||||||
};
|
StringContainsMatcher Contains( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes );
|
||||||
|
//! Creates matcher that accepts strings that _end_ with `str`
|
||||||
} // namespace StdString
|
EndsWithMatcher EndsWith( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes );
|
||||||
|
//! Creates matcher that accepts strings that _start_ with `str`
|
||||||
|
StartsWithMatcher StartsWith( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes );
|
||||||
// The following functions create the actual matcher objects.
|
//! Creates matcher that accepts strings matching `regex`
|
||||||
// This allows the types to be inferred
|
RegexMatcher Matches( std::string const& regex, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes );
|
||||||
|
|
||||||
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 );
|
|
||||||
StdString::RegexMatcher Matches( std::string const& regex, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes );
|
|
||||||
|
|
||||||
} // namespace Matchers
|
} // namespace Matchers
|
||||||
} // namespace Catch
|
} // namespace Catch
|
||||||
|
Loading…
Reference in New Issue
Block a user