PageRenderTime 76ms CodeModel.GetById 15ms app.highlight 54ms RepoModel.GetById 1ms app.codeStats 1ms

/core/externals/google-toolbox-for-mac/Foundation/GTMValidatingContainersTest.m

http://macfuse.googlecode.com/
Objective C | 378 lines | 286 code | 69 blank | 23 comment | 16 complexity | ed01af6574a5a5642d8cfefa7a9848df MD5 | raw file
  1//
  2//  GTMValidatingContainersTest.m
  3//
  4//  Copyright 2008 Google Inc.
  5//
  6//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7//  use this file except in compliance with the License.  You may obtain a copy
  8//  of the License at
  9//
 10//  http://www.apache.org/licenses/LICENSE-2.0
 11//
 12//  Unless required by applicable law or agreed to in writing, software
 13//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 14//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 15//  License for the specific language governing permissions and limitations under
 16//  the License.
 17//
 18
 19#import "GTMValidatingContainers.h"
 20#import "GTMSenTestCase.h"
 21#import "GTMUnitTestDevLog.h"
 22
 23#pragma mark Test Support Declarations
 24@protocol GTMVCTestProtocol
 25@end
 26
 27@interface GTMVCTestClass : NSObject
 28+ (id)instance;
 29@end
 30
 31@interface GTMVCTestSubClass : GTMVCTestClass <GTMVCTestProtocol>
 32- (void)foo;
 33@end
 34
 35@interface GTMVCValidatingTests : GTMTestCase {
 36  GTMVCTestClass *testClass_;
 37  GTMVCTestSubClass *testSubClass_;
 38}
 39@end
 40
 41@interface GTMVCValidatorTests : GTMVCValidatingTests
 42@end
 43
 44@interface GTMVCContainerTests : GTMVCValidatingTests {
 45  GTMConformsToProtocolValidator *validator_;
 46  SEL selector_;
 47}
 48@end
 49
 50@interface GTMVCArrayTests : GTMVCContainerTests
 51@end
 52
 53@interface GTMVCDictionaryTests : GTMVCContainerTests
 54@end
 55
 56@interface GTMVCSetTests : GTMVCContainerTests
 57@end
 58
 59@interface GTMValidateContainerTests : GTMTestCase
 60@end
 61
 62#pragma mark -
 63#pragma mark Test Support Definitions
 64
 65@implementation GTMVCTestClass
 66+ (id)instance {
 67  return [[[self alloc] init] autorelease];
 68}
 69
 70- (NSString*)description {
 71  return NSStringFromClass([self class]);
 72}
 73@end
 74
 75@implementation GTMVCTestSubClass
 76- (void)foo {
 77}
 78@end
 79
 80@implementation GTMVCContainerTests
 81- (void)setUp {
 82  [super setUp];
 83  Protocol *prot = @protocol(GTMVCTestProtocol);
 84  validator_ = [[GTMConformsToProtocolValidator alloc] initWithProtocol:prot];
 85  selector_ = @selector(validateObject:forContainer:);
 86}
 87
 88- (void)tearDown {
 89  [validator_ release];
 90  [super tearDown];
 91}
 92@end
 93
 94@implementation GTMVCValidatingTests
 95
 96- (void)setUp {
 97  [super setUp];
 98  testClass_ = [[GTMVCTestClass alloc] init];
 99  testSubClass_ = [[GTMVCTestSubClass alloc] init];
100}
101
102- (void)tearDown {
103  [testClass_ release];
104  [testSubClass_ release];
105  [super tearDown];
106}
107
108@end
109
110@implementation GTMVCValidatorTests
111
112- (void)testKindOfClassValidator {
113#if GTM_CONTAINERS_VALIDATE && GTM_CONTAINERS_VALIDATION_FAILED_LOG && !GTM_CONTAINERS_VALIDATION_FAILED_ASSERT
114  [GTMUnitTestDevLog expectString:@"nil class"];
115  GTMKindOfClassValidator *validator;
116  validator = [GTMKindOfClassValidator validateAgainstClass:nil];
117  STAssertNil(validator, @"should be nil");
118
119  Class cls = [GTMVCTestClass class];
120  validator = [GTMKindOfClassValidator validateAgainstClass:cls];
121  STAssertNotNil(validator, @"should be valid");
122
123  BOOL isGood = [validator validateObject:testClass_ forContainer:nil];
124  STAssertTrue(isGood, @"should be validated");
125
126  isGood = [validator validateObject:testSubClass_ forContainer:nil];
127  STAssertTrue(isGood, @"should be validated");
128
129  isGood = [validator validateObject:[NSNumber numberWithInt:0]
130                        forContainer:nil];
131  STAssertFalse(isGood, @"should fail");
132#else  // GTM_CONTAINERS_VALIDATE && GTM_CONTAINERS_VALIDATION_FAILED_LOG && !GTM_CONTAINERS_VALIDATION_FAILED_ASSERT
133  GTMKindOfClassValidator *validator;
134  validator = [GTMKindOfClassValidator validateAgainstClass:nil];
135  STAssertNil(validator, @"should be nil");
136
137  Class cls = [GTMVCTestClass class];
138  validator = [GTMKindOfClassValidator validateAgainstClass:cls];
139  STAssertNil(validator, @"should be nil");
140#endif  // GTM_CONTAINERS_VALIDATE && GTM_CONTAINERS_VALIDATION_FAILED_LOG && !GTM_CONTAINERS_VALIDATION_FAILED_ASSERT
141}
142
143- (void)testMemberOfClassValidator {
144#if GTM_CONTAINERS_VALIDATE && GTM_CONTAINERS_VALIDATION_FAILED_LOG && !GTM_CONTAINERS_VALIDATION_FAILED_ASSERT
145  [GTMUnitTestDevLog expectString:@"nil class"];
146  GTMMemberOfClassValidator *validator;
147  validator = [GTMMemberOfClassValidator validateAgainstClass:nil];
148  STAssertNil(validator, @"should be nil");
149
150  Class cls = [GTMVCTestClass class];
151  validator = [GTMMemberOfClassValidator validateAgainstClass:cls];
152  STAssertNotNil(validator, @"should be valid");
153
154  BOOL isGood = [validator validateObject:testClass_ forContainer:nil];
155  STAssertTrue(isGood, @"should be validated");
156
157  isGood = [validator validateObject:testSubClass_ forContainer:nil];
158  STAssertFalse(isGood, @"should fail");
159
160  isGood = [validator validateObject:nil forContainer:nil];
161  STAssertFalse(isGood, @"should fail");
162
163  isGood = [validator validateObject:[NSNumber numberWithInt:0]
164                        forContainer:nil];
165  STAssertFalse(isGood, @"should fail");
166#else  // GTM_CONTAINERS_VALIDATE && GTM_CONTAINERS_VALIDATION_FAILED_LOG && !GTM_CONTAINERS_VALIDATION_FAILED_ASSERT
167  GTMMemberOfClassValidator *validator;
168  validator = [GTMMemberOfClassValidator validateAgainstClass:nil];
169  STAssertNil(validator, @"should be nil");
170
171  Class cls = [GTMVCTestClass class];
172  validator = [GTMMemberOfClassValidator validateAgainstClass:cls];
173  STAssertNil(validator, @"should be nil");
174#endif  // GTM_CONTAINERS_VALIDATE && GTM_CONTAINERS_VALIDATION_FAILED_LOG && !GTM_CONTAINERS_VALIDATION_FAILED_ASSERT
175}
176
177- (void)testConformsToProtocolValidator {
178#if GTM_CONTAINERS_VALIDATE && GTM_CONTAINERS_VALIDATION_FAILED_LOG && !GTM_CONTAINERS_VALIDATION_FAILED_ASSERT
179  [GTMUnitTestDevLog expectString:@"nil protocol"];
180  GTMConformsToProtocolValidator *validator;
181  validator = [GTMConformsToProtocolValidator validateAgainstProtocol:nil];
182  STAssertNil(validator, @"should be nil");
183
184  Protocol *prot = @protocol(GTMVCTestProtocol);
185  validator = [GTMConformsToProtocolValidator validateAgainstProtocol:prot];
186  STAssertNotNil(validator, @"should be valid");
187
188  BOOL isGood = [validator validateObject:testClass_ forContainer:nil];
189  STAssertFalse(isGood, @"should fail");
190
191  isGood = [validator validateObject:testSubClass_ forContainer:nil];
192  STAssertTrue(isGood, @"should succeed");
193
194  isGood = [validator validateObject:nil forContainer:nil];
195  STAssertFalse(isGood, @"should fail");
196#else  // GTM_CONTAINERS_VALIDATE && GTM_CONTAINERS_VALIDATION_FAILED_LOG && !GTM_CONTAINERS_VALIDATION_FAILED_ASSERT
197  GTMConformsToProtocolValidator *validator;
198  validator = [GTMConformsToProtocolValidator validateAgainstProtocol:nil];
199  STAssertNil(validator, @"should be nil");
200
201  Protocol *prot = @protocol(GTMVCTestProtocol);
202  validator = [GTMConformsToProtocolValidator validateAgainstProtocol:prot];
203  STAssertNil(validator, @"should be nil");
204#endif  // GTM_CONTAINERS_VALIDATE && GTM_CONTAINERS_VALIDATION_FAILED_LOG && !GTM_CONTAINERS_VALIDATION_FAILED_ASSERT
205}
206
207- (void)testRespondsToSelectorValidator {
208#if GTM_CONTAINERS_VALIDATE && GTM_CONTAINERS_VALIDATION_FAILED_LOG && !GTM_CONTAINERS_VALIDATION_FAILED_ASSERT
209  [GTMUnitTestDevLog expectString:@"nil selector"];
210  GTMRespondsToSelectorValidator *validator;
211  validator = [GTMRespondsToSelectorValidator validateAgainstSelector:nil];
212  STAssertNil(validator, @"should be nil");
213
214  SEL sel = @selector(foo);
215  validator = [GTMRespondsToSelectorValidator validateAgainstSelector:sel];
216  STAssertNotNil(validator, @"should be valid");
217
218  BOOL isGood = [validator validateObject:testClass_ forContainer:nil];
219  STAssertFalse(isGood, @"should fail");
220
221  isGood = [validator validateObject:testSubClass_ forContainer:nil];
222  STAssertTrue(isGood, @"should succeed");
223
224  isGood = [validator validateObject:nil forContainer:nil];
225  STAssertFalse(isGood, @"should fail");
226#else  // GTM_CONTAINERS_VALIDATE && GTM_CONTAINERS_VALIDATION_FAILED_LOG && !GTM_CONTAINERS_VALIDATION_FAILED_ASSERT
227  GTMRespondsToSelectorValidator *validator;
228  validator = [GTMRespondsToSelectorValidator validateAgainstSelector:nil];
229  STAssertNil(validator, @"should be nil");
230
231  SEL sel = @selector(foo);
232  validator = [GTMRespondsToSelectorValidator validateAgainstSelector:sel];
233  STAssertNil(validator, @"should be nil");
234#endif  // GTM_CONTAINERS_VALIDATE && GTM_CONTAINERS_VALIDATION_FAILED_LOG && !GTM_CONTAINERS_VALIDATION_FAILED_ASSERT
235}
236
237
238@end
239
240@implementation GTMVCArrayTests
241- (void)testContainer {
242  GTMValidatingArray *array;
243  array = [GTMValidatingArray validatingArrayWithTarget:validator_
244                                               selector:selector_];
245  STAssertNotNil(array, @"should be valid");
246
247  array = [[[GTMValidatingArray alloc] initValidatingWithTarget:validator_
248                                                       selector:selector_] autorelease];
249  STAssertNotNil(array, @"should be valid");
250
251  [GTMUnitTestDevLog expectPattern:@"GTMVCTestClass failed container verification for GTMValidatingArray .*"];
252  [array addObject:testSubClass_];
253  [array addObject:testClass_];
254  STAssertEquals([array objectAtIndex:0], testSubClass_, @"");
255
256  [GTMUnitTestDevLog expectPattern:@"GTMVCTestClass failed container verification for GTMValidatingArray .*"];
257  [array insertObject:testClass_ atIndex:0];
258  [array insertObject:testSubClass_ atIndex:0];
259  [GTMUnitTestDevLog expectPattern:@"GTMVCTestClass failed container verification for GTMValidatingArray .*"];
260  [array replaceObjectAtIndex:0 withObject:testClass_];
261  [array replaceObjectAtIndex:0 withObject:testSubClass_];
262  [array removeLastObject];
263  [array removeObjectAtIndex:0];
264  NSUInteger expectedCount = 0U;
265#if !(GTM_CONTAINERS_VALIDATE && GTM_CONTAINERS_VALIDATION_FAILED_LOG && !GTM_CONTAINERS_VALIDATION_FAILED_ASSERT)
266  // If we're not validating, we don't expect any logs
267  [GTMUnitTestDevLog resetExpectedLogs];
268  expectedCount = 2U;
269#endif  // !(GTM_CONTAINERS_VALIDATE && GTM_CONTAINERS_VALIDATION_FAILED_LOG && !GTM_CONTAINERS_VALIDATION_FAILED_ASSERT)
270  STAssertEquals([array count], expectedCount, @"should have no objects left");
271
272}
273@end
274
275@implementation GTMVCDictionaryTests
276- (void)testContainer {
277  GTMValidatingDictionary *dictionary;
278  dictionary = [GTMValidatingDictionary validatingDictionaryWithTarget:validator_
279                                                              selector:selector_];
280  STAssertNotNil(dictionary, @"should be valid");
281
282  dictionary = [[[GTMValidatingDictionary alloc] initValidatingWithTarget:validator_
283                                                                 selector:selector_] autorelease];
284  STAssertNotNil(dictionary, @"should be valid");
285
286  [GTMUnitTestDevLog expectPattern:@"GTMVCTestClass failed container verification for GTMValidatingDictionary .*"];
287  [dictionary setObject:testClass_ forKey:@"Key1"];
288  [dictionary setObject:testSubClass_ forKey:@"Key2"];
289  STAssertEquals([dictionary objectForKey:@"Key2"], testSubClass_, @"");
290  STAssertNotNil([dictionary keyEnumerator], @"");
291
292  [dictionary removeObjectForKey:@"Key2"];
293  [dictionary removeObjectForKey:@"Key1"];
294  STAssertEquals([dictionary count], (NSUInteger)0, @"should have no objects left");
295
296  // So we get full code coverage
297  [testSubClass_ foo];
298#if !(GTM_CONTAINERS_VALIDATE && GTM_CONTAINERS_VALIDATION_FAILED_LOG && !GTM_CONTAINERS_VALIDATION_FAILED_ASSERT)
299  // If we're not validating, we don't expect any logs
300  [GTMUnitTestDevLog resetExpectedLogs];
301#endif  // !(GTM_CONTAINERS_VALIDATE && GTM_CONTAINERS_VALIDATION_FAILED_LOG && !GTM_CONTAINERS_VALIDATION_FAILED_ASSERT)
302}
303@end
304
305@implementation GTMVCSetTests
306- (void)testContainer {
307  GTMValidatingSet *set;
308  set = [GTMValidatingSet validatingSetWithTarget:validator_
309                                         selector:selector_];
310  STAssertNotNil(set, @"should be valid");
311
312  set = [[[GTMValidatingSet alloc] initValidatingWithTarget:validator_
313                                                   selector:selector_] autorelease];
314  STAssertNotNil(set, @"should be valid");
315
316  [GTMUnitTestDevLog expectPattern:@"GTMVCTestClass failed container verification for GTMValidatingSet .*"];
317  [set addObject:testClass_];
318  [set addObject:testSubClass_];
319  STAssertEqualObjects([set member:testSubClass_], testSubClass_, @"");
320  STAssertNotNil([set objectEnumerator], @"");
321
322  [set removeObject:testClass_];
323  [set removeObject:testSubClass_];
324#if !(GTM_CONTAINERS_VALIDATE && GTM_CONTAINERS_VALIDATION_FAILED_LOG && !GTM_CONTAINERS_VALIDATION_FAILED_ASSERT)
325  // If we're not validating, we don't expect any logs
326  [GTMUnitTestDevLog resetExpectedLogs];
327#endif  // !(GTM_CONTAINERS_VALIDATE && GTM_CONTAINERS_VALIDATION_FAILED_LOG && !GTM_CONTAINERS_VALIDATION_FAILED_ASSERT)
328  STAssertEquals([set count], (NSUInteger)0, @"should have no objects left");
329}
330@end
331
332@implementation GTMValidateContainerTests
333- (void)testValidatingContainers {
334  NSDictionary *homogenousDict = [NSDictionary dictionaryWithObjectsAndKeys:
335                                  [GTMVCTestSubClass instance], @"key1",
336                                  [GTMVCTestSubClass instance], @"key2",
337                                  nil];
338  NSDictionary *heterogenousDict = [NSDictionary dictionaryWithObjectsAndKeys:
339                                    [GTMVCTestClass instance], @"key1",
340                                    [GTMVCTestSubClass instance], @"key2",
341                                    nil];
342
343  // Test bad container
344  [GTMUnitTestDevLog expectPattern:@"container does not respont to -objectEnumerator: .*"];
345  _GTMValidateContainerContainsKindOfClass([NSString string],
346                                           [GTMVCTestSubClass class]);
347
348  _GTMValidateContainerContainsKindOfClass(homogenousDict,
349                                           [GTMVCTestSubClass class]);
350  _GTMValidateContainerContainsKindOfClass(heterogenousDict,
351                                           [GTMVCTestClass class]);
352  [GTMUnitTestDevLog expectPattern:@"GTMVCTestClass failed container verification for .*"];
353  _GTMValidateContainerContainsKindOfClass(heterogenousDict,
354                                           [GTMVCTestSubClass class]);
355
356  _GTMValidateContainerContainsMemberOfClass(homogenousDict,
357                                             [GTMVCTestSubClass class]);
358  [GTMUnitTestDevLog expectPattern:@"GTMVCTestSubClass failed container verification for .*"];
359  _GTMValidateContainerContainsMemberOfClass(heterogenousDict,
360                                             [GTMVCTestClass class]);
361
362  _GTMValidateContainerConformsToProtocol(homogenousDict,
363                                           @protocol(GTMVCTestProtocol));
364  [GTMUnitTestDevLog expectPattern:@"GTMVCTestClass failed container verification for .*"];
365  _GTMValidateContainerConformsToProtocol(heterogenousDict,
366                                             @protocol(GTMVCTestProtocol));
367
368  _GTMValidateContainerItemsRespondToSelector(homogenousDict,
369                                          @selector(foo));
370  [GTMUnitTestDevLog expectPattern:@"GTMVCTestClass failed container verification for .*"];
371  _GTMValidateContainerItemsRespondToSelector(heterogenousDict,
372                                          @selector(foo));
373#if !(GTM_CONTAINERS_VALIDATE && GTM_CONTAINERS_VALIDATION_FAILED_LOG && !GTM_CONTAINERS_VALIDATION_FAILED_ASSERT)
374  // If we're not validating, we don't expect any logs
375  [GTMUnitTestDevLog resetExpectedLogs];
376#endif  // !(GTM_CONTAINERS_VALIDATE && GTM_CONTAINERS_VALIDATION_FAILED_LOG && !GTM_CONTAINERS_VALIDATION_FAILED_ASSERT)
377}
378@end