From b39f50926317c40964d281130ec229f5f73afa2b Mon Sep 17 00:00:00 2001 From: cacaodev Date: Thu, 29 Dec 2011 17:02:38 +0100 Subject: [PATCH] CPArray (CPPredicate): -filteredArrayUsingPredicate: and -filterUsingPredicate: Use CPArray methods (not js array) because the receiver can be a CPArray subclass. With tests. Fixes the bug reported here: https://groups.google.com/forum/?pli=1#!topic/objectivej/8q9hqbZZUv0 --- Foundation/CPPredicate/CPPredicate.j | 4 ++-- Tests/AppKit/CPArrayControllerTest.j | 12 ++++++++++++ Tests/Foundation/CPPredicateTest.j | 13 +++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/Foundation/CPPredicate/CPPredicate.j b/Foundation/CPPredicate/CPPredicate.j index b45fbe282..ecb64fb1f 100644 --- a/Foundation/CPPredicate/CPPredicate.j +++ b/Foundation/CPPredicate/CPPredicate.j @@ -210,7 +210,7 @@ for (; i < count; i++) { - var object = self[i]; + var object = [self objectAtIndex:i]; if ([predicate evaluateWithObject:object]) result.push(object); } @@ -224,7 +224,7 @@ while (count--) { - if (![predicate evaluateWithObject:self[count]]) + if (![predicate evaluateWithObject:[self objectAtIndex:count]]) splice(count, 1); } } diff --git a/Tests/AppKit/CPArrayControllerTest.j b/Tests/AppKit/CPArrayControllerTest.j index 0ecbfac0b..276854bab 100644 --- a/Tests/AppKit/CPArrayControllerTest.j +++ b/Tests/AppKit/CPArrayControllerTest.j @@ -547,6 +547,18 @@ [self assertTrue:[[arrayController arrangedObjects] count] > 0]; } +- (void)testArrangedObjectsWithPredicateFilteringAfterContentArrayBinding +{ + _contentArray = [self makeTestArray]; + + var arrayController = [[CPArrayController alloc] init]; + [arrayController bind:@"contentArray" toObject:self withKeyPath:@"_contentArray" options:nil]; + [arrayController setFilterPredicate:[CPPredicate predicateWithFormat:@"department.name BEGINSWITH 'Capp'"]]; + + var arrangedCount = [[arrayController arrangedObjects] count]; + [self assertTrue:(arrangedCount == 2) message:@"Count should be 2 and is " + arrangedCount]; +} + /** In a table with arranged contents like [1, 1, 2, 1], selecting the second '1' and removing it should result in [1, 2, 1] - not [2]. E.g. we don't use removeObject:1 but only remove the diff --git a/Tests/Foundation/CPPredicateTest.j b/Tests/Foundation/CPPredicateTest.j index a1ea70d53..569c445a4 100644 --- a/Tests/Foundation/CPPredicateTest.j +++ b/Tests/Foundation/CPPredicateTest.j @@ -3,6 +3,7 @@ @implementation CPPredicateTest : OJTestCase { CPDictionary dict; + CPArray simpleArray; } - (id)init @@ -26,6 +27,8 @@ d = [CPDictionary dictionaryWithObjects:objects forKeys:keys]; [dict setObject:d forKey:@"Record2"]; + + simpleArray = [CPArray arrayWithObjects:@"a", @"b", @"ac", @"bc"]; } return self; @@ -436,6 +439,16 @@ [self assert:pred1 equals:pred2]; } +- (void)testProxyArrayFiltering +{ + var proxyArray = [self mutableArrayValueForKey:@"simpleArray"], + predicate = [CPPredicate predicateWithFormat:@"SELF CONTAINS 'a'"]; + + var filtered = [proxyArray filteredArrayUsingPredicate:predicate]; + + [self assertTrue:([filtered count] == 2) message:@"Count should be 2 and is " + [filtered count]]; +} + @end @implementation CPObject (PredicateTesting)