1 // Copyright 2010 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
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"
15 // Chromium compiles this file as ARC, but other dependencies pull it in and
16 // compile it as non-ARC. Be sure that this file compiles correctly with either
23 #import <Foundation/Foundation.h>
25 namespace testing::internal {
27 static std::string StringFromNSString(NSString* string) {
28 // Note that -[NSString UTF8String] is banned in Chromium code because
29 // base::SysNSStringToUTF8() is safer, but //testing isn't allowed to depend
30 // on //base, so deliberately ignore that function ban.
31 const char* utf_string = string.UTF8String;
32 return utf_string ? std::string(utf_string) : std::string("(nil nsstring)");
35 // Handles nil values for |obj| properly by using safe printing of %@ in
36 // -stringWithFormat:.
37 std::string StringDescription(id<NSObject> obj) {
38 return StringFromNSString([NSString stringWithFormat:@"%@", obj]);
41 // This overloaded version allows comparison between ObjC objects that conform
42 // to the NSObject protocol. Used to implement {ASSERT|EXPECT}_EQ().
43 GTEST_API_ AssertionResult CmpHelperNSEQ(const char* expected_expression,
44 const char* actual_expression,
45 id<NSObject> expected,
46 id<NSObject> actual) {
47 if (expected == actual || [expected isEqual:actual]) {
48 return AssertionSuccess();
50 return EqFailure(expected_expression, actual_expression,
51 StringDescription(expected), StringDescription(actual),
55 // This overloaded version allows comparison between ObjC objects that conform
56 // to the NSObject protocol. Used to implement {ASSERT|EXPECT}_NE().
57 GTEST_API_ AssertionResult CmpHelperNSNE(const char* expected_expression,
58 const char* actual_expression,
59 id<NSObject> expected,
60 id<NSObject> actual) {
61 if (expected != actual && ![expected isEqual:actual]) {
62 return AssertionSuccess();
65 msg << "Expected: (" << expected_expression << ") != (" << actual_expression
66 << "), actual: " << StringDescription(expected)
67 << " vs " << StringDescription(actual);
68 return AssertionFailure(msg);
71 #if !defined(GTEST_OS_IOS)
73 GTEST_API_ AssertionResult CmpHelperNSEQ(const char* expected_expression,
74 const char* actual_expression,
75 const NSRect& expected,
76 const NSRect& actual) {
77 if (NSEqualRects(expected, actual)) {
78 return AssertionSuccess();
80 return EqFailure(expected_expression, actual_expression,
81 StringFromNSString(NSStringFromRect(expected)),
82 StringFromNSString(NSStringFromRect(actual)), false);
85 GTEST_API_ AssertionResult CmpHelperNSNE(const char* expected_expression,
86 const char* actual_expression,
87 const NSRect& expected,
88 const NSRect& actual) {
89 if (!NSEqualRects(expected, actual)) {
90 return AssertionSuccess();
93 msg << "Expected: (" << expected_expression << ") != (" << actual_expression
94 << "), actual: " << StringFromNSString(NSStringFromRect(expected))
95 << " vs " << StringFromNSString(NSStringFromRect(actual));
96 return AssertionFailure(msg);
100 GTEST_API_ AssertionResult CmpHelperNSEQ(const char* expected_expression,
101 const char* actual_expression,
102 const NSPoint& expected,
103 const NSPoint& actual) {
104 if (NSEqualPoints(expected, actual)) {
105 return AssertionSuccess();
107 return EqFailure(expected_expression, actual_expression,
108 StringFromNSString(NSStringFromPoint(expected)),
109 StringFromNSString(NSStringFromPoint(actual)), false);
112 GTEST_API_ AssertionResult CmpHelperNSNE(const char* expected_expression,
113 const char* actual_expression,
114 const NSPoint& expected,
115 const NSPoint& actual) {
116 if (!NSEqualPoints(expected, actual)) {
117 return AssertionSuccess();
120 msg << "Expected: (" << expected_expression << ") != (" << actual_expression
121 << "), actual: " << StringFromNSString(NSStringFromPoint(expected))
122 << " vs " << StringFromNSString(NSStringFromPoint(actual));
123 return AssertionFailure(msg);
126 GTEST_API_ AssertionResult CmpHelperNSEQ(const char* expected_expression,
127 const char* actual_expression,
128 const NSRange& expected,
129 const NSRange& actual) {
130 if (NSEqualRanges(expected, actual)) {
131 return AssertionSuccess();
133 return EqFailure(expected_expression, actual_expression,
134 StringFromNSString(NSStringFromRange(expected)),
135 StringFromNSString(NSStringFromRange(actual)), false);
138 GTEST_API_ AssertionResult CmpHelperNSNE(const char* expected_expression,
139 const char* actual_expression,
140 const NSRange& expected,
141 const NSRange& actual) {
142 if (!NSEqualRanges(expected, actual)) {
143 return AssertionSuccess();
146 msg << "Expected: (" << expected_expression << ") != (" << actual_expression
147 << "), actual: " << StringFromNSString(NSStringFromRange(expected))
148 << " vs " << StringFromNSString(NSStringFromRange(actual));
149 return AssertionFailure(msg);
152 #endif // !GTEST_OS_IOS
154 } // namespace testing::internal
156 #endif // GTEST_OS_MAC