mirror of
https://github.com/catchorg/Catch2.git
synced 2025-06-04 10:58:06 +00:00
Added a handful of "built-in" matchers
This commit is contained in:
parent
eca5637c58
commit
a6a40b3ba9
4 changed files with 117 additions and 30 deletions
|
@ -186,33 +186,33 @@ TEST_CASE("./succeeding/atomic if", "")
|
|||
REQUIRE(x == 0);
|
||||
}
|
||||
|
||||
namespace Matchers
|
||||
{
|
||||
struct ContainsStdString
|
||||
{
|
||||
ContainsStdString( const std::string& substr ) : m_substr( substr ){}
|
||||
|
||||
bool operator()( const std::string& str ) const
|
||||
{
|
||||
return str.find( m_substr ) != std::string::npos;
|
||||
}
|
||||
|
||||
friend std::ostream& operator<<( std::ostream& os, const ContainsStdString& matcher )
|
||||
{
|
||||
os << "contains: \"" << matcher.m_substr << "\"";
|
||||
return os;
|
||||
}
|
||||
std::string m_substr;
|
||||
};
|
||||
}
|
||||
|
||||
inline Matchers::ContainsStdString Contains( const std::string& substr ){ return Matchers::ContainsStdString( substr ); }
|
||||
|
||||
|
||||
TEST_CASE("./succeeding/matcher", "")
|
||||
inline const char* testStringForMatching()
|
||||
{
|
||||
const char* actualStr = "this string contains 'abc' as a substring";
|
||||
REQUIRE_THAT( actualStr, Contains( "string" ) );
|
||||
CHECK_THAT( actualStr, Contains( "not there" ) );
|
||||
CHECK_THAT( actualStr, Contains( "a2bc" ) );
|
||||
return "this string contains 'abc' as a substring";
|
||||
}
|
||||
|
||||
using namespace Catch::Matchers;
|
||||
|
||||
TEST_CASE("./succeeding/matchers", "")
|
||||
{
|
||||
REQUIRE_THAT( testStringForMatching(), Contains( "string" ) );
|
||||
CHECK_THAT( testStringForMatching(), Contains( "abc" ) );
|
||||
|
||||
CHECK_THAT( testStringForMatching(), StartsWith( "this" ) );
|
||||
CHECK_THAT( testStringForMatching(), EndsWith( "substring" ) );
|
||||
}
|
||||
|
||||
TEST_CASE("./failing/matchers/Contains", "")
|
||||
{
|
||||
CHECK_THAT( testStringForMatching(), Contains( "not there" ) );
|
||||
}
|
||||
|
||||
TEST_CASE("./failing/matchers/StartsWith", "")
|
||||
{
|
||||
CHECK_THAT( testStringForMatching(), StartsWith( "string" ) );
|
||||
}
|
||||
|
||||
TEST_CASE("./failing/matchers/EndsWith", "")
|
||||
{
|
||||
CHECK_THAT( testStringForMatching(), EndsWith( "this" ) );
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue