forked from cedarbdd/cedar
/
CedarDoubleImpl.mm
92 lines (73 loc) · 3.05 KB
/
CedarDoubleImpl.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#import "CedarDoubleImpl.h"
#import "StubbedMethod.h"
@interface CedarDoubleImpl () {
Cedar::Doubles::StubbedMethod::selector_map_t stubbed_methods_;
}
@property (nonatomic, retain, readwrite) NSMutableArray *sent_messages;
@property (nonatomic, assign) NSObject<CedarDouble> *parent_double;
@end
@implementation CedarDoubleImpl
@synthesize sent_messages = sent_messages_, parent_double = parent_double_;
- (id)init {
[super doesNotRecognizeSelector:_cmd];
return nil;
}
- (id)initWithDouble:(NSObject<CedarDouble> *)parent_double {
if (self = [super init]) {
self.sent_messages = [NSMutableArray array];
self.parent_double = parent_double;
}
return self;
}
- (void)dealloc {
self.parent_double = nil;
self.sent_messages = nil;
[super dealloc];
}
- (void)reset_sent_messages {
[self.sent_messages removeAllObjects];
}
- (Cedar::Doubles::StubbedMethod::selector_map_t &)stubbed_methods {
return stubbed_methods_;
}
- (Cedar::Doubles::StubbedMethod &)add_stub:(const Cedar::Doubles::StubbedMethod &)stubbed_method {
const SEL & selector = stubbed_method.selector();
if (![self.parent_double respondsToSelector:selector]) {
[[NSException exceptionWithName:NSInternalInconsistencyException
reason:[NSString stringWithFormat:@"Attempting to stub method <%s>, which double does not respond to", sel_getName(selector)]
userInfo:nil]
raise];
}
Cedar::Doubles::StubbedMethod::selector_map_t::iterator it = stubbed_methods_.find(selector);
if (it != stubbed_methods_.end()) {
[[NSException exceptionWithName:NSInternalInconsistencyException
reason:[NSString stringWithFormat:@"The method <%s> is already stubbed", sel_getName(selector)]
userInfo:nil] raise];
}
stubbed_method.validate_against_instance(self.parent_double);
Cedar::Doubles::StubbedMethod::shared_ptr_t stubbed_method_ptr = Cedar::Doubles::StubbedMethod::shared_ptr_t(new Cedar::Doubles::StubbedMethod(stubbed_method));
stubbed_methods_[selector] = stubbed_method_ptr;
return *stubbed_method_ptr;
}
- (BOOL)invoke_stubbed_method:(NSInvocation *)invocation {
Cedar::Doubles::StubbedMethod::selector_map_t::iterator it = stubbed_methods_.find(invocation.selector);
if (it == stubbed_methods_.end()) {
return false;
}
Cedar::Doubles::StubbedMethod::shared_ptr_t stubbed_method_ptr = it->second;
if (stubbed_method_ptr->matches(invocation)) {
stubbed_method_ptr->invoke(invocation);
return true;
} else {
NSString * reason = [NSString stringWithFormat:@"Wrong arguments supplied to stub"];
[[NSException exceptionWithName:NSInternalInconsistencyException
reason:reason
userInfo:nil] raise];
return false;
}
}
- (void)record_method_invocation:(NSInvocation *)invocation {
[invocation retainArguments];
[self.sent_messages addObject:invocation];
}
@end