[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 | |
[email protected] | 69ceabd | 2013-03-29 20:17:05 | [diff] [blame] | 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] UTF8String]; |
| 24 | } |
| 25 | |
[email protected] | ecebcc9 | 2010-08-06 02:39:26 | [diff] [blame] | 26 | // This overloaded version allows comparison between ObjC objects that conform |
| 27 | // to the NSObject protocol. Used to implement {ASSERT|EXPECT}_EQ(). |
| 28 | GTEST_API_ AssertionResult CmpHelperNSEQ(const char* expected_expression, |
| 29 | const char* actual_expression, |
| 30 | id<NSObject> expected, |
| 31 | id<NSObject> actual) { |
[email protected] | 22f7359 | 2010-11-10 21:50:38 | [diff] [blame] | 32 | if (expected == actual || [expected isEqual:actual]) { |
[email protected] | ecebcc9 | 2010-08-06 02:39:26 | [diff] [blame] | 33 | return AssertionSuccess(); |
| 34 | } |
| 35 | return EqFailure(expected_expression, |
| 36 | actual_expression, |
[email protected] | 69ceabd | 2013-03-29 20:17:05 | [diff] [blame] | 37 | std::string(StringDescription(expected)), |
| 38 | std::string(StringDescription(actual)), |
[email protected] | ecebcc9 | 2010-08-06 02:39:26 | [diff] [blame] | 39 | false); |
| 40 | } |
| 41 | |
| 42 | // This overloaded version allows comparison between ObjC objects that conform |
| 43 | // to the NSObject protocol. Used to implement {ASSERT|EXPECT}_NE(). |
| 44 | GTEST_API_ AssertionResult CmpHelperNSNE(const char* expected_expression, |
| 45 | const char* actual_expression, |
| 46 | id<NSObject> expected, |
| 47 | id<NSObject> actual) { |
[email protected] | 22f7359 | 2010-11-10 21:50:38 | [diff] [blame] | 48 | if (expected != actual && ![expected isEqual:actual]) { |
[email protected] | ecebcc9 | 2010-08-06 02:39:26 | [diff] [blame] | 49 | return AssertionSuccess(); |
| 50 | } |
| 51 | Message msg; |
| 52 | msg << "Expected: (" << expected_expression << ") != (" << actual_expression |
[email protected] | 69ceabd | 2013-03-29 20:17:05 | [diff] [blame] | 53 | << "), actual: " << StringDescription(expected) |
| 54 | << " vs " << StringDescription(actual); |
[email protected] | ecebcc9 | 2010-08-06 02:39:26 | [diff] [blame] | 55 | return AssertionFailure(msg); |
| 56 | } |
| 57 | |
jackhou | b22ade8 | 2015-07-01 00:44:39 | [diff] [blame] | 58 | #if !defined(GTEST_OS_IOS) |
| 59 | |
| 60 | GTEST_API_ AssertionResult CmpHelperNSEQ(const char* expected_expression, |
| 61 | const char* actual_expression, |
| 62 | const NSRect& expected, |
| 63 | const NSRect& actual) { |
| 64 | if (NSEqualRects(expected, actual)) { |
| 65 | return AssertionSuccess(); |
| 66 | } |
| 67 | return EqFailure(expected_expression, |
| 68 | actual_expression, |
| 69 | [NSStringFromRect(expected) UTF8String], |
| 70 | [NSStringFromRect(actual) UTF8String], |
| 71 | false); |
| 72 | |
| 73 | } |
| 74 | |
| 75 | GTEST_API_ AssertionResult CmpHelperNSNE(const char* expected_expression, |
| 76 | const char* actual_expression, |
| 77 | const NSRect& expected, |
| 78 | const NSRect& actual) { |
| 79 | if (!NSEqualRects(expected, actual)) { |
| 80 | return AssertionSuccess(); |
| 81 | } |
| 82 | Message msg; |
| 83 | msg << "Expected: (" << expected_expression << ") != (" << actual_expression |
| 84 | << "), actual: " << [NSStringFromRect(expected) UTF8String] |
| 85 | << " vs " << [NSStringFromRect(actual) UTF8String]; |
| 86 | return AssertionFailure(msg); |
| 87 | |
| 88 | } |
| 89 | |
| 90 | GTEST_API_ AssertionResult CmpHelperNSEQ(const char* expected_expression, |
| 91 | const char* actual_expression, |
| 92 | const NSPoint& expected, |
| 93 | const NSPoint& actual) { |
| 94 | if (NSEqualPoints(expected, actual)) { |
| 95 | return AssertionSuccess(); |
| 96 | } |
| 97 | return EqFailure(expected_expression, |
| 98 | actual_expression, |
| 99 | [NSStringFromPoint(expected) UTF8String], |
| 100 | [NSStringFromPoint(actual) UTF8String], |
| 101 | false); |
| 102 | |
| 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 |
| 114 | << "), actual: " << [NSStringFromPoint(expected) UTF8String] |
| 115 | << " vs " << [NSStringFromPoint(actual) UTF8String]; |
| 116 | return AssertionFailure(msg); |
| 117 | |
| 118 | } |
| 119 | |
| 120 | #endif // !GTEST_OS_IOS |
| 121 | |
[email protected] | ecebcc9 | 2010-08-06 02:39:26 | [diff] [blame] | 122 | } // namespace internal |
| 123 | } // namespace testing |
| 124 | |
| 125 | #endif // GTEST_OS_MAC |