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

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