File tree Expand file tree Collapse file tree
JSONModelDemoTests/UnitTests Expand file tree Collapse file tree Original file line number Diff line number Diff line change 3838- (id )initWithArray : (NSArray *)array modelClass : (Class )cls ;
3939
4040- (id )objectAtIndex : (NSUInteger )index ;
41+ - (id )objectAtIndexedSubscript : (NSUInteger )index ;
4142- (void )forwardInvocation : (NSInvocation *)anInvocation ;
4243- (NSUInteger )count ;
44+ - (id )firstObject ;
45+ - (id )lastObject ;
4346
4447/* *
4548 * Looks up the array's contents and tries to find a JSONModel object
Original file line number Diff line number Diff line change @@ -34,27 +34,32 @@ - (id)initWithArray:(NSArray *)array modelClass:(Class)cls
3434 return self;
3535}
3636
37- -(id )firstObject
37+ - (id )firstObject
3838{
3939 return [self objectAtIndex: 0 ];
4040}
4141
42- -(id )lastObject
42+ - (id )lastObject
4343{
44- return [self objectAtIndex: _storage.count- 1 ];
44+ return [self objectAtIndex: _storage.count - 1 ];
4545}
4646
47- -(id )objectAtIndex : (NSUInteger )index
47+ - (id )objectAtIndex : (NSUInteger )index
4848{
49- id obj = _storage[index];
50- if (![obj isMemberOfClass: _targetClass]) {
49+ return [self objectAtIndexedSubscript: index];
50+ }
51+
52+ - (id )objectAtIndexedSubscript : (NSUInteger )index
53+ {
54+ id object = _storage[index];
55+ if (![object isMemberOfClass: _targetClass]) {
5156 NSError * err = nil ;
52- obj = [[_targetClass alloc ] initWithDictionary: obj error: &err];
53- if (obj ) {
54- _storage[index] = obj ;
57+ object = [[_targetClass alloc ] initWithDictionary: object error: &err];
58+ if (object ) {
59+ _storage[index] = object ;
5560 }
5661 }
57- return obj ;
62+ return object ;
5863}
5964
6065- (void )forwardInvocation : (NSInvocation *)anInvocation
Original file line number Diff line number Diff line change @@ -38,14 +38,24 @@ -(void)testLoading
3838 XCTAssertEqualObjects ([[repos.repositories[0 ] class ] description ], @" GitHubRepoModel" , @" .properties[0] is not a GitHubRepoModel" );
3939}
4040
41- -(void )testCount
41+ - (void )testCount
4242{
4343 XCTAssertEqualObjects (@(repos.repositories .count ), @100 , @" wrong count" );
44+ }
4445
45- NSError *err;
46- repos = [[ReposModel alloc ] initWithString: @" {}" error: &err];
46+ - (void )testReadArray
47+ {
48+ JSONModelArray *array = [JSONModelArray new ];
4749
48- XCTAssertEqualObjects (@(repos.repositories .count ), @0 , @" wrong count" );
50+ XCTAssertEqualObjects (@(array.count ), @0 , @" wrong count" );
51+ XCTAssertNil ([array firstObject ], @" first object of an empty array should be nil" );
52+ XCTAssertNil ([array lastObject ], @" last object of an empty array should be nil" );
53+ // XCTAssertThrows(array[0], @"read of empty array should throw an exception");
54+ // XCTAssertThrows(array[2], @"read of empty array should throw an exception");
55+ // XCTAssertNilThrows(array[-2], @"read of empty array should throw an exception");
56+ XCTAssertNil (array[0 ], @" read of empty array should be nil" );
57+ XCTAssertNil (array[2 ], @" read of empty array should be nil" );
58+ XCTAssertNil (array[-2 ], @" read of empty array should be nil" );
4959}
5060
5161-(void )testFirstObject
You can’t perform that action at this time.
0 commit comments