/core/externals/google-toolbox-for-mac/UnitTesting/GTMNSObject+BindingUnitTesting.h

http://macfuse.googlecode.com/ · C Header · 120 lines · 35 code · 8 blank · 77 comment · 5 complexity · cd9e0e72c2ddd1bfcb3c058a8c094ca0 MD5 · raw file

  1. //
  2. // GTMNSObject+BindingUnitTesting.h
  3. //
  4. // Utilities for doing advanced unittesting with object bindings.
  5. //
  6. // Copyright 2006-2008 Google Inc.
  7. //
  8. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  9. // use this file except in compliance with the License. You may obtain a copy
  10. // of the License at
  11. //
  12. // http://www.apache.org/licenses/LICENSE-2.0
  13. //
  14. // Unless required by applicable law or agreed to in writing, software
  15. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  17. // License for the specific language governing permissions and limitations under
  18. // the License.
  19. //
  20. #include <Foundation/Foundation.h>
  21. // Utility functions for GTMTestExposedBindings Macro. Don't use it directly
  22. // but use the macro below instead
  23. BOOL GTMDoExposedBindingsFunctionCorrectly(NSObject *object,
  24. NSArray **errors);
  25. // Tests the setters and getters for exposed bindings
  26. // For objects that expose bindings, this tests them for you, saving you from
  27. // having to write a whole pile of set/get test code if you add binding support.
  28. // You will need to implement valueClassForBinding: for your bindings,
  29. // and you may possibly want to implement unitTestExposedBindingsToIgnore
  30. // and unitTestExposedBindingsTestValues. See descriptions of those
  31. // methods below for details.
  32. // Implemented as a macro to match the rest of the SenTest macros.
  33. //
  34. // Args:
  35. // a1: The object to be checked.
  36. // description: A format string as in the printf() function.
  37. // Can be nil or an empty string but must be present.
  38. // ...: A variable number of arguments to the format string. Can be absent.
  39. //
  40. #define GTMTestExposedBindings(a1, description, ...) \
  41. do { \
  42. NSObject *a1Object = (a1); \
  43. NSArray *errors = nil; \
  44. BOOL isGood = GTMDoExposedBindingsFunctionCorrectly(a1Object, &errors); \
  45. if (!isGood) { \
  46. NSString *failString; \
  47. GTM_FOREACH_OBJECT(failString, errors) { \
  48. if (description != nil) { \
  49. STFail(@"%@: %@", failString, STComposeString(description, ##__VA_ARGS__)); \
  50. } else { \
  51. STFail(@"%@", failString); \
  52. } \
  53. } \
  54. } \
  55. } while(0)
  56. // Utility class for setting up Binding Tests. Basically a pair of a value to
  57. // set a binding to, followed by the expected return value.
  58. // See description of gtm_unitTestExposedBindingsTestValues: below
  59. // for example of usage.
  60. @interface GTMBindingUnitTestData : NSObject {
  61. @private
  62. id valueToSet_;
  63. id expectedValue_;
  64. }
  65. + (id)testWithIdentityValue:(id)value;
  66. + (id)testWithValue:(id)value expecting:(id)expecting;
  67. - (id)initWithValue:(id)value expecting:(id)expecting;
  68. - (id)valueToSet;
  69. - (id)expectedValue;
  70. @end
  71. @interface NSObject (GTMBindingUnitTestingAdditions)
  72. // Allows you to ignore certain bindings when running GTMTestExposedBindings
  73. // If you have bindings you want to ignore, add them to the array returned
  74. // by this method. The standard way to implement this would be:
  75. // - (NSMutableArray*)unitTestExposedBindingsToIgnore {
  76. // NSMutableArray *array = [super unitTestExposedBindingsToIgnore];
  77. // [array addObject:@"bindingToIgnore1"];
  78. // ...
  79. // return array;
  80. // }
  81. // The NSObject implementation by default will ignore NSFontBoldBinding,
  82. // NSFontFamilyNameBinding, NSFontItalicBinding, NSFontNameBinding and
  83. // NSFontSizeBinding if your exposed bindings contains NSFontBinding because
  84. // the NSFont*Bindings are NOT KVC/KVO compliant.
  85. - (NSMutableArray*)gtm_unitTestExposedBindingsToIgnore;
  86. // Allows you to set up test values for your different bindings.
  87. // if you have certain values you want to test against your bindings, add
  88. // them to the array returned by this method. The array is an array of
  89. // GTMBindingUnitTestData.
  90. // The standard way to implement this would be:
  91. // - (NSMutableArray*)gtm_unitTestExposedBindingsTestValues:(NSString*)binding {
  92. // NSMutableArray *dict = [super unitTestExposedBindingsTestValues:binding];
  93. // if ([binding isEqualToString:@"myBinding"]) {
  94. // MySpecialBindingValueSet *value
  95. // = [[[MySpecialBindingValueSet alloc] init] autorelease];
  96. // [array addObject:[GTMBindingUnitTestData testWithIdentityValue:value]];
  97. // ...
  98. // else if ([binding isEqualToString:@"myBinding2"]) {
  99. // ...
  100. // }
  101. // return array;
  102. // }
  103. // The NSObject implementation handles many of the default bindings, and
  104. // gives you a reasonable set of test values to start.
  105. // See the implementation for the current list of bindings, and values that we
  106. // set for those bindings.
  107. - (NSMutableArray*)gtm_unitTestExposedBindingsTestValues:(NSString*)binding;
  108. // A special version of isEqualTo to test whether two binding values are equal
  109. // by default it calls directly to isEqualTo: but can be overridden for special
  110. // cases (like NSImages) where the standard isEqualTo: isn't sufficient.
  111. - (BOOL)gtm_unitTestIsEqualTo:(id)value;
  112. @end