|
| 1 | +// |
| 2 | +// InitWithDataTests.m |
| 3 | +// JSONModelDemo_iOS |
| 4 | +// |
| 5 | +// Created by Johnykutty on 14/09/14. |
| 6 | +// Copyright (c) 2014 Underplot ltd. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import <XCTest/XCTest.h> |
| 10 | +#import "PrimitivesModel.h" |
| 11 | +#import "NestedModel.h" |
| 12 | +#import "CopyrightModel.h" |
| 13 | + |
| 14 | +@interface InitWithDataTests : XCTestCase |
| 15 | + |
| 16 | +@end |
| 17 | + |
| 18 | +@implementation InitWithDataTests |
| 19 | + |
| 20 | +- (void)setUp |
| 21 | +{ |
| 22 | + [super setUp]; |
| 23 | + // Put setup code here. This method is called before the invocation of each test method in the class. |
| 24 | +} |
| 25 | + |
| 26 | +- (void)tearDown |
| 27 | +{ |
| 28 | + // Put teardown code here. This method is called after the invocation of each test method in the class. |
| 29 | + [super tearDown]; |
| 30 | +} |
| 31 | + |
| 32 | +-(void)testForNilInputFromData |
| 33 | +{ |
| 34 | + JSONModelError* err = nil; |
| 35 | + |
| 36 | + //test for nil string input |
| 37 | + CopyrightModel* cpModel = [[CopyrightModel alloc] initWithData:nil |
| 38 | + error:&err]; |
| 39 | + cpModel=nil; |
| 40 | + |
| 41 | + XCTAssertTrue(err!=nil, @"No error returned when initialized with nil string"); |
| 42 | + XCTAssertTrue(err.code == kJSONModelErrorNilInput, @"Wrong error for nil string input"); |
| 43 | +} |
| 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 | + |
| 142 | +@end |
0 commit comments