diff options
author | pkl@chromium.org <pkl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-03-29 20:17:05 +0000 |
---|---|---|
committer | pkl@chromium.org <pkl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-03-29 20:17:05 +0000 |
commit | 69ceabd3799404eed6bda5d3f03a795b0d174931 (patch) | |
tree | 36ce1d848e9ad591983fd0b09948074aed7c6150 /testing | |
parent | 712ed5cb346b6d6239e907523c4c3453bdae16ea (diff) | |
download | chromium_src-69ceabd3799404eed6bda5d3f03a795b0d174931.zip chromium_src-69ceabd3799404eed6bda5d3f03a795b0d174931.tar.gz chromium_src-69ceabd3799404eed6bda5d3f03a795b0d174931.tar.bz2 |
Added a StringDescription() function to safely print object pointers that may be nil.
If expected or actual is nil, calling [[obj description] UTF8String]
returns nil and results in a call to std::string constructor with
a nil pointer. This causes a C++ exception.
Review URL: https://chromiumcodereview.appspot.com/13318003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@191439 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'testing')
-rw-r--r-- | testing/gtest_mac.mm | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/testing/gtest_mac.mm b/testing/gtest_mac.mm index c36b18b..b39d258 100644 --- a/testing/gtest_mac.mm +++ b/testing/gtest_mac.mm @@ -17,6 +17,12 @@ namespace testing { namespace internal { +// Handles nil values for |obj| properly by using safe printing of %@ in +// -stringWithFormat:. +static inline const char* StringDescription(id<NSObject> obj) { + return [[NSString stringWithFormat:@"%@", obj] UTF8String]; +} + // This overloaded version allows comparison between ObjC objects that conform // to the NSObject protocol. Used to implement {ASSERT|EXPECT}_EQ(). GTEST_API_ AssertionResult CmpHelperNSEQ(const char* expected_expression, @@ -28,8 +34,8 @@ GTEST_API_ AssertionResult CmpHelperNSEQ(const char* expected_expression, } return EqFailure(expected_expression, actual_expression, - std::string([[expected description] UTF8String]), - std::string([[actual description] UTF8String]), + std::string(StringDescription(expected)), + std::string(StringDescription(actual)), false); } @@ -44,8 +50,8 @@ GTEST_API_ AssertionResult CmpHelperNSNE(const char* expected_expression, } Message msg; msg << "Expected: (" << expected_expression << ") != (" << actual_expression - << "), actual: " << std::string([[expected description] UTF8String]) - << " vs " << std::string([[actual description] UTF8String]); + << "), actual: " << StringDescription(expected) + << " vs " << StringDescription(actual); return AssertionFailure(msg); } |