Skip to content

Commit

Permalink
Unwind nil behavior introduced in previous commit and add test covera…
Browse files Browse the repository at this point in the history
…ge for asObject behaviors.
  • Loading branch information
blakewatters committed Jul 5, 2011
1 parent dddc4c3 commit 82ce444
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
9 changes: 4 additions & 5 deletions Code/ObjectMapping/RKObjectMappingResult.m
Expand Up @@ -53,13 +53,12 @@ - (NSArray*)asCollection {
- (id)asObject {
NSArray* collection = [self asCollection];
NSUInteger count = [collection count];
if (count > 1) {
RKLogWarning(@"Coerced object mapping result containing %d objects into singular result.", [collection count]);
} else if (count == 1) {
return [collection objectAtIndex:0];
if (count == 0) {
return nil;
}

return nil;
if (count > 1) RKLogWarning(@"Coerced object mapping result containing %d objects into singular result.", count);
return [collection objectAtIndex:0];
}

- (NSError*)asError {
Expand Down
11 changes: 11 additions & 0 deletions Specs/ObjectMapping/RKObjectMappingResultSpec.m
Expand Up @@ -29,4 +29,15 @@ - (void)itShouldNotCrashWhenAsObjectIsInvokedOnAnEmptyResult {
}
}

- (void)itShouldReturnNilForAnEmptyCollectionCoercedToAsObject {
RKObjectMappingResult* result = [RKObjectMappingResult mappingResultWithDictionary:[NSDictionary dictionary]];
assertThat([result asObject], is(equalTo(nil)));
}

- (void)itShouldReturnTheFirstObjectInTheCollectionWhenCoercedToAsObject {
NSDictionary* dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"one", @"one", @"two", @"two", nil];
RKObjectMappingResult* result = [RKObjectMappingResult mappingResultWithDictionary:dictionary];
assertThat([result asObject], is(equalTo(@"one")));
}

@end

0 comments on commit 82ce444

Please sign in to comment.