Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[ios, macos] Re-introduce Filter tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-guerra committed Apr 13, 2018
1 parent f8cd773 commit 44f8507
Show file tree
Hide file tree
Showing 5 changed files with 285 additions and 311 deletions.
22 changes: 13 additions & 9 deletions platform/darwin/src/NSComparisonPredicate+MGLAdditions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -142,30 +142,34 @@ - (id)mgl_jsonExpressionObject {
format:@"NSPredicateOperatorType:%lu is not supported.", (unsigned long)self.predicateOperatorType];
}
if (op) {
id leftExpression = self.leftExpression.mgl_jsonExpressionObject;
id rightExpression = self.rightExpression.mgl_jsonExpressionObject;
id leftJSONExpressionObject = self.leftExpression.mgl_jsonExpressionObject;
id rightJSONExpressionObject = self.rightExpression.mgl_jsonExpressionObject;

switch (self.predicateOperatorType) {
case NSLessThanPredicateOperatorType:
case NSLessThanOrEqualToPredicateOperatorType:
case NSGreaterThanPredicateOperatorType:
case NSGreaterThanOrEqualToPredicateOperatorType: {
leftExpression = [self mgl_expressionWithExpressionType:self.leftExpression fallbackExpression:self.rightExpression];
rightExpression = [self mgl_expressionWithExpressionType:self.rightExpression fallbackExpression:self.leftExpression];
leftJSONExpressionObject = [self mgl_typedJSONWithExpressionObject:self.leftExpression fallbackExpression:self.rightExpression];
rightJSONExpressionObject = [self mgl_typedJSONWithExpressionObject:self.rightExpression fallbackExpression:self.leftExpression];
break;
}

default:
break;
}

return @[op, leftExpression, rightExpression];
return @[op, leftJSONExpressionObject, rightJSONExpressionObject];
}
return nil;
}

- (id)mgl_expressionWithExpressionType:(NSExpression *)expressionElement fallbackExpression:(NSExpression *)fallbackExpression {
NSExpression *expression = expressionElement.expressionType == NSConstantValueExpressionType ? expressionElement : fallbackExpression;
/**
Infers the expressionObject type if it can not then tries to infer the type using fallbackExpression.
ExpressionFilters of type >, >=, <, <= requires that each compared value provides its type.
*/
- (id)mgl_typedJSONWithExpressionObject:(NSExpression *)expressionObject fallbackExpression:(NSExpression *)fallbackExpression {
NSExpression *expression = expressionObject.expressionType == NSConstantValueExpressionType ? expressionObject : fallbackExpression;
NSString *type;
if (expression.expressionType == NSConstantValueExpressionType && [expression.constantValue isKindOfClass:NSNumber.class]) {
NSNumber *number = (NSNumber *)expression.constantValue;
Expand All @@ -180,10 +184,10 @@ - (id)mgl_expressionWithExpressionType:(NSExpression *)expressionElement fallbac
}

if (type) {
return @[type, expressionElement.mgl_jsonExpressionObject];
return @[type, expressionObject.mgl_jsonExpressionObject];
}

return expressionElement.mgl_jsonExpressionObject;
return expressionObject.mgl_jsonExpressionObject;
}

@end
8 changes: 2 additions & 6 deletions platform/darwin/src/NSExpression+MGLAdditions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -551,11 +551,7 @@ + (instancetype)expressionWithMGLJSONObject:(id)object {
return [NSExpression expressionForAggregate:MGLSubexpressionsWithJSONObjects(argumentObjects.firstObject)];
}
return [NSExpression expressionWithMGLJSONObject:argumentObjects.firstObject];
} else if ([op isEqualToString:@"number"] ||
[op isEqualToString:@"string"] ||
[op isEqualToString:@"boolean"]) {
return [NSExpression mgl_expressionWithJSONObject:argumentObjects.firstObject];
} else if ([op isEqualToString:@"to-boolean"]) {
} else if ([op isEqualToString:@"to-boolean"] || [op isEqualToString:@"boolean"]) {
NSExpression *operand = [NSExpression expressionWithMGLJSONObject:argumentObjects.firstObject];
return [NSExpression expressionForFunction:operand selectorName:@"boolValue" arguments:@[]];
} else if ([op isEqualToString:@"to-number"] || [op isEqualToString:@"number"]) {
Expand All @@ -566,7 +562,7 @@ + (instancetype)expressionWithMGLJSONObject:(id)object {
argumentObjects = [argumentObjects subarrayWithRange:NSMakeRange(1, argumentObjects.count - 1)];
NSArray *subexpressions = MGLSubexpressionsWithJSONObjects(argumentObjects);
return [NSExpression expressionForFunction:operand selectorName:@"mgl_numberWithFallbackValues:" arguments:subexpressions];
} else if ([op isEqualToString:@"to-string"]) {
} else if ([op isEqualToString:@"to-string"] || [op isEqualToString:@"string"]) {
NSExpression *operand = [NSExpression expressionWithMGLJSONObject:argumentObjects.firstObject];
return [NSExpression expressionWithFormat:@"CAST(%@, 'NSString')", operand];
} else if ([op isEqualToString:@"get"]) {
Expand Down
8 changes: 5 additions & 3 deletions platform/darwin/src/NSPredicate+MGLAdditions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#import "MGLValueEvaluator.h"
#import "MGLStyleValue_Private.h"

#include <mbgl/style/conversion/filter.hpp>

class FilterEvaluator {
public:

Expand Down Expand Up @@ -208,14 +210,14 @@ @implementation NSPredicate (MGLAdditions)
{
mbgl::style::conversion::Error valueError;
NSArray *jsonObject = self.mgl_jsonExpressionObject;
auto value = mbgl::style::conversion::convert<std::unique_ptr<mbgl::style::expression::Expression>>(mbgl::style::conversion::makeConvertible(jsonObject), valueError, mbgl::style::expression::type::Boolean);
mbgl::style::ExpressionFilter filter;
auto value = mbgl::style::conversion::convert<mbgl::style::Filter>(mbgl::style::conversion::makeConvertible(jsonObject), valueError);

if (!value) {
[NSException raise:NSInvalidArgumentException
format:@"Invalid filter value: %@", @(valueError.message.c_str())];
return {};
}
filter.expression = std::move(*value);
mbgl::style::Filter filter = std::move(*value);

return filter;
}
Expand Down
6 changes: 2 additions & 4 deletions platform/darwin/test/MGLExpressionTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,6 @@ - (void)testArithmeticExpressionObject {
{
NSExpression *expression = [NSExpression expressionForFunction:@"ceiling:" arguments:@[MGLConstantExpression(@1.5)]];
NSArray *jsonExpression = @[@"ceil", @1.5];
NSArray *jsonExpression = @[@"+", jsonTruncation, @[@"case", @[@">", @[@"number", @[@"%", @1.5, @1]], @[@"number", @0]], @1, @0]];
XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression);
XCTAssertEqualObjects([expression expressionValueWithObject:nil context:nil], @2);
}
Expand All @@ -407,7 +406,6 @@ - (void)testArithmeticExpressionObject {
{
NSExpression *expression = [NSExpression expressionForFunction:@"ceiling:" arguments:@[MGLConstantExpression(@2)]];
NSArray *jsonExpression = @[@"ceil", @2];
NSArray *jsonExpression = @[@"+", jsonTruncation, @[@"case", @[@">", @[@"number", @[@"%", @2, @1]], @[@"number", @0]], @1, @0]];
XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression);
XCTAssertEqualObjects([expression expressionValueWithObject:nil context:nil], @2);
}
Expand Down Expand Up @@ -766,8 +764,8 @@ - (void)testConditionalExpressionObject {
@"case",
@[
@"<",
@[@"get", @"area"],
@80000
@[@"number", @[@"get", @"area"]],
@[@"number", @80000]
],
@[@"get", @"abbr"],
@[@"get", @"name_en"]
Expand Down
Loading

0 comments on commit 44f8507

Please sign in to comment.