Skip to content

Commit 1c8c777

Browse files
author
Josh Holtz
committed
Fixed unit tests to compare classes correctly
1 parent 997c267 commit 1c8c777

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

Classes/JSONAPIResource.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@
2727

2828
- (void)linkLinks:(NSDictionary*)linked;
2929

30+
- (NSDictionary *)mapKeysToProperties;
31+
3032
@end

Classes/JSONAPIResource.m

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,27 @@ - (id)initWithDictionary:(NSDictionary*)dict withLinked:(NSDictionary*)linked {
6363
if (self) {
6464
[self setWithDictionary:dict];
6565
[self linkLinks:linked];
66+
67+
68+
for (NSString *key in [self mapKeysToProperties]) {
69+
if ([key hasPrefix:@"links."] == YES) {
70+
71+
NSString *propertyName = [[self mapKeysToProperties] objectForKey:key];
72+
NSString *linkedResource = [key stringByReplacingOccurrencesOfString:@"links." withString:@""];
73+
74+
id resource = [self linkedResourceForKey:linkedResource];
75+
if (resource != nil) {
76+
@try {
77+
[self setValue:resource forKey:propertyName];
78+
}
79+
@catch (NSException *exception) {
80+
NSLog(@"JSONAPIResource Warning - %@", [exception description]);
81+
}
82+
}
83+
84+
}
85+
}
86+
6687
}
6788
return self;
6889
}

Project/JSONAPITests/JSONAPITests.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ - (void)testResourceModels {
219219
JSONAPI *jsonAPI = [[JSONAPI alloc] initWithDictionary:json];
220220
PostResource *postResource = [jsonAPI resourceForKey:@"posts"];
221221

222-
XCTAssertEqual([postResource class], [PostResource class], @"Post resource is not of type PostResource, but %@", [postResource class]);
223-
XCTAssertEqual([postResource.author class], [PeopleResource class], @"Post resource's author is not of type PeopleResource, but %@", [postResource.author class]);
222+
XCTAssert([postResource class] == [PostResource class], @"Post resource is not of type PostResource, but %@", [postResource class]);
223+
XCTAssert([postResource.author class] == [PeopleResource class], @"Post resource's author is not of type PeopleResource, but %@", [postResource.author class]);
224224
XCTAssertEqualObjects(postResource.name, [post objectForKey:@"name"], @"Post name is not equal to %@", [post objectForKey:@"name"]);
225225
XCTAssertEqualObjects(postResource.author.name, [linkedAuthor9 objectForKey:@"name"], @"Author name is not equal to %@", [post objectForKey:@"name"]);
226226
}
@@ -260,8 +260,8 @@ - (void)testMapKeysToProperties {
260260
JSONAPI *jsonAPI = [[JSONAPI alloc] initWithDictionary:json];
261261
PostResource *postResource = [jsonAPI resourceForKey:@"posts"];
262262

263-
XCTAssertEqual([postResource class], [PostResource class], @"Post resource is not of type PostResource, but %@", [postResource class]);
264-
XCTAssertEqual([postResource.mapAuthor class], [PeopleResource class], @"Post resource's author is not of type PeopleResource, but %@", [postResource.mapAuthor class]);
263+
XCTAssert([postResource class] == [PostResource class], @"Post resource is not of type PostResource, but %@", [postResource class]);
264+
XCTAssert([postResource.mapAuthor class] == [PeopleResource class], @"Post resource's author is not of type PeopleResource, but %@", [postResource.mapAuthor class]);
265265
XCTAssertEqualObjects(postResource.mapName, [post objectForKey:@"name"], @"Post name is not equal to %@", [post objectForKey:@"name"]);
266266
XCTAssertEqualObjects(postResource.author.name, [linkedAuthor9 objectForKey:@"name"], @"Author name is not equal to %@", [post objectForKey:@"name"]);
267267
}

0 commit comments

Comments
 (0)