diff options
author | Minh Nguyễn <mxn@1ec5.org> | 2018-04-12 13:17:04 -0700 |
---|---|---|
committer | Minh Nguyễn <mxn@1ec5.org> | 2018-04-16 14:46:51 -0700 |
commit | 4f2367a16bd325c24c864cc8ce38aa198d4132da (patch) | |
tree | d045edcae84fae21c60cbda29cb0442965de915a | |
parent | e84dfa265e22f43ead495af070bd7fb21c020b79 (diff) | |
download | qtlocation-mapboxgl-4f2367a16bd325c24c864cc8ce38aa198d4132da.tar.gz |
[ios, macos] Test token replacement
Added tests for replacement of tokens with key paths in expressions. Fixed token replacement for raw strings in stop dictionaries. Avoid sticking a single string inside an mgl_join: call.
-rw-r--r-- | platform/darwin/src/NSExpression+MGLAdditions.mm | 6 | ||||
-rw-r--r-- | platform/darwin/test/MGLExpressionTests.mm | 43 |
2 files changed, 49 insertions, 0 deletions
diff --git a/platform/darwin/src/NSExpression+MGLAdditions.mm b/platform/darwin/src/NSExpression+MGLAdditions.mm index f1b98247d3..4c6e04ffc0 100644 --- a/platform/darwin/src/NSExpression+MGLAdditions.mm +++ b/platform/darwin/src/NSExpression+MGLAdditions.mm @@ -366,6 +366,9 @@ NS_ARRAY_OF(NSExpression *) *MGLCollectionByReplacingTokensWithKeyPaths(NS_ARRAY NS_DICTIONARY_OF(NSNumber *, NSExpression *) *MGLStopDictionaryByReplacingTokensWithKeyPaths(NS_DICTIONARY_OF(NSNumber *, NSExpression *) *stops) { __block NSMutableDictionary *upgradedStops; [stops enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull zoomLevel, NSExpression * _Nonnull value, BOOL * _Nonnull stop) { + if (![value isKindOfClass:[NSExpression class]]) { + value = [NSExpression expressionForConstantValue:value]; + } NSExpression *upgradedValue = value.mgl_expressionByReplacingTokensWithKeyPaths; if (upgradedValue != value) { if (!upgradedStops) { @@ -399,6 +402,9 @@ NS_DICTIONARY_OF(NSNumber *, NSExpression *) *MGLStopDictionaryByReplacingTokens [components addObject:[NSExpression expressionForKeyPath:token]]; } } + if (components.count == 1) { + return components.firstObject; + } return [NSExpression expressionForFunction:@"mgl_join:" arguments:@[[NSExpression expressionForAggregate:components]]]; } diff --git a/platform/darwin/test/MGLExpressionTests.mm b/platform/darwin/test/MGLExpressionTests.mm index b050c5f652..4d91c41bed 100644 --- a/platform/darwin/test/MGLExpressionTests.mm +++ b/platform/darwin/test/MGLExpressionTests.mm @@ -866,4 +866,47 @@ using namespace std::string_literals; } } +#pragma mark - Localization tests + +- (void)testTokenReplacement { + { + NSExpression *tokenized = MGLConstantExpression(@""); + NSExpression *expected = tokenized; + XCTAssertEqualObjects(tokenized.mgl_expressionByReplacingTokensWithKeyPaths, expected); + } + { + NSExpression *tokenized = MGLConstantExpression(@"{"); + NSExpression *expected = tokenized; + XCTAssertEqualObjects(tokenized.mgl_expressionByReplacingTokensWithKeyPaths, expected); + } + { + NSExpression *tokenized = MGLConstantExpression(@"{token"); + NSExpression *expected = tokenized; + XCTAssertEqualObjects(tokenized.mgl_expressionByReplacingTokensWithKeyPaths, expected); + } + { + NSExpression *tokenized = MGLConstantExpression(@"{token}"); + NSExpression *expected = [NSExpression expressionForKeyPath:@"token"]; + XCTAssertEqualObjects(tokenized.mgl_expressionByReplacingTokensWithKeyPaths, expected); + } + { + NSExpression *tokenized = MGLConstantExpression(@"{token} {token}"); + NSExpression *expected = [NSExpression expressionWithFormat:@"mgl_join({token, ' ', token})"]; + XCTAssertEqualObjects(tokenized.mgl_expressionByReplacingTokensWithKeyPaths, expected); + } + { + NSExpression *tokenized = [NSExpression expressionWithFormat:@"mgl_step:from:stops:($zoomLevel, '{short}', %@)", @{ + @1: MGLConstantExpression(@"{short}"), + @2: @"…", + @3: @"{long}", + }]; + NSExpression *expected = [NSExpression expressionWithFormat:@"mgl_step:from:stops:($zoomLevel, short, %@)", @{ + @1: [NSExpression expressionForKeyPath:@"short"], + @2: @"…", + @3: [NSExpression expressionForKeyPath:@"long"], + }]; + XCTAssertEqualObjects(tokenized.mgl_expressionByReplacingTokensWithKeyPaths, expected); + } +} + @end |