Permalink
Browse files

Adding a check to spy_on that prevents proxies of proxies

  • Loading branch information...
loopyzort committed Mar 1, 2012
1 parent 05500d8 commit 32e9970d98fb0737b40f2d0950592690ce11883a
Showing with 19 additions and 1 deletion.
  1. +6 −0 Source/Doubles/CDRSpy.mm
  2. +5 −1 Source/Headers/Doubles/CDRSpy.h
  3. +8 −0 Spec/Doubles/CDRSpySpec.mm
View
@@ -49,6 +49,7 @@ - (NSMethodSignature *)methodSignatureForSelector:(SEL)sel {
- (BOOL)respondsToSelector:(SEL)selector {
__block BOOL respondsToSelector = sel_isEqual(selector, @selector(sent_messages));
+ respondsToSelector = respondsToSelector || sel_isEqual(selector, @selector(is_cedar_spy));
[self asOriginalObject:^{
respondsToSelector = respondsToSelector || [self respondsToSelector:selector];
@@ -61,6 +62,11 @@ - (NSArray *)sent_messages {
return objc_getAssociatedObject(self, @"sent-messages");
}
+-(BOOL)is_cedar_spy
+{
+ return YES;
+}
+
#pragma mark Private interface
- (void)asOriginalObject:(void(^)())block {
Class spyClass = object_getClass(self);
@@ -5,15 +5,19 @@
+ (void)interceptMessagesForInstance:(id)instance;
- (NSArray *)sent_messages;
+- (BOOL)is_cedar_spy;
@end
namespace Cedar { namespace Doubles {
inline void CDR_spy_on(id instance) {
- [CDRSpy interceptMessagesForInstance:instance];
+ if(![instance respondsToSelector:@selector(is_cedar_spy)]) {
+ [CDRSpy interceptMessagesForInstance:instance];
+ }
}
}}
+
#ifndef CEDAR_DOUBLES_COMPATIBILITY_MODE
#define spy_on(x) CDR_spy_on((x))
#endif
@@ -44,6 +44,14 @@
it(@"should return the description of the spied-upon object", ^{
incrementer.description should contain(@"SimpleIncrementer");
});
+
+ it(@"should only create one spy for a given object" , ^{
+ ((CDRSpy *)incrementer).sent_messages should be_empty;
+ spy_on(incrementer);
+ [incrementer increment];
+ ((CDRSpy *)incrementer).sent_messages should_not be_empty;
+ });
+
});
SPEC_END

0 comments on commit 32e9970

Please sign in to comment.