[email protected] | ecebcc9 | 2010-08-06 02:39:26 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #import "gtest_mac.h" |
| 6 | |
[email protected] | c64fb19f | 2012-11-16 10:47:50 | [diff] [blame] | 7 | #include <string> |
| 8 | |
pwnall | 52e27a3 | 2017-05-05 00:44:27 | [diff] [blame] | 9 | #include "testing/gtest/include/gtest/gtest.h" |
| 10 | #include "third_party/googletest/src/googletest/include/gtest/internal/gtest-port.h" |
| 11 | #include "third_party/googletest/src/googletest/include/gtest/internal/gtest-string.h" |
[email protected] | ecebcc9 | 2010-08-06 02:39:26 | [diff] [blame] | 12 | |
| 13 | #ifdef GTEST_OS_MAC |
| 14 | |
| 15 | #import <Foundation/Foundation.h> |
| 16 | |
| 17 | namespace testing { |
| 18 | namespace internal { |
| 19 | |
Elly Fong-Jones | 96e8b3f8 | 2019-02-27 21:41:53 | [diff] [blame] | 20 | static std::string StringFromNSString(NSString* string) { |
| 21 | // Note that -[NSString UTF8String] is banned in chromium code because |
| 22 | // base::SysNSStringToUTF8() is safer, but //testing isn't allowed to depend |
| 23 | // on //base, so deliberately ignore that function ban. |
Elly Fong-Jones | 3fe8d8a | 2019-03-01 16:48:19 | [diff] [blame] | 24 | const char* utf_string = [string UTF8String]; |
| 25 | return utf_string ? std::string(utf_string) : std::string("(nil nsstring)"); |
Elly Fong-Jones | 96e8b3f8 | 2019-02-27 21:41:53 | [diff] [blame] | 26 | } |
| 27 | |
[email protected] | 69ceabd | 2013-03-29 20:17:05 | [diff] [blame] | 28 | // Handles nil values for |obj| properly by using safe printing of %@ in |
| 29 | // -stringWithFormat:. |
Elly Fong-Jones | 3fe8d8a | 2019-03-01 16:48:19 | [diff] [blame] | 30 | std::string StringDescription(id<NSObject> obj) { |
| 31 | return StringFromNSString([NSString stringWithFormat:@"%@", obj]); |
[email protected] | 69ceabd | 2013-03-29 20:17:05 | [diff] [blame] | 32 | } |
| 33 | |
[email protected] | ecebcc9 | 2010-08-06 02:39:26 | [diff] [blame] | 34 | // This overloaded version allows comparison between ObjC objects that conform |
| 35 | // to the NSObject protocol. Used to implement {ASSERT|EXPECT}_EQ(). |
| 36 | GTEST_API_ AssertionResult CmpHelperNSEQ(const char* expected_expression, |
| 37 | const char* actual_expression, |
| 38 | id<NSObject> expected, |
| 39 | id<NSObject> actual) { |
[email protected] | 22f7359 | 2010-11-10 21:50:38 | [diff] [blame] | 40 | if (expected == actual || [expected isEqual:actual]) { |
[email protected] | ecebcc9 | 2010-08-06 02:39:26 | [diff] [blame] | 41 | return AssertionSuccess(); |
| 42 | } |
Elly Fong-Jones | 96e8b3f8 | 2019-02-27 21:41:53 | [diff] [blame] | 43 | return EqFailure(expected_expression, actual_expression, |
| 44 | StringDescription(expected), StringDescription(actual), |
[email protected] | ecebcc9 | 2010-08-06 02:39:26 | [diff] [blame] | 45 | false); |
| 46 | } |
| 47 | |
| 48 | // This overloaded version allows comparison between ObjC objects that conform |
| 49 | // to the NSObject protocol. Used to implement {ASSERT|EXPECT}_NE(). |
| 50 | GTEST_API_ AssertionResult CmpHelperNSNE(const char* expected_expression, |
| 51 | const char* actual_expression, |
| 52 | id<NSObject> expected, |
| 53 | id<NSObject> actual) { |
[email protected] | 22f7359 | 2010-11-10 21:50:38 | [diff] [blame] | 54 | if (expected != actual && ![expected isEqual:actual]) { |
[email protected] | ecebcc9 | 2010-08-06 02:39:26 | [diff] [blame] | 55 | return AssertionSuccess(); |
| 56 | } |
| 57 | Message msg; |
| 58 | msg << "Expected: (" << expected_expression << ") != (" << actual_expression |
[email protected] | 69ceabd | 2013-03-29 20:17:05 | [diff] [blame] | 59 | << "), actual: " << StringDescription(expected) |
| 60 | << " vs " << StringDescription(actual); |
[email protected] | ecebcc9 | 2010-08-06 02:39:26 | [diff] [blame] | 61 | return AssertionFailure(msg); |
| 62 | } |
| 63 | |
jackhou | b22ade8 | 2015-07-01 00:44:39 | [diff] [blame] | 64 | #if !defined(GTEST_OS_IOS) |
| 65 | |
| 66 | GTEST_API_ AssertionResult CmpHelperNSEQ(const char* expected_expression, |
| 67 | const char* actual_expression, |
| 68 | const NSRect& expected, |
| 69 | const NSRect& actual) { |
| 70 | if (NSEqualRects(expected, actual)) { |
| 71 | return AssertionSuccess(); |
| 72 | } |
Elly Fong-Jones | 96e8b3f8 | 2019-02-27 21:41:53 | [diff] [blame] | 73 | return EqFailure(expected_expression, actual_expression, |
| 74 | StringFromNSString(NSStringFromRect(expected)), |
| 75 | StringFromNSString(NSStringFromRect(actual)), false); |
jackhou | b22ade8 | 2015-07-01 00:44:39 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | GTEST_API_ AssertionResult CmpHelperNSNE(const char* expected_expression, |
| 79 | const char* actual_expression, |
| 80 | const NSRect& expected, |
| 81 | const NSRect& actual) { |
| 82 | if (!NSEqualRects(expected, actual)) { |
| 83 | return AssertionSuccess(); |
| 84 | } |
| 85 | Message msg; |
| 86 | msg << "Expected: (" << expected_expression << ") != (" << actual_expression |
Elly Fong-Jones | 96e8b3f8 | 2019-02-27 21:41:53 | [diff] [blame] | 87 | << "), actual: " << StringFromNSString(NSStringFromRect(expected)) |
| 88 | << " vs " << StringFromNSString(NSStringFromRect(actual)); |
jackhou | b22ade8 | 2015-07-01 00:44:39 | [diff] [blame] | 89 | return AssertionFailure(msg); |
| 90 | |
| 91 | } |
| 92 | |
| 93 | GTEST_API_ AssertionResult CmpHelperNSEQ(const char* expected_expression, |
| 94 | const char* actual_expression, |
| 95 | const NSPoint& expected, |
| 96 | const NSPoint& actual) { |
| 97 | if (NSEqualPoints(expected, actual)) { |
| 98 | return AssertionSuccess(); |
| 99 | } |
Elly Fong-Jones | 96e8b3f8 | 2019-02-27 21:41:53 | [diff] [blame] | 100 | return EqFailure(expected_expression, actual_expression, |
| 101 | StringFromNSString(NSStringFromPoint(expected)), |
| 102 | StringFromNSString(NSStringFromPoint(actual)), false); |
jackhou | b22ade8 | 2015-07-01 00:44:39 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | GTEST_API_ AssertionResult CmpHelperNSNE(const char* expected_expression, |
| 106 | const char* actual_expression, |
| 107 | const NSPoint& expected, |
| 108 | const NSPoint& actual) { |
| 109 | if (!NSEqualPoints(expected, actual)) { |
| 110 | return AssertionSuccess(); |
| 111 | } |
| 112 | Message msg; |
| 113 | msg << "Expected: (" << expected_expression << ") != (" << actual_expression |
Elly Fong-Jones | 96e8b3f8 | 2019-02-27 21:41:53 | [diff] [blame] | 114 | << "), actual: " << StringFromNSString(NSStringFromPoint(expected)) |
| 115 | << " vs " << StringFromNSString(NSStringFromPoint(actual)); |
jackhou | b22ade8 | 2015-07-01 00:44:39 | [diff] [blame] | 116 | return AssertionFailure(msg); |
Elly Fong-Jones | 96e8b3f8 | 2019-02-27 21:41:53 | [diff] [blame] | 117 | } |
jackhou | b22ade8 | 2015-07-01 00:44:39 | [diff] [blame] | 118 | |
Elly Fong-Jones | 96e8b3f8 | 2019-02-27 21:41:53 | [diff] [blame] | 119 | GTEST_API_ AssertionResult CmpHelperNSEQ(const char* expected_expression, |
| 120 | const char* actual_expression, |
| 121 | const NSRange& expected, |
| 122 | const NSRange& actual) { |
| 123 | if (NSEqualRanges(expected, actual)) { |
| 124 | return AssertionSuccess(); |
| 125 | } |
| 126 | return EqFailure(expected_expression, actual_expression, |
| 127 | StringFromNSString(NSStringFromRange(expected)), |
| 128 | StringFromNSString(NSStringFromRange(actual)), false); |
| 129 | } |
| 130 | |
| 131 | GTEST_API_ AssertionResult CmpHelperNSNE(const char* expected_expression, |
| 132 | const char* actual_expression, |
| 133 | const NSRange& expected, |
| 134 | const NSRange& actual) { |
| 135 | if (!NSEqualRanges(expected, actual)) { |
| 136 | return AssertionSuccess(); |
| 137 | } |
| 138 | Message msg; |
| 139 | msg << "Expected: (" << expected_expression << ") != (" << actual_expression |
| 140 | << "), actual: " << StringFromNSString(NSStringFromRange(expected)) |
| 141 | << " vs " << StringFromNSString(NSStringFromRange(actual)); |
| 142 | return AssertionFailure(msg); |
jackhou | b22ade8 | 2015-07-01 00:44:39 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | #endif // !GTEST_OS_IOS |
| 146 | |
[email protected] | ecebcc9 | 2010-08-06 02:39:26 | [diff] [blame] | 147 | } // namespace internal |
| 148 | } // namespace testing |
| 149 | |
| 150 | #endif // GTEST_OS_MAC |