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

Side by Side 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: Nils. Fix comments. Add AutoreleasePool. 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 unified diff | Download patch | Annotate | Revision Log
« base/mac/foundation_util.h ('K') | « base/mac/foundation_util.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/mac/foundation_util.h" 5 #include "base/mac/foundation_util.h"
6 6
7 #include "base/mac/scoped_cftyperef.h" 7 #include "base/mac/scoped_cftyperef.h"
8 #include "base/mac/scoped_nsautorelease_pool.h"
8 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
9 10
10 TEST(FoundationUtilTest, CFCast) { 11 TEST(FoundationUtilTest, CFCast) {
11 // Build out the CF types to be tested as empty containers. 12 // Build out the CF types to be tested as empty containers.
12 base::mac::ScopedCFTypeRef<CFTypeRef> test_array( 13 base::mac::ScopedCFTypeRef<CFTypeRef> test_array(
13 CFArrayCreate(NULL, NULL, 0, &kCFTypeArrayCallBacks)); 14 CFArrayCreate(NULL, NULL, 0, &kCFTypeArrayCallBacks));
14 base::mac::ScopedCFTypeRef<CFTypeRef> test_array_mutable( 15 base::mac::ScopedCFTypeRef<CFTypeRef> test_array_mutable(
15 CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks)); 16 CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks));
16 base::mac::ScopedCFTypeRef<CFTypeRef> test_bag( 17 base::mac::ScopedCFTypeRef<CFTypeRef> test_bag(
17 CFBagCreate(NULL, NULL, 0, &kCFTypeBagCallBacks)); 18 CFBagCreate(NULL, NULL, 0, &kCFTypeBagCallBacks));
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 EXPECT_FALSE(base::mac::CFCastStrict<CFBagRef>(NULL)); 153 EXPECT_FALSE(base::mac::CFCastStrict<CFBagRef>(NULL));
153 EXPECT_FALSE(base::mac::CFCastStrict<CFBooleanRef>(NULL)); 154 EXPECT_FALSE(base::mac::CFCastStrict<CFBooleanRef>(NULL));
154 EXPECT_FALSE(base::mac::CFCastStrict<CFDataRef>(NULL)); 155 EXPECT_FALSE(base::mac::CFCastStrict<CFDataRef>(NULL));
155 EXPECT_FALSE(base::mac::CFCastStrict<CFDateRef>(NULL)); 156 EXPECT_FALSE(base::mac::CFCastStrict<CFDateRef>(NULL));
156 EXPECT_FALSE(base::mac::CFCastStrict<CFDictionaryRef>(NULL)); 157 EXPECT_FALSE(base::mac::CFCastStrict<CFDictionaryRef>(NULL));
157 EXPECT_FALSE(base::mac::CFCastStrict<CFNullRef>(NULL)); 158 EXPECT_FALSE(base::mac::CFCastStrict<CFNullRef>(NULL));
158 EXPECT_FALSE(base::mac::CFCastStrict<CFNumberRef>(NULL)); 159 EXPECT_FALSE(base::mac::CFCastStrict<CFNumberRef>(NULL));
159 EXPECT_FALSE(base::mac::CFCastStrict<CFSetRef>(NULL)); 160 EXPECT_FALSE(base::mac::CFCastStrict<CFSetRef>(NULL));
160 EXPECT_FALSE(base::mac::CFCastStrict<CFStringRef>(NULL)); 161 EXPECT_FALSE(base::mac::CFCastStrict<CFStringRef>(NULL));
161 } 162 }
163
164 TEST(FoundationUtilTest, ObjCCast) {
165 base::mac::ScopedNSAutoreleasePool pool;
166
167 id test_array = [NSArray arrayWithObjects:
Mark Mentovai 2011/10/20 14:14:16 [NSArray array], [NSMutableArray array], [NSDictio
KushalP 2011/10/20 14:56:33 I decided to choose slightly more interesting test
168 @"whoomp", @"there", @"it", @"is", nil];
169 id test_array_mutable = [NSMutableArray arrayWithObjects:
170 @"a man", @"a plan",
171 @"a canal", @"panama",
172 nil];
173 id test_data = [NSData data];
Mark Mentovai 2011/10/20 14:14:16 Why no test_data_mutable? You had a mutable versio
KushalP 2011/10/20 14:56:33 Added On 2011/10/20 14:14:16, Mark Mentovai wrote
174 id test_date = [NSDate date];
175 id test_dict =
176 [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:42]
177 forKey:@"meaning"];
178 id test_dict_mutable = [NSMutableDictionary dictionaryWithCapacity:10];
179 id test_number = [NSNumber numberWithInt:42];
180 id test_null = [NSNull null];
181 id test_set = [NSSet setWithObject:@"string object"];
182 id test_set_mutable = [NSMutableSet setWithCapacity:10];
183 id test_str = [NSString string];
184 id test_str_const = @"bonjour";
185 id test_str_mutable = [NSMutableString stringWithCapacity:10];
186
187 // Make sure the allocations of NS types are good.
188 EXPECT_TRUE(test_array);
189 EXPECT_TRUE(test_array_mutable);
190 EXPECT_TRUE(test_data);
191 EXPECT_TRUE(test_date);
192 EXPECT_TRUE(test_dict);
193 EXPECT_TRUE(test_dict_mutable);
194 EXPECT_TRUE(test_number);
195 EXPECT_TRUE(test_null);
196 EXPECT_TRUE(test_set);
197 EXPECT_TRUE(test_set_mutable);
198 EXPECT_TRUE(test_str);
199 EXPECT_TRUE(test_str_const);
200 EXPECT_TRUE(test_str_mutable);
201
202 // Casting the id correctly provides the same pointer.
203 EXPECT_EQ(test_array, base::mac::ObjCCast<NSArray>(test_array));
204 EXPECT_EQ(test_array_mutable,
205 base::mac::ObjCCast<NSArray>(test_array_mutable));
206 EXPECT_EQ(test_data, base::mac::ObjCCast<NSData>(test_data));
207 EXPECT_EQ(test_date, base::mac::ObjCCast<NSDate>(test_date));
208 EXPECT_EQ(test_dict, base::mac::ObjCCast<NSDictionary>(test_dict));
209 EXPECT_EQ(test_dict_mutable,
210 base::mac::ObjCCast<NSDictionary>(test_dict_mutable));
211 EXPECT_EQ(test_number, base::mac::ObjCCast<NSNumber>(test_number));
212 EXPECT_EQ(test_null, base::mac::ObjCCast<NSNull>(test_null));
213 EXPECT_EQ(test_set, base::mac::ObjCCast<NSSet>(test_set));
214 EXPECT_EQ(test_set_mutable, base::mac::ObjCCast<NSSet>(test_set_mutable));
215 EXPECT_EQ(test_str, base::mac::ObjCCast<NSString>(test_str));
216 EXPECT_EQ(test_str_const, base::mac::ObjCCast<NSString>(test_str_const));
217 EXPECT_EQ(test_str_mutable,
218 base::mac::ObjCCast<NSString>(test_str_mutable));
219
220 // When given an incorrect ObjC cast, provide nil.
221 EXPECT_FALSE(base::mac::ObjCCast<NSString>(test_array));
222 EXPECT_FALSE(base::mac::ObjCCast<NSString>(test_array_mutable));
223 EXPECT_FALSE(base::mac::ObjCCast<NSString>(test_data));
224 EXPECT_FALSE(base::mac::ObjCCast<NSSet>(test_date));
225 EXPECT_FALSE(base::mac::ObjCCast<NSSet>(test_dict));
226 EXPECT_FALSE(base::mac::ObjCCast<NSNumber>(test_dict_mutable));
227 EXPECT_FALSE(base::mac::ObjCCast<NSNull>(test_number));
228 EXPECT_FALSE(base::mac::ObjCCast<NSDictionary>(test_null));
229 EXPECT_FALSE(base::mac::ObjCCast<NSDictionary>(test_set));
230 EXPECT_FALSE(base::mac::ObjCCast<NSDate>(test_set_mutable));
231 EXPECT_FALSE(base::mac::ObjCCast<NSData>(test_str));
232 EXPECT_FALSE(base::mac::ObjCCast<NSData>(test_str_const));
233 EXPECT_FALSE(base::mac::ObjCCast<NSArray>(test_str_mutable));
234
235 // Giving a nil provides a nil.
236 EXPECT_FALSE(base::mac::ObjCCast<NSArray>(nil));
237 EXPECT_FALSE(base::mac::ObjCCast<NSData>(nil));
238 EXPECT_FALSE(base::mac::ObjCCast<NSDate>(nil));
239 EXPECT_FALSE(base::mac::ObjCCast<NSDictionary>(nil));
240 EXPECT_FALSE(base::mac::ObjCCast<NSNull>(nil));
241 EXPECT_FALSE(base::mac::ObjCCast<NSNumber>(nil));
242 EXPECT_FALSE(base::mac::ObjCCast<NSSet>(nil));
243 EXPECT_FALSE(base::mac::ObjCCast<NSString>(nil));
244
245 // ObjCCastStrict: correct cast results in correct pointer being returned.
246 EXPECT_EQ(test_array, base::mac::ObjCCastStrict<NSArray>(test_array));
247 EXPECT_EQ(test_array_mutable,
248 base::mac::ObjCCastStrict<NSArray>(test_array_mutable));
249 EXPECT_EQ(test_data, base::mac::ObjCCastStrict<NSData>(test_data));
250 EXPECT_EQ(test_date, base::mac::ObjCCastStrict<NSDate>(test_date));
251 EXPECT_EQ(test_dict, base::mac::ObjCCastStrict<NSDictionary>(test_dict));
252 EXPECT_EQ(test_dict_mutable,
253 base::mac::ObjCCastStrict<NSDictionary>(test_dict_mutable));
254 EXPECT_EQ(test_number, base::mac::ObjCCastStrict<NSNumber>(test_number));
255 EXPECT_EQ(test_null, base::mac::ObjCCastStrict<NSNull>(test_null));
256 EXPECT_EQ(test_set, base::mac::ObjCCastStrict<NSSet>(test_set));
257 EXPECT_EQ(test_set_mutable,
258 base::mac::ObjCCastStrict<NSSet>(test_set_mutable));
259 EXPECT_EQ(test_str, base::mac::ObjCCastStrict<NSString>(test_str));
260 EXPECT_EQ(test_str_const,
261 base::mac::ObjCCastStrict<NSString>(test_str_const));
262 EXPECT_EQ(test_str_mutable,
263 base::mac::ObjCCastStrict<NSString>(test_str_mutable));
264
265 // ObjCCastStrict: Giving a nil provides a nil.
266 EXPECT_FALSE(base::mac::ObjCCastStrict<NSArray>(nil));
267 EXPECT_FALSE(base::mac::ObjCCastStrict<NSData>(nil));
268 EXPECT_FALSE(base::mac::ObjCCastStrict<NSDate>(nil));
269 EXPECT_FALSE(base::mac::ObjCCastStrict<NSDictionary>(nil));
270 EXPECT_FALSE(base::mac::ObjCCastStrict<NSNull>(nil));
271 EXPECT_FALSE(base::mac::ObjCCastStrict<NSNumber>(nil));
272 EXPECT_FALSE(base::mac::ObjCCastStrict<NSSet>(nil));
273 EXPECT_FALSE(base::mac::ObjCCastStrict<NSString>(nil));
274 }
OLDNEW
« 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