-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathDCArrayOfTweetsOnUserTests.m
More file actions
120 lines (90 loc) · 4.87 KB
/
DCArrayOfTweetsOnUserTests.m
File metadata and controls
120 lines (90 loc) · 4.87 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
//
// DCArrayOfTweetsOnUserTests.m
// DCKeyValueObjectMappingTests
//
// Created by Diego Chohfi on 4/16/12.
// Copyright (c) 2012 dchohfi. All rights reserved.
//
#import "DCArrayOfTweetsOnUserTests.h"
#import "DCKeyValueObjectMapping.h"
#import "DCArrayMapping.h"
#import "DCParserConfiguration.h"
#import "Tweet.h"
#import "User.h"
@interface DCArrayOfTweetsOnUserTests()
@property(nonatomic, strong) NSMutableDictionary *jsonParsed;
@end
@implementation DCArrayOfTweetsOnUserTests
@synthesize jsonParsed;
-(void)setUp{
[super setUp];
NSString *caminhoJson = [[NSBundle
bundleForClass: [self class]]
pathForResource:@"user"
ofType:@"json"];
NSData *data = [NSData dataWithContentsOfFile:caminhoJson];
NSError *error;
jsonParsed = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingMutableContainers
error:&error];
}
- (void) testShouldCreateAnUserWithTweets {
DCArrayMapping *mapper = [DCArrayMapping mapperForClassElements:[Tweet class]
forAttribute:@"tweets"
onClass:[User class]];
DCParserConfiguration *configuration = [DCParserConfiguration configuration];
[configuration addArrayMapper:mapper];
DCKeyValueObjectMapping *parser = [DCKeyValueObjectMapping mapperForClass:[User class]
andConfiguration:configuration];
User *user = [parser parseDictionary:jsonParsed];
STAssertNotNil(user.tweets, @"Tweets should not be nil");
STAssertEquals((int)user.tweets.count, 2, @"Should have 2 tweets on array of tweets");
}
- (void) testShouldCreateAnUserWithOneImplicitTweet {
DCArrayMapping *mapper = [DCArrayMapping mapperForClassElements:[Tweet class]
forAttribute:@"tweets"
onClass:[User class]];
DCParserConfiguration *configuration = [DCParserConfiguration configuration];
[configuration addArrayMapper:mapper];
DCKeyValueObjectMapping *parser = [DCKeyValueObjectMapping mapperForClass:[User class]
andConfiguration:configuration];
NSArray *tweetArray = [jsonParsed valueForKey:@"tweets"];
Tweet *tweet = [tweetArray objectAtIndex:0];
[jsonParsed setValue:tweet forKey:@"tweets"];
User *user = [parser parseDictionary:jsonParsed];
STAssertNotNil(user.tweets, @"Tweets should not be nil");
STAssertEquals((int)user.tweets.count, 1, @"Should have 1 tweet on array of tweets");
}
- (void) testShouldCreateUserWithNilOnTweetsArray{
DCObjectMapping *objectMapping = [DCObjectMapping mapKeyPath:@"tweets_nullable"
toAttribute:@"tweets"
onClass:[User class]];
DCArrayMapping *mapper = [DCArrayMapping mapperForClass:[Tweet class]
onMapping:objectMapping];
DCParserConfiguration *configuration = [DCParserConfiguration configuration];
[configuration addArrayMapper:mapper];
NSMutableDictionary *copy = [jsonParsed mutableCopy];
[copy removeObjectForKey:@"tweets"];
DCKeyValueObjectMapping *parser = [DCKeyValueObjectMapping mapperForClass:[User class]
andConfiguration:configuration];
User *user = [parser parseDictionary:copy];
STAssertEqualObjects(user.tweets, nil, @"Tweets should be nil", nil);
}
- (void) testShouldCreateUserWithEmptyTweetsOnArray{
DCObjectMapping *objectMapping = [DCObjectMapping mapKeyPath:@"tweets_empty"
toAttribute:@"tweets"
onClass:[User class]];
DCArrayMapping *mapper = [DCArrayMapping mapperForClass:[Tweet class]
onMapping:objectMapping];
DCParserConfiguration *configuration = [DCParserConfiguration configuration];
[configuration addArrayMapper:mapper];
NSMutableDictionary *copy = [jsonParsed mutableCopy];
[copy removeObjectForKey:@"tweets"];
DCKeyValueObjectMapping *parser = [DCKeyValueObjectMapping mapperForClass:[User class]
andConfiguration:configuration];
User *user = [parser parseDictionary:copy];
NSArray *tweets = user.tweets;
STAssertNotNil(tweets, @"Tweets should be nil");
STAssertEquals((int)[tweets count], (int)0, @"Tweets length should be 0");
}
@end