Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #120 from tibr/master

Fixing a crash when stubbing and spying on an object at the same time.
  • Loading branch information...
commit e3c9aafd1e44b7a5f24ff7c06869f254e823b1b6 2 parents 3c829ef + e5028c0
Allen Ding authored
View
3  Kiwi/KWExample.m
@@ -241,8 +241,7 @@ - (void)visitItNode:(KWItNode *)aNode {
}
// Always clear stubs and spies at the end of it blocks
- KWClearAllMessageSpies();
- KWClearAllObjectStubs();
+ KWClearStubsAndSpies();
}];
}
View
3  Kiwi/KWIntercept.h
@@ -32,6 +32,9 @@ Class KWRealClassForClass(Class aClass);
Class KWSetupObjectInterceptSupport(id anObject);
void KWSetupMethodInterceptSupport(Class interceptClass, SEL aSelector);
+#pragma mark - Managing Stubs & Spies
+void KWClearStubsAndSpies(void);
+
#pragma mark -
#pragma mark Managing Objects Stubs
View
18 Kiwi/KWIntercept.m
@@ -12,6 +12,7 @@
static const char * const KWInterceptClassSuffix = "_KWIntercept";
static NSMutableDictionary *KWObjectStubs = nil;
static NSMutableDictionary *KWMessageSpies = nil;
+static NSMutableArray *KWRestoredObjects = nil;
#pragma mark -
#pragma mark Intercept Enabled Method Implementations
@@ -212,6 +213,15 @@ Class KWInterceptedSuperclass(id anObject, SEL aSelector) {
return originalSuperclass;
}
+#pragma mark - Managing Stubs & Spies
+
+void KWClearStubsAndSpies(void) {
+ KWRestoredObjects = [NSMutableArray array];
+ KWClearAllMessageSpies();
+ KWClearAllObjectStubs();
+ KWRestoredObjects = nil;
+}
+
#pragma mark -
#pragma mark Managing Objects Stubs
@@ -250,7 +260,11 @@ void KWClearObjectStubs(id anObject) {
void KWClearAllObjectStubs(void) {
for (NSValue *objectKey in KWObjectStubs) {
id stubbedObject = [objectKey nonretainedObjectValue];
+ if ([KWRestoredObjects containsObject:stubbedObject]) {
+ continue;
+ }
KWRestoreOriginalClass(stubbedObject);
+ [KWRestoredObjects addObject:stubbedObject];
}
[KWObjectStubs removeAllObjects];
}
@@ -298,7 +312,11 @@ void KWClearObjectSpy(id anObject, id aSpy, KWMessagePattern *aMessagePattern) {
void KWClearAllMessageSpies(void) {
for (NSValue *objectKey in KWMessageSpies) {
id spiedObject = [objectKey nonretainedObjectValue];
+ if ([KWRestoredObjects containsObject:spiedObject]) {
+ continue;
+ }
KWRestoreOriginalClass(spiedObject);
+ [KWRestoredObjects addObject:spiedObject];
}
[KWMessageSpies removeAllObjects];
}
View
3  Kiwi/KWTestCase.m
@@ -67,8 +67,7 @@ - (void)setUpExampleEnvironment {
}
- (void)tearDownExampleEnvironment {
- KWClearAllMessageSpies();
- KWClearAllObjectStubs();
+ KWClearStubsAndSpies();
}
#pragma mark -
View
6 Tests/KWMockTest.m
@@ -19,8 +19,7 @@ @interface KWMockTest : SenTestCase
@implementation KWMockTest
- (void)tearDown {
- KWClearAllMessageSpies();
- KWClearAllObjectStubs();
+ KWClearStubsAndSpies();
}
- (void)testItShouldInitializeForAClassWithANameAsANullObject {
@@ -62,8 +61,7 @@ - (void)testItShouldBeOkToStubOnSingletons {
KWMessagePattern *firstMessagePattern = [KWMessagePattern messagePatternWithSelector:@selector(notifyEarth)];
[[Galaxy sharedGalaxy] addMessageSpy:firstSpy forMessagePattern:firstMessagePattern];
- KWClearAllMessageSpies();
- KWClearAllObjectStubs();
+ KWClearStubsAndSpies();
TestSpy *secondSpy = [TestSpy testSpy];
KWMessagePattern *secondMessagePattern = [KWMessagePattern messagePatternWithSelector:@selector(notifyPlanet:)];
View
3  Tests/KWRealObjectSpyTest.m
@@ -18,8 +18,7 @@ @interface KWRealObjectSpyTest : SenTestCase
@implementation KWRealObjectSpyTest
- (void)tearDown {
- KWClearAllMessageSpies();
- KWClearAllObjectStubs();
+ KWClearStubsAndSpies();
}
- (void)testItShouldNotifySpies {
View
3  Tests/KWRealObjectStubTest.m
@@ -18,8 +18,7 @@ @interface KWRealObjectStubTest : SenTestCase
@implementation KWRealObjectStubTest
- (void)tearDown {
- KWClearAllMessageSpies();
- KWClearAllObjectStubs();
+ KWClearStubsAndSpies();
}
- (void)testItShouldRaiseWhenStubbingNonExistentMethods {
View
3  Tests/KWStubTest.m
@@ -19,8 +19,7 @@ @interface KWStubTest : SenTestCase
@implementation KWStubTest
- (void)tearDown {
- KWClearAllMessageSpies();
- KWClearAllObjectStubs();
+ KWClearStubsAndSpies();
}
- (void)testItShouldProcessMatchedInvocations {
Please sign in to comment.
Something went wrong with that request. Please try again.