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
This commit is contained in:
cacaodev
2011-12-29 17:16:53 +01:00
parent 1ea95bf6e0
commit b39f509263
3 changed files with 27 additions and 2 deletions
+2 -2
View File
@@ -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);
}
}
+12
View File
@@ -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
+13
View File
@@ -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)