/Tests/NSObjectAssociatedObjectTest.m

http://github.com/zwaldowski/BlocksKit · Objective C · 56 lines · 38 code · 11 blank · 7 comment · 0 complexity · ce78bb8133c5461550c053a284e97237 MD5 · raw file

  1. //
  2. // NSObjectAssociatedObjectTest.m
  3. // BlocksKit Unit Tests
  4. //
  5. #import "NSObjectAssociatedObjectTest.h"
  6. static char kAssociationKey;
  7. static char kNotFoundKey;
  8. @implementation NSObjectAssociatedObjectTest
  9. - (void)tearDown {
  10. [self removeAllAssociatedObjects];
  11. }
  12. - (void)testAssociatedRetainValue {
  13. NSMutableString *subject = [[NSMutableString alloc] initWithString:@"Hello"];
  14. [self associateValue:subject withKey:&kAssociationKey];
  15. [subject appendString:@" BlocksKit"];
  16. [subject release];
  17. // Value is retained
  18. NSString *associated = [self associatedValueForKey:&kAssociationKey];
  19. GHAssertEqualStrings(associated,@"Hello BlocksKit",@"associated value is %@",associated);
  20. }
  21. - (void)testAssociatedCopyValue {
  22. NSMutableString *subject = [[NSMutableString alloc] initWithString:@"Hello"];
  23. [self associateCopyOfValue:subject withKey:&kAssociationKey];
  24. [subject appendString:@" BlocksKit"];
  25. [subject release];
  26. // Value is copied
  27. NSString *associated = [self associatedValueForKey:&kAssociationKey];
  28. GHAssertEqualStrings(associated,@"Hello",@"associated value is %@",associated);
  29. }
  30. - (void)testAssociatedAssignValue {
  31. NSString *subject = [[NSString alloc] initWithString:@"Hello BlocksKit"];
  32. [self weaklyAssociateValue:subject withKey:&kAssociationKey];
  33. [subject release];
  34. subject = nil;
  35. #if BK_HAS_APPKIT
  36. //zeroing weak reference is not available for iOS
  37. NSString *associated = [self associatedValueForKey:&kAssociationKey];
  38. GHAssertNil(associated,@"associated value is nil");
  39. #endif
  40. }
  41. - (void)testAssociatedNotFound {
  42. NSString *associated = [self associatedValueForKey:&kNotFoundKey];
  43. GHAssertNil(associated,@"associated value is not found for kNotFoundKey");
  44. }
  45. @end