Turn CaseSensitive into proper enum class

This commit is contained in:
Martin Hořeňovský 2020-08-18 13:44:37 +02:00
parent 6a46b344c0
commit e418e75c74
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
5 changed files with 22 additions and 24 deletions

View File

@ -3,9 +3,7 @@
namespace Catch { namespace Catch {
struct CaseSensitive { enum class CaseSensitive { Yes, No };
enum Choice { Yes, No };
};
} // namespace Catch } // namespace Catch

View File

@ -12,7 +12,7 @@
namespace Catch { namespace Catch {
WildcardPattern::WildcardPattern( std::string const& pattern, WildcardPattern::WildcardPattern( std::string const& pattern,
CaseSensitive::Choice caseSensitivity ) CaseSensitive caseSensitivity )
: m_caseSensitivity( caseSensitivity ), : m_caseSensitivity( caseSensitivity ),
m_pattern( normaliseString( pattern ) ) m_pattern( normaliseString( pattern ) )
{ {

View File

@ -24,13 +24,13 @@ namespace Catch
public: public:
WildcardPattern( std::string const& pattern, CaseSensitive::Choice caseSensitivity ); WildcardPattern( std::string const& pattern, CaseSensitive caseSensitivity );
virtual ~WildcardPattern() = default; virtual ~WildcardPattern() = default;
virtual bool matches( std::string const& str ) const; virtual bool matches( std::string const& str ) const;
private: private:
std::string normaliseString( std::string const& str ) const; std::string normaliseString( std::string const& str ) const;
CaseSensitive::Choice m_caseSensitivity; CaseSensitive m_caseSensitivity;
WildcardPosition m_wildcard = NoWildcard; WildcardPosition m_wildcard = NoWildcard;
std::string m_pattern; std::string m_pattern;
}; };

View File

@ -12,7 +12,7 @@
namespace Catch { namespace Catch {
namespace Matchers { namespace Matchers {
CasedString::CasedString( std::string const& str, CaseSensitive::Choice caseSensitivity ) CasedString::CasedString( std::string const& str, CaseSensitive caseSensitivity )
: m_caseSensitivity( caseSensitivity ), : m_caseSensitivity( caseSensitivity ),
m_str( adjustString( str ) ) m_str( adjustString( str ) )
{} {}
@ -74,11 +74,11 @@ namespace Matchers {
RegexMatcher::RegexMatcher(std::string regex, CaseSensitive::Choice caseSensitivity): m_regex(std::move(regex)), m_caseSensitivity(caseSensitivity) {} RegexMatcher::RegexMatcher(std::string regex, CaseSensitive caseSensitivity): m_regex(std::move(regex)), m_caseSensitivity(caseSensitivity) {}
bool RegexMatcher::match(std::string const& matchee) const { bool RegexMatcher::match(std::string const& matchee) const {
auto flags = std::regex::ECMAScript; // ECMAScript is the default syntax option anyway auto flags = std::regex::ECMAScript; // ECMAScript is the default syntax option anyway
if (m_caseSensitivity == CaseSensitive::Choice::No) { if (m_caseSensitivity == CaseSensitive::No) {
flags |= std::regex::icase; flags |= std::regex::icase;
} }
auto reg = std::regex(m_regex, flags); auto reg = std::regex(m_regex, flags);
@ -86,24 +86,24 @@ namespace Matchers {
} }
std::string RegexMatcher::describe() const { std::string RegexMatcher::describe() const {
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::Yes)? " case sensitively" : " case insensitively");
} }
StringEqualsMatcher Equals( std::string const& str, CaseSensitive::Choice caseSensitivity ) { StringEqualsMatcher Equals( std::string const& str, CaseSensitive caseSensitivity ) {
return StringEqualsMatcher( CasedString( str, caseSensitivity) ); return StringEqualsMatcher( CasedString( str, caseSensitivity) );
} }
StringContainsMatcher Contains( std::string const& str, CaseSensitive::Choice caseSensitivity ) { StringContainsMatcher Contains( std::string const& str, CaseSensitive caseSensitivity ) {
return StringContainsMatcher( CasedString( str, caseSensitivity) ); return StringContainsMatcher( CasedString( str, caseSensitivity) );
} }
EndsWithMatcher EndsWith( std::string const& str, CaseSensitive::Choice caseSensitivity ) { EndsWithMatcher EndsWith( std::string const& str, CaseSensitive caseSensitivity ) {
return EndsWithMatcher( CasedString( str, caseSensitivity) ); return EndsWithMatcher( CasedString( str, caseSensitivity) );
} }
StartsWithMatcher StartsWith( std::string const& str, CaseSensitive::Choice caseSensitivity ) { StartsWithMatcher StartsWith( std::string const& str, CaseSensitive caseSensitivity ) {
return StartsWithMatcher( CasedString( str, caseSensitivity) ); return StartsWithMatcher( CasedString( str, caseSensitivity) );
} }
RegexMatcher Matches(std::string const& regex, CaseSensitive::Choice caseSensitivity) { RegexMatcher Matches(std::string const& regex, CaseSensitive caseSensitivity) {
return RegexMatcher(regex, caseSensitivity); return RegexMatcher(regex, caseSensitivity);
} }

View File

@ -17,11 +17,11 @@ namespace Catch {
namespace Matchers { namespace Matchers {
struct CasedString { struct CasedString {
CasedString( std::string const& str, CaseSensitive::Choice caseSensitivity ); CasedString( std::string const& str, CaseSensitive 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;
CaseSensitive::Choice m_caseSensitivity; CaseSensitive m_caseSensitivity;
std::string m_str; std::string m_str;
}; };
@ -51,25 +51,25 @@ namespace Matchers {
}; };
struct RegexMatcher final : MatcherBase<std::string> { struct RegexMatcher final : MatcherBase<std::string> {
RegexMatcher( std::string regex, CaseSensitive::Choice caseSensitivity ); RegexMatcher( std::string regex, CaseSensitive caseSensitivity );
bool match( std::string const& matchee ) const override; bool match( std::string const& matchee ) const override;
std::string describe() const override; std::string describe() const override;
private: private:
std::string m_regex; std::string m_regex;
CaseSensitive::Choice m_caseSensitivity; CaseSensitive m_caseSensitivity;
}; };
//! Creates matcher that accepts strings that are exactly equal to `str` //! Creates matcher that accepts strings that are exactly equal to `str`
StringEqualsMatcher Equals( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ); StringEqualsMatcher Equals( std::string const& str, CaseSensitive caseSensitivity = CaseSensitive::Yes );
//! Creates matcher that accepts strings that contain `str` //! Creates matcher that accepts strings that contain `str`
StringContainsMatcher Contains( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ); StringContainsMatcher Contains( std::string const& str, CaseSensitive caseSensitivity = CaseSensitive::Yes );
//! Creates matcher that accepts strings that _end_ with `str` //! Creates matcher that accepts strings that _end_ with `str`
EndsWithMatcher EndsWith( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ); EndsWithMatcher EndsWith( std::string const& str, CaseSensitive caseSensitivity = CaseSensitive::Yes );
//! Creates matcher that accepts strings that _start_ with `str` //! Creates matcher that accepts strings that _start_ with `str`
StartsWithMatcher StartsWith( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ); StartsWithMatcher StartsWith( std::string const& str, CaseSensitive caseSensitivity = CaseSensitive::Yes );
//! Creates matcher that accepts strings matching `regex` //! Creates matcher that accepts strings matching `regex`
RegexMatcher Matches( std::string const& regex, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ); RegexMatcher Matches( std::string const& regex, CaseSensitive caseSensitivity = CaseSensitive::Yes );
} // namespace Matchers } // namespace Matchers
} // namespace Catch } // namespace Catch