From 22741a0ce1171562c9f8e495a2a419cb6c39b869 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Paczos?= Date: Thu, 24 May 2018 17:35:21 +0200 Subject: [android] - raw expression support --- .../mapboxsdk/style/expressions/Expression.java | 73 ++++++- .../mapboxsdk/style/layers/PropertyFactory.java | 21 +- .../style/layers/property_factory.java.ejs | 21 +- .../style/expressions/ExpressionTest.java | 231 +++++++++++++-------- 4 files changed, 255 insertions(+), 91 deletions(-) diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/expressions/Expression.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/expressions/Expression.java index 44ad5e83ed..e32cdffc93 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/expressions/Expression.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/expressions/Expression.java @@ -5,8 +5,10 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.annotation.Size; +import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; +import com.google.gson.JsonNull; import com.google.gson.JsonPrimitive; import com.mapbox.mapboxsdk.style.layers.PropertyFactory; import com.mapbox.mapboxsdk.style.layers.PropertyValue; @@ -210,7 +212,8 @@ public class Expression { * @return the color expression */ public static Expression color(@ColorInt int color) { - return toColor(literal(PropertyFactory.colorToRgbaString(color))); + int[] rgba = PropertyFactory.colorToRgbaArray(color); + return rgba(rgba[0], rgba[1], rgba[2], rgba[3]); } /** @@ -2426,6 +2429,7 @@ public class Expression { * ); * } * + * * @param number number to get value from * @return expression * @see Style specification @@ -3452,6 +3456,10 @@ public class Expression { /** * Returns a string representation of the object that matches the definition set in the style specification. + *

+ * If this expression contains a coma (,) delimited literal, like 'rgba(r, g, b, a)`, + * it will be enclosed with double quotes ("). + *

* * @return a string representation of the object. */ @@ -3463,7 +3471,17 @@ public class Expression { for (Object argument : arguments) { builder.append(", "); if (argument instanceof ExpressionLiteral) { - builder.append(((ExpressionLiteral) argument).toValue()); + Object literalValue = ((ExpressionLiteral) argument).toValue(); + + // special case for handling unusual input like 'rgba(r, g, b, a)' + if (literalValue instanceof String) { + if (((String) literalValue).contains(",")) { + builder.append("\"").append(literalValue).append("\""); + continue; + } + } + + builder.append(literalValue); } else { builder.append(argument.toString()); } @@ -3473,6 +3491,26 @@ public class Expression { return builder.toString(); } + /** + * Returns a DSL equivalent of a raw expression. + *

+ * If your raw expression contains a coma (,) delimited literal it has to be enclosed with double quotes ("), + * for example + *

+ *
+   *   {@code
+   *   ["to-color", "rgba(255, 0, 0, 255)"]
+   *   }
+   * 
+ * + * @param rawExpression the raw expression + * @return the resulting expression + * @see Style specification + */ + public static Expression raw(@NonNull String rawExpression) { + return Converter.convert(rawExpression); + } + /** * Indicates whether some other object is "equal to" this one. * @@ -3527,6 +3565,11 @@ public class Expression { * @param object the object to be treated as literal */ public ExpressionLiteral(@NonNull Object object) { + if (object instanceof String) { + object = unwrapStringLiteral((String) object); + } else if (object instanceof Number) { + object = ((Number) object).floatValue(); + } this.literal = object; } @@ -3583,6 +3626,15 @@ public class Expression { result = 31 * result + (literal != null ? literal.hashCode() : 0); return result; } + + private String unwrapStringLiteral(String value) { + if (value.length() > 1 && + value.charAt(0) == '\"' && value.charAt(value.length() - 1) == '\"') { + return value.substring(1, value.length() - 1); + } else { + return value; + } + } } /** @@ -3652,10 +3704,12 @@ public class Expression { } /** - * Converts a JsonArray to an expression. + * Converts a JsonArray or a raw expression to a Java expression. */ public final static class Converter { + private static final Gson gson = new Gson(); + /** * Converts a JsonArray to an expression * @@ -3677,6 +3731,8 @@ public class Expression { arguments.add(convert((JsonArray) jsonElement)); } else if (jsonElement instanceof JsonPrimitive) { arguments.add(convert((JsonPrimitive) jsonElement)); + } else if (jsonElement instanceof JsonNull) { + arguments.add(new Expression.ExpressionLiteral("")); } else { throw new RuntimeException("Unsupported expression conversion for " + jsonElement.getClass()); } @@ -3701,6 +3757,17 @@ public class Expression { throw new RuntimeException("Unsupported literal expression conversion for " + jsonPrimitive.getClass()); } } + + /** + * Converts a raw expression to a DSL equivalent. + * + * @param rawExpression the raw expression to convert + * @return the resulting expression + * @see Style specification + */ + public static Expression convert(@NonNull String rawExpression) { + return convert(gson.fromJson(rawExpression, JsonArray.class)); + } } /** diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/PropertyFactory.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/PropertyFactory.java index 1dd8eddab9..57638920be 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/PropertyFactory.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/PropertyFactory.java @@ -2375,9 +2375,24 @@ public class PropertyFactory { return new LayoutPropertyValue<>("text-optional", value); } - public static String colorToRgbaString(@ColorInt int value) { - return String.format(Locale.US,"rgba(%d, %d, %d, %d)", - (value >> 16) & 0xFF, (value >> 8) & 0xFF, value & 0xFF, (value >> 24) & 0xFF); + /** + * Converts Android color int to "rbga(r, g, b, a)" String equivalent. + * + * @param color Android color int + * @return String rgba color + */ + public static String colorToRgbaString(@ColorInt int color) { + return String.format(Locale.US, "rgba(%d, %d, %d, %d)", + (color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF, (color >> 24) & 0xFF); } + /** + * Converts Android color int to rgba int array. + * + * @param color Android color int + * @return int rgba array + */ + public static int[] colorToRgbaArray(@ColorInt int color) { + return new int[] {(color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF, (color >> 24) & 0xFF}; + } } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/property_factory.java.ejs b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/property_factory.java.ejs index 6480dde3c8..ce0489409c 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/property_factory.java.ejs +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/property_factory.java.ejs @@ -85,9 +85,24 @@ public class PropertyFactory { } <% } -%> - public static String colorToRgbaString(@ColorInt int value) { - return String.format(Locale.US,"rgba(%d, %d, %d, %d)", - (value >> 16) & 0xFF, (value >> 8) & 0xFF, value & 0xFF, (value >> 24) & 0xFF); + /** + * Converts Android color int to "rbga(r, g, b, a)" String equivalent. + * + * @param color Android color int + * @return String rgba color + */ + public static String colorToRgbaString(@ColorInt int color) { + return String.format(Locale.US, "rgba(%d, %d, %d, %d)", + (color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF, (color >> 24) & 0xFF); } + /** + * Converts Android color int to rgba int array. + * + * @param color Android color int + * @return int rgba array + */ + public static int[] colorToRgbaArray(@ColorInt int color) { + return new int[] {(color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF, (color >> 24) & 0xFF}; + } } diff --git a/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/style/expressions/ExpressionTest.java b/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/style/expressions/ExpressionTest.java index 45833e8556..f87fd31172 100644 --- a/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/style/expressions/ExpressionTest.java +++ b/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/style/expressions/ExpressionTest.java @@ -2,6 +2,8 @@ package com.mapbox.mapboxsdk.style.expressions; import android.graphics.Color; +import com.mapbox.mapboxsdk.style.layers.PropertyFactory; + import org.junit.Test; import java.util.Arrays; @@ -57,6 +59,7 @@ import static com.mapbox.mapboxsdk.style.expressions.Expression.pi; import static com.mapbox.mapboxsdk.style.expressions.Expression.pow; import static com.mapbox.mapboxsdk.style.expressions.Expression.product; import static com.mapbox.mapboxsdk.style.expressions.Expression.properties; +import static com.mapbox.mapboxsdk.style.expressions.Expression.raw; import static com.mapbox.mapboxsdk.style.expressions.Expression.rgb; import static com.mapbox.mapboxsdk.style.expressions.Expression.rgba; import static com.mapbox.mapboxsdk.style.expressions.Expression.round; @@ -88,28 +91,28 @@ public class ExpressionTest { @Test public void testRgb() throws Exception { - Object[] expected = new Object[] {"rgb", 0, 0, 0}; + Object[] expected = new Object[] {"rgb", 0f, 0f, 0f}; Object[] actual = rgb(literal(0), literal(0), literal(0)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testRgbLiteral() throws Exception { - Object[] expected = new Object[] {"rgb", 0, 0, 0}; + Object[] expected = new Object[] {"rgb", 0f, 0f, 0f}; Object[] actual = rgb(0, 0, 0).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testRgba() throws Exception { - Object[] expected = new Object[] {"rgba", 0, 0, 0, 1}; + Object[] expected = new Object[] {"rgba", 0f, 0f, 0f, 1f}; Object[] actual = rgba(literal(0), literal(0), literal(0), literal(1)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testRgbaLiteral() throws Exception { - Object[] expected = new Object[] {"rgba", 0, 0, 0, 1}; + Object[] expected = new Object[] {"rgba", 0f, 0f, 0f, 1f}; Object[] actual = rgba(0, 0, 0, 1).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @@ -117,90 +120,90 @@ public class ExpressionTest { @Test public void testToRgba() throws Exception { Object[] expected = new Object[] {"to-rgba", new Object[] {"to-color", "rgba(255, 0, 0, 255)"}}; - Object[] actual = toRgba(color(Color.RED)).toArray(); + Object[] actual = toRgba(toColor(literal(PropertyFactory.colorToRgbaString(Color.RED)))).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testEq() throws Exception { - Object[] expected = new Object[] {"==", 1, 1}; + Object[] expected = new Object[] {"==", 1f, 1f}; Object[] actual = eq(literal(1), literal(1)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testEqLiteral() throws Exception { - Object[] expected = new Object[] {"==", 1, 1}; + Object[] expected = new Object[] {"==", 1f, 1f}; Object[] actual = eq(literal(1), 1).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testNeq() throws Exception { - Object[] expected = new Object[] {"!=", 0, 1}; + Object[] expected = new Object[] {"!=", 0f, 1f}; Object[] actual = neq(literal(0), literal(1)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testNeqLiteral() throws Exception { - Object[] expected = new Object[] {"!=", 0, 1}; + Object[] expected = new Object[] {"!=", 0f, 1f}; Object[] actual = neq(literal(0), 1).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testGt() throws Exception { - Object[] expected = new Object[] {">", 0, 1}; + Object[] expected = new Object[] {">", 0f, 1f}; Object[] actual = gt(literal(0), literal(1)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testGtLiteral() throws Exception { - Object[] expected = new Object[] {">", 0, 1}; + Object[] expected = new Object[] {">", 0f, 1f}; Object[] actual = gt(literal(0), 1).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testLt() throws Exception { - Object[] expected = new Object[] {"<", 1, 0}; + Object[] expected = new Object[] {"<", 1f, 0f}; Object[] actual = lt(literal(1), literal(0)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testLtLiteral() throws Exception { - Object[] expected = new Object[] {"<", 1, 0}; + Object[] expected = new Object[] {"<", 1f, 0f}; Object[] actual = lt(literal(1), 0).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testGte() throws Exception { - Object[] expected = new Object[] {">=", 1, 1}; + Object[] expected = new Object[] {">=", 1f, 1f}; Object[] actual = gte(literal(1), literal(1)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testGteLiteral() throws Exception { - Object[] expected = new Object[] {">=", 1, 1}; + Object[] expected = new Object[] {">=", 1f, 1f}; Object[] actual = gte(literal(1), 1).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testLte() throws Exception { - Object[] expected = new Object[] {"<=", 1, 1}; + Object[] expected = new Object[] {"<=", 1f, 1f}; Object[] actual = lte(literal(1), literal(1)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testLteLiteral() throws Exception { - Object[] expected = new Object[] {"<=", 1, 1}; + Object[] expected = new Object[] {"<=", 1f, 1f}; Object[] actual = lte(literal(1), 1).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @@ -368,21 +371,21 @@ public class ExpressionTest { @Test public void testAt() throws Exception { - Object[] expected = new Object[] {"at", 3, new Object[] {"literal", new Object[] {"one", "two"}}}; + Object[] expected = new Object[] {"at", 3f, new Object[] {"literal", new Object[] {"one", "two"}}}; Object[] actual = at(literal(3), literal(new Object[] {"one", "two"})).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testAtLiteral() throws Exception { - Object[] expected = new Object[] {"at", 3, new Object[] {"literal", new Object[] {"one", "two"}}}; + Object[] expected = new Object[] {"at", 3f, new Object[] {"literal", new Object[] {"one", "two"}}}; Object[] actual = at(3, literal(new Object[] {"one", "two"})).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testAtExpression() throws Exception { - Object[] expected = new Object[] {"at", 3, new Object[] {"properties"}}; + Object[] expected = new Object[] {"at", 3f, new Object[] {"properties"}}; Object[] actual = at(literal(3), properties()).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @@ -501,56 +504,56 @@ public class ExpressionTest { @Test public void testSum() throws Exception { - Object[] expected = new Object[] {"+", 1, 2}; + Object[] expected = new Object[] {"+", 1f, 2f}; Object[] actual = sum(literal(1), literal(2)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testSumLiteral() throws Exception { - Object[] expected = new Object[] {"+", 1, 2}; + Object[] expected = new Object[] {"+", 1f, 2f}; Object[] actual = sum(1, 2).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testProduct() throws Exception { - Object[] expected = new Object[] {"*", 1, 2}; + Object[] expected = new Object[] {"*", 1f, 2f}; Object[] actual = product(literal(1), literal(2)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testProductLiteral() throws Exception { - Object[] expected = new Object[] {"*", 1, 2}; + Object[] expected = new Object[] {"*", 1f, 2f}; Object[] actual = product(1, 2).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testSubtract() throws Exception { - Object[] expected = new Object[] {"-", 2, 1}; + Object[] expected = new Object[] {"-", 2f, 1f}; Object[] actual = subtract(literal(2), literal(1)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testSubtractLiteral() throws Exception { - Object[] expected = new Object[] {"-", 2, 1}; + Object[] expected = new Object[] {"-", 2f, 1f}; Object[] actual = subtract(2, 1).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testDivision() throws Exception { - Object[] expected = new Object[] {"/", 2, 1}; + Object[] expected = new Object[] {"/", 2f, 1f}; Object[] actual = division(literal(2), literal(1)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testDivisionLiteral() throws Exception { - Object[] expected = new Object[] {"/", 2, 1}; + Object[] expected = new Object[] {"/", 2f, 1f}; Object[] actual = division(2, 1).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @@ -558,203 +561,203 @@ public class ExpressionTest { @Test public void testDivisionWithNestedGet() throws Exception { Object nestedGet = new Object[] {"get", "key"}; - Object[] expected = new Object[] {"/", 2, nestedGet}; + Object[] expected = new Object[] {"/", 2f, nestedGet}; Object[] actual = division(literal(2), get(literal("key"))).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testMod() throws Exception { - Object[] expected = new Object[] {"%", 1, 3}; + Object[] expected = new Object[] {"%", 1f, 3f}; Object[] actual = mod(literal(1), literal(3)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testModLiteral() throws Exception { - Object[] expected = new Object[] {"%", 1, 3}; + Object[] expected = new Object[] {"%", 1f, 3f}; Object[] actual = mod(1, 3).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testPow() throws Exception { - Object[] expected = new Object[] {"^", 2, 3}; + Object[] expected = new Object[] {"^", 2f, 3f}; Object[] actual = pow(literal(2), literal(3)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testPowLiteral() throws Exception { - Object[] expected = new Object[] {"^", 2, 3}; + Object[] expected = new Object[] {"^", 2f, 3f}; Object[] actual = pow(2, 3).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testSqrt() throws Exception { - Object[] expected = new Object[] {"sqrt", 4}; + Object[] expected = new Object[] {"sqrt", 4f}; Object[] actual = sqrt(literal(4)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testSqrtLiteral() throws Exception { - Object[] expected = new Object[] {"sqrt", 4}; + Object[] expected = new Object[] {"sqrt", 4f}; Object[] actual = sqrt(4).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testLog10() throws Exception { - Object[] expected = new Object[] {"log10", 10}; - Object[] actual = log10(literal(10)).toArray(); + Object[] expected = new Object[] {"log10", 10f}; + Object[] actual = log10(literal(10f)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testLog10Literal() throws Exception { - Object[] expected = new Object[] {"log10", 10}; + Object[] expected = new Object[] {"log10", 10f}; Object[] actual = log10(10).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testLn() throws Exception { - Object[] expected = new Object[] {"ln", 2}; + Object[] expected = new Object[] {"ln", 2f}; Object[] actual = ln(literal(2)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testLnLiteral() throws Exception { - Object[] expected = new Object[] {"ln", 2}; + Object[] expected = new Object[] {"ln", 2f}; Object[] actual = ln(2).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testLog2() throws Exception { - Object[] expected = new Object[] {"log2", 16}; + Object[] expected = new Object[] {"log2", 16f}; Object[] actual = log2(literal(16)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testLog2Literal() throws Exception { - Object[] expected = new Object[] {"log2", 16}; + Object[] expected = new Object[] {"log2", 16f}; Object[] actual = log2(16).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testSin() throws Exception { - Object[] expected = new Object[] {"sin", 45}; + Object[] expected = new Object[] {"sin", 45f}; Object[] actual = sin(literal(45)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testSinLiteral() throws Exception { - Object[] expected = new Object[] {"sin", 45}; + Object[] expected = new Object[] {"sin", 45f}; Object[] actual = sin(45).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testCos() throws Exception { - Object[] expected = new Object[] {"cos", 45}; + Object[] expected = new Object[] {"cos", 45f}; Object[] actual = cos(literal(45)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testCosLiteral() throws Exception { - Object[] expected = new Object[] {"cos", 45}; + Object[] expected = new Object[] {"cos", 45f}; Object[] actual = cos(45).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testTan() throws Exception { - Object[] expected = new Object[] {"tan", 45}; + Object[] expected = new Object[] {"tan", 45f}; Object[] actual = tan(literal(45)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testTanLiteral() throws Exception { - Object[] expected = new Object[] {"tan", 45}; + Object[] expected = new Object[] {"tan", 45f}; Object[] actual = tan(45).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testAsin() throws Exception { - Object[] expected = new Object[] {"asin", 45}; + Object[] expected = new Object[] {"asin", 45f}; Object[] actual = asin(literal(45)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testAsinLiteral() throws Exception { - Object[] expected = new Object[] {"asin", 45}; + Object[] expected = new Object[] {"asin", 45f}; Object[] actual = asin(45).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testAcos() throws Exception { - Object[] expected = new Object[] {"acos", 45}; + Object[] expected = new Object[] {"acos", 45f}; Object[] actual = acos(literal(45)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testAcosLiteral() throws Exception { - Object[] expected = new Object[] {"acos", 45}; + Object[] expected = new Object[] {"acos", 45f}; Object[] actual = acos(45).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testAtan() throws Exception { - Object[] expected = new Object[] {"atan", 45}; + Object[] expected = new Object[] {"atan", 45f}; Object[] actual = atan(literal(45)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testAtanLiteral() throws Exception { - Object[] expected = new Object[] {"atan", 45}; + Object[] expected = new Object[] {"atan", 45f}; Object[] actual = atan(45).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testMin() throws Exception { - Object[] expected = new Object[] {"min", 0, 1, 2, 3}; + Object[] expected = new Object[] {"min", 0f, 1f, 2f, 3f}; Object[] actual = min(literal(0), literal(1), literal(2), literal(3)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testMinLiteral() throws Exception { - Object[] expected = new Object[] {"min", 0, 1, 2, 3}; + Object[] expected = new Object[] {"min", 0f, 1f, 2f, 3f}; Object[] actual = min(0, 1, 2, 3).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testMax() throws Exception { - Object[] expected = new Object[] {"max", 0, 1, 2, 3}; + Object[] expected = new Object[] {"max", 0f, 1f, 2f, 3f}; Object[] actual = max(literal(0), literal(1), literal(2), literal(3)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testMaxLiteral() throws Exception { - Object[] expected = new Object[] {"max", 0, 1, 2, 3}; + Object[] expected = new Object[] {"max", 0f, 1f, 2f, 3f}; Object[] actual = max(0, 1, 2, 3).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @@ -833,7 +836,7 @@ public class ExpressionTest { @Test public void testNumber() throws Exception { - Object[] expected = new Object[] {"number", 1}; + Object[] expected = new Object[] {"number", 1f}; Object[] actual = number(literal(1)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @@ -855,7 +858,7 @@ public class ExpressionTest { @Test public void testToString() throws Exception { - Object[] expected = new Object[] {"to-string", 3}; + Object[] expected = new Object[] {"to-string", 3f}; Object[] actual = Expression.toString(literal(3)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @@ -925,14 +928,14 @@ public class ExpressionTest { @Test public void testStepBasic() throws Exception { - Object[] expected = new Object[] {"step", 12, 11, 0, 111, 1, 1111}; + Object[] expected = new Object[] {"step", 12f, 11f, 0f, 111f, 1f, 1111f}; Object[] actual = step(literal(12), literal(11), literal(0), literal(111), literal(1), literal(1111)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testStepBasicLiteral() throws Exception { - Object[] expected = new Object[] {"step", new Object[] {"get", "line-width"}, 11, 0, 111, 1, 1111}; + Object[] expected = new Object[] {"step", new Object[] {"get", "line-width"}, 11f, 0f, 111f, 1f, 1111f}; Object[] actual = step(get("line-width"), literal(11), stop(0, 111), stop(1, 1111)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @@ -941,7 +944,7 @@ public class ExpressionTest { public void testStepExpression() throws Exception { Object[] input = new Object[] {"get", "key"}; Object[] number = new Object[] {"to-number", input}; - Object[] expected = new Object[] {"step", number, 11, 0, 111, 1, 1111}; + Object[] expected = new Object[] {"step", number, 11f, 0f, 111f, 1f, 1111f}; Object[] actual = step(toNumber(get(literal("key"))), literal(11), literal(0), literal(111), literal(1), literal(1111)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); @@ -951,14 +954,14 @@ public class ExpressionTest { public void testStepExpressionLiteral() throws Exception { Object[] input = new Object[] {"get", "key"}; Object[] number = new Object[] {"to-number", input}; - Object[] expected = new Object[] {"step", number, 11, 0, 111, 1, 1111}; + Object[] expected = new Object[] {"step", number, 11f, 0f, 111f, 1f, 1111f}; Object[] actual = step(toNumber(get("key")), literal(11), stop(0, 111), stop(1, 1111)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testLinear() throws Exception { - Object[] expected = new Object[] {"interpolate", new Object[] {"linear"}, 12, 0, 1, 1, 2, 2, 3}; + Object[] expected = new Object[] {"interpolate", new Object[] {"linear"}, 12f, 0f, 1f, 1f, 2f, 2f, 3f}; Object[] actual = interpolate( linear(), literal(12), literal(0), literal(1), @@ -970,16 +973,16 @@ public class ExpressionTest { @Test public void testLinearStops() throws Exception { - Object[] expected = new Object[] {"interpolate", new Object[] {"linear"}, 12, 0, 1, 1, 2, 2, 3}; + Object[] expected = new Object[] {"interpolate", new Object[] {"linear"}, 12f, 0f, 1f, 1f, 2f, 2f, 3f}; Object[] actual = interpolate(linear(), literal(12), stop(0, 1), stop(1, 2), stop(2, 3)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testExponential() throws Exception { - Object[] exponential = new Object[] {"exponential", 12}; + Object[] exponential = new Object[] {"exponential", 12f}; Object[] get = new Object[] {"get", "x"}; - Object[] expected = new Object[] {"interpolate", exponential, get, 0, 100, 200}; + Object[] expected = new Object[] {"interpolate", exponential, get, 0f, 100f, 200f}; Object[] actual = interpolate(exponential(literal(12)), get(literal("x")), literal(0), literal(100), literal(200)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); @@ -987,9 +990,9 @@ public class ExpressionTest { @Test public void testExponentialLiteral() throws Exception { - Object[] exponential = new Object[] {"exponential", 12}; + Object[] exponential = new Object[] {"exponential", 12f}; Object[] get = new Object[] {"get", "x"}; - Object[] expected = new Object[] {"interpolate", exponential, get, 0, 100, 100, 200}; + Object[] expected = new Object[] {"interpolate", exponential, get, 0f, 100f, 100f, 200f}; Object[] actual = interpolate(exponential(12), get("x"), stop(0, 100), stop(100, 200)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @@ -999,16 +1002,16 @@ public class ExpressionTest { Object[] getX = new Object[] {"get", "x"}; Object[] exponential = new Object[] {"exponential", getX}; Object[] getY = new Object[] {"get", "y"}; - Object[] expected = new Object[] {"interpolate", exponential, getY, 0, 100, 100, 200}; + Object[] expected = new Object[] {"interpolate", exponential, getY, 0f, 100f, 100f, 200f}; Object[] actual = interpolate(exponential(get("x")), get("y"), stop(0, 100), stop(100, 200)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @Test public void testCubicBezier() throws Exception { - Object[] cubicBezier = new Object[] {"cubic-bezier", 1, 1, 1, 1}; + Object[] cubicBezier = new Object[] {"cubic-bezier", 1f, 1f, 1f, 1f}; Object[] get = new Object[] {"get", "x"}; - Object[] expected = new Object[] {"interpolate", cubicBezier, get, 0, 100, 100, 200}; + Object[] expected = new Object[] {"interpolate", cubicBezier, get, 0f, 100f, 100f, 200f}; Object[] actual = interpolate(cubicBezier(literal(1), literal(1), literal(1), literal(1)), get(literal("x")), literal(0), literal(100), literal(100), literal(200)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); @@ -1016,9 +1019,9 @@ public class ExpressionTest { @Test public void testCubicBezierLiteral() throws Exception { - Object[] cubicBezier = new Object[] {"cubic-bezier", 1, 1, 1, 1}; + Object[] cubicBezier = new Object[] {"cubic-bezier", 1f, 1f, 1f, 1f}; Object[] get = new Object[] {"get", "x"}; - Object[] expected = new Object[] {"interpolate", cubicBezier, get, 0, 100, 100, 200}; + Object[] expected = new Object[] {"interpolate", cubicBezier, get, 0f, 100f, 100f, 200f}; Object[] actual = interpolate(cubicBezier(1, 1, 1, 1), get("x"), stop(0, 100), stop(100, 200)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } @@ -1028,8 +1031,8 @@ public class ExpressionTest { Object[] getX = new Object[] {"get", "x"}; Object[] getY = new Object[] {"get", "y"}; Object[] getZ = new Object[] {"get", "z"}; - Object[] cubicBezier = new Object[] {"cubic-bezier", getZ, 1, getY, 1}; - Object[] expected = new Object[] {"interpolate", cubicBezier, getX, 0, 100, 200}; + Object[] cubicBezier = new Object[] {"cubic-bezier", getZ, 1f, getY, 1f}; + Object[] expected = new Object[] {"interpolate", cubicBezier, getX, 0f, 100f, 200f}; Object[] actual = interpolate(cubicBezier(get(literal("z")), literal(1), get(literal("y")), literal(1)), get(literal("x")), literal(0), literal(100), literal(200)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); @@ -1040,8 +1043,8 @@ public class ExpressionTest { Object[] getX = new Object[] {"get", "x"}; Object[] getY = new Object[] {"get", "y"}; Object[] getZ = new Object[] {"get", "z"}; - Object[] cubicBezier = new Object[] {"cubic-bezier", getZ, 1, getY, 1}; - Object[] expected = new Object[] {"interpolate", cubicBezier, getX, 0, 100, 100, 200}; + Object[] cubicBezier = new Object[] {"cubic-bezier", getZ, 1f, getY, 1f}; + Object[] expected = new Object[] {"interpolate", cubicBezier, getX, 0f, 100f, 100f, 200f}; Object[] actual = interpolate(cubicBezier(get("z"), literal(1), get("y"), literal(1)), get("x"), stop(0, 100), stop(100, 200)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); @@ -1056,14 +1059,15 @@ public class ExpressionTest { @Test public void testExpressionMinToString() throws Exception { - String expected = "[\"min\", 0, 1, 2, 3]"; + String expected = "[\"min\", 0.0, 1.0, 2.0, 3.0]"; String actual = min(0, 1, 2, 3).toString(); assertEquals("toString should match", expected, actual); } @Test public void testExpressionExponentialToString() throws Exception { - String expected = "[\"interpolate\", [\"cubic-bezier\", 1, 1, 1, 1], [\"get\", x], 0, 100, 100, 200]"; + String expected = "[\"interpolate\", [\"cubic-bezier\", 1.0, 1.0, 1.0, 1.0]," + + " [\"get\", x], 0.0, 100.0, 100.0, 200.0]"; String actual = interpolate(cubicBezier(literal(1), literal(1), literal(1), literal(1)), get(literal("x")), literal(0), literal(100), literal(100), literal(200)).toString(); assertEquals("toString should match", expected, actual); @@ -1096,7 +1100,7 @@ public class ExpressionTest { @Test public void testColorConversion() { Expression greenColor = color(0xFF00FF00); - Object[] expected = new Object[] {"to-color", "rgba(0, 255, 0, 255)"}; + Object[] expected = new Object[] {"rgba", 0f, 255f, 0f, 255f}; assertTrue("expression should match", Arrays.deepEquals(expected, greenColor.toArray())); } @@ -1165,4 +1169,67 @@ public class ExpressionTest { Object[] actual = floor(literal(2.2f)).toArray(); assertTrue("expression should match", Arrays.deepEquals(expected, actual)); } + + @Test + public void testRawEmpty() { + String raw = "[\"get\", ]"; + Expression expected = get(""); + assertEquals("expressions should match", raw(raw), expected); + + raw = "[\"get\", key]"; + expected = get("key"); + assertEquals("expressions should match", raw(raw), expected); + } + + @Test + public void testRawAndroidColors() { + Expression expected = interpolate(linear(), zoom(), + stop(12, step(get("stroke-width"), + color(Color.BLACK), + stop(1f, color(Color.RED)), + stop(2f, color(Color.WHITE)), + stop(3f, color(Color.BLUE)) + )), + stop(15, step(get("stroke-width"), + color(Color.BLACK), + stop(1f, color(Color.YELLOW)), + stop(2f, color(Color.LTGRAY)), + stop(3f, color(Color.CYAN)) + )), + stop(18, step(get("stroke-width"), + color(Color.BLACK), + stop(1f, color(Color.WHITE)), + stop(2f, color(Color.GRAY)), + stop(3f, color(Color.GREEN)) + )) + ); + assertEquals("expressions should match", expected, raw(expected.toString())); + } + + @Test + public void testRawRgbaColor() { + Expression expected = interpolate( + exponential(2f), zoom(), + literal(5f), literal("rgba(0, 0, 0, 255)"), + literal(10.5f), literal("rgb(255, 0, 0)"), + literal(15), color(Color.GREEN), + literal(20), literal(PropertyFactory.colorToRgbaString(Color.BLUE))); + assertEquals("expressions should match", expected, raw(expected.toString())); + } + + @Test + public void testRawMatchStrings() { + Expression expected = match(get("property"), literal(""), + stop("layer1", "image1"), + stop("layer2", "image2")); + assertEquals("expressions should match", expected, raw(expected.toString())); + } + + @Test + public void testRawMatchNumbers() { + Expression expected = match(get("property"), literal(""), + stop("layer1", 2), + stop("layer2", 2.7)); + assertEquals("expressions should match", expected, raw(expected.toString())); + } } \ No newline at end of file -- cgit v1.2.1