/core/externals/google-toolbox-for-mac/Foundation/GTMLightweightProxyTest.m
Objective C | 111 lines | 60 code | 17 blank | 34 comment | 6 complexity | c63f25aab47477bff29850f1ecbb0dac MD5 | raw file
1// 2// GTMLightweightProxyTest.m 3// 4// Copyright 2006-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 "GTMSenTestCase.h" 20#import "GTMLightweightProxy.h" 21 22@interface GTMLightweightProxy (GTMLightweightProxyTest) 23- (id)init; 24@end 25 26@interface GTMLightweightProxyTest : GTMTestCase 27- (BOOL)returnYes; 28@end 29 30// Declare a non-existent method that we can call without compiler warnings. 31@interface GTMLightweightProxyTest (GTMLightweightProxyTestMadeUpMethodDeclaration) 32- (void)someMadeUpMethod; 33@end 34 35@implementation GTMLightweightProxyTest 36 37- (void)testInit { 38 id proxy = [[[GTMLightweightProxy alloc] 39 initWithRepresentedObject:self] autorelease]; 40 STAssertNotNil(proxy, nil); 41 42 proxy = [[[GTMLightweightProxy alloc] init] autorelease]; 43 STAssertNotNil(proxy, nil); 44} 45 46- (void)testProxy { 47 id proxy 48 = [[[GTMLightweightProxy alloc] initWithRepresentedObject:self] autorelease]; 49 STAssertEqualObjects(self, [proxy representedObject], 50 @"Represented object setup failed"); 51 52 // Check that it identifies itself as a proxy. 53 STAssertTrue([proxy isProxy], @"Should identify as a proxy"); 54 // Check that it passes class requests on 55 STAssertTrue([proxy isMemberOfClass:[self class]], 56 @"Should pass class requests through"); 57 58 // Check that it claims to respond to its selectors. 59 STAssertTrue([proxy respondsToSelector:@selector(initWithRepresentedObject:)], 60 @"Claims not to respond to initWithRepresentedObject:"); 61 STAssertTrue([proxy respondsToSelector:@selector(representedObject)], 62 @"Claims not to respond to representedObject:"); 63 STAssertTrue([proxy respondsToSelector:@selector(setRepresentedObject:)], 64 @"Claims not to respond to setRepresentedObject:"); 65 // Check that it responds to its represented object's selectors 66 STAssertTrue([proxy respondsToSelector:@selector(returnYes)], 67 @"Claims not to respond to returnYes"); 68 // ... but not to made up selectors. 69#if !(__IPHONE_OS_VERSION_MIN_REQUIRED == __IPHONE_3_2 || __IPHONE_OS_VERSION_MIN_REQUIRED == __IPHONE_4_0) 70 // Exceptions thrown by - (void)doesNotRecognizeSelector:(SEL)aSelector 71 // does not get caught on iOS 3.2 and greater. 72 // http://openradar.appspot.com/radar?id=420401 73 STAssertThrows([proxy someMadeUpMethod], 74 @"Calling a bogus method should throw"); 75#endif 76 77 // Check that callthrough works. 78 STAssertTrue([proxy returnYes], 79 @"Calling through to the represented object failed"); 80 81 // Check that nilling out the represented object works. 82 [proxy setRepresentedObject:nil]; 83 STAssertTrue([proxy respondsToSelector:@selector(setRepresentedObject:)], 84 @"Claims not to respond to setRepresentedObject: after nilling" 85 @" out represented object"); 86 STAssertFalse([proxy respondsToSelector:@selector(returnYes)], 87 @"Claims to respond to returnYes after nilling out represented" 88 @" object"); 89 // Calling through once the represented object is nil should fail silently 90 STAssertNoThrow([proxy returnYes], 91 @"Calling through without a represented object should fail" 92 @" silently"); 93 94 // ... even when they are made up. 95#if !(__IPHONE_OS_VERSION_MIN_REQUIRED == __IPHONE_3_2 || __IPHONE_OS_VERSION_MIN_REQUIRED == __IPHONE_4_0) 96 // Exceptions thrown by - (void)doesNotRecognizeSelector:(SEL)aSelector 97 // does not get caught on iOS 3.2 and greater. 98 // http://openradar.appspot.com/radar?id=420401 99 100 STAssertNoThrow([proxy someMadeUpMethod], 101 @"Calling a bogus method on a nilled proxy should not throw"); 102#endif 103 104} 105 106// Simple method to test calling through the proxy. 107- (BOOL)returnYes { 108 return YES; 109} 110 111@end