Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(365)

Unified Diff: testing/gtest_mac.mm

Issue 13318003: Added a StringDescription() function to safely print object pointers that may be nil. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: obj is enough Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: testing/gtest_mac.mm
diff --git a/testing/gtest_mac.mm b/testing/gtest_mac.mm
index c36b18bb554c57efd085db984b2d585b0a5e276b..b39d258c13d15a42c1a644a8a1666f55b3073bf0 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);
}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698