From 8599ad4a3c38c1030ca9b878e73a25d9e449fa2e Mon Sep 17 00:00:00 2001 From: Martin Carlberg Date: Fri, 25 Sep 2015 17:07:15 +0200 Subject: [PATCH] =?UTF-8?q?Fixed:=20The=20init=20method=20=E2=80=99initWit?= =?UTF-8?q?hObjects:count:=E2=80=99=20for=20CPArray=20did=20not=20work=20o?= =?UTF-8?q?n=20native=20CPJavaScriptArray=20when=20=E2=80=99count=E2=80=99?= =?UTF-8?q?=20is=20not=20the=20same=20as=20the=20length=20on=20the=20provi?= =?UTF-8?q?ded=20array.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A test case for this is also added in the test class CPArrayTest --- Foundation/CPArray/_CPJavaScriptArray.j | 2 +- Tests/Foundation/CPArrayTest.j | 25 +++++++++++++++++++++++++ Tests/Foundation/CPMutableArrayTest.j | 15 +++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/Foundation/CPArray/_CPJavaScriptArray.j b/Foundation/CPArray/_CPJavaScriptArray.j index 0246790dd..d0ed91782 100644 --- a/Foundation/CPArray/_CPJavaScriptArray.j +++ b/Foundation/CPArray/_CPJavaScriptArray.j @@ -91,7 +91,7 @@ var concat = Array.prototype.concat, - (id)initWithObjects:(CPArray)objects count:(CPUInteger)aCount { if ([objects isKindOfClass:_CPJavaScriptArray]) - return slice.call(objects, 0); + return slice.call(objects, 0, aCount); var array = [], index = 0; diff --git a/Tests/Foundation/CPArrayTest.j b/Tests/Foundation/CPArrayTest.j index d3d1bb3b8..3b113fe00 100644 --- a/Tests/Foundation/CPArrayTest.j +++ b/Tests/Foundation/CPArrayTest.j @@ -11,6 +11,16 @@ return ConcreteArray; } +- (void)test_initWithObjects_count_ +{ + var arrayClass = [[self class] arrayClass], + array = [[arrayClass alloc] initWithObjects:[@"hello", @"bye", @"goodnight", @"seeya"] count:2]; + + [self assert:[array count] equals:2]; + [self assert:[array objectAtIndex:0] equals:@"hello"]; + [self assert:[array objectAtIndex:1] equals:@"bye"]; +} + - (void)test_containsObject_ { var arrayClass = [[self class] arrayClass], @@ -763,6 +773,21 @@ return self; } +- (id)initWithObjects:(CPArray)objects count:(CPUInteger)aCount +{ + self = [super init]; + + if (self) + { + array = []; + + for (var index = 0; index < aCount; ++index) + array.push([objects objectAtIndex:index]); + } + + return self; +} + - (CPUInteger)count { return array.length; diff --git a/Tests/Foundation/CPMutableArrayTest.j b/Tests/Foundation/CPMutableArrayTest.j index cdc75b7b4..03d8129b3 100644 --- a/Tests/Foundation/CPMutableArrayTest.j +++ b/Tests/Foundation/CPMutableArrayTest.j @@ -555,6 +555,21 @@ return self; } +- (id)initWithObjects:(CPArray)objects count:(CPUInteger)aCount +{ + self = [super init]; + + if (self) + { + array = []; + + for (var index = 0; index < aCount; ++index) + array.push([objects objectAtIndex:index]); + } + + return self; +} + - (CPUInteger)count { return array.length;