gtest_mac_unittest.mm 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright (c) 2012 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. // Note that while this file is in testing/ and tests GTest macros, it is built
  5. // as part of Chromium's unit_tests target because the project does not build
  6. // or run GTest's internal test suite.
  7. #import "testing/gtest_mac.h"
  8. #import <Foundation/Foundation.h>
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. #include "third_party/googletest/src/googletest/include/gtest/internal/gtest-port.h"
  11. namespace testing {
  12. namespace internal {
  13. // This function is tested within this file, but it's not part of the public
  14. // API, and since it's a free function there's no way to friend the test for it.
  15. extern std::string StringDescription(id<NSObject> obj);
  16. }
  17. }
  18. TEST(GTestMac, NSStringComparators) {
  19. // This test wants to really guarantee that s1 and s2 aren't the same address,
  20. // so it constructs an autoreleased string this way. In theory this could be
  21. // done via [NSString stringWithString:] but that causes an error about using
  22. // a redundant literal :)
  23. NSString* s1 = [NSString stringWithFormat:@"%@", @"a"];
  24. NSString* s2 = @"a";
  25. EXPECT_NSEQ(@"a", @"a");
  26. EXPECT_NE(s1, s2);
  27. EXPECT_NSEQ(s1, s2);
  28. ASSERT_NE(s1, s2);
  29. ASSERT_NSEQ(s1, s2);
  30. ASSERT_NSNE(@"a", @"b");
  31. EXPECT_NSEQ(nil, nil);
  32. EXPECT_NSNE(nil, @"a");
  33. EXPECT_NSNE(@"a", nil);
  34. }
  35. TEST(GTestMac, NSNumberComparators) {
  36. EXPECT_NSNE(@2, @42);
  37. EXPECT_NSEQ(@42, @42);
  38. }
  39. #if !defined(GTEST_OS_IOS)
  40. TEST(GTestMac, NSRectComparators) {
  41. ASSERT_NSEQ(NSMakeRect(1, 2, 3, 4), NSMakeRect(1, 2, 3, 4));
  42. ASSERT_NSNE(NSMakeRect(1, 2, 3, 4), NSMakeRect(5, 6, 7, 8));
  43. EXPECT_NSEQ(NSMakeRect(1, 2, 3, 4), NSMakeRect(1, 2, 3, 4));
  44. EXPECT_NSNE(NSMakeRect(1, 2, 3, 4), NSMakeRect(5, 6, 7, 8));
  45. }
  46. TEST(GTestMac, NSPointComparators) {
  47. ASSERT_NSEQ(NSMakePoint(1, 2), NSMakePoint(1, 2));
  48. ASSERT_NSNE(NSMakePoint(1, 2), NSMakePoint(1, 3));
  49. ASSERT_NSNE(NSMakePoint(1, 2), NSMakePoint(2, 2));
  50. EXPECT_NSEQ(NSMakePoint(3, 4), NSMakePoint(3, 4));
  51. EXPECT_NSNE(NSMakePoint(3, 4), NSMakePoint(3, 3));
  52. EXPECT_NSNE(NSMakePoint(3, 4), NSMakePoint(4, 3));
  53. }
  54. TEST(GTestMac, NSRangeComparators) {
  55. ASSERT_NSEQ(NSMakeRange(1, 2), NSMakeRange(1, 2));
  56. ASSERT_NSNE(NSMakeRange(1, 2), NSMakeRange(1, 3));
  57. ASSERT_NSNE(NSMakeRange(1, 2), NSMakeRange(2, 2));
  58. EXPECT_NSEQ(NSMakeRange(3, 4), NSMakeRange(3, 4));
  59. EXPECT_NSNE(NSMakeRange(3, 4), NSMakeRange(3, 3));
  60. EXPECT_NSNE(NSMakeRange(3, 4), NSMakeRange(4, 4));
  61. }
  62. TEST(GTestMac, StringDescription) {
  63. using testing::internal::StringDescription;
  64. EXPECT_EQ(StringDescription(@4), "4");
  65. EXPECT_EQ(StringDescription(@"foo"), "foo");
  66. EXPECT_EQ(StringDescription(nil), "(null)");
  67. }
  68. #endif // !GTEST_OS_IOS