NEW: CPExpression +expressionForBlock:arguments:

An expression that returns the result of evaluating a block.

With tests.
This commit is contained in:
cacaodev
2015-12-27 15:44:36 +01:00
parent 0ac08456bc
commit 5ab10f4024
4 changed files with 129 additions and 0 deletions
+1
View File
@@ -28,3 +28,4 @@
@import "_CPAggregateExpression.j"
@import "_CPSetExpression.j"
@import "_CPSubqueryExpression.j"
@import "_CPBlockExpression.j"
@@ -0,0 +1,83 @@
/*
* _CPBlockExpression.j
*
* Created by cacaodev.
* Copyright 2015.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@import "_CPExpression.j"
@implementation _CPBlockExpression : CPExpression
{
Function _block;
CPArray _arguments;
}
- (id)initWithBlock:(Function)aBlock arguments:(CPArray)arguments
{
self = [super initWithExpressionType:CPBlockExpressionType];
if (self)
{
_block = aBlock;
_arguments = arguments;
}
return self;
}
- (BOOL)isEqual:(id)object
{
if (self === object)
return YES;
if (object === nil || object.isa !== self.isa || [object expressionBlock] !== _block || ![[object arguments] isEqual:_arguments])
return NO;
return YES;
}
- (Function)expressionBlock
{
return _block;
}
- (CPArray)arguments
{
return _arguments;
}
- (id)expressionValueWithObject:(id)object context:(CPDictionary)context
{
var args = [];
if (_arguments !== nil)
{
args = _arguments.map(function(expression){
return [expression expressionValueWithObject:object context:context];
});
}
return _block(object, args, context);
}
- (CPString)description
{
return [CPString stringWithFormat:@"Block(0x%@)", [CPString stringWithHash:[self UID]]];
}
@end
+25
View File
@@ -65,6 +65,10 @@ CPIntersectSetExpressionType = 8;
An expression that combines two nested expression results by set subtraction.
*/
CPMinusSetExpressionType = 9;
/*!
An expression that returns the result of evaluating a block.
*/
CPBlockExpressionType = 10;
/*!
@ingroup foundation
@@ -259,6 +263,27 @@ CPMinusSetExpressionType = 9;
return [[_CPSubqueryExpression alloc] initWithExpression:expression usingIteratorVariable:variable predicate:predicate];
}
/*!
Returns Creates an NSExpression object that will use the Block for evaluating objects.
@param aBlock The Block is applied to the object to be evaluated.
The Block takes three arguments and returns a value:
evaluatedObject
The object to be evaluated.
expressions
An array of predicate expressions that evaluates to a collection.
context
A dictionary that the expression can use to store temporary state for one predicate evaluation.
@discussion Note that context is mutable, and that it can only be accessed during the evaluation of the expression.
@param arguments An array containing NSExpression objects that will be used as parameters during the invocation of the block.
*/
+ (CPExpression)expressionForBlock:(Function)aBlock arguments:(CPArray)args
{
return [[_CPBlockExpression alloc] initWithBlock:aBlock arguments:args];
}
// Getting Information About an Expression
/*!
Returns the expression type for the receiver.
+20
View File
@@ -79,6 +79,13 @@
var expression_minusset = [CPExpression expressionForMinusSet:set with:array];
[self assertNotNull:expression_minusset message:"MinusSet Expression should not be nil"];
var expression_block = [CPExpression expressionForBlock:function(obj, args, bindings)
{
return obj;
} arguments:nil];
[self assertNotNull:expression_block message:"Block Expression should not be nil"];
}
- (void)testSetExpressionEvaluation
@@ -147,6 +154,19 @@
[self assertTrue:([eval isEqual:expected]) message:"'" + [expression predicateFormat] + "' result is "+ eval + "but should be " + expected];
}
- (void)testBlockExpressionEvaluation
{
var block = function(obj, args, bindings)
{
return [obj stringByAppendingString:[args componentsJoinedByString:"-"]];
};
var expression = [CPExpression expressionForBlock:block arguments:[[CPExpression expressionForConstantValue:"A"], [CPExpression expressionForConstantValue:"B"]]];
var eval = [expression expressionValueWithObject:"OBJ" context:@{}];
[self assertTrue:([eval isEqual:"OBJA-B"]) message:"'" + [expression description] + "' result is "+ eval + "but should be " + "OBJA-B"];
}
- (void)testOptions
{
var pred = [[CPComparisonPredicate alloc] initWithLeftExpression:[CPExpression expressionForConstantValue:@"àa"] rightExpression:[CPExpression expressionForConstantValue:@"aà"] modifier:CPDirectPredicateModifier type:CPLikePredicateOperatorType options:3];