Skip to content

Commit e58a6f9

Browse files
committed
Test case added
jsonmodel#1. InitWithDataTests.m file added jsonmodel#2. testForNilInputFromData failed because no error - fixed
1 parent 02619d9 commit e58a6f9

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

JSONModel/JSONModel/JSONModel.m

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,11 @@ -(id)init
114114

115115
-(instancetype)initWithData:(NSData *)data error:(NSError *__autoreleasing *)err
116116
{
117-
if (!data) return nil;
118-
117+
//check for nil input
118+
if (!data) {
119+
if (err) *err = [JSONModelError errorInputIsNil];
120+
return nil;
121+
}
119122
//read the json
120123
JSONModelError* initError = nil;
121124
id obj = [NSJSONSerialization JSONObjectWithData:data
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
@end

JSONModelDemo_iOS.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
4A50001D19C5DCCF00C161A0 /* InitWithDataTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 4A50001C19C5DCCF00C161A0 /* InitWithDataTests.m */; };
1011
69286BDA17FA280900D1BA81 /* nestedDataWithDictionaryError.json in Resources */ = {isa = PBXBuildFile; fileRef = 69286BD917FA280900D1BA81 /* nestedDataWithDictionaryError.json */; };
1112
69286BDB17FA280900D1BA81 /* nestedDataWithDictionaryError.json in Resources */ = {isa = PBXBuildFile; fileRef = 69286BD917FA280900D1BA81 /* nestedDataWithDictionaryError.json */; };
1213
697852FD17D934B5006BFCD0 /* nestedDataWithTypeMismatchOnImages.json in Resources */ = {isa = PBXBuildFile; fileRef = 697852FC17D934B5006BFCD0 /* nestedDataWithTypeMismatchOnImages.json */; };
@@ -151,6 +152,7 @@
151152
/* End PBXContainerItemProxy section */
152153

153154
/* Begin PBXFileReference section */
155+
4A50001C19C5DCCF00C161A0 /* InitWithDataTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = InitWithDataTests.m; sourceTree = "<group>"; };
154156
69286BD917FA280900D1BA81 /* nestedDataWithDictionaryError.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = nestedDataWithDictionaryError.json; sourceTree = "<group>"; };
155157
697852FC17D934B5006BFCD0 /* nestedDataWithTypeMismatchOnImages.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = nestedDataWithTypeMismatchOnImages.json; sourceTree = "<group>"; };
156158
697852FE17D93546006BFCD0 /* nestedDataWithTypeMismatchOnImagesObject.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = nestedDataWithTypeMismatchOnImagesObject.json; sourceTree = "<group>"; };
@@ -419,6 +421,7 @@
419421
9C735D6E170C007900FF96F5 /* InitFromWebTests.h */,
420422
9C735D6F170C007900FF96F5 /* InitFromWebTests.m */,
421423
9CCAFD911901B44300314886 /* SpecialPropertiesTests.m */,
424+
4A50001C19C5DCCF00C161A0 /* InitWithDataTests.m */,
422425
);
423426
path = UnitTests;
424427
sourceTree = "<group>";
@@ -948,6 +951,7 @@
948951
9C66E035168CF0AA0015CCDF /* JSONKeyMapper.m in Sources */,
949952
9C66E037168CF0AA0015CCDF /* JSONValueTransformer.m in Sources */,
950953
9CB1EE42172C1136004BAA07 /* SpecialPropertyNameTests.m in Sources */,
954+
4A50001D19C5DCCF00C161A0 /* InitWithDataTests.m in Sources */,
951955
9CD425751701FE0000A42AA1 /* HTTPClientSuite.m in Sources */,
952956
9CD425781701FF2100A42AA1 /* MTTestSemaphor.m in Sources */,
953957
9CD4257B1702002900A42AA1 /* MockNSURLConnection.m in Sources */,

0 commit comments

Comments
 (0)