Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(732)

Unified Diff: base/mac/foundation_util_unittest.mm

Issue 8356024: Create ObjCCast<>() and ObjCCastStrict<>() methods (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« base/mac/foundation_util.h ('K') | « base/mac/foundation_util.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/mac/foundation_util_unittest.mm
diff --git a/base/mac/foundation_util_unittest.mm b/base/mac/foundation_util_unittest.mm
index cfd4fc60b3e335c69aab24d25af372108df0fc6f..8f2f899e5e33e9cfec35f7b1f4b4cfa4ccd78591 100644
--- a/base/mac/foundation_util_unittest.mm
+++ b/base/mac/foundation_util_unittest.mm
@@ -159,3 +159,107 @@ TEST(FoundationUtilTest, CFCast) {
EXPECT_FALSE(base::mac::CFCastStrict<CFSetRef>(NULL));
EXPECT_FALSE(base::mac::CFCastStrict<CFStringRef>(NULL));
}
+
+TEST(FoundationUtilTest, ObjCCast) {
+ id test_array = [NSArray arrayWithObjects:
Mark Mentovai 2011/10/19 22:59:32 Let’s get a ScopedNSAutoreleasePool in here to mak
+ @"whoomp", @"there", @"it", @"is", nil];
+ id test_array_mutable = [NSMutableArray arrayWithObjects:
+ @"a man", @"a plan",
+ @"a canal", @"panama",
+ nil];
+ id test_data = [NSData data];
+ id test_date = [NSDate date];
+ id test_dict = [NSDictionary
+ dictionaryWithObject:[NSNumber numberWithInt:42]
Mark Mentovai 2011/10/19 22:59:32 Formatting nit: I find it clearer to do id test_
+ forKey:@"meaning"];
+ id test_dict_mutable = [NSMutableDictionary dictionaryWithCapacity:10];
+ id test_number = [NSNumber numberWithInt:42];
+ id test_null = [NSNull null];
+ id test_set = [NSSet setWithObject:@"string object"];
+ id test_set_mutable = [NSMutableSet setWithCapacity:10];
+ id test_str = @"bonjour";
Mark Mentovai 2011/10/19 22:59:32 As in the CF test: This one is test_str_const. Yo
+ id test_str_mutable = [NSMutableString stringWithCapacity:10];
+
+ // Make sure the allocations of CF types are good.
Mark Mentovai 2011/10/19 22:59:32 Adjust the comments because this test isn’t about
+ EXPECT_TRUE(test_array);
+ EXPECT_TRUE(test_array_mutable);
+ EXPECT_TRUE(test_data);
+ EXPECT_TRUE(test_date);
+ EXPECT_TRUE(test_dict);
+ EXPECT_TRUE(test_dict_mutable);
+ EXPECT_TRUE(test_number);
+ EXPECT_TRUE(test_null);
+ EXPECT_TRUE(test_set);
+ EXPECT_TRUE(test_set_mutable);
+ EXPECT_TRUE(test_str);
+ EXPECT_TRUE(test_str_mutable);
+
+ // Casting the id correctly provides the same pointer.
+ EXPECT_EQ(test_array, base::mac::ObjCCast<NSArray>(test_array));
+ EXPECT_EQ(test_array_mutable,
+ base::mac::ObjCCast<NSArray>(test_array_mutable));
+ EXPECT_EQ(test_data, base::mac::ObjCCast<NSData>(test_data));
+ EXPECT_EQ(test_date, base::mac::ObjCCast<NSDate>(test_date));
+ EXPECT_EQ(test_dict, base::mac::ObjCCast<NSDictionary>(test_dict));
+ EXPECT_EQ(test_dict_mutable,
+ base::mac::ObjCCast<NSDictionary>(test_dict_mutable));
+ EXPECT_EQ(test_number, base::mac::ObjCCast<NSNumber>(test_number));
+ EXPECT_EQ(test_null, base::mac::ObjCCast<NSNull>(test_null));
+ EXPECT_EQ(test_set, base::mac::ObjCCast<NSSet>(test_set));
+ EXPECT_EQ(test_set_mutable, base::mac::ObjCCast<NSSet>(test_set_mutable));
+ EXPECT_EQ(test_str, base::mac::ObjCCast<NSString>(test_str));
+ EXPECT_EQ(test_str_mutable,
+ base::mac::ObjCCast<NSString>(test_str_mutable));
+
+ // When given an incorrect ObjC cast, provide NULL.
Mark Mentovai 2011/10/19 22:59:32 nil.
+ EXPECT_FALSE(base::mac::ObjCCast<NSString>(test_array));
+ EXPECT_FALSE(base::mac::ObjCCast<NSString>(test_array_mutable));
+ EXPECT_FALSE(base::mac::ObjCCast<NSString>(test_data));
+ EXPECT_FALSE(base::mac::ObjCCast<NSSet>(test_date));
+ EXPECT_FALSE(base::mac::ObjCCast<NSSet>(test_dict));
+ EXPECT_FALSE(base::mac::ObjCCast<NSNumber>(test_dict_mutable));
+ EXPECT_FALSE(base::mac::ObjCCast<NSNull>(test_number));
+ EXPECT_FALSE(base::mac::ObjCCast<NSDictionary>(test_null));
+ EXPECT_FALSE(base::mac::ObjCCast<NSDictionary>(test_set));
+ EXPECT_FALSE(base::mac::ObjCCast<NSDate>(test_set_mutable));
+ EXPECT_FALSE(base::mac::ObjCCast<NSData>(test_str));
+ EXPECT_FALSE(base::mac::ObjCCast<NSArray>(test_str_mutable));
+
+ // Giving a NULL provides a NULL.
Mark Mentovai 2011/10/19 22:59:32 nil, and nil as the argument to each of these cast
+ EXPECT_FALSE(base::mac::ObjCCast<NSArray>(NULL));
+ EXPECT_FALSE(base::mac::ObjCCast<NSData>(NULL));
+ EXPECT_FALSE(base::mac::ObjCCast<NSDate>(NULL));
+ EXPECT_FALSE(base::mac::ObjCCast<NSDictionary>(NULL));
+ EXPECT_FALSE(base::mac::ObjCCast<NSNull>(NULL));
+ EXPECT_FALSE(base::mac::ObjCCast<NSNumber>(NULL));
+ EXPECT_FALSE(base::mac::ObjCCast<NSSet>(NULL));
+ EXPECT_FALSE(base::mac::ObjCCast<NSString>(NULL));
+
+ // ObjCCastStrict: correct cast results in correct pointer being returned.
+ EXPECT_EQ(test_array, base::mac::ObjCCastStrict<NSArray>(test_array));
+ EXPECT_EQ(test_array_mutable,
+ base::mac::ObjCCastStrict<NSArray>(test_array_mutable));
+ EXPECT_EQ(test_data, base::mac::ObjCCastStrict<NSData>(test_data));
+ EXPECT_EQ(test_date, base::mac::ObjCCastStrict<NSDate>(test_date));
+ EXPECT_EQ(test_dict, base::mac::ObjCCastStrict<NSDictionary>(test_dict));
+ EXPECT_EQ(test_dict_mutable,
+ base::mac::ObjCCastStrict<NSDictionary>(test_dict_mutable));
+ EXPECT_EQ(test_number, base::mac::ObjCCastStrict<NSNumber>(test_number));
+ EXPECT_EQ(test_null, base::mac::ObjCCastStrict<NSNull>(test_null));
+ EXPECT_EQ(test_set, base::mac::ObjCCastStrict<NSSet>(test_set));
+ EXPECT_EQ(test_set_mutable,
+ base::mac::ObjCCastStrict<NSSet>(test_set_mutable));
+ EXPECT_EQ(test_str, base::mac::ObjCCastStrict<NSString>(test_str));
+ EXPECT_EQ(test_str_mutable,
+ base::mac::ObjCCastStrict<NSString>(test_str_mutable));
+
+ // ObjCCastStrict: Giving a NULL provides a NULL.
Mark Mentovai 2011/10/19 22:59:32 And again here.
+ EXPECT_FALSE(base::mac::ObjCCastStrict<NSArray>(NULL));
+ EXPECT_FALSE(base::mac::ObjCCastStrict<NSData>(NULL));
+ EXPECT_FALSE(base::mac::ObjCCastStrict<NSDate>(NULL));
+ EXPECT_FALSE(base::mac::ObjCCastStrict<NSDictionary>(NULL));
+ EXPECT_FALSE(base::mac::ObjCCastStrict<NSNull>(NULL));
+ EXPECT_FALSE(base::mac::ObjCCastStrict<NSNumber>(NULL));
+ EXPECT_FALSE(base::mac::ObjCCastStrict<NSSet>(NULL));
+ EXPECT_FALSE(base::mac::ObjCCastStrict<NSString>(NULL));
+}
« base/mac/foundation_util.h ('K') | « base/mac/foundation_util.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698