From 32e70b223509cb5a166e1110ee56ad19a5238592 Mon Sep 17 00:00:00 2001 From: Phil Nash Date: Tue, 12 Mar 2013 18:49:22 +0000 Subject: [PATCH] Fixes nil NSString issue reported in #159 --- include/internal/catch_tostring.hpp | 4 ++++ projects/XCode4/OCTest/OCTest/CatchOCTestCase.mm | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/include/internal/catch_tostring.hpp b/include/internal/catch_tostring.hpp index ef9b5efc..f87127e0 100644 --- a/include/internal/catch_tostring.hpp +++ b/include/internal/catch_tostring.hpp @@ -161,9 +161,13 @@ inline std::string toString( std::nullptr_t ) { #ifdef __OBJC__ inline std::string toString( NSString const * const& nsstring ) { + if( !nsstring ) + return "nil"; return std::string( "@\"" ) + [nsstring UTF8String] + "\""; } inline std::string toString( NSString * CATCH_ARC_STRONG const& nsstring ) { + if( !nsstring ) + return "nil"; return std::string( "@\"" ) + [nsstring UTF8String] + "\""; } inline std::string toString( NSObject* const& nsObject ) { diff --git a/projects/XCode4/OCTest/OCTest/CatchOCTestCase.mm b/projects/XCode4/OCTest/OCTest/CatchOCTestCase.mm index 9fdb17a7..1ed7aa5f 100644 --- a/projects/XCode4/OCTest/OCTest/CatchOCTestCase.mm +++ b/projects/XCode4/OCTest/OCTest/CatchOCTestCase.mm @@ -74,4 +74,9 @@ OC_TEST_CASE( "OCTest/matchers", "Matches work with OC types (NSString so far)" REQUIRE_THAT( @"This is a string", EndsWith( @"string" ) ); } +OC_TEST_CASE( "OCTest/matchers/nil", "nil NSString should not crash the test" ) +{ + REQUIRE_THAT( (NSString*)nil, Equals( @"This should fail, but not crash" ) ); +} + @end