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

Commit

Permalink
[darwin] Fix image roundrip conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
alexshalamov committed Nov 7, 2019
1 parent ec2f86f commit b51b959
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 18 deletions.
5 changes: 4 additions & 1 deletion platform/darwin/scripts/generate-style-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,15 @@ global.objCTestValue = function (property, layerType, arraysAsStructs, indent) {
case 'number':
return '@"1"';
case 'formatted':
case 'resolvedImage':
// Special 'string' case to handle constant expression text-field that automatically
// converts Formatted back to string.
return layerType === 'string' ?
`@"'${_.startCase(propertyName)}'"` :
`@"${_.startCase(propertyName)}"`;
case 'resolvedImage':
return layerType === 'string' ?
`@"${_.startCase(propertyName)}"` :
`@"MGL_FUNCTION('image', '${_.startCase(propertyName)}')"`;
case 'string':
return `@"'${_.startCase(propertyName)}'"`;
case 'enum':
Expand Down
1 change: 1 addition & 0 deletions platform/darwin/src/NSExpression+MGLAdditions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,7 @@ - (id)mgl_jsonExpressionObject {

return expressionObject;
}

return self.arguments.mgl_jsonExpressionObject;
} else if (op == [MGLColor class] && [function isEqualToString:@"colorWithRed:green:blue:alpha:"]) {
NSArray *arguments = self.arguments.mgl_jsonExpressionObject;
Expand Down
6 changes: 3 additions & 3 deletions platform/darwin/test/MGLBackgroundStyleLayerTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -140,22 +140,22 @@ - (void)testProperties {
@"background-pattern should be unset initially.");
NSExpression *defaultExpression = layer.backgroundPattern;

NSExpression *constantExpression = [NSExpression expressionWithFormat:@"Background Pattern"];
NSExpression *constantExpression = [NSExpression expressionForConstantValue:@"Background Pattern"];
layer.backgroundPattern = constantExpression;
mbgl::style::PropertyValue<mbgl::style::expression::Image> propertyValue = { "Background Pattern" };
XCTAssertEqual(rawLayer->getBackgroundPattern(), propertyValue,
@"Setting backgroundPattern to a constant value expression should update background-pattern.");
XCTAssertEqualObjects(layer.backgroundPattern, constantExpression,
@"backgroundPattern should round-trip constant value expressions.");

constantExpression = [NSExpression expressionWithFormat:@"Background Pattern"];
constantExpression = [NSExpression expressionWithFormat:@"MGL_FUNCTION('image', 'Background Pattern')"];
NSExpression *functionExpression = [NSExpression expressionWithFormat:@"mgl_step:from:stops:($zoomLevel, %@, %@)", constantExpression, @{@18: constantExpression}];
layer.backgroundPattern = functionExpression;

{
using namespace mbgl::style::expression::dsl;
propertyValue = mbgl::style::PropertyExpression<mbgl::style::expression::Image>(
step(zoom(), literal("Background Pattern"), 18.0, literal("Background Pattern"))
step(zoom(), image(literal("Background Pattern")), 18.0, image(literal("Background Pattern")))
);
}

Expand Down
6 changes: 3 additions & 3 deletions platform/darwin/test/MGLFillExtrusionStyleLayerTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -386,22 +386,22 @@ - (void)testProperties {
@"fill-extrusion-pattern should be unset initially.");
NSExpression *defaultExpression = layer.fillExtrusionPattern;

NSExpression *constantExpression = [NSExpression expressionWithFormat:@"Fill Extrusion Pattern"];
NSExpression *constantExpression = [NSExpression expressionForConstantValue:@"Fill Extrusion Pattern"];
layer.fillExtrusionPattern = constantExpression;
mbgl::style::PropertyValue<mbgl::style::expression::Image> propertyValue = { "Fill Extrusion Pattern" };
XCTAssertEqual(rawLayer->getFillExtrusionPattern(), propertyValue,
@"Setting fillExtrusionPattern to a constant value expression should update fill-extrusion-pattern.");
XCTAssertEqualObjects(layer.fillExtrusionPattern, constantExpression,
@"fillExtrusionPattern should round-trip constant value expressions.");

constantExpression = [NSExpression expressionWithFormat:@"Fill Extrusion Pattern"];
constantExpression = [NSExpression expressionWithFormat:@"MGL_FUNCTION('image', 'Fill Extrusion Pattern')"];
NSExpression *functionExpression = [NSExpression expressionWithFormat:@"mgl_step:from:stops:($zoomLevel, %@, %@)", constantExpression, @{@18: constantExpression}];
layer.fillExtrusionPattern = functionExpression;

{
using namespace mbgl::style::expression::dsl;
propertyValue = mbgl::style::PropertyExpression<mbgl::style::expression::Image>(
step(zoom(), literal("Fill Extrusion Pattern"), 18.0, literal("Fill Extrusion Pattern"))
step(zoom(), image(literal("Fill Extrusion Pattern")), 18.0, image(literal("Fill Extrusion Pattern")))
);
}

Expand Down
6 changes: 3 additions & 3 deletions platform/darwin/test/MGLFillStyleLayerTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -333,22 +333,22 @@ - (void)testProperties {
@"fill-pattern should be unset initially.");
NSExpression *defaultExpression = layer.fillPattern;

NSExpression *constantExpression = [NSExpression expressionWithFormat:@"Fill Pattern"];
NSExpression *constantExpression = [NSExpression expressionForConstantValue:@"Fill Pattern"];
layer.fillPattern = constantExpression;
mbgl::style::PropertyValue<mbgl::style::expression::Image> propertyValue = { "Fill Pattern" };
XCTAssertEqual(rawLayer->getFillPattern(), propertyValue,
@"Setting fillPattern to a constant value expression should update fill-pattern.");
XCTAssertEqualObjects(layer.fillPattern, constantExpression,
@"fillPattern should round-trip constant value expressions.");

constantExpression = [NSExpression expressionWithFormat:@"Fill Pattern"];
constantExpression = [NSExpression expressionWithFormat:@"MGL_FUNCTION('image', 'Fill Pattern')"];
NSExpression *functionExpression = [NSExpression expressionWithFormat:@"mgl_step:from:stops:($zoomLevel, %@, %@)", constantExpression, @{@18: constantExpression}];
layer.fillPattern = functionExpression;

{
using namespace mbgl::style::expression::dsl;
propertyValue = mbgl::style::PropertyExpression<mbgl::style::expression::Image>(
step(zoom(), literal("Fill Pattern"), 18.0, literal("Fill Pattern"))
step(zoom(), image(literal("Fill Pattern")), 18.0, image(literal("Fill Pattern")))
);
}

Expand Down
6 changes: 3 additions & 3 deletions platform/darwin/test/MGLLineStyleLayerTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -659,22 +659,22 @@ - (void)testProperties {
@"line-pattern should be unset initially.");
NSExpression *defaultExpression = layer.linePattern;

NSExpression *constantExpression = [NSExpression expressionWithFormat:@"Line Pattern"];
NSExpression *constantExpression = [NSExpression expressionForConstantValue:@"Line Pattern"];
layer.linePattern = constantExpression;
mbgl::style::PropertyValue<mbgl::style::expression::Image> propertyValue = { "Line Pattern" };
XCTAssertEqual(rawLayer->getLinePattern(), propertyValue,
@"Setting linePattern to a constant value expression should update line-pattern.");
XCTAssertEqualObjects(layer.linePattern, constantExpression,
@"linePattern should round-trip constant value expressions.");

constantExpression = [NSExpression expressionWithFormat:@"Line Pattern"];
constantExpression = [NSExpression expressionWithFormat:@"MGL_FUNCTION('image', 'Line Pattern')"];
NSExpression *functionExpression = [NSExpression expressionWithFormat:@"mgl_step:from:stops:($zoomLevel, %@, %@)", constantExpression, @{@18: constantExpression}];
layer.linePattern = functionExpression;

{
using namespace mbgl::style::expression::dsl;
propertyValue = mbgl::style::PropertyExpression<mbgl::style::expression::Image>(
step(zoom(), literal("Line Pattern"), 18.0, literal("Line Pattern"))
step(zoom(), image(literal("Line Pattern")), 18.0, image(literal("Line Pattern")))
);
}

Expand Down
8 changes: 8 additions & 0 deletions platform/darwin/test/MGLStyleLayerTests.mm.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
<% if (property.type === 'formatted') { -%>
NSExpression *constantExpression = [NSExpression expressionWithFormat:<%- objCTestValue(property, 'string', true, 3) %>];
<% } else if (property.type === 'resolvedImage'){ -%>
NSExpression *constantExpression = [NSExpression expressionForConstantValue:<%- objCTestValue(property, 'string', true, 3) %>];
<% } else { -%>
NSExpression *constantExpression = [NSExpression expressionWithFormat:<%- objCTestValue(property, type, true, 3) %>];
<% } -%>
Expand All @@ -98,6 +100,8 @@
propertyValue = mbgl::style::PropertyExpression<<%- mbglType(property) %>>(
<% if (property.type === 'formatted') { -%>
step(zoom(), format(<%- mbglExpressionTestValue(property, type) %>), 18.0, format(<%- mbglExpressionTestValue(property, type) %>))
<% } else if (property.type === 'resolvedImage') { -%>
step(zoom(), image(literal(<%- mbglExpressionTestValue(property, type) %>)), 18.0, image(literal(<%- mbglExpressionTestValue(property, type) %>)))
<% } else { -%>
step(zoom(), literal(<%- mbglExpressionTestValue(property, type) %>), 18.0, literal(<%- mbglExpressionTestValue(property, type) %>))
<% } -%>
Expand Down Expand Up @@ -179,6 +183,8 @@
propertyValue = mbgl::style::PropertyExpression<<%- mbglType(property) %>>(
<% if (property.type === 'formatted') { -%>
format(toString(get(literal("token"))))
<% } else if (property.type === 'resolvedImage') { -%>
image(toString(get(literal("token"))))
<% } else { -%>
toString(get(literal("token")))
<% } -%>
Expand All @@ -192,6 +198,8 @@
MGLAttributedExpression *tokenAttibutedExpression = [[MGLAttributedExpression alloc] initWithExpression:[NSExpression expressionWithFormat:@"CAST(token, 'NSString')"]
attributes:@{}];
NSExpression* tokenExpression = [NSExpression mgl_expressionForAttributedExpressions:@[[NSExpression expressionForConstantValue:tokenAttibutedExpression]]];
<% } else if (property.type === 'resolvedImage') { -%>
NSExpression* tokenExpression = [NSExpression expressionWithFormat:@"MGL_FUNCTION('image', CAST(token, \"NSString\"))"];
<% } else { -%>
NSExpression* tokenExpression = [NSExpression expressionWithFormat:@"CAST(token, \"NSString\")"];
<% } -%>
Expand Down
10 changes: 5 additions & 5 deletions platform/darwin/test/MGLSymbolStyleLayerTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -182,22 +182,22 @@ - (void)testProperties {
@"icon-image should be unset initially.");
NSExpression *defaultExpression = layer.iconImageName;

NSExpression *constantExpression = [NSExpression expressionWithFormat:@"Icon Image"];
NSExpression *constantExpression = [NSExpression expressionForConstantValue:@"Icon Image"];
layer.iconImageName = constantExpression;
mbgl::style::PropertyValue<mbgl::style::expression::Image> propertyValue = { "Icon Image" };
XCTAssertEqual(rawLayer->getIconImage(), propertyValue,
@"Setting iconImageName to a constant value expression should update icon-image.");
XCTAssertEqualObjects(layer.iconImageName, constantExpression,
@"iconImageName should round-trip constant value expressions.");

constantExpression = [NSExpression expressionWithFormat:@"Icon Image"];
constantExpression = [NSExpression expressionWithFormat:@"MGL_FUNCTION('image', 'Icon Image')"];
NSExpression *functionExpression = [NSExpression expressionWithFormat:@"mgl_step:from:stops:($zoomLevel, %@, %@)", constantExpression, @{@18: constantExpression}];
layer.iconImageName = functionExpression;

{
using namespace mbgl::style::expression::dsl;
propertyValue = mbgl::style::PropertyExpression<mbgl::style::expression::Image>(
step(zoom(), literal("Icon Image"), 18.0, literal("Icon Image"))
step(zoom(), image(literal("Icon Image")), 18.0, image(literal("Icon Image")))
);
}

Expand All @@ -219,14 +219,14 @@ - (void)testProperties {
{
using namespace mbgl::style::expression::dsl;
propertyValue = mbgl::style::PropertyExpression<mbgl::style::expression::Image>(
toString(get(literal("token")))
image(toString(get(literal("token"))))
);
}

XCTAssertEqual(rawLayer->getIconImage(), propertyValue,
@"Setting iconImageName to a constant string with tokens should convert to an expression.");

NSExpression* tokenExpression = [NSExpression expressionWithFormat:@"CAST(token, \"NSString\")"];
NSExpression* tokenExpression = [NSExpression expressionWithFormat:@"MGL_FUNCTION('image', CAST(token, \"NSString\"))"];
XCTAssertEqualObjects(layer.iconImageName, tokenExpression,
@"Setting iconImageName to a constant string with tokens should convert to an expression.");
}
Expand Down

0 comments on commit b51b959

Please sign in to comment.