FIXED: Subquery expressions were ignoring evaluation context

When the predicate part of a subquery expression was containing
variables, the substitution was ignored.
Now, the subpredicate can contain variables that will be substituted
when calling evaluateWithObject:substitutionVariables:.

Added expression test and predicate parsing test.
This commit is contained in:
cacaodev
2015-12-23 18:49:07 +01:00
parent 51a88a23ac
commit 80a3549ef5
2 changed files with 16 additions and 14 deletions
+12 -10
View File
@@ -47,23 +47,25 @@
_collection = collection; _collection = collection;
_variableExpression = variableExpression; _variableExpression = variableExpression;
} }
return self; return self;
} }
- (id)expressionValueWithObject:(id)object context:(id)context - (id)expressionValueWithObject:(id)object context:(id)aContext
{ {
var collection = [_collection expressionValueWithObject:object context:context], var collection = [_collection expressionValueWithObject:object context:aContext],
count = [collection count],
result = [CPArray array], result = [CPArray array],
bindings = @{ [self variable]: [CPExpression expressionForEvaluatedObject] }, variable = [self variable],
i = 0; context = aContext || @{};
for (; i < count; i++) if ([context objectForKey:variable] == nil)
[context setObject:[CPExpression expressionForEvaluatedObject] forKey:variable];
[collection enumerateObjectsUsingBlock:function(exp, idx, stop)
{ {
var item = [collection objectAtIndex:i]; if ([_subpredicate evaluateWithObject:exp substitutionVariables:context])
if ([_subpredicate evaluateWithObject:item substitutionVariables:bindings]) [result addObject:exp];
[result addObject:item]; }];
}
return result; return result;
} }
+4 -4
View File
@@ -139,10 +139,10 @@
{ {
var collection = [CPExpression expressionForKeyPath:@"Record1.Children"], var collection = [CPExpression expressionForKeyPath:@"Record1.Children"],
iteratorVariable = @"x", iteratorVariable = @"x",
predicate = [CPPredicate predicateWithFormat:@"$x BEGINSWITH 'Kid'"]; predicate = [CPPredicate predicateWithFormat:@"$x BEGINSWITH $KidVariable"];
var expression = [CPExpression expressionForSubquery:collection usingIteratorVariable:iteratorVariable predicate:predicate], var expression = [CPExpression expressionForSubquery:collection usingIteratorVariable:iteratorVariable predicate:predicate],
eval = [expression expressionValueWithObject:dict context:nil], eval = [expression expressionValueWithObject:dict context:@{"KidVariable":[CPExpression expressionForConstantValue:"Kid"]}],
expected = [CPArray arrayWithObjects:"Kid1", "Kid2"]; expected = [CPArray arrayWithObjects:"Kid1", "Kid2"];
[self assertTrue:([eval isEqual:expected]) message:"'" + [expression predicateFormat] + "' result is "+ eval + "but should be " + expected]; [self assertTrue:([eval isEqual:expected]) message:"'" + [expression predicateFormat] + "' result is "+ eval + "but should be " + expected];
} }
@@ -369,8 +369,8 @@
[self assertTrue:[predicate evaluateWithObject:nil] message:"Predicate " + predicate + " should be TRUE"]; [self assertTrue:[predicate evaluateWithObject:nil] message:"Predicate " + predicate + " should be TRUE"];
// TEST Subquery -- This means: search people who have 2 boys. // TEST Subquery -- This means: search people who have 2 boys.
predicate = [CPPredicate predicateWithFormat: @"SUBQUERY(Record1.Children, $x, $x BEGINSWITH 'Kid')[SIZE] = 2"]; predicate = [CPPredicate predicateWithFormat: @"SUBQUERY(Record1.Children, $x, $x BEGINSWITH $Begining)[SIZE] = 2"];
[self assertTrue:[predicate evaluateWithObject:dict] message:"Predicate " + predicate + " should evaluate to TRUE"]; [self assertTrue:[predicate evaluateWithObject:dict substitutionVariables:@{"Begining":"Kid"}] message:"Predicate " + predicate + " should evaluate to TRUE"];
// Test Set expressions // Test Set expressions
// Parsing is ok but the evaluation of this predicate will return NO because: // Parsing is ok but the evaluation of this predicate will return NO because: