mirror of
https://github.com/catchorg/Catch2.git
synced 2024-11-22 21:36:11 +01:00
Added initial built-in matchers for NSString (ObjC)
This commit is contained in:
parent
a6a40b3ba9
commit
966f5dbff2
@ -53,6 +53,32 @@ namespace Detail
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline std::string makeString
|
||||
(
|
||||
T* p
|
||||
)
|
||||
{
|
||||
if( !p )
|
||||
return INTERNAL_CATCH_STRINGIFY( NULL );
|
||||
std::ostringstream oss;
|
||||
oss << p;
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline std::string makeString
|
||||
(
|
||||
const T* p
|
||||
)
|
||||
{
|
||||
if( !p )
|
||||
return INTERNAL_CATCH_STRINGIFY( NULL );
|
||||
std::ostringstream oss;
|
||||
oss << p;
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
}// end namespace Detail
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
@ -167,39 +193,6 @@ inline std::string toString
|
||||
return value ? "true" : "false";
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
inline std::string toString
|
||||
(
|
||||
void* p
|
||||
)
|
||||
{
|
||||
if( !p )
|
||||
return INTERNAL_CATCH_STRINGIFY( NULL );
|
||||
std::ostringstream oss;
|
||||
oss << p;
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
template<typename T>
|
||||
inline std::string toString
|
||||
(
|
||||
T* p
|
||||
)
|
||||
{
|
||||
return Catch::toString( static_cast<void*>( p ) );
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
template<typename T>
|
||||
inline std::string toString
|
||||
(
|
||||
const T* p
|
||||
)
|
||||
{
|
||||
return Catch::toString( static_cast<void*>( const_cast<T*>( p ) ) );
|
||||
}
|
||||
|
||||
struct TestFailureException
|
||||
{
|
||||
};
|
||||
|
@ -172,7 +172,92 @@ namespace Catch
|
||||
}
|
||||
return noTestMethods;
|
||||
}
|
||||
}
|
||||
|
||||
template<>
|
||||
inline std::string toString<NSString*>( NSString* const& nsstring )
|
||||
{
|
||||
return std::string( "@\"" ) + [nsstring UTF8String] + "\"";
|
||||
}
|
||||
|
||||
namespace Matchers
|
||||
{
|
||||
namespace Impl
|
||||
{
|
||||
namespace NSStringMatchers
|
||||
{
|
||||
struct StringHolder
|
||||
{
|
||||
StringHolder( NSString* substr ) : m_substr( [substr copy] ){}
|
||||
StringHolder()
|
||||
{
|
||||
[m_substr release];
|
||||
}
|
||||
|
||||
NSString* m_substr;
|
||||
};
|
||||
|
||||
struct Contains : StringHolder
|
||||
{
|
||||
Contains( NSString* substr ) : StringHolder( substr ){}
|
||||
|
||||
bool operator()( NSString* str ) const
|
||||
{
|
||||
return [str rangeOfString:m_substr].location != NSNotFound;
|
||||
}
|
||||
|
||||
friend std::ostream& operator<<( std::ostream& os, const Contains& matcher )
|
||||
{
|
||||
os << "contains: " << Catch::toString( matcher.m_substr );
|
||||
return os;
|
||||
}
|
||||
};
|
||||
|
||||
struct StartsWith : StringHolder
|
||||
{
|
||||
StartsWith( NSString* substr ) : StringHolder( substr ){}
|
||||
|
||||
bool operator()( NSString* str ) const
|
||||
{
|
||||
return [str rangeOfString:m_substr].location == 0;
|
||||
}
|
||||
|
||||
friend std::ostream& operator<<( std::ostream& os, const StartsWith& matcher )
|
||||
{
|
||||
os << "starts with: " << Catch::toString( matcher.m_substr );
|
||||
return os;
|
||||
}
|
||||
};
|
||||
struct EndsWith : StringHolder
|
||||
{
|
||||
EndsWith( NSString* substr ) : StringHolder( substr ){}
|
||||
|
||||
bool operator()( NSString* str ) const
|
||||
{
|
||||
return [str rangeOfString:m_substr].location == [str length] - [m_substr length];
|
||||
}
|
||||
|
||||
friend std::ostream& operator<<( std::ostream& os, const EndsWith& matcher )
|
||||
{
|
||||
os << "ends with: " << Catch::toString( matcher.m_substr );
|
||||
return os;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace NSStringMatchers
|
||||
} // namespace Impl
|
||||
|
||||
inline Impl::NSStringMatchers::Contains
|
||||
Contains( NSString* substr ){ return Impl::NSStringMatchers::Contains( substr ); }
|
||||
inline Impl::NSStringMatchers::StartsWith
|
||||
StartsWith( NSString* substr ){ return Impl::NSStringMatchers::StartsWith( substr ); }
|
||||
inline Impl::NSStringMatchers::EndsWith
|
||||
EndsWith( NSString* substr ){ return Impl::NSStringMatchers::EndsWith( substr ); }
|
||||
|
||||
} // namespace Matchers
|
||||
|
||||
using namespace Matchers;
|
||||
|
||||
} // namespace Catch
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
#define OC_TEST_CASE( name, desc )\
|
||||
|
@ -42,4 +42,12 @@ OC_TEST_CASE( "OCTest/test2", "This is another test case" )
|
||||
REQUIRE( obj.int_val == 2 );
|
||||
}
|
||||
|
||||
using namespace Catch::Matchers;
|
||||
OC_TEST_CASE( "OCTest/matchers", "Matches work with OC types (NSString so far)" )
|
||||
{
|
||||
REQUIRE_THAT( @"This is a string", Contains( @"is a" ) );
|
||||
REQUIRE_THAT( @"This is a string", StartsWith( @"This" ) );
|
||||
REQUIRE_THAT( @"This is a string", EndsWith( @"string" ) );
|
||||
}
|
||||
|
||||
@end
|
||||
|
Loading…
Reference in New Issue
Block a user