forked from jsonmodel/jsonmodel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleDataErrorTests.m
More file actions
163 lines (126 loc) · 7.38 KB
/
SimpleDataErrorTests.m
File metadata and controls
163 lines (126 loc) · 7.38 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//
// SimpleDataErrorTests.m
// JSONModelDemo
//
// Created by Marin Todorov on 13/12/2012.
// Copyright (c) 2012 Underplot ltd. All rights reserved.
//
#import "SimpleDataErrorTests.h"
#import "PrimitivesModel.h"
#import "NestedModel.h"
#import "CopyrightModel.h"
@implementation SimpleDataErrorTests
-(void)testMissingKeysError
{
NSString* filePath = [[NSBundle bundleForClass:[JSONModel class]].resourcePath stringByAppendingPathComponent:@"primitivesWithErrors.json"];
NSString* jsonContents = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
XCTAssertNotNil(jsonContents, @"Can't fetch test data file contents.");
NSError* err;
PrimitivesModel* p = [[PrimitivesModel alloc] initWithString: jsonContents error:&err];
XCTAssertNil(p, @"Model is not nil, when input is invalid");
XCTAssertNotNil(err, @"No error when keys are missing.");
XCTAssertTrue(err.code == kJSONModelErrorInvalidData, @"Wrong error for missing keys");
NSArray* missingKeys = err.userInfo[kJSONModelMissingKeys];
missingKeys = [missingKeys sortedArrayUsingSelector:@selector(compare:)];
XCTAssertTrue(missingKeys, @"error does not have kJSONModelMissingKeys keys in user info");
XCTAssertTrue([missingKeys[0] isEqualToString:@"intNumber"],@"missing field intNumber not found in missingKeys");
XCTAssertTrue([missingKeys[1] isEqualToString:@"longNumber"],@"missing field longNumber not found in missingKeys");
}
-(void)testTypeMismatchErrorImages
{
NSString* filePath = [[NSBundle bundleForClass:[JSONModel class]].resourcePath stringByAppendingPathComponent:@"nestedDataWithTypeMismatchOnImages.json"];
NSString* jsonContents = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
XCTAssertNotNil(jsonContents, @"Can't fetch test data file contents.");
NSError* err = nil;
NestedModel* p = [[NestedModel alloc] initWithString: jsonContents error:&err];
XCTAssertNil(p, @"Model is not nil, when input is invalid");
XCTAssertNotNil(err, @"No error when types mismatch.");
XCTAssertTrue(err.code == kJSONModelErrorInvalidData, @"Wrong error for type mismatch");
NSString* mismatchDescription = err.userInfo[kJSONModelTypeMismatch];
XCTAssertTrue(mismatchDescription, @"error does not have kJSONModelTypeMismatch key in user info");
XCTAssertTrue([mismatchDescription rangeOfString:@"'images'"].location != NSNotFound, @"error should mention that the 'images' property (expecting an Array) is mismatched.");
// Make sure that the error is at the expected key-path
XCTAssertEqualObjects(err.userInfo[kJSONModelKeyPath], @"images", @"kJSONModelKeyPath does not contain the expected path of the error.");
}
-(void)testTypeMismatchErrorImagesObject
{
NSString* filePath = [[NSBundle bundleForClass:[JSONModel class]].resourcePath stringByAppendingPathComponent:@"nestedDataWithTypeMismatchOnImagesObject.json"];
NSString* jsonContents = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
XCTAssertNotNil(jsonContents, @"Can't fetch test data file contents.");
NSError* err;
NestedModel* p = [[NestedModel alloc] initWithString: jsonContents error:&err];
XCTAssertNil(p, @"Model is not nil, when input is invalid");
XCTAssertNotNil(err, @"No error when types mismatch.");
XCTAssertTrue(err.code == kJSONModelErrorInvalidData, @"Wrong error for type mismatch");
NSString* mismatchDescription = err.userInfo[kJSONModelTypeMismatch];
XCTAssertTrue(mismatchDescription, @"error does not have kJSONModelTypeMismatch key in user info");
XCTAssertTrue([mismatchDescription rangeOfString:@"'imagesObject'"].location != NSNotFound, @"error should mention that the 'imagesObject' property (expecting a Dictionary) is mismatched.");
// Make sure that the error is at the expected key-path
XCTAssertEqualObjects(err.userInfo[kJSONModelKeyPath], @"imagesObject", @"kJSONModelKeyPath does not contain the expected path of the error.");
}
-(void)testBrokenJSON
{
NSString* jsonContents = @"{[1,23,4],\"123\":123,}";
NSError* err;
PrimitivesModel* p = [[PrimitivesModel alloc] initWithString: jsonContents error:&err];
XCTAssertNil(p, @"Model is not nil, when input is invalid");
XCTAssertNotNil(err, @"No error when keys are missing.");
XCTAssertTrue(err.code == kJSONModelErrorBadJSON, @"Wrong error for bad JSON");
}
- (NSError*)performTestErrorsInNestedModelFile:(NSString*)jsonFilename
{
NSString* filePath = [[NSBundle bundleForClass:[JSONModel class]].resourcePath stringByAppendingPathComponent:jsonFilename];
NSString* jsonContents = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
XCTAssertNotNil(jsonContents, @"Can't fetch test data file contents.");
NSError* err = nil;
NestedModel* n = [[NestedModel alloc] initWithString: jsonContents error:&err];
XCTAssertNotNil(err, @"No error thrown when loading invalid data");
XCTAssertNil(n, @"Model is not nil, when invalid data input");
XCTAssertTrue(err.code == kJSONModelErrorInvalidData, @"Wrong error for missing keys");
// Make sure that 'name' is listed as the missing key
XCTAssertEqualObjects(err.userInfo[kJSONModelMissingKeys][0], @"name", @"'name' should be the missing key.");
return err;
}
-(void)testErrorsInNestedModelsArray
{
NSError* err = [self performTestErrorsInNestedModelFile:@"nestedDataWithArrayError.json"];
// Make sure that the error is at the expected key-path
XCTAssertEqualObjects(err.userInfo[kJSONModelKeyPath], @"images[1]", @"kJSONModelKeyPath does not contain the expected path of the error.");
}
-(void)testErrorsInNestedModelsDictionary
{
NSError* err = [self performTestErrorsInNestedModelFile:@"nestedDataWithDictionaryError.json"];
// Make sure that the error is at the expected key-path
XCTAssertEqualObjects(err.userInfo[kJSONModelKeyPath], @"imagesObject.image2", @"kJSONModelKeyPath does not contain the expected path of the error.");
}
-(void)testForNilInputFromString
{
JSONModelError* err = nil;
//test for nil string input
CopyrightModel* cpModel = [[CopyrightModel alloc] initWithString:nil
error:&err];
cpModel=nil;
XCTAssertTrue(err!=nil, @"No error returned when initialized with nil string");
XCTAssertTrue(err.code == kJSONModelErrorNilInput, @"Wrong error for nil string input");
}
-(void)testForNilInputFromDictionary
{
JSONModelError* err = nil;
//test for nil string input
CopyrightModel* cpModel = [[CopyrightModel alloc] initWithDictionary:nil
error:&err];
cpModel=nil;
XCTAssertTrue(err!=nil, @"No error returned when initialized with nil dictionary");
XCTAssertTrue(err.code == kJSONModelErrorNilInput, @"Wrong error for nil dictionary input");
}
-(void)testForNullValuesForRequiredProperty
{
JSONModelError* err = nil;
NSString* jsonString = @"{\"author\":\"Marin\",\"year\":null}";
CopyrightModel* cpModel = [[CopyrightModel alloc] initWithString:jsonString
error:&err];
cpModel = nil;
XCTAssertTrue(err, @"No error returned when initialized with nil dictionary");
XCTAssertTrue(err.code == kJSONModelErrorInvalidData, @"Wrong error null value for a required property");
}
@end