Skip to content

Commit

Permalink
Add test case for recursive models from @cspickert
Browse files Browse the repository at this point in the history
  • Loading branch information
jmah committed Aug 5, 2015
1 parent 4ca6073 commit 4f00e0f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
15 changes: 15 additions & 0 deletions MantleTests/MTLJSONAdapterSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -591,4 +591,19 @@ + (NSValueTransformer *)NSDateJSONTransformer {
expect(weakTransformer).toEventually(beNil());
});

it(@"should support recursive models", ^{
NSDictionary *dictionary = @{
@"owner": @{@"name": @"Cameron"},
@"users": @[
@{@"name": @"Dimitri"},
@{@"name": @"John"},
],
};

NSError *error = nil;
MTLRecursiveGroupModel *group = [MTLJSONAdapter modelOfClass:[MTLRecursiveGroupModel class] fromJSONDictionary:dictionary error:&error];
expect(group).notTo(beNil());
expect(@(group.users.count)).to(equal(@2));
});

QuickSpecEnd
15 changes: 15 additions & 0 deletions MantleTests/MTLTestModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,18 @@ extern const NSInteger MTLTestModelNameMissing;
@property (readwrite, nonatomic, strong) id optionalImplementedProperty;

@end


@interface MTLRecursiveUserModel : MTLModel <MTLJSONSerializing>

@property (nonatomic, copy, readonly) NSString *name;
@property (nonatomic, copy, readonly) NSArray *groups;

@end

@interface MTLRecursiveGroupModel : MTLModel <MTLJSONSerializing>

@property (nonatomic, readonly) MTLRecursiveUserModel *owner;
@property (nonatomic, readonly) NSArray *users;

@end
28 changes: 28 additions & 0 deletions MantleTests/MTLTestModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -472,3 +472,31 @@ - (NSString *)flavor {
@implementation MTLOptionalPropertyModel

@end

@implementation MTLRecursiveUserModel

+ (NSDictionary *)JSONKeyPathsByPropertyKey {
return @{@"name": @"name", @"groups": @"groups"};
}

+ (NSValueTransformer *)groupsJSONTransformer {
return [MTLJSONAdapter arrayTransformerWithModelClass:[MTLRecursiveGroupModel class]];
}

@end

@implementation MTLRecursiveGroupModel

+ (NSDictionary *)JSONKeyPathsByPropertyKey {
return @{@"owner": @"owner", @"users": @"users"};
}

+ (NSValueTransformer *)ownerJSONTransformer {
return [MTLJSONAdapter dictionaryTransformerWithModelClass:[MTLRecursiveUserModel class]];
}

+ (NSValueTransformer *)usersJSONTransformer {
return [MTLJSONAdapter arrayTransformerWithModelClass:[MTLRecursiveUserModel class]];
}

@end

0 comments on commit 4f00e0f

Please sign in to comment.