diff options
author | Fabian Guerra Soto <fabian.guerra@mapbox.com> | 2019-03-15 16:10:27 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-15 16:10:27 -0700 |
commit | 7939ff213a98de055e93ddd893ccae40b82a7d16 (patch) | |
tree | f23371b62e4ce1e4964361a04c0b11a39ede8569 /platform/darwin | |
parent | c8cfdb1ced822711e772dfcc8f708b1a7a68b5fc (diff) | |
download | qtlocation-mapboxgl-7939ff213a98de055e93ddd893ccae40b82a7d16.tar.gz |
[ios, macos] Add format expression convenience methods support. (#14094)
Added the ios/macos format expression bindings.
The equivalent is mgl_attributed: or mgl_attributed({}) when making an expression using the expressionForFormat selector.
A new constructor called mgl_expressionForAttributedExpressions:(NSArray<NSExpression*>*)attributedExpressions was added to NSExpression+MGLAdditions category.
Updated the symbol style layer text test to use the bindings.
Updated the Predicates and Expressions.md and For Style Authors.md.ejs template with the bindings.
Diffstat (limited to 'platform/darwin')
-rw-r--r-- | platform/darwin/docs/guides/For Style Authors.md.ejs | 1 | ||||
-rw-r--r-- | platform/darwin/docs/guides/Predicates and Expressions.md | 16 | ||||
-rwxr-xr-x | platform/darwin/scripts/generate-style-code.js | 2 | ||||
-rw-r--r-- | platform/darwin/src/MGLAttributedExpression.h | 44 | ||||
-rw-r--r-- | platform/darwin/src/MGLAttributedExpression.m | 59 | ||||
-rw-r--r-- | platform/darwin/src/NSExpression+MGLAdditions.h | 10 | ||||
-rw-r--r-- | platform/darwin/src/NSExpression+MGLAdditions.mm | 68 | ||||
-rw-r--r-- | platform/darwin/test/MGLExpressionTests.mm | 99 | ||||
-rw-r--r-- | platform/darwin/test/MGLStyleLayerTests.mm.ejs | 13 | ||||
-rw-r--r-- | platform/darwin/test/MGLSymbolStyleLayerTests.mm | 9 |
10 files changed, 299 insertions, 22 deletions
diff --git a/platform/darwin/docs/guides/For Style Authors.md.ejs b/platform/darwin/docs/guides/For Style Authors.md.ejs index ead3b81ce6..cf0f79f419 100644 --- a/platform/darwin/docs/guides/For Style Authors.md.ejs +++ b/platform/darwin/docs/guides/For Style Authors.md.ejs @@ -403,6 +403,7 @@ In style specification | Method, function, or predicate type | Format string syn `zoom` | `NSExpression.zoomLevelVariableExpression` | `$zoomLevel` `heatmap-density` | `NSExpression.heatmapDensityVariableExpression` | `$heatmapDensity` `line-progress` | `NSExpression.lineProgressVariableExpression` | `$lineProgress` +`format` | `+[NSExpression mgl_expressionForAttributedExpressions:]` or `mgl_attributed:` | `mgl_attributed({x, y, z})` For operators that have no corresponding `NSExpression` symbol, use the `MGL_FUNCTION()` format string syntax. diff --git a/platform/darwin/docs/guides/Predicates and Expressions.md b/platform/darwin/docs/guides/Predicates and Expressions.md index 0bb26b3bfd..5f5d9a22a8 100644 --- a/platform/darwin/docs/guides/Predicates and Expressions.md +++ b/platform/darwin/docs/guides/Predicates and Expressions.md @@ -534,6 +534,22 @@ This function corresponds to the [`coalesce`](https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-coalesce) operator in the Mapbox Style Specification. +### `mgl_attributed:` + +<dl> +<dt>Selector:</dt> +<dd><code>mgl_attributed:</code></dd> +<dt>Format string syntax:</dt> +<dd><code>mgl_attributed({x, y, z})</code></dd> +</dl> + +Concatenates and returns the array of `MGLAttributedExpression` objects, for use +with the `MGLSymbolStyleLayer.text` property. + +This function corresponds to the +[`format`](https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-types-format) +operator in the Mapbox Style Specification. + ### `MGL_LET` <dl> diff --git a/platform/darwin/scripts/generate-style-code.js b/platform/darwin/scripts/generate-style-code.js index 704b0a29f4..a8bdec7865 100755 --- a/platform/darwin/scripts/generate-style-code.js +++ b/platform/darwin/scripts/generate-style-code.js @@ -107,7 +107,7 @@ global.objCTestValue = function (property, layerType, arraysAsStructs, indent) { // converts Formatted back to string. return layerType === 'string' ? `@"'${_.startCase(propertyName)}'"` : - `@"MGL_FUNCTION('format', '${_.startCase(propertyName)}', %@)", @{}`; + `@"${_.startCase(propertyName)}"`; case 'string': return `@"'${_.startCase(propertyName)}'"`; case 'enum': diff --git a/platform/darwin/src/MGLAttributedExpression.h b/platform/darwin/src/MGLAttributedExpression.h new file mode 100644 index 0000000000..aa5d51c66e --- /dev/null +++ b/platform/darwin/src/MGLAttributedExpression.h @@ -0,0 +1,44 @@ +#import "MGLFoundation.h" + +NS_ASSUME_NONNULL_BEGIN + +typedef NSString * MGLAttributedExpressionKey NS_EXTENSIBLE_STRING_ENUM; + +FOUNDATION_EXTERN MGL_EXPORT MGLAttributedExpressionKey const MGLFontNamesAttribute; +FOUNDATION_EXTERN MGL_EXPORT MGLAttributedExpressionKey const MGLFontSizeAttribute; + +/** + An `MGLAttributedExpression` object associates text formatting attibutes (such as font size or + font names) to an `NSExpression`. + */ +MGL_EXPORT +@interface MGLAttributedExpression : NSObject + +/** + The expression content of the receiver as `NSExpression`. + */ +@property (strong, nonatomic) NSExpression *expression; + +/** + The formatting attributes. + */ +@property (strong, nonatomic, readonly) NSDictionary<MGLAttributedExpressionKey, id> *attributes; + +/** + Returns an `MGLAttributedExpression` object initialized with an expression and no attribute information. + */ +- (instancetype)initWithExpression:(NSExpression *)expression; + +/** + Returns an `MGLAttributedExpression` object initialized with an expression and text format attributes. + */ +- (instancetype)initWithExpression:(NSExpression *)expression attributes:(nullable NSDictionary <MGLAttributedExpressionKey, id> *)attrs; + +/** + Creates an `MGLAttributedExpression` object initialized with an expression and the format attributes for font names and font size. + */ ++ (instancetype)attributedExpression:(NSExpression *)expression fontNames:(nullable NSArray<NSString*> *)fontNames fontSize:(nullable NSNumber *)fontSize; + +@end + +NS_ASSUME_NONNULL_END diff --git a/platform/darwin/src/MGLAttributedExpression.m b/platform/darwin/src/MGLAttributedExpression.m new file mode 100644 index 0000000000..715f74e42f --- /dev/null +++ b/platform/darwin/src/MGLAttributedExpression.m @@ -0,0 +1,59 @@ +#import "MGLAttributedExpression.h" +#import "MGLLoggingConfiguration_Private.h" + +const MGLAttributedExpressionKey MGLFontNamesAttribute = @"text-font"; +const MGLAttributedExpressionKey MGLFontSizeAttribute = @"font-scale"; + +@implementation MGLAttributedExpression + +- (instancetype)initWithExpression:(NSExpression *)expression { + self = [self initWithExpression:expression attributes:nil]; + return self; +} + ++ (instancetype)attributedExpression:(NSExpression *)expression fontNames:(nullable NSArray<NSString *> *)fontNames fontSize:(nullable NSNumber *)fontSize { + MGLAttributedExpression *attributedExpression; + + NSMutableDictionary *attrs = [NSMutableDictionary dictionary]; + + if (fontNames && fontNames.count > 0) { + attrs[MGLFontNamesAttribute] = fontNames; + } + + if (fontSize) { + attrs[MGLFontSizeAttribute] = fontSize; + } + + attributedExpression = [[self alloc] initWithExpression:expression attributes:attrs]; + return attributedExpression; +} + +- (instancetype)initWithExpression:(NSExpression *)expression attributes:(NSDictionary<MGLAttributedExpressionKey,id> *)attrs { + if (self = [super init]) + { + MGLLogInfo(@"Starting %@ initialization.", NSStringFromClass([self class])); + _expression = expression; + _attributes = attrs; + + MGLLogInfo(@"Finalizing %@ initialization.", NSStringFromClass([self class])); + } + return self; +} + +- (BOOL)isEqual:(id)object { + BOOL result = NO; + + if ([object isKindOfClass:[self class]]) { + MGLAttributedExpression *otherObject = object; + result = [self.expression isEqual:otherObject.expression] && + [_attributes isEqual:otherObject.attributes]; + } + + return result; +} + +- (NSString *)description { + return [NSString stringWithFormat:@"MGLAttributedExpression<Expression: %@ Attributes: %@>", self.expression, self.attributes]; +} + +@end diff --git a/platform/darwin/src/NSExpression+MGLAdditions.h b/platform/darwin/src/NSExpression+MGLAdditions.h index 1e6fd6fc46..a19ec1af2e 100644 --- a/platform/darwin/src/NSExpression+MGLAdditions.h +++ b/platform/darwin/src/NSExpression+MGLAdditions.h @@ -7,6 +7,8 @@ #import "MGLTypes.h" +@class MGLAttributedExpression; + NS_ASSUME_NONNULL_BEGIN typedef NSString *MGLExpressionInterpolationMode NS_TYPED_ENUM; @@ -150,6 +152,14 @@ FOUNDATION_EXTERN MGL_EXPORT const MGLExpressionInterpolationMode MGLExpressionI */ + (instancetype)mgl_expressionForMatchingExpression:(nonnull NSExpression *)inputExpression inDictionary:(nonnull NSDictionary<NSExpression *, NSExpression *> *)matchedExpressions defaultExpression:(nonnull NSExpression *)defaultExpression NS_SWIFT_NAME(init(forMGLMatchingKey:in:default:)); +/** + Returns an attributed function expression specifying an `MGLAttributedExpression` constant + expression array. + + @param attributedExpressions The `MGLAttributedExpression` constant expression array. + */ ++ (instancetype)mgl_expressionForAttributedExpressions:(nonnull NSArray<NSExpression *> *)attributedExpressions NS_SWIFT_NAME(init(forAttributedExpressions:)); + #pragma mark Concatenating String Expressions /** diff --git a/platform/darwin/src/NSExpression+MGLAdditions.mm b/platform/darwin/src/NSExpression+MGLAdditions.mm index 6aaba4dd90..c4e2908888 100644 --- a/platform/darwin/src/NSExpression+MGLAdditions.mm +++ b/platform/darwin/src/NSExpression+MGLAdditions.mm @@ -11,6 +11,7 @@ #import "NSPredicate+MGLAdditions.h" #import "NSValue+MGLStyleAttributeAdditions.h" #import "MGLVectorTileSource_Private.h" +#import "MGLAttributedExpression.h" #import <objc/runtime.h> @@ -75,6 +76,7 @@ const MGLExpressionInterpolationMode MGLExpressionInterpolationModeCubicBezier = INSTALL_METHOD(mgl_atan:); INSTALL_METHOD(mgl_tan:); INSTALL_METHOD(mgl_log2:); + INSTALL_METHOD(mgl_attributed:); // Install functions that resemble control structures, taking arbitrary // numbers of arguments. Vararg aftermarket functions need to be declared @@ -96,6 +98,12 @@ const MGLExpressionInterpolationMode MGLExpressionInterpolationModeCubicBezier = return [components componentsJoinedByString:@""]; } +- (NSString *)mgl_attributed:(NSArray<MGLAttributedExpression *> *)attributedExpressions { + [NSException raise:NSInvalidArgumentException + format:@"Text format expressions lack underlying Objective-C implementations."]; + return nil; +} + /** Rounds the given number to the nearest integer. If the number is halfway between two integers, this method rounds it away from zero. @@ -229,7 +237,6 @@ const MGLExpressionInterpolationMode MGLExpressionInterpolationModeCubicBezier = return nil; } - /** A placeholder for a catch-all method that evaluates an arbitrary number of arguments as an expression according to the Mapbox Style Specification’s @@ -591,6 +598,10 @@ const MGLExpressionInterpolationMode MGLExpressionInterpolationModeCubicBezier = arguments:optionsArray]; } ++ (instancetype)mgl_expressionForAttributedExpressions:(nonnull NSArray<NSExpression *> *)attributedExpressions { + return [NSExpression expressionForFunction:@"mgl_attributed:" arguments:attributedExpressions]; +} + - (instancetype)mgl_expressionByAppendingExpression:(nonnull NSExpression *)expression { NSExpression *subexpression = [NSExpression expressionForAggregate:@[self, expression]]; return [NSExpression expressionForFunction:@"mgl_join:" arguments:@[subexpression]]; @@ -860,6 +871,22 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) { return [NSExpression expressionForFunction:@"MGL_MATCH" arguments:optionsArray]; + } else if ([op isEqualToString:@"format"]) { + NSMutableArray *attributedExpressions = [NSMutableArray array]; + + for (NSUInteger index = 0; index < argumentObjects.count; index+=2) { + NSExpression *expression = [NSExpression expressionWithMGLJSONObject:argumentObjects[index]]; + NSMutableDictionary *attrs = [NSMutableDictionary dictionary]; + if ((index + 1) < argumentObjects.count) { + attrs = argumentObjects[index + 1]; + } + + MGLAttributedExpression *attributedExpression = [[MGLAttributedExpression alloc] initWithExpression:expression attributes:attrs]; + + [attributedExpressions addObject:[NSExpression expressionForConstantValue:attributedExpression]]; + } + return [NSExpression expressionForFunction:@"mgl_attributed:" arguments:attributedExpressions]; + } else if ([op isEqualToString:@"coalesce"]) { NSMutableArray *expressions = [NSMutableArray array]; for (id operand in argumentObjects) { @@ -971,6 +998,23 @@ 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]; + } + if (attributedExpression.attributes) { + [attributes addObject:attributedExpression.attributes]; + } else { + [attributes addObject:@{}]; + } + + return attributes; + } return self.constantValue; } @@ -1115,6 +1159,9 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) { } [NSException raise:NSInvalidArgumentException format:@"Casting expression to %@ not yet implemented.", type]; + } else if ([function isEqualToString:@"mgl_attributed:"]) { + return [self mgl_jsonFormatExpressionObject]; + } else if ([function isEqualToString:@"MGL_FUNCTION"] || [function isEqualToString:@"MGL_FUNCTION:"]) { NSExpression *firstOp = self.arguments.firstObject; @@ -1363,6 +1410,25 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) { return expressionObject; } +- (id)mgl_jsonFormatExpressionObject { + NSArray<NSExpression *> *attributedExpressions; + NSExpression *formatArray = self.arguments.firstObject; + + if ([formatArray respondsToSelector:@selector(constantValue)] && [formatArray.constantValue isKindOfClass:[NSArray class]]) { + attributedExpressions = (NSArray *)formatArray.constantValue; + } else { + attributedExpressions = self.arguments; + } + + NSMutableArray *expressionObject = [NSMutableArray arrayWithObjects:@"format", nil]; + + for (NSUInteger index = 0; index < attributedExpressions.count; index++) { + [expressionObject addObjectsFromArray:attributedExpressions[index].mgl_jsonExpressionObject]; + } + + return expressionObject; +} + #pragma mark Localization /** diff --git a/platform/darwin/test/MGLExpressionTests.mm b/platform/darwin/test/MGLExpressionTests.mm index bcc30e49fd..8b8a79f184 100644 --- a/platform/darwin/test/MGLExpressionTests.mm +++ b/platform/darwin/test/MGLExpressionTests.mm @@ -12,6 +12,7 @@ #else #import "NSColor+MGLAdditions.h" #endif +#import "MGLAttributedExpression.h" #define MGLAssertEqualValues(actual, expected, ...) \ XCTAssertTrue(actual.is<__typeof__(expected)>()); \ @@ -990,6 +991,87 @@ using namespace std::string_literals; } } +- (void)testFormatExpressionObject { + { + 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(compatibilityExpression, expression); + 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 mgl_expressionForAttributedExpressions:@[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); + } +} + - (void)testGenericExpressionObject { { NSExpression *expression = [NSExpression expressionWithFormat:@"MGL_FUNCTION('random', 1, 2, 3, 4, 5)"]; @@ -1002,23 +1084,6 @@ using namespace std::string_literals; XCTAssertThrowsSpecificNamed([expression expressionValueWithObject:nil context:nil], NSException, NSInvalidArgumentException); } { - NSExpression *expression = [NSExpression expressionWithFormat:@"MGL_FUNCTION('format', 'foo', %@, '\r', %@, 'biz', %@, 'bar', %@)", @{@"font-scale": @1.2}, @{}, @{@"font-scale": @1.0}, @{@"font-scale": @0.8}]; - NSArray *jsonExpression = @[@"format", @"foo", @{@"font-scale": @1.2}, @"\r", @{}, @"biz", @{@"font-scale": @1.0}, @"bar", @{@"font-scale": @0.8}]; - - XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression); - - NSExpression *encodedExpression = [NSExpression expressionWithMGLJSONObject:jsonExpression]; - - // Expressions encoded from a json create a different constant abstract type - // thus even tho knowing that the constant values are the same, the base - // class is not. This compares the resulting array which is type agnostic encoding. - for (NSUInteger index = 0; index < jsonExpression.count; index++) { - NSExpression *left = encodedExpression.mgl_jsonExpressionObject[index]; - NSExpression *right = expression.mgl_jsonExpressionObject[index]; - XCTAssertEqualObjects(left.mgl_jsonExpressionObject, right.mgl_jsonExpressionObject); - } - } - { NSArray *arguments = @[ MGLConstantExpression(@"one"), MGLConstantExpression(@1), [NSExpression expressionForVariable:@"one"], diff --git a/platform/darwin/test/MGLStyleLayerTests.mm.ejs b/platform/darwin/test/MGLStyleLayerTests.mm.ejs index ba878b8bbe..4a38070007 100644 --- a/platform/darwin/test/MGLStyleLayerTests.mm.ejs +++ b/platform/darwin/test/MGLStyleLayerTests.mm.ejs @@ -8,6 +8,9 @@ #import "MGLStyleLayerTests.h" #import "../../darwin/src/NSDate+MGLAdditions.h" +<% if (type === 'symbol') { -%> +#include "../../darwin/src/MGLAttributedExpression.h" +<% } -%> #import "MGLStyleLayer_Private.h" @@ -80,7 +83,13 @@ XCTAssertEqualObjects(layer.<%- objCName(property) %>, constantExpression, @"<%- objCName(property) %> should round-trip constant value expressions."); +<% if (property.type !== 'formatted') { -%> constantExpression = [NSExpression expressionWithFormat:<%- objCTestValue(property, type, false, 3) %>]; +<% } else { -%> + MGLAttributedExpression *attributedConstantExpression = [[MGLAttributedExpression alloc] initWithExpression:[NSExpression expressionWithFormat:<%- objCTestValue(property, 'string', true, 3) %>] + attributes:@{}]; + constantExpression = [NSExpression mgl_expressionForAttributedExpressions:@[[NSExpression expressionForConstantValue:attributedConstantExpression]]]; +<% } -%> NSExpression *functionExpression = [NSExpression expressionWithFormat:@"mgl_step:from:stops:($zoomLevel, %@, %@)", constantExpression, @{@18: constantExpression}]; layer.<%- objCName(property) %> = functionExpression; @@ -180,7 +189,9 @@ @"Setting <%- objCName(property) %> to a constant string with tokens should convert to an expression."); <% if (property.type === 'formatted') { -%> - NSExpression* tokenExpression = [NSExpression expressionWithFormat:@"MGL_FUNCTION('format', CAST(token, 'NSString'), %@)", @{}]; + MGLAttributedExpression *tokenAttibutedExpression = [[MGLAttributedExpression alloc] initWithExpression:[NSExpression expressionWithFormat:@"CAST(token, 'NSString')"] + attributes:@{}]; + NSExpression* tokenExpression = [NSExpression mgl_expressionForAttributedExpressions:@[[NSExpression expressionForConstantValue:tokenAttibutedExpression]]]; <% } else { -%> NSExpression* tokenExpression = [NSExpression expressionWithFormat:@"CAST(token, \"NSString\")"]; <% } -%> diff --git a/platform/darwin/test/MGLSymbolStyleLayerTests.mm b/platform/darwin/test/MGLSymbolStyleLayerTests.mm index f02c5d496e..2f4206a96b 100644 --- a/platform/darwin/test/MGLSymbolStyleLayerTests.mm +++ b/platform/darwin/test/MGLSymbolStyleLayerTests.mm @@ -3,6 +3,7 @@ #import "MGLStyleLayerTests.h" #import "../../darwin/src/NSDate+MGLAdditions.h" +#include "../../darwin/src/MGLAttributedExpression.h" #import "MGLStyleLayer_Private.h" @@ -1104,7 +1105,9 @@ XCTAssertEqualObjects(layer.text, constantExpression, @"text should round-trip constant value expressions."); - constantExpression = [NSExpression expressionWithFormat:@"MGL_FUNCTION('format', 'Text Field', %@)", @{}]; + MGLAttributedExpression *attributedConstantExpression = [[MGLAttributedExpression alloc] initWithExpression:[NSExpression expressionWithFormat:@"'Text Field'"] + attributes:@{}]; + constantExpression = [NSExpression mgl_expressionForAttributedExpressions:@[[NSExpression expressionForConstantValue:attributedConstantExpression]]]; NSExpression *functionExpression = [NSExpression expressionWithFormat:@"mgl_step:from:stops:($zoomLevel, %@, %@)", constantExpression, @{@18: constantExpression}]; layer.text = functionExpression; @@ -1140,7 +1143,9 @@ XCTAssertEqual(rawLayer->getTextField(), propertyValue, @"Setting text to a constant string with tokens should convert to an expression."); - NSExpression* tokenExpression = [NSExpression expressionWithFormat:@"MGL_FUNCTION('format', CAST(token, 'NSString'), %@)", @{}]; + MGLAttributedExpression *tokenAttibutedExpression = [[MGLAttributedExpression alloc] initWithExpression:[NSExpression expressionWithFormat:@"CAST(token, 'NSString')"] + attributes:@{}]; + NSExpression* tokenExpression = [NSExpression mgl_expressionForAttributedExpressions:@[[NSExpression expressionForConstantValue:tokenAttibutedExpression]]]; XCTAssertEqualObjects(layer.text, tokenExpression, @"Setting text to a constant string with tokens should convert to an expression."); } |