mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-09-22 18:30:43 +00:00
Fix for objectAtIndex: throwing out of range exceptions.
Closes #105. Reviewed by me.
This commit is contained in:
@@ -26,6 +26,7 @@
|
||||
@import "CPSortDescriptor.j"
|
||||
@import "CPException.j"
|
||||
|
||||
|
||||
/* @ignore */
|
||||
@implementation _CPArrayEnumerator : CPEnumerator
|
||||
{
|
||||
@@ -520,6 +521,9 @@
|
||||
*/
|
||||
- (id)objectAtIndex:(int)anIndex
|
||||
{
|
||||
if (anIndex >= length)
|
||||
[CPException raise:CPRangeException reason:@"index (" + anIndex + @") beyond bounds (" + length + @")"];
|
||||
|
||||
return self[anIndex];
|
||||
}
|
||||
|
||||
|
||||
@@ -115,6 +115,46 @@
|
||||
equals:2];
|
||||
}
|
||||
|
||||
- (void)testIndexOutOfBounds
|
||||
{
|
||||
try
|
||||
{
|
||||
[[] objectAtIndex:0];
|
||||
[self assert:false];
|
||||
}
|
||||
catch (anException)
|
||||
{
|
||||
[self assert:[anException name] equals:CPRangeException];
|
||||
[self assert:[anException reason] equals:@"index (0) beyond bounds (0)"];
|
||||
}
|
||||
|
||||
[[0, 1, 2] objectAtIndex:0];
|
||||
[[0, 1, 2] objectAtIndex:1];
|
||||
[[0, 1, 2] objectAtIndex:2];
|
||||
|
||||
try
|
||||
{
|
||||
[[0, 1, 2] objectAtIndex:3];
|
||||
[self assert:false];
|
||||
}
|
||||
catch (anException)
|
||||
{
|
||||
[self assert:[anException name] equals:CPRangeException];
|
||||
[self assert:[anException reason] equals:@"index (3) beyond bounds (3)"];
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
[[0, 1, 2] objectAtIndex:4];
|
||||
[self assert:false];
|
||||
}
|
||||
catch (anException)
|
||||
{
|
||||
[self assert:[anException name] equals:CPRangeException];
|
||||
[self assert:[anException reason] equals:@"index (4) beyond bounds (3)"];
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation CPArray (reverse)
|
||||
|
||||
Reference in New Issue
Block a user