Skip to content

Commit bef8d57

Browse files
committed
More tests added
added methods for checking initWithData Just copied from SimpleDataErrorTests and replaced stringWithContentsOfFile with dataWithContentsOfFile and init with data accordingly Added methods jsonmodel#1. testErrorsInNestedModelsDictionary jsonmodel#2. testErrorsInNestedModelsArray jsonmodel#3. testMissingKeysError jsonmodel#4. testTypeMismatchErrorImages jsonmodel#5. testTypeMismatchErrorImagesObject
1 parent e58a6f9 commit bef8d57

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

JSONModelDemoTests/UnitTests/InitWithDataTests.m

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,102 @@ -(void)testForNilInputFromData
4141
XCTAssertTrue(err!=nil, @"No error returned when initialized with nil string");
4242
XCTAssertTrue(err.code == kJSONModelErrorNilInput, @"Wrong error for nil string input");
4343
}
44+
45+
-(void)testErrorsInNestedModelsArray
46+
{
47+
NSError* err = [self performTestErrorsInNestedModelFile:@"nestedDataWithArrayError.json"];
48+
49+
// Make sure that the error is at the expected key-path
50+
XCTAssertEqualObjects(err.userInfo[kJSONModelKeyPath], @"images[1]", @"kJSONModelKeyPath does not contain the expected path of the error.");
51+
}
52+
53+
-(void)testErrorsInNestedModelsDictionary
54+
{
55+
NSError* err = [self performTestErrorsInNestedModelFile:@"nestedDataWithDictionaryError.json"];
56+
57+
// Make sure that the error is at the expected key-path
58+
XCTAssertEqualObjects(err.userInfo[kJSONModelKeyPath], @"imagesObject.image2", @"kJSONModelKeyPath does not contain the expected path of the error.");
59+
}
60+
61+
- (NSError*)performTestErrorsInNestedModelFile:(NSString*)jsonFilename
62+
{
63+
NSString* filePath = [[NSBundle bundleForClass:[JSONModel class]].resourcePath stringByAppendingPathComponent:jsonFilename];
64+
NSData *jsonData = [NSData dataWithContentsOfFile:filePath];
65+
66+
XCTAssertNotNil(jsonData, @"Can't fetch test data file contents.");
67+
68+
NSError* err = nil;
69+
NestedModel* n = [[NestedModel alloc] initWithData: jsonData error:&err];
70+
XCTAssertNotNil(err, @"No error thrown when loading invalid data");
71+
72+
XCTAssertNil(n, @"Model is not nil, when invalid data input");
73+
XCTAssertTrue(err.code == kJSONModelErrorInvalidData, @"Wrong error for missing keys");
74+
75+
// Make sure that 'name' is listed as the missing key
76+
XCTAssertEqualObjects(err.userInfo[kJSONModelMissingKeys][0], @"name", @"'name' should be the missing key.");
77+
return err;
78+
}
79+
80+
-(void)testMissingKeysError
81+
{
82+
NSString* filePath = [[NSBundle bundleForClass:[JSONModel class]].resourcePath stringByAppendingPathComponent:@"primitivesWithErrors.json"];
83+
NSData *jsonData = [NSData dataWithContentsOfFile:filePath];
84+
85+
XCTAssertNotNil(jsonData, @"Can't fetch test data file contents.");
86+
87+
NSError* err;
88+
PrimitivesModel* p = [[PrimitivesModel alloc] initWithData: jsonData error:&err];
89+
XCTAssertNil(p, @"Model is not nil, when input is invalid");
90+
XCTAssertNotNil(err, @"No error when keys are missing.");
91+
92+
XCTAssertTrue(err.code == kJSONModelErrorInvalidData, @"Wrong error for missing keys");
93+
NSArray* missingKeys = err.userInfo[kJSONModelMissingKeys];
94+
missingKeys = [missingKeys sortedArrayUsingSelector:@selector(compare:)];
95+
XCTAssertTrue(missingKeys, @"error does not have kJSONModelMissingKeys keys in user info");
96+
XCTAssertTrue([missingKeys[0] isEqualToString:@"intNumber"],@"missing field intNumber not found in missingKeys");
97+
XCTAssertTrue([missingKeys[1] isEqualToString:@"longNumber"],@"missing field longNumber not found in missingKeys");
98+
}
99+
100+
-(void)testTypeMismatchErrorImages
101+
{
102+
NSString* filePath = [[NSBundle bundleForClass:[JSONModel class]].resourcePath stringByAppendingPathComponent:@"nestedDataWithTypeMismatchOnImages.json"];
103+
NSData *jsonData = [NSData dataWithContentsOfFile:filePath];
104+
105+
XCTAssertNotNil(jsonData, @"Can't fetch test data file contents.");
106+
107+
NSError* err = nil;
108+
NestedModel* p = [[NestedModel alloc] initWithData: jsonData error:&err];
109+
XCTAssertNil(p, @"Model is not nil, when input is invalid");
110+
XCTAssertNotNil(err, @"No error when types mismatch.");
111+
112+
XCTAssertTrue(err.code == kJSONModelErrorInvalidData, @"Wrong error for type mismatch");
113+
NSString* mismatchDescription = err.userInfo[kJSONModelTypeMismatch];
114+
XCTAssertTrue(mismatchDescription, @"error does not have kJSONModelTypeMismatch key in user info");
115+
XCTAssertTrue([mismatchDescription rangeOfString:@"'images'"].location != NSNotFound, @"error should mention that the 'images' property (expecting an Array) is mismatched.");
116+
117+
// Make sure that the error is at the expected key-path
118+
XCTAssertEqualObjects(err.userInfo[kJSONModelKeyPath], @"images", @"kJSONModelKeyPath does not contain the expected path of the error.");
119+
}
120+
121+
-(void)testTypeMismatchErrorImagesObject
122+
{
123+
NSString* filePath = [[NSBundle bundleForClass:[JSONModel class]].resourcePath stringByAppendingPathComponent:@"nestedDataWithTypeMismatchOnImagesObject.json"];
124+
NSData *jsonData = [NSData dataWithContentsOfFile:filePath];
125+
126+
XCTAssertNotNil(jsonData, @"Can't fetch test data file contents.");
127+
128+
NSError* err;
129+
NestedModel* p = [[NestedModel alloc] initWithData: jsonData error:&err];
130+
XCTAssertNil(p, @"Model is not nil, when input is invalid");
131+
XCTAssertNotNil(err, @"No error when types mismatch.");
132+
133+
XCTAssertTrue(err.code == kJSONModelErrorInvalidData, @"Wrong error for type mismatch");
134+
NSString* mismatchDescription = err.userInfo[kJSONModelTypeMismatch];
135+
XCTAssertTrue(mismatchDescription, @"error does not have kJSONModelTypeMismatch key in user info");
136+
XCTAssertTrue([mismatchDescription rangeOfString:@"'imagesObject'"].location != NSNotFound, @"error should mention that the 'imagesObject' property (expecting a Dictionary) is mismatched.");
137+
138+
// Make sure that the error is at the expected key-path
139+
XCTAssertEqualObjects(err.userInfo[kJSONModelKeyPath], @"imagesObject", @"kJSONModelKeyPath does not contain the expected path of the error.");
140+
}
141+
44142
@end

0 commit comments

Comments
 (0)