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

Side by Side 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: fixed indentation Created 7 years, 8 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 unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #import "gtest_mac.h" 5 #import "gtest_mac.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include <gtest/internal/gtest-port.h> 9 #include <gtest/internal/gtest-port.h>
10 #include <gtest/internal/gtest-string.h> 10 #include <gtest/internal/gtest-string.h>
11 #include <gtest/gtest.h> 11 #include <gtest/gtest.h>
12 12
13 #ifdef GTEST_OS_MAC 13 #ifdef GTEST_OS_MAC
14 14
15 #import <Foundation/Foundation.h> 15 #import <Foundation/Foundation.h>
16 16
17 namespace testing { 17 namespace testing {
18 namespace internal { 18 namespace internal {
19 19
20 // Handles nil values for |obj| properly by using safe printing of %@ in
21 // -stringWithFormat:.
22 static inline const char* StringDescription(id<NSObject> obj) {
23 return [[NSString stringWithFormat:@"%@", [obj description]] UTF8String];
Robert Sesek 2013/03/29 16:33:59 Just pass |obj| as the format argument, which will
pkl (ping after 24h if needed) 2013/03/29 16:38:45 Done.
24 }
25
20 // This overloaded version allows comparison between ObjC objects that conform 26 // This overloaded version allows comparison between ObjC objects that conform
21 // to the NSObject protocol. Used to implement {ASSERT|EXPECT}_EQ(). 27 // to the NSObject protocol. Used to implement {ASSERT|EXPECT}_EQ().
22 GTEST_API_ AssertionResult CmpHelperNSEQ(const char* expected_expression, 28 GTEST_API_ AssertionResult CmpHelperNSEQ(const char* expected_expression,
23 const char* actual_expression, 29 const char* actual_expression,
24 id<NSObject> expected, 30 id<NSObject> expected,
25 id<NSObject> actual) { 31 id<NSObject> actual) {
26 if (expected == actual || [expected isEqual:actual]) { 32 if (expected == actual || [expected isEqual:actual]) {
27 return AssertionSuccess(); 33 return AssertionSuccess();
28 } 34 }
29 return EqFailure(expected_expression, 35 return EqFailure(expected_expression,
30 actual_expression, 36 actual_expression,
31 std::string([[expected description] UTF8String]), 37 std::string(StringDescription(expected)),
32 std::string([[actual description] UTF8String]), 38 std::string(StringDescription(actual)),
33 false); 39 false);
34 } 40 }
35 41
36 // This overloaded version allows comparison between ObjC objects that conform 42 // This overloaded version allows comparison between ObjC objects that conform
37 // to the NSObject protocol. Used to implement {ASSERT|EXPECT}_NE(). 43 // to the NSObject protocol. Used to implement {ASSERT|EXPECT}_NE().
38 GTEST_API_ AssertionResult CmpHelperNSNE(const char* expected_expression, 44 GTEST_API_ AssertionResult CmpHelperNSNE(const char* expected_expression,
39 const char* actual_expression, 45 const char* actual_expression,
40 id<NSObject> expected, 46 id<NSObject> expected,
41 id<NSObject> actual) { 47 id<NSObject> actual) {
42 if (expected != actual && ![expected isEqual:actual]) { 48 if (expected != actual && ![expected isEqual:actual]) {
43 return AssertionSuccess(); 49 return AssertionSuccess();
44 } 50 }
45 Message msg; 51 Message msg;
46 msg << "Expected: (" << expected_expression << ") != (" << actual_expression 52 msg << "Expected: (" << expected_expression << ") != (" << actual_expression
47 << "), actual: " << std::string([[expected description] UTF8String]) 53 << "), actual: " << StringDescription(expected)
48 << " vs " << std::string([[actual description] UTF8String]); 54 << " vs " << StringDescription(actual);
49 return AssertionFailure(msg); 55 return AssertionFailure(msg);
50 } 56 }
51 57
52 } // namespace internal 58 } // namespace internal
53 } // namespace testing 59 } // namespace testing
54 60
55 #endif // GTEST_OS_MAC 61 #endif // GTEST_OS_MAC
OLDNEW
« 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