diff options
author | Fabian Guerra <fabian.guerra@mapbox.com> | 2019-03-13 17:43:46 -0700 |
---|---|---|
committer | Fabian Guerra <fabian.guerra@mapbox.com> | 2019-03-14 12:59:45 -0700 |
commit | 89eca912f1e139ae1a3dea80c09aa850772c1193 (patch) | |
tree | c27ed6dd32af716c519d76c88189e6fed94549b3 | |
parent | 5056c6075aafefa6ccd399ccb63f3aad0024e1bd (diff) | |
download | qtlocation-mapboxgl-89eca912f1e139ae1a3dea80c09aa850772c1193.tar.gz |
[ios, macos] Improve format expression tests.
-rw-r--r-- | platform/darwin/src/NSExpression+MGLAdditions.mm | 38 | ||||
-rw-r--r-- | platform/darwin/test/MGLExpressionTests.mm | 62 |
2 files changed, 70 insertions, 30 deletions
diff --git a/platform/darwin/src/NSExpression+MGLAdditions.mm b/platform/darwin/src/NSExpression+MGLAdditions.mm index b53fe63d55..ec8ea813fb 100644 --- a/platform/darwin/src/NSExpression+MGLAdditions.mm +++ b/platform/darwin/src/NSExpression+MGLAdditions.mm @@ -879,7 +879,7 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) { MGLAttributedExpression *attributedExpression = [[MGLAttributedExpression alloc] initWithExpression:expression attributes:attrs]; - [attributedExpressions addObject:attributedExpression]; + [attributedExpressions addObject:[NSExpression expressionForConstantValue:attributedExpression]]; } NSExpression *subexpression = [NSExpression expressionForConstantValue:attributedExpressions]; return [NSExpression expressionForFunction:@"mgl_attributed:" arguments:@[subexpression]]; @@ -995,6 +995,19 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) { return @[@"literal", @[@(mglValue[0]), @(mglValue[1]), @(mglValue[2]), @(mglValue[3])]]; } } + if ([constantValue isKindOfClass:[MGLAttributedExpression class]]) { + MGLAttributedExpression *attributedExpression = (MGLAttributedExpression *)constantValue; + id jsonObject = attributedExpression.expression.mgl_jsonExpressionObject; + NSMutableArray *attributes = [NSMutableArray array]; + if ([jsonObject isKindOfClass:[NSArray class]]) { + [attributes addObjectsFromArray:jsonObject]; + } else { + [attributes addObject:jsonObject]; + } + [attributes addObject:attributedExpression.attributes]; + + return attributes; + } return self.constantValue; } @@ -1139,9 +1152,7 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) { } [NSException raise:NSInvalidArgumentException format:@"Casting expression to %@ not yet implemented.", type]; - } else if ([function isEqualToString:@"mgl_attributed:"] || - [function isEqualToString:@"MGL_FORMAT:"] || - [function isEqualToString:@"MGL_FORMAT"]) { + } else if ([function isEqualToString:@"mgl_attributed:"]) { return [self mgl_jsonFormatExpressionObject]; } else if ([function isEqualToString:@"MGL_FUNCTION"] || @@ -1393,26 +1404,19 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) { } - (id)mgl_jsonFormatExpressionObject { - BOOL isAftermarketFunction = [self.function hasPrefix:@"mgl_attributed:"]; - NSExpression *formatParameter; - NSArray<MGLAttributedExpression *> *attributedExpressions; + NSArray<NSExpression *> *attributedExpressions; + NSExpression *formatArray = self.arguments.firstObject; - if (isAftermarketFunction) { - formatParameter = self.arguments.firstObject; + if ([formatArray respondsToSelector:@selector(constantValue)] && [formatArray.constantValue isKindOfClass:[NSArray class]]) { + attributedExpressions = (NSArray *)formatArray.constantValue; } else { - formatParameter = self.operand; - } - - if ([formatParameter respondsToSelector:@selector(constantValue)] && [formatParameter.constantValue isKindOfClass:[NSArray class]]) { - attributedExpressions = (NSArray *)formatParameter.constantValue; + attributedExpressions = self.arguments; } NSMutableArray *expressionObject = [NSMutableArray arrayWithObjects:@"format", nil]; for (NSUInteger index = 0; index < attributedExpressions.count; index++) { - MGLAttributedExpression *attributedExpression = attributedExpressions[index]; - [expressionObject addObject:attributedExpression.expression.mgl_jsonExpressionObject]; - [expressionObject addObject:attributedExpression.attributes]; + [expressionObject addObjectsFromArray:attributedExpressions[index].mgl_jsonExpressionObject]; } return expressionObject; diff --git a/platform/darwin/test/MGLExpressionTests.mm b/platform/darwin/test/MGLExpressionTests.mm index 1152ed3c13..278d61765a 100644 --- a/platform/darwin/test/MGLExpressionTests.mm +++ b/platform/darwin/test/MGLExpressionTests.mm @@ -993,23 +993,59 @@ using namespace std::string_literals; - (void)testFormatExpressionObject { { - MGLAttributedExpression *attribute1 = [MGLAttributedExpression initWithExpression:[NSExpression expressionForConstantValue:@"foo"] - fontNames:nil - fontSize:@(1.2)]; - MGLAttributedExpression *attribute2 = [MGLAttributedExpression initWithExpression:[NSExpression expressionForConstantValue:@"biz"] - fontNames:nil - fontSize:@(1.0)]; - MGLAttributedExpression *attribute3 = [MGLAttributedExpression initWithExpression:[NSExpression expressionForConstantValue:@"bar"] - fontNames:nil - fontSize:@(0.8)]; - MGLAttributedExpression *attribute4 = [MGLAttributedExpression initWithExpression:[NSExpression expressionForConstantValue:@"\r"] - fontNames:@[] - fontSize:nil]; - NSExpression *expression = [NSExpression expressionWithFormat:@"mgl_attributed(%@)", @[attribute1, attribute4, attribute2, attribute3]]; + MGLAttributedExpression *attribute1 = [MGLAttributedExpression attributedExpression:[NSExpression expressionForConstantValue:@"foo"] + fontNames:nil + fontSize:@(1.2)]; + MGLAttributedExpression *attribute2 = [MGLAttributedExpression attributedExpression:[NSExpression expressionForConstantValue:@"biz"] + fontNames:nil + fontSize:@(1.0)]; + MGLAttributedExpression *attribute3 = [MGLAttributedExpression attributedExpression:[NSExpression expressionForConstantValue:@"bar"] + fontNames:nil + fontSize:@(0.8)]; + MGLAttributedExpression *attribute4 = [MGLAttributedExpression attributedExpression:[NSExpression expressionForConstantValue:@"\r"] + fontNames:@[] + fontSize:nil]; + NSExpression *expression = [NSExpression expressionWithFormat:@"mgl_attributed(%@)", @[MGLConstantExpression(attribute1), + MGLConstantExpression(attribute4), + MGLConstantExpression(attribute2), + MGLConstantExpression(attribute3)]]; NSArray *jsonExpression = @[@"format", @"foo", @{@"font-scale": @1.2}, @"\r", @{}, @"biz", @{@"font-scale": @1.0}, @"bar", @{@"font-scale": @0.8}]; XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression); XCTAssertEqualObjects([NSExpression expressionWithMGLJSONObject:jsonExpression], expression); } + { + MGLAttributedExpression *attribute1 = [MGLAttributedExpression attributedExpression:[NSExpression expressionForConstantValue:@"foo"] + fontNames:nil + fontSize:@(1.2)]; + MGLAttributedExpression *attribute2 = [MGLAttributedExpression attributedExpression:[NSExpression expressionForConstantValue:@"biz"] + fontNames:nil + fontSize:@(1.0)]; + MGLAttributedExpression *attribute3 = [MGLAttributedExpression attributedExpression:[NSExpression expressionForConstantValue:@"bar"] + fontNames:nil + fontSize:@(0.8)]; + MGLAttributedExpression *attribute4 = [MGLAttributedExpression attributedExpression:[NSExpression expressionForConstantValue:@"\n"] + fontNames:@[] + fontSize:nil]; + NSExpression *expression = [NSExpression expressionWithFormat:@"mgl_attributed(%@)", @[MGLConstantExpression(attribute1), + MGLConstantExpression(attribute4), + MGLConstantExpression(attribute2), + MGLConstantExpression(attribute3)]]; + NSArray *jsonExpression = @[@"format", @"foo", @{@"font-scale": @1.2}, @"\n", @{}, @"biz", @{@"font-scale": @1.0}, @"bar", @{@"font-scale": @0.8}]; + XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression); + XCTAssertEqualObjects([NSExpression expressionWithMGLJSONObject:jsonExpression], expression); + } + { + MGLAttributedExpression *attribute1 = [MGLAttributedExpression attributedExpression:[NSExpression expressionForConstantValue:@"foo"] + fontNames:nil + fontSize:@(1.2)]; + NSExpression *expression = [NSExpression expressionWithFormat:@"mgl_attributed(%@)", @[MGLConstantExpression(attribute1)]]; + + NSExpression *compatibilityExpression = [NSExpression expressionForFunction:@"mgl_attributed:" arguments:@[MGLConstantExpression(attribute1)]]; + NSArray *jsonExpression = @[@"format", @"foo", @{@"font-scale": @1.2}]; + XCTAssertEqualObjects(compatibilityExpression.mgl_jsonExpressionObject, expression.mgl_jsonExpressionObject); + XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression); + XCTAssertEqualObjects([NSExpression expressionWithMGLJSONObject:jsonExpression], expression); + } } - (void)testGenericExpressionObject { |