diff options
author | Fabian Guerra <fabian.guerra@mapbox.com> | 2018-04-13 18:08:34 -0400 |
---|---|---|
committer | Fabian Guerra <fabian.guerra@mapbox.com> | 2018-04-16 13:41:59 -0400 |
commit | e0dde382e148febb63e96388760f1ebeac778315 (patch) | |
tree | 932d13b2b956814bd8e1b9c6f1c6e40b85c1988b | |
parent | bce8c17a468b70cfe34d9f250451c5ec0440929a (diff) | |
download | qtlocation-mapboxgl-e0dde382e148febb63e96388760f1ebeac778315.tar.gz |
[ios, macos] Remove typed NSComparisonPredicate's comparable values.
-rw-r--r-- | platform/darwin/src/NSComparisonPredicate+MGLAdditions.mm | 45 | ||||
-rw-r--r-- | platform/darwin/test/MGLExpressionTests.mm | 4 | ||||
-rw-r--r-- | platform/darwin/test/MGLPredicateTests.mm | 56 |
3 files changed, 31 insertions, 74 deletions
diff --git a/platform/darwin/src/NSComparisonPredicate+MGLAdditions.mm b/platform/darwin/src/NSComparisonPredicate+MGLAdditions.mm index def3dec1a0..380215ff32 100644 --- a/platform/darwin/src/NSComparisonPredicate+MGLAdditions.mm +++ b/platform/darwin/src/NSComparisonPredicate+MGLAdditions.mm @@ -142,52 +142,9 @@ format:@"NSPredicateOperatorType:%lu is not supported.", (unsigned long)self.predicateOperatorType]; } if (op) { - id leftJSONExpressionObject = self.leftExpression.mgl_jsonExpressionObject; - id rightJSONExpressionObject = self.rightExpression.mgl_jsonExpressionObject; - - switch (self.predicateOperatorType) { - case NSLessThanPredicateOperatorType: - case NSLessThanOrEqualToPredicateOperatorType: - case NSGreaterThanPredicateOperatorType: - case NSGreaterThanOrEqualToPredicateOperatorType: { - leftJSONExpressionObject = [self mgl_typedJSONWithExpressionObject:self.leftExpression fallbackExpression:self.rightExpression]; - rightJSONExpressionObject = [self mgl_typedJSONWithExpressionObject:self.rightExpression fallbackExpression:self.leftExpression]; - break; - } - - default: - break; - } - - return @[op, leftJSONExpressionObject, rightJSONExpressionObject]; + return @[op, self.leftExpression.mgl_jsonExpressionObject, self.rightExpression.mgl_jsonExpressionObject]; } return nil; } -/** - 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; - if ((strcmp([number objCType], @encode(char)) == 0) || - (strcmp([number objCType], @encode(BOOL)) == 0)) { - type = @"boolean"; - } else { - type = @"number"; - } - } else if (expression.expressionType == NSConstantValueExpressionType && [expression.constantValue isKindOfClass:NSString.class]) { - type = @"string"; - } - - if (type) { - return @[type, expressionObject.mgl_jsonExpressionObject]; - } - - return expressionObject.mgl_jsonExpressionObject; -} - @end diff --git a/platform/darwin/test/MGLExpressionTests.mm b/platform/darwin/test/MGLExpressionTests.mm index 8eff4ae39e..b050c5f652 100644 --- a/platform/darwin/test/MGLExpressionTests.mm +++ b/platform/darwin/test/MGLExpressionTests.mm @@ -764,8 +764,8 @@ using namespace std::string_literals; @"case", @[ @"<", - @[@"number", @[@"get", @"area"]], - @[@"number", @80000] + @[@"get", @"area"], + @80000 ], @[@"get", @"abbr"], @[@"get", @"name_en"] diff --git a/platform/darwin/test/MGLPredicateTests.mm b/platform/darwin/test/MGLPredicateTests.mm index a14ea9aaeb..2fa3023563 100644 --- a/platform/darwin/test/MGLPredicateTests.mm +++ b/platform/darwin/test/MGLPredicateTests.mm @@ -247,40 +247,40 @@ namespace mbgl { mustRoundTrip:NO]; } { - NSPredicate *predicate = [NSPredicate predicateWithFormat:@"x < 5"]; - NSArray *jsonExpression = @[@"<", @[@"number", @[@"get", @"x"]], @[@"number", @5]]; + NSPredicate *predicate = [NSPredicate predicateWithFormat:@"CAST(x, 'NSNumber') < 5"]; + NSArray *jsonExpression = @[@"<", @[@"to-number", @[@"get", @"x"]], @5]; XCTAssertEqualObjects(predicate.mgl_jsonExpressionObject, jsonExpression); XCTAssertEqualObjects([NSPredicate mgl_predicateWithJSONObject:jsonExpression], predicate); [self testSymmetryWithPredicate:[NSPredicate mgl_predicateWithJSONObject:jsonExpression] mustRoundTrip:NO]; } { - NSPredicate *predicate = [NSPredicate predicateWithFormat:@"x > 5"]; - NSArray *jsonExpression = @[@">", @[@"number", @[@"get", @"x"]], @[@"number", @5]]; + NSPredicate *predicate = [NSPredicate predicateWithFormat:@"CAST(x, 'NSNumber') > 5"]; + NSArray *jsonExpression = @[@">", @[@"to-number", @[@"get", @"x"]], @5]; XCTAssertEqualObjects(predicate.mgl_jsonExpressionObject, jsonExpression); XCTAssertEqualObjects([NSPredicate mgl_predicateWithJSONObject:jsonExpression], predicate); [self testSymmetryWithPredicate:[NSPredicate mgl_predicateWithJSONObject:jsonExpression] mustRoundTrip:NO]; } { - NSPredicate *predicate = [NSPredicate predicateWithFormat:@"x <= 5"]; - NSArray *jsonExpression = @[@"<=", @[@"number", @[@"get", @"x"]], @[@"number", @5]]; + NSPredicate *predicate = [NSPredicate predicateWithFormat:@"CAST(x, 'NSNumber') <= 5"]; + NSArray *jsonExpression = @[@"<=", @[@"to-number", @[@"get", @"x"]], @5]; XCTAssertEqualObjects(predicate.mgl_jsonExpressionObject, jsonExpression); XCTAssertEqualObjects([NSPredicate mgl_predicateWithJSONObject:jsonExpression], predicate); [self testSymmetryWithPredicate:[NSPredicate mgl_predicateWithJSONObject:jsonExpression] mustRoundTrip:NO]; } { - NSPredicate *predicate = [NSPredicate predicateWithFormat:@"x >= 5"]; - NSArray *jsonExpression = @[@">=", @[@"number", @[@"get", @"x"]], @[@"number", @5]]; + NSPredicate *predicate = [NSPredicate predicateWithFormat:@"CAST(x, 'NSNumber') >= 5"]; + NSArray *jsonExpression = @[@">=", @[@"to-number", @[@"get", @"x"]], @5]; XCTAssertEqualObjects(predicate.mgl_jsonExpressionObject, jsonExpression); XCTAssertEqualObjects([NSPredicate mgl_predicateWithJSONObject:jsonExpression], predicate); [self testSymmetryWithPredicate:[NSPredicate mgl_predicateWithJSONObject:jsonExpression] mustRoundTrip:NO]; } { - NSPredicate *predicate = [NSPredicate predicateWithFormat:@"x > 'value'"]; - NSArray *jsonExpression = @[@">", @[@"string", @[@"get", @"x"]], @[@"string", @"value"]]; + NSPredicate *predicate = [NSPredicate predicateWithFormat:@"CAST(x, 'NSString') > 'value'"]; + NSArray *jsonExpression = @[@">", @[@"to-string", @[@"get", @"x"]], @"value"]; XCTAssertEqualObjects(predicate.mgl_jsonExpressionObject, jsonExpression); XCTAssertEqualObjects([NSPredicate mgl_predicateWithJSONObject:jsonExpression], predicate); [self testSymmetryWithPredicate:[NSPredicate mgl_predicateWithJSONObject:jsonExpression] @@ -357,40 +357,40 @@ namespace mbgl { mustRoundTrip:NO]; } { - NSPredicate *predicate = [NSPredicate predicateWithFormat:@"a < 'b'"]; - NSArray *jsonExpression = @[@"<", @[@"string", @[@"get", @"a"]], @[@"string", @"b"]]; + NSPredicate *predicate = [NSPredicate predicateWithFormat:@"CAST(a, 'NSString') < 'b'"]; + NSArray *jsonExpression = @[@"<", @[@"to-string", @[@"get", @"a"]], @"b"]; XCTAssertEqualObjects(predicate.mgl_jsonExpressionObject, jsonExpression); XCTAssertEqualObjects([NSPredicate mgl_predicateWithJSONObject:jsonExpression], predicate); [self testSymmetryWithPredicate:[NSPredicate mgl_predicateWithJSONObject:jsonExpression] mustRoundTrip:NO]; } { - NSPredicate *predicate = [NSPredicate predicateWithFormat:@"a <= 'b'"]; - NSArray *jsonExpression = @[@"<=", @[@"string", @[@"get", @"a"]], @[@"string", @"b"]]; + NSPredicate *predicate = [NSPredicate predicateWithFormat:@"CAST(a, 'NSString') <= 'b'"]; + NSArray *jsonExpression = @[@"<=", @[@"to-string", @[@"get", @"a"]], @"b"]; XCTAssertEqualObjects(predicate.mgl_jsonExpressionObject, jsonExpression); XCTAssertEqualObjects([NSPredicate mgl_predicateWithJSONObject:jsonExpression], predicate); [self testSymmetryWithPredicate:[NSPredicate mgl_predicateWithJSONObject:jsonExpression] mustRoundTrip:NO]; } { - NSPredicate *predicate = [NSPredicate predicateWithFormat:@"a > 'b'"]; - NSArray *jsonExpression = @[@">", @[@"string", @[@"get", @"a"]], @[@"string", @"b"]]; + NSPredicate *predicate = [NSPredicate predicateWithFormat:@"CAST(a, 'NSString') > 'b'"]; + NSArray *jsonExpression = @[@">", @[@"to-string", @[@"get", @"a"]], @"b"]; XCTAssertEqualObjects(predicate.mgl_jsonExpressionObject, jsonExpression); XCTAssertEqualObjects([NSPredicate mgl_predicateWithJSONObject:jsonExpression], predicate); [self testSymmetryWithPredicate:[NSPredicate mgl_predicateWithJSONObject:jsonExpression] mustRoundTrip:NO]; } { - NSPredicate *predicate = [NSPredicate predicateWithFormat:@"a >= 'b'"]; - NSArray *jsonExpression = @[@">=", @[@"string", @[@"get", @"a"]], @[@"string", @"b"]]; + NSPredicate *predicate = [NSPredicate predicateWithFormat:@"CAST(a, 'NSString') >= 'b'"]; + NSArray *jsonExpression = @[@">=", @[@"to-string", @[@"get", @"a"]], @"b"]; XCTAssertEqualObjects(predicate.mgl_jsonExpressionObject, jsonExpression); XCTAssertEqualObjects([NSPredicate mgl_predicateWithJSONObject:jsonExpression], predicate); [self testSymmetryWithPredicate:[NSPredicate mgl_predicateWithJSONObject:jsonExpression] mustRoundTrip:NO]; } { - NSPredicate *predicate = [NSPredicate predicateWithFormat:@"a BETWEEN {'b', 'z'}"]; - NSArray *jsonExpression =@[@"all", @[@"<=", @[@"string", @"b"], @[@"string", @[@"get", @"a"]]], @[@"<=", @[@"string", @[@"get", @"a"]], @[@"string", @"z"]]]; + NSPredicate *predicate = [NSPredicate predicateWithFormat:@"CAST(a, 'NSString') BETWEEN {'b', 'z'}"]; + NSArray *jsonExpression =@[@"all", @[@"<=", @"b", @[@"to-string", @[@"get", @"a"]]], @[@"<=", @[@"to-string", @[@"get", @"a"]], @"z"]]; XCTAssertEqualObjects(predicate.mgl_jsonExpressionObject, jsonExpression); XCTAssertEqualObjects([NSPredicate mgl_predicateWithJSONObject:jsonExpression], predicate); [self testSymmetryWithPredicate:[NSPredicate mgl_predicateWithJSONObject:jsonExpression] @@ -398,33 +398,33 @@ namespace mbgl { } { NSExpression *limits = [NSExpression expressionForAggregate:@[[NSExpression expressionForConstantValue:@10], [NSExpression expressionForConstantValue:@100]]]; - NSPredicate *predicate = [NSPredicate predicateWithFormat:@"x BETWEEN %@", limits]; - NSArray *jsonExpression = @[@"all", @[@">=", @[@"number", @[@"get", @"x"]], @[@"number", @10]], @[@"<=", @[@"number", @[@"get", @"x"]], @[@"number", @100]]]; + NSPredicate *predicate = [NSPredicate predicateWithFormat:@"CAST(x, 'NSNumber') BETWEEN %@", limits]; + NSArray *jsonExpression = @[@"all", @[@">=", @[@"to-number", @[@"get", @"x"]], @10], @[@"<=", @[@"to-number", @[@"get", @"x"]], @100]]; XCTAssertEqualObjects([NSPredicate mgl_predicateWithJSONObject:jsonExpression], predicate); [self testSymmetryWithPredicate:[NSPredicate mgl_predicateWithJSONObject:jsonExpression] mustRoundTrip:NO]; } { - NSArray *expected = @[@"all", @[@"<=", @[@"number", @10], @[@"number", @[@"get", @"x"]]], @[@"<=", @[@"number", @[@"get", @"x"]], @[@"number", @100]]]; + NSArray *expected = @[@"all", @[@"<=", @10, @[@"to-number", @[@"get", @"x"]]], @[@"<=", @[@"to-number", @[@"get", @"x"]], @100]]; NSExpression *limits = [NSExpression expressionForAggregate:@[[NSExpression expressionForConstantValue:@10], [NSExpression expressionForConstantValue:@100]]]; - NSPredicate *predicate = [NSPredicate predicateWithFormat:@"x BETWEEN %@", limits]; + NSPredicate *predicate = [NSPredicate predicateWithFormat:@"CAST(x, 'NSNumber') BETWEEN %@", limits]; XCTAssertEqualObjects(predicate.mgl_jsonExpressionObject, expected); XCTAssertEqualObjects([NSPredicate mgl_predicateWithJSONObject:expected], predicate); [self testSymmetryWithPredicate:[NSPredicate mgl_predicateWithJSONObject:expected] mustRoundTrip:NO]; } { - NSArray *expected = @[@"all", @[@"<=", @[@"number", @10], @[@"number", @[@"get", @"x"]]], @[@">=", @[@"number", @100], @[@"number", @[@"get", @"x"]]]]; + NSArray *expected = @[@"all", @[@"<=", @10, @[@"to-number", @[@"get", @"x"]]], @[@">=", @100, @[@"to-number", @[@"get", @"x"]]]]; NSExpression *limits = [NSExpression expressionForAggregate:@[[NSExpression expressionForConstantValue:@10], [NSExpression expressionForConstantValue:@100]]]; - NSPredicate *predicate = [NSPredicate predicateWithFormat:@"x BETWEEN %@", limits]; + NSPredicate *predicate = [NSPredicate predicateWithFormat:@"CAST(x, 'NSNumber') BETWEEN %@", limits]; XCTAssertEqualObjects([NSPredicate mgl_predicateWithJSONObject:expected], predicate); [self testSymmetryWithPredicate:[NSPredicate mgl_predicateWithJSONObject:expected] mustRoundTrip:NO]; } { - NSArray *expected = @[@"all", @[@">=", @[@"number", @[@"get", @"x"]], @[@"number", @10]], @[@">=", @[@"number", @100], @[@"number", @[@"get", @"x"]]]]; + NSArray *expected = @[@"all", @[@">=", @[@"to-number", @[@"get", @"x"]], @10], @[@">=", @100, @[@"to-number", @[@"get", @"x"]]]]; NSExpression *limits = [NSExpression expressionForAggregate:@[[NSExpression expressionForConstantValue:@10], [NSExpression expressionForConstantValue:@100]]]; - NSPredicate *predicate = [NSPredicate predicateWithFormat:@"x BETWEEN %@", limits]; + NSPredicate *predicate = [NSPredicate predicateWithFormat:@"CAST(x, 'NSNumber') BETWEEN %@", limits]; XCTAssertEqualObjects([NSPredicate mgl_predicateWithJSONObject:expected], predicate); [self testSymmetryWithPredicate:[NSPredicate mgl_predicateWithJSONObject:expected] mustRoundTrip:NO]; |