Last active
August 29, 2015 14:23
-
-
Save bgerstle/65b375f6c7780c924ec8 to your computer and use it in GitHub Desktop.
Retain cycle unit tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface WMFRetainCycleTests : XCTestCase | |
@property (strong, nonatomic) void(^block)(id); | |
@end | |
@implementation WMFRetainCycleTests | |
- (void)testUsingArgument { | |
WMFRetainCycleTests* noCycle = [WMFRetainCycleTests new]; | |
__weak WMFRetainCycleTests* weakRef = noCycle; | |
noCycle.block = ^ (id y) { | |
NSLog([y description]); | |
}; | |
noCycle.block(noCycle); //< call block with itself | |
noCycle = nil; //< nil out strong reference | |
XCTAssertNil(weakRef); //< weak reference is nil! ✅ | |
} | |
- (void)testReferencingCycle { | |
WMFRetainCycleTests* cycle = [WMFRetainCycleTests new]; | |
__weak WMFRetainCycleTests* weakRef = cycle; | |
cycle.block = ^ (id y) { | |
NSLog([cycle description]); //< warning: captuing 'cycle' strongly in this block may create a retain cycle | |
}; | |
cycle.block(cycle); //< try calling with itself for parity w/ previous test | |
cycle = nil; | |
XCTAssertNil(weakRef); //< failure! 🚨 | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment