-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathExpecta+OCMock.m
199 lines (139 loc) · 5.03 KB
/
Expecta+OCMock.m
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#import "Expecta+OCMock.h"
#import "EXPMatcherHelpers.h"
#import <OCMock/OCMock.h>
#import <OCMock/OCPartialMockObject.h>
#import <OCMock/OCMExpectationRecorder.h>
#import <OCMock/OCMStubRecorder.h>
#import <OCMock/OCMInvocationStub.h>
#import <objc/runtime.h>
#import <XCTest/XCTest.h>
@interface OCMExpectationRecorder (Private)
- (OCMInvocationStub *)stub;
@end
@interface ORExpectaOCMockMatcher : NSObject <EXPMatcher>
- (instancetype)initWithExpectation:(EXPExpect *)expectation;
@end
@interface ORExpectaOCMockMatcher()
@property (nonatomic, weak) EXPExpect *expectation;
@property (nonatomic, assign) SEL selector;
@property (nonatomic, copy) NSArray *arguments;
@property (nonatomic, strong) id returning;
@property (nonatomic, strong) OCMExpectationRecorder *selectorCheckRecorder;
@property (nonatomic, strong) OCPartialMockObject *mock;
@end
@implementation ORExpectaOCMockMatcher
- (instancetype)initWithExpectation:(EXPExpect *)expectation
{
self = [super init];
if (!self) { return nil; }
_expectation = expectation;
_mock = expectation.actual;
_selectorCheckRecorder = [_mock expect];
[self.selectorCheckRecorder andForwardToRealObject];
return self;
}
- (void)updateMatcher
{
[(NSMutableArray *)self.selectorCheckRecorder.stub.invocationActions removeAllObjects];
// Yeah, I'm doing it. I know what it is under the hood.
NSInvocation *invocation = [self representedInvocation];
[invocation invoke];
}
- (NSInvocation *)representedInvocation
{
NSMethodSignature *mySignature = [self.mock.realObject.class instanceMethodSignatureForSelector:self.selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:mySignature];
invocation.selector = self.selector;
invocation.target = self.selectorCheckRecorder;
for (__unsafe_unretained id object in self.arguments) {
[invocation setArgument:&object atIndex:[self.arguments indexOfObject:object] +2];
}
return invocation;
}
- (BOOL)matches:(id)actual
{
return YES;
}
- (void)dealloc
{
id mock = self.mock;
NSException *theException = nil;
@try {
[mock verify];
}
@catch (NSException *exception) {
theException = exception;
}
if (_returning) {
// The return value is checked at the end
NSInvocation *invocation = [self representedInvocation];
[invocation setTarget:self.mock.realObject];
[invocation invoke];
const char *retType = [invocation.methodSignature methodReturnType];
BOOL resultIsObject = (strcmp(retType, @encode(id)) == 0 || strcmp(retType, @encode(void)) == 0);
id result;
if (resultIsObject) {
CFTypeRef cfResult;
[invocation getReturnValue:&cfResult];
result = (__bridge_transfer id)cfResult;
CFRetain(cfResult);
} else {
// This is a really simple implementation, only supports NSIntegers
NSInteger resultValue;
[invocation getReturnValue:&resultValue];
result = [NSNumber numberWithInteger:resultValue];
}
if (![result isEqual:self.returning]) {
_XCTFailureHandler(self.expectation.testCase, YES, self.expectation.fileName , self.expectation.lineNumber, FALSE, @"Expected a match on the return value");
}
}
if (theException) {
EXPFail(self.expectation.testCase, self.expectation.lineNumber, self.expectation.fileName, @"Fail");
[theException raise];
}
[mock stopMocking];
}
@end
/// For passing data between recieve and with
@interface EXPExpect (receiveMatcherPrivate)
@property (nonatomic, strong) id _expectaOCMatcher;
@end
@implementation EXPExpect (receiveMatcherPrivate)
@dynamic _expectaOCMatcher;
@end
@implementation EXPExpect (receiveMatcher)
@dynamic receive;
- (EXPExpect *(^) (SEL)) receive {
__block id actual = self.actual;
ORExpectaOCMockMatcher *matcher = [[ORExpectaOCMockMatcher alloc] initWithExpectation:self];
objc_setAssociatedObject(self, @selector(_expectaOCMatcher), matcher, OBJC_ASSOCIATION_RETAIN);
EXPExpect *(^matcherBlock) (SEL selector) = [^ (SEL selector) {
matcher.selector = selector;
[matcher updateMatcher];
actual = matcher.mock;
[self applyMatcher:matcher to:&actual];
return self;
} copy];
return matcherBlock;
}
@dynamic with;
- (EXPExpect *(^) (NSArray *)) with {
EXPExpect *(^matcherBlock) (id object) = [^ (id object) {
ORExpectaOCMockMatcher *matcher = objc_getAssociatedObject(self, @selector(_expectaOCMatcher));
matcher.arguments = object;
[matcher updateMatcher];
return self;
} copy];
return matcherBlock;
}
@dynamic returning;
- (EXPExpect *(^) (id)) returning {
EXPExpect *(^matcherBlock) (id object) = [^ (id object) {
ORExpectaOCMockMatcher *matcher = objc_getAssociatedObject(self, @selector(_expectaOCMatcher));
matcher.returning = object;
[matcher updateMatcher];
return self;
} copy];
return matcherBlock;
}
@end