Skip to content
This repository has been archived by the owner on Mar 15, 2022. It is now read-only.

Commit

Permalink
Working
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Crooke committed Feb 26, 2015
1 parent 689b8af commit a1f22c8
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 22 deletions.
6 changes: 6 additions & 0 deletions ObjcAssociatedObjectHelpers.xcodeproj/project.pbxproj
Expand Up @@ -10,9 +10,11 @@
1B75995371FA4BF5AF00B6D6 /* libPods-ObjcAssociatedObjectHelpersTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 510A0A5EEF4A43AEB1DFC61C /* libPods-ObjcAssociatedObjectHelpersTests.a */; };
4918B9B2168498F000D4EAC4 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 49AEF0F61619F565009D9F9B /* Foundation.framework */; };
4918B9C11684991B00D4EAC4 /* NSObject+AssociatedDictionary.m in Sources */ = {isa = PBXBuildFile; fileRef = 499C34B1161AF68000BA2AC9 /* NSObject+AssociatedDictionary.m */; };
4959D45B1A9F5FF300F414FB /* WeakContainer.m in Sources */ = {isa = PBXBuildFile; fileRef = 49629DD21A9F5EE600BA8910 /* WeakContainer.m */; };
495C4D151A59CDDC0039DB95 /* ObjcAssociatedObjectHelpers.m in Sources */ = {isa = PBXBuildFile; fileRef = 499E28471A586A68004174F2 /* ObjcAssociatedObjectHelpers.m */; };
495C4D161A59D0CE0039DB95 /* ObjcAssociatedObjectHelpers.m in Sources */ = {isa = PBXBuildFile; fileRef = 499E28471A586A68004174F2 /* ObjcAssociatedObjectHelpers.m */; };
495C4D171A59D0CF0039DB95 /* ObjcAssociatedObjectHelpers.m in Sources */ = {isa = PBXBuildFile; fileRef = 499E28471A586A68004174F2 /* ObjcAssociatedObjectHelpers.m */; };
49629DD31A9F5EE600BA8910 /* WeakContainer.m in Sources */ = {isa = PBXBuildFile; fileRef = 49629DD21A9F5EE600BA8910 /* WeakContainer.m */; };
499C34C7161AFD7200BA2AC9 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 49AEF0F61619F565009D9F9B /* Foundation.framework */; };
499E28481A586A68004174F2 /* ObjcAssociatedObjectHelpers.m in Sources */ = {isa = PBXBuildFile; fileRef = 499E28471A586A68004174F2 /* ObjcAssociatedObjectHelpers.m */; };
49AD1B5716C282A10061BDC1 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 49AEF0F61619F565009D9F9B /* Foundation.framework */; };
Expand Down Expand Up @@ -41,6 +43,7 @@
15F57830FA80C0B05DFFC53F /* Pods-ObjcAssociatedObjectHelpersLibTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ObjcAssociatedObjectHelpersLibTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-ObjcAssociatedObjectHelpersLibTests/Pods-ObjcAssociatedObjectHelpersLibTests.debug.xcconfig"; sourceTree = "<group>"; };
1CA662592899DE55A0584B20 /* Pods-ObjcAssociatedObjectHelpersLibTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ObjcAssociatedObjectHelpersLibTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-ObjcAssociatedObjectHelpersLibTests/Pods-ObjcAssociatedObjectHelpersLibTests.release.xcconfig"; sourceTree = "<group>"; };
4918B9AE168498F000D4EAC4 /* ObjcAssociatedObjectHelpersLibTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = ObjcAssociatedObjectHelpersLibTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
49629DD21A9F5EE600BA8910 /* WeakContainer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WeakContainer.m; sourceTree = "<group>"; };
499C34B0161AF68000BA2AC9 /* NSObject+AssociatedDictionary.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSObject+AssociatedDictionary.h"; sourceTree = "<group>"; };
499C34B1161AF68000BA2AC9 /* NSObject+AssociatedDictionary.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSObject+AssociatedDictionary.m"; sourceTree = "<group>"; };
499E28471A586A68004174F2 /* ObjcAssociatedObjectHelpers.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ObjcAssociatedObjectHelpers.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -147,6 +150,7 @@
isa = PBXGroup;
children = (
49DEFFE518B615BC00E10224 /* Specs.m */,
49629DD21A9F5EE600BA8910 /* WeakContainer.m */,
49DEFFE818B6172E00E10224 /* TestClass.h */,
49DEFFE918B6172E00E10224 /* TestClass.m */,
);
Expand Down Expand Up @@ -404,6 +408,7 @@
files = (
495C4D161A59D0CE0039DB95 /* ObjcAssociatedObjectHelpers.m in Sources */,
49BC3AB818B61B7C00679CF8 /* TestClass.m in Sources */,
49629DD31A9F5EE600BA8910 /* WeakContainer.m in Sources */,
49DEFFE618B615BC00E10224 /* Specs.m in Sources */,
4918B9C11684991B00D4EAC4 /* NSObject+AssociatedDictionary.m in Sources */,
);
Expand Down Expand Up @@ -431,6 +436,7 @@
files = (
495C4D171A59D0CF0039DB95 /* ObjcAssociatedObjectHelpers.m in Sources */,
49BC3AB918B61B7D00679CF8 /* TestClass.m in Sources */,
4959D45B1A9F5FF300F414FB /* WeakContainer.m in Sources */,
49DEFFE718B615BC00E10224 /* Specs.m in Sources */,
49BF24A8161B0805000B1590 /* NSObject+AssociatedDictionary.m in Sources */,
);
Expand Down
4 changes: 2 additions & 2 deletions ObjcAssociatedObjectHelpers/ObjcAssociatedObjectHelpers.h
Expand Up @@ -126,7 +126,7 @@ static void* getterName##Key = _OBJC_ASC_QUOTE(getterName); \
#define SYNTHESIZE_ASC_OBJ_WEAK_BLOCK(getterName, setterName, getterBlock, setterBlock) \
static void* getterName##Key = _OBJC_ASC_QUOTE(getterName); \
- (void)setterName:(id)value { \
id wrapped = [__ObjCAscWeakContainer wrapObject:setterBlock(value)]; \
id wrapped = [__ObjCAscWeakContainer wrapObject:value]; \
@synchronized(self) { \
_OBJC_ASC_WRAP_KVO_SETTER(getterName, objc_setAssociatedObject(self, getterName##Key, wrapped, OBJC_ASSOCIATION_RETAIN)); \
} \
Expand All @@ -136,7 +136,7 @@ static void* getterName##Key = _OBJC_ASC_QUOTE(getterName); \
@synchronized(self) { \
wrapped = objc_getAssociatedObject(self, getterName##Key); \
}; \
return getterBlock(wrapped._object); \
return wrapped._object; \
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion ObjcAssociatedObjectHelpers/ObjcAssociatedObjectHelpers.m
Expand Up @@ -28,7 +28,7 @@
@implementation __ObjCAscWeakContainer
+ (instancetype)wrapObject:(id)object {
__ObjCAscWeakContainer *wrapper = [[self alloc] init];
// wrapper._object = object;
wrapper._object = object;
return wrapper;
}
@end
19 changes: 0 additions & 19 deletions UnitTests/Specs.m
Expand Up @@ -265,24 +265,5 @@
});
});

describe(@"weak container", ^{

it(@"should get and set a value as normal", ^{
NSMutableArray *array = [NSMutableArray arrayWithObject:@"foo"];
testObject.weakObject = array.firstObject;
expect(testObject.weakObject).notTo.beNil();
expect(testObject.weakObject == array.firstObject).to.beTruthy();
});

it(@"should nil the weak property", ^{
NSMutableArray *array = [NSMutableArray arrayWithObject:@"foo"];
testObject.weakObject = array.firstObject;
expect(testObject.weakObject).notTo.beNil();
expect(testObject.weakObject == array.firstObject).to.beTruthy();
[array removeAllObjects];
expect(testObject.weakObject).to.beNil();
});
});

SpecEnd

44 changes: 44 additions & 0 deletions UnitTests/WeakContainer.m
@@ -0,0 +1,44 @@
//
// WeakContainer.m
// ObjcAssociatedObjectHelpers
//
// Created by Jonathan Crooke on 26/02/2015.
// Copyright (c) 2015 jbsoft. All rights reserved.
//

#import <XCTest/XCTest.h>
#import "ObjcAssociatedObjectHelpers.h"
#import "TestClass.h"

@interface WeakContainer : XCTestCase
@property (nonatomic, strong) TestClass *testObject;
@property (nonatomic, strong) NSArray *strongContainer;
@end

@implementation WeakContainer

- (void)testGetAndSetAValueAsNormal {
XCTAssertNotNil(self.testObject.weakObject);
XCTAssertEqual(self.testObject.weakObject, self.strongContainer.firstObject);
}

- (void)testNilOfWeakProperty {
XCTestExpectation *expectation = [self expectationWithDescription:@"Weak property becomes nil"];
self.strongContainer = nil;
dispatch_async(dispatch_get_main_queue(), ^{
XCTAssertNil(self.testObject.weakObject);
[expectation fulfill];
});
[self waitForExpectationsWithTimeout:1 handler:nil];
}

#pragma mark -

- (void)setUp {
[super setUp];
self.testObject = [[TestClass alloc] init];
self.strongContainer = @[[[NSObject alloc] init]];
self.testObject.weakObject = self.strongContainer.firstObject;
}

@end

0 comments on commit a1f22c8

Please sign in to comment.