forked from RestKit/RestKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRKMappingTest.m
More file actions
248 lines (200 loc) · 8.95 KB
/
Copy pathRKMappingTest.m
File metadata and controls
248 lines (200 loc) · 8.95 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//
// RKMappingTest.m
// RestKit
//
// Created by Blake Watters on 2/17/12.
// Copyright (c) 2009-2012 RestKit. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "RKMappingTest.h"
BOOL RKObjectIsValueEqualToValue(id sourceValue, id destinationValue);
///-----------------------------------------------------------------------------
///-----------------------------------------------------------------------------
@interface RKMappingTestEvent : NSObject
@property (nonatomic, strong, readonly) RKObjectAttributeMapping *mapping;
@property (nonatomic, strong, readonly) id value;
@property (nonatomic, readonly) NSString *sourceKeyPath;
@property (nonatomic, readonly) NSString *destinationKeyPath;
+ (RKMappingTestEvent *)eventWithMapping:(RKObjectAttributeMapping *)mapping value:(id)value;
@end
@interface RKMappingTestEvent ()
@property (nonatomic, strong, readwrite) id value;
@property (nonatomic, strong, readwrite) RKObjectAttributeMapping *mapping;
@end
@implementation RKMappingTestEvent
@synthesize value;
@synthesize mapping;
+ (RKMappingTestEvent *)eventWithMapping:(RKObjectAttributeMapping *)mapping value:(id)value
{
RKMappingTestEvent *event = [RKMappingTestEvent new];
event.value = value;
event.mapping = mapping;
return event;
}
- (NSString *)sourceKeyPath
{
return self.mapping.sourceKeyPath;
}
- (NSString *)destinationKeyPath
{
return self.mapping.destinationKeyPath;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"%@: mapped sourceKeyPath '%@' => destinationKeyPath '%@' with value: %@", [self class], self.sourceKeyPath, self.destinationKeyPath, self.value];
}
@end
///-----------------------------------------------------------------------------
///-----------------------------------------------------------------------------
@interface RKMappingTest () <RKObjectMappingOperationDelegate>
@property (nonatomic, strong, readwrite) RKObjectMapping *mapping;
@property (nonatomic, strong, readwrite) id sourceObject;
@property (nonatomic, strong, readwrite) id destinationObject;
@property (nonatomic, strong) NSMutableArray *expectations;
@property (nonatomic, strong) NSMutableArray *events;
@property (nonatomic, assign, getter = hasPerformedMapping) BOOL performedMapping;
// Method Definitions for old compilers
- (void)performMapping;
- (void)verifyExpectation:(RKMappingTestExpectation *)expectation;
@end
@implementation RKMappingTest
@synthesize sourceObject = _sourceObject;
@synthesize destinationObject = _destinationObject;
@synthesize mapping = _mapping;
@synthesize rootKeyPath = _rootKeyPath;
@synthesize expectations = _expectations;
@synthesize events = _events;
@synthesize verifiesOnExpect = _verifiesOnExpect;
@synthesize performedMapping = _performedMapping;
+ (RKMappingTest *)testForMapping:(RKObjectMapping *)mapping object:(id)sourceObject
{
return [[self alloc] initWithMapping:mapping sourceObject:sourceObject destinationObject:nil];
}
+ (RKMappingTest *)testForMapping:(RKObjectMapping *)mapping sourceObject:(id)sourceObject destinationObject:(id)destinationObject
{
return [[self alloc] initWithMapping:mapping sourceObject:sourceObject destinationObject:destinationObject];
}
- (id)initWithMapping:(RKObjectMapping *)mapping sourceObject:(id)sourceObject destinationObject:(id)destinationObject
{
NSAssert(sourceObject != nil, @"Cannot perform a mapping operation without a sourceObject object");
NSAssert(mapping != nil, @"Cannot perform a mapping operation without a mapping");
self = [super init];
if (self) {
_sourceObject = sourceObject;
_destinationObject = destinationObject;
_mapping = mapping;
_expectations = [NSMutableArray new];
_events = [NSMutableArray new];
_verifiesOnExpect = NO;
_performedMapping = NO;
}
return self;
}
- (void)addExpectation:(RKMappingTestExpectation *)expectation
{
[self.expectations addObject:expectation];
if (self.verifiesOnExpect) {
[self performMapping];
[self verifyExpectation:expectation];
}
}
- (void)expectMappingFromKeyPath:(NSString *)sourceKeyPath toKeyPath:(NSString *)destinationKeyPath
{
[self addExpectation:[RKMappingTestExpectation expectationWithSourceKeyPath:sourceKeyPath destinationKeyPath:destinationKeyPath]];
}
- (void)expectMappingFromKeyPath:(NSString *)sourceKeyPath toKeyPath:(NSString *)destinationKeyPath withValue:(id)value
{
[self addExpectation:[RKMappingTestExpectation expectationWithSourceKeyPath:sourceKeyPath destinationKeyPath:destinationKeyPath value:value]];
}
- (void)expectMappingFromKeyPath:(NSString *)sourceKeyPath toKeyPath:(NSString *)destinationKeyPath passingTest:(BOOL (^)(RKObjectAttributeMapping *mapping, id value))evaluationBlock
{
[self addExpectation:[RKMappingTestExpectation expectationWithSourceKeyPath:sourceKeyPath destinationKeyPath:destinationKeyPath evaluationBlock:evaluationBlock]];
}
- (RKMappingTestEvent *)eventMatchingKeyPathsForExpectation:(RKMappingTestExpectation *)expectation
{
for (RKMappingTestEvent *event in self.events) {
if ([event.sourceKeyPath isEqualToString:expectation.sourceKeyPath] && [event.destinationKeyPath isEqualToString:expectation.destinationKeyPath]) {
return event;
}
}
return nil;
}
- (BOOL)event:(RKMappingTestEvent *)event satisfiesExpectation:(RKMappingTestExpectation *)expectation
{
if (expectation.evaluationBlock) {
// Let the expectation block evaluate the match
return expectation.evaluationBlock(event.mapping, event.value);
} else if (expectation.value) {
// Use RestKit comparison magic to match values
return RKObjectIsValueEqualToValue(event.value, expectation.value);
}
// We only wanted to know that a mapping occured between the keyPaths
return YES;
}
- (void)performMapping
{
NSAssert(self.mapping.objectClass, @"Cannot test a mapping that does not have a destination objectClass");
// Ensure repeated invocations of verify only result in a single mapping operation
if (! self.hasPerformedMapping) {
id sourceObject = self.rootKeyPath ? [self.sourceObject valueForKeyPath:self.rootKeyPath] : self.sourceObject;
if (nil == self.destinationObject) {
self.destinationObject = [self.mapping mappableObjectForData:self.sourceObject];
}
RKObjectMappingOperation *mappingOperation = [RKObjectMappingOperation mappingOperationFromObject:sourceObject toObject:self.destinationObject withMapping:self.mapping];
NSError *error = nil;
mappingOperation.delegate = self;
BOOL success = [mappingOperation performMapping:&error];
if (! success) {
[NSException raise:NSInternalInconsistencyException format:@"%@: failure when mapping from %@ to %@ with mapping %@",
[self description], self.sourceObject, self.destinationObject, self.mapping];
}
self.performedMapping = YES;
}
}
- (void)verifyExpectation:(RKMappingTestExpectation *)expectation
{
RKMappingTestEvent *event = [self eventMatchingKeyPathsForExpectation:expectation];
if (event) {
// Found a matching event, check if it satisfies the expectation
if (! [self event:event satisfiesExpectation:expectation]) {
[NSException raise:NSInternalInconsistencyException format:@"%@: expectation not satisfied: %@, but instead got %@ '%@'",
[self description], expectation, [event.value class], event.value];
}
} else {
// No match
[NSException raise:NSInternalInconsistencyException format:@"%@: expectation not satisfied: %@, but did not.",
[self description], [expectation mappingDescription]];
}
}
- (void)verify
{
[self performMapping];
for (RKMappingTestExpectation *expectation in self.expectations) {
[self verifyExpectation:expectation];
}
}
#pragma mark - RKObjecMappingOperationDelegate
- (void)addEvent:(RKMappingTestEvent *)event
{
[self.events addObject:event];
}
- (void)objectMappingOperation:(RKObjectMappingOperation *)operation didSetValue:(id)value forKeyPath:(NSString *)keyPath usingMapping:(RKObjectAttributeMapping *)mapping
{
[self addEvent:[RKMappingTestEvent eventWithMapping:mapping value:value]];
}
- (void)objectMappingOperation:(RKObjectMappingOperation *)operation didNotSetUnchangedValue:(id)value forKeyPath:(NSString *)keyPath usingMapping:(RKObjectAttributeMapping *)mapping
{
[self addEvent:[RKMappingTestEvent eventWithMapping:mapping value:value]];
}
@end