/core/externals/update-engine/externals/google-toolbox-for-mac/UnitTesting/GTMUnitTestingBindingTest.m

http://macfuse.googlecode.com/ · Objective C · 116 lines · 74 code · 18 blank · 24 comment · 4 complexity · 19a6a6e90f2374cc79b4172fc4178a15 MD5 · raw file

  1. //
  2. // GTMUnitTestingBindingTest.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. #import "GTMSenTestCase.h"
  19. #import "GTMUnitTestingTest.h"
  20. #import "GTMNSObject+BindingUnitTesting.h"
  21. @interface GTMUnitTestingBindingTest : GTMTestCase {
  22. int expectedFailureCount_;
  23. }
  24. @end
  25. @interface GTMUnitTestingBindingBadClass : NSObject
  26. @end
  27. @implementation GTMUnitTestingBindingTest
  28. // Iterates through all of our subviews testing the exposed bindings
  29. - (void)doSubviewBindingTest:(NSView*)view {
  30. NSArray *subviews = [view subviews];
  31. NSView *subview;
  32. GTM_FOREACH_OBJECT(subview, subviews) {
  33. GTMTestExposedBindings(subview, @"testing %@", subview);
  34. [self doSubviewBindingTest:subview];
  35. }
  36. }
  37. - (void)testBindings {
  38. // Get our window to work with and test it's bindings
  39. GTMUnitTestingTestController *testWindowController
  40. = [[GTMUnitTestingTestController alloc] initWithWindowNibName:@"GTMUnitTestingTest"];
  41. NSWindow *window = [testWindowController window];
  42. GTMTestExposedBindings(window, @"Window failed binding test");
  43. [self doSubviewBindingTest:[window contentView]];
  44. [window close];
  45. [testWindowController release];
  46. // Run a test against something with no bindings.
  47. // We're expecting a failure here.
  48. expectedFailureCount_ = 1;
  49. GTMTestExposedBindings(@"foo", @"testing no bindings");
  50. STAssertEquals(expectedFailureCount_, 0, @"Didn't get expected failures testing bindings");
  51. // Run test against some with bad bindings.
  52. // We're expecting failures here.
  53. expectedFailureCount_ = 4;
  54. GTMUnitTestingBindingBadClass *bad = [[[GTMUnitTestingBindingBadClass alloc] init] autorelease];
  55. GTMTestExposedBindings(bad, @"testing bad bindings");
  56. STAssertEquals(expectedFailureCount_, 0, @"Didn't get expected failures testing bad bindings");
  57. }
  58. - (void)failWithException:(NSException *)anException {
  59. if (expectedFailureCount_ > 0) {
  60. expectedFailureCount_ -= 1;
  61. } else {
  62. [super failWithException:anException]; // COV_NF_LINE - not expecting exception
  63. }
  64. }
  65. @end
  66. // Forces several error cases in our binding tests to test them
  67. @implementation GTMUnitTestingBindingBadClass
  68. NSString *const kGTMKeyWithNoClass = @"keyWithNoClass";
  69. NSString *const kGTMKeyWithNoValue = @"keyWithNoValue";
  70. NSString *const kGTMKeyWeCantSet = @"keyWeCantSet";
  71. NSString *const kGTMKeyThatIsntEqual = @"keyThatIsntEqual";
  72. - (NSArray *)exposedBindings {
  73. return [NSArray arrayWithObjects:kGTMKeyWithNoClass,
  74. kGTMKeyWithNoValue,
  75. kGTMKeyWeCantSet,
  76. kGTMKeyThatIsntEqual,
  77. nil];
  78. }
  79. - (NSArray*)gtm_unitTestExposedBindingsTestValues:(NSString*)binding {
  80. GTMBindingUnitTestData *data
  81. = [GTMBindingUnitTestData testWithIdentityValue:kGTMKeyThatIsntEqual];
  82. return [NSArray arrayWithObject:data];
  83. }
  84. - (Class)valueClassForBinding:(NSString*)binding {
  85. return [binding isEqualTo:kGTMKeyWithNoClass] ? nil : [NSString class];
  86. }
  87. - (id)valueForKey:(NSString*)binding {
  88. if ([binding isEqualTo:kGTMKeyWithNoValue]) {
  89. [NSException raise:NSUndefinedKeyException format:@""];
  90. }
  91. return @"foo";
  92. }
  93. - (void)setValue:(id)value forKey:(NSString*)binding {
  94. if ([binding isEqualTo:kGTMKeyWeCantSet]) {
  95. [NSException raise:NSUndefinedKeyException format:@""];
  96. }
  97. }
  98. @end