blob: b490f558bdbe651381970f32c7f80adbe9fb35db [file] [log] [blame]
dsinclair39075a42016-04-25 06:33:07 -07001// 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#import "gtest_mac.h"
5#include <string>
6#include <gtest/gtest.h>
7#include <gtest/internal/gtest-port.h>
8#include <gtest/internal/gtest-string.h>
9#ifdef GTEST_OS_MAC
10#import <Foundation/Foundation.h>
11namespace testing {
12namespace internal {
13// Handles nil values for |obj| properly by using safe printing of %@ in
14// -stringWithFormat:.
15static inline const char* StringDescription(id<NSObject> obj) {
16 return [[NSString stringWithFormat:@"%@", obj] UTF8String];
17}
18// This overloaded version allows comparison between ObjC objects that conform
19// to the NSObject protocol. Used to implement {ASSERT|EXPECT}_EQ().
20GTEST_API_ AssertionResult CmpHelperNSEQ(const char* expected_expression,
21 const char* actual_expression,
22 id<NSObject> expected,
23 id<NSObject> actual) {
24 if (expected == actual || [expected isEqual:actual]) {
25 return AssertionSuccess();
26 }
27 return EqFailure(expected_expression, actual_expression,
28 std::string(StringDescription(expected)),
29 std::string(StringDescription(actual)), false);
30}
31// This overloaded version allows comparison between ObjC objects that conform
32// to the NSObject protocol. Used to implement {ASSERT|EXPECT}_NE().
33GTEST_API_ AssertionResult CmpHelperNSNE(const char* expected_expression,
34 const char* actual_expression,
35 id<NSObject> expected,
36 id<NSObject> actual) {
37 if (expected != actual && ![expected isEqual:actual]) {
38 return AssertionSuccess();
39 }
40 Message msg;
41 msg << "Expected: (" << expected_expression << ") != (" << actual_expression
42 << "), actual: " << StringDescription(expected) << " vs "
43 << StringDescription(actual);
44 return AssertionFailure(msg);
45}
46#if !defined(GTEST_OS_IOS)
47GTEST_API_ AssertionResult CmpHelperNSEQ(const char* expected_expression,
48 const char* actual_expression,
49 const NSRect& expected,
50 const NSRect& actual) {
51 if (NSEqualRects(expected, actual)) {
52 return AssertionSuccess();
53 }
54 return EqFailure(expected_expression, actual_expression,
55 [NSStringFromRect(expected) UTF8String],
56 [NSStringFromRect(actual) UTF8String], false);
57}
58GTEST_API_ AssertionResult CmpHelperNSNE(const char* expected_expression,
59 const char* actual_expression,
60 const NSRect& expected,
61 const NSRect& actual) {
62 if (!NSEqualRects(expected, actual)) {
63 return AssertionSuccess();
64 }
65 Message msg;
66 msg << "Expected: (" << expected_expression << ") != (" << actual_expression
67 << "), actual: " << [NSStringFromRect(expected) UTF8String] << " vs "
68 << [NSStringFromRect(actual) UTF8String];
69 return AssertionFailure(msg);
70}
71GTEST_API_ AssertionResult CmpHelperNSEQ(const char* expected_expression,
72 const char* actual_expression,
73 const NSPoint& expected,
74 const NSPoint& actual) {
75 if (NSEqualPoints(expected, actual)) {
76 return AssertionSuccess();
77 }
78 return EqFailure(expected_expression, actual_expression,
79 [NSStringFromPoint(expected) UTF8String],
80 [NSStringFromPoint(actual) UTF8String], false);
81}
82GTEST_API_ AssertionResult CmpHelperNSNE(const char* expected_expression,
83 const char* actual_expression,
84 const NSPoint& expected,
85 const NSPoint& actual) {
86 if (!NSEqualPoints(expected, actual)) {
87 return AssertionSuccess();
88 }
89 Message msg;
90 msg << "Expected: (" << expected_expression << ") != (" << actual_expression
91 << "), actual: " << [NSStringFromPoint(expected) UTF8String] << " vs "
92 << [NSStringFromPoint(actual) UTF8String];
93 return AssertionFailure(msg);
94}
95#endif // !GTEST_OS_IOS
96} // namespace internal
97} // namespace testing
98#endif // GTEST_OS_MAC