blob: ccbfb1ad7dc19831e7e009059a2738eee31dc0bd [file] [log] [blame]
[email protected]ecebcc92010-08-06 02:39:261// 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]c64fb19f2012-11-16 10:47:507#include <string>
8
pwnall52e27a32017-05-05 00:44:279#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]ecebcc92010-08-06 02:39:2612
13#ifdef GTEST_OS_MAC
14
15#import <Foundation/Foundation.h>
16
17namespace testing {
18namespace internal {
19
Elly Fong-Jones96e8b3f82019-02-27 21:41:5320static 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-Jones3fe8d8a2019-03-01 16:48:1924 const char* utf_string = [string UTF8String];
25 return utf_string ? std::string(utf_string) : std::string("(nil nsstring)");
Elly Fong-Jones96e8b3f82019-02-27 21:41:5326}
27
[email protected]69ceabd2013-03-29 20:17:0528// Handles nil values for |obj| properly by using safe printing of %@ in
29// -stringWithFormat:.
Elly Fong-Jones3fe8d8a2019-03-01 16:48:1930std::string StringDescription(id<NSObject> obj) {
31 return StringFromNSString([NSString stringWithFormat:@"%@", obj]);
[email protected]69ceabd2013-03-29 20:17:0532}
33
[email protected]ecebcc92010-08-06 02:39:2634// This overloaded version allows comparison between ObjC objects that conform
35// to the NSObject protocol. Used to implement {ASSERT|EXPECT}_EQ().
36GTEST_API_ AssertionResult CmpHelperNSEQ(const char* expected_expression,
37 const char* actual_expression,
38 id<NSObject> expected,
39 id<NSObject> actual) {
[email protected]22f73592010-11-10 21:50:3840 if (expected == actual || [expected isEqual:actual]) {
[email protected]ecebcc92010-08-06 02:39:2641 return AssertionSuccess();
42 }
Elly Fong-Jones96e8b3f82019-02-27 21:41:5343 return EqFailure(expected_expression, actual_expression,
44 StringDescription(expected), StringDescription(actual),
[email protected]ecebcc92010-08-06 02:39:2645 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().
50GTEST_API_ AssertionResult CmpHelperNSNE(const char* expected_expression,
51 const char* actual_expression,
52 id<NSObject> expected,
53 id<NSObject> actual) {
[email protected]22f73592010-11-10 21:50:3854 if (expected != actual && ![expected isEqual:actual]) {
[email protected]ecebcc92010-08-06 02:39:2655 return AssertionSuccess();
56 }
57 Message msg;
58 msg << "Expected: (" << expected_expression << ") != (" << actual_expression
[email protected]69ceabd2013-03-29 20:17:0559 << "), actual: " << StringDescription(expected)
60 << " vs " << StringDescription(actual);
[email protected]ecebcc92010-08-06 02:39:2661 return AssertionFailure(msg);
62}
63
jackhoub22ade82015-07-01 00:44:3964#if !defined(GTEST_OS_IOS)
65
66GTEST_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-Jones96e8b3f82019-02-27 21:41:5373 return EqFailure(expected_expression, actual_expression,
74 StringFromNSString(NSStringFromRect(expected)),
75 StringFromNSString(NSStringFromRect(actual)), false);
jackhoub22ade82015-07-01 00:44:3976}
77
78GTEST_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-Jones96e8b3f82019-02-27 21:41:5387 << "), actual: " << StringFromNSString(NSStringFromRect(expected))
88 << " vs " << StringFromNSString(NSStringFromRect(actual));
jackhoub22ade82015-07-01 00:44:3989 return AssertionFailure(msg);
90
91}
92
93GTEST_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-Jones96e8b3f82019-02-27 21:41:53100 return EqFailure(expected_expression, actual_expression,
101 StringFromNSString(NSStringFromPoint(expected)),
102 StringFromNSString(NSStringFromPoint(actual)), false);
jackhoub22ade82015-07-01 00:44:39103}
104
105GTEST_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-Jones96e8b3f82019-02-27 21:41:53114 << "), actual: " << StringFromNSString(NSStringFromPoint(expected))
115 << " vs " << StringFromNSString(NSStringFromPoint(actual));
jackhoub22ade82015-07-01 00:44:39116 return AssertionFailure(msg);
Elly Fong-Jones96e8b3f82019-02-27 21:41:53117}
jackhoub22ade82015-07-01 00:44:39118
Elly Fong-Jones96e8b3f82019-02-27 21:41:53119GTEST_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
131GTEST_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);
jackhoub22ade82015-07-01 00:44:39143}
144
145#endif // !GTEST_OS_IOS
146
[email protected]ecebcc92010-08-06 02:39:26147} // namespace internal
148} // namespace testing
149
150#endif // GTEST_OS_MAC