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

Fix literal wrapping in comparison expressions #12022

Merged
merged 1 commit into from
May 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
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;
Expand Down Expand Up @@ -180,6 +179,8 @@ public static Expression literal(boolean bool) {
public static Expression literal(@NonNull Object object) {
if (object.getClass().isArray()) {
return literal(ExpressionArray.toObjectArray(object));
} else if (object instanceof Expression) {
throw new RuntimeException("Can't convert an expression to a literal");
}
return new ExpressionLiteral(object);
}
Expand Down Expand Up @@ -426,7 +427,7 @@ public static Expression eq(Expression compareOne, boolean compareTwo) {
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-==">Style specification</a>
*/
public static Expression eq(@NonNull Expression compareOne, @NonNull String compareTwo) {
return eq(literal(compareOne), literal(compareTwo));
return eq(compareOne, literal(compareTwo));
}

/**
Expand All @@ -449,7 +450,7 @@ public static Expression eq(@NonNull Expression compareOne, @NonNull String comp
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-==">Style specification</a>
*/
public static Expression eq(@NonNull Expression compareOne, @NonNull Number compareTwo) {
return eq(literal(compareOne), literal(compareTwo));
return eq(compareOne, literal(compareTwo));
}

/**
Expand Down Expand Up @@ -496,7 +497,7 @@ public static Expression neq(@NonNull Expression compareOne, @NonNull Expression
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-!=">Style specification</a>
*/
public static Expression neq(Expression compareOne, boolean compareTwo) {
return new Expression("!=", literal(compareOne), literal(compareTwo));
return new Expression("!=", compareOne, literal(compareTwo));
}

/**
Expand All @@ -519,7 +520,7 @@ public static Expression neq(Expression compareOne, boolean compareTwo) {
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-!=">Style specification</a>
*/
public static Expression neq(@NonNull Expression compareOne, @NonNull String compareTwo) {
return new Expression("!=", literal(compareOne), literal(compareTwo));
return new Expression("!=", compareOne, literal(compareTwo));
}

/**
Expand All @@ -542,7 +543,7 @@ public static Expression neq(@NonNull Expression compareOne, @NonNull String com
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-!=">Style specification</a>
*/
public static Expression neq(@NonNull Expression compareOne, @NonNull Number compareTwo) {
return new Expression("!=", literal(compareOne), literal(compareTwo));
return new Expression("!=", compareOne, literal(compareTwo));
}

/**
Expand Down Expand Up @@ -589,7 +590,7 @@ public static Expression gt(@NonNull Expression compareOne, @NonNull Expression
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-%3E">Style specification</a>
*/
public static Expression gt(@NonNull Expression compareOne, @NonNull Number compareTwo) {
return new Expression(">", literal(compareOne), literal(compareTwo));
return new Expression(">", compareOne, literal(compareTwo));
}

/**
Expand All @@ -612,7 +613,7 @@ public static Expression gt(@NonNull Expression compareOne, @NonNull Number comp
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-%3E">Style specification</a>
*/
public static Expression gt(@NonNull Expression compareOne, @NonNull String compareTwo) {
return new Expression(">", literal(compareOne), literal(compareTwo));
return new Expression(">", compareOne, literal(compareTwo));
}

/**
Expand Down Expand Up @@ -659,7 +660,7 @@ public static Expression lt(@NonNull Expression compareOne, @NonNull Expression
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-%3C">Style specification</a>
*/
public static Expression lt(@NonNull Expression compareOne, @NonNull Number compareTwo) {
return new Expression("<", literal(compareOne), literal(compareTwo));
return new Expression("<", compareOne, literal(compareTwo));
}

/**
Expand All @@ -682,7 +683,7 @@ public static Expression lt(@NonNull Expression compareOne, @NonNull Number comp
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-%3C">Style specification</a>
*/
public static Expression lt(@NonNull Expression compareOne, @NonNull String compareTwo) {
return new Expression("<", literal(compareOne), literal(compareTwo));
return new Expression("<", compareOne, literal(compareTwo));
}

/**
Expand Down Expand Up @@ -729,7 +730,7 @@ public static Expression gte(@NonNull Expression compareOne, @NonNull Expression
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-%3E%3D">Style specification</a>
*/
public static Expression gte(@NonNull Expression compareOne, @NonNull Number compareTwo) {
return new Expression(">=", literal(compareOne), literal(compareTwo));
return new Expression(">=", compareOne, literal(compareTwo));
}

/**
Expand All @@ -752,7 +753,7 @@ public static Expression gte(@NonNull Expression compareOne, @NonNull Number com
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-%3E%3D">Style specification</a>
*/
public static Expression gte(@NonNull Expression compareOne, @NonNull String compareTwo) {
return new Expression(">=", literal(compareOne), literal(compareTwo));
return new Expression(">=", compareOne, literal(compareTwo));
}

/**
Expand Down Expand Up @@ -799,7 +800,7 @@ public static Expression lte(@NonNull Expression compareOne, @NonNull Expression
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-%3C%3D">Style specification</a>
*/
public static Expression lte(@NonNull Expression compareOne, @NonNull Number compareTwo) {
return new Expression("<=", literal(compareOne), literal(compareTwo));
return new Expression("<=", compareOne, literal(compareTwo));
}

/**
Expand All @@ -822,7 +823,7 @@ public static Expression lte(@NonNull Expression compareOne, @NonNull Number com
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-%3C%3D">Style specification</a>
*/
public static Expression lte(@NonNull Expression compareOne, @NonNull String compareTwo) {
return new Expression("<=", literal(compareOne), literal(compareTwo));
return new Expression("<=", compareOne, literal(compareTwo));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
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;
Expand Down Expand Up @@ -138,6 +136,13 @@ public void testEqLiteral() throws Exception {
assertTrue("expression should match", Arrays.deepEquals(expected, actual));
}

@Test
public void testEqExpression() throws Exception {
Object[] expected = new Object[] {"==",new Object[]{"get", "hello"}, 1f};
Object[] actual = eq(get("hello"), 1).toArray();
assertTrue("expression should match", Arrays.deepEquals(expected, actual));
}

@Test
public void testNeq() throws Exception {
Object[] expected = new Object[] {"!=", 0f, 1f};
Expand All @@ -152,6 +157,13 @@ public void testNeqLiteral() throws Exception {
assertTrue("expression should match", Arrays.deepEquals(expected, actual));
}

@Test
public void testNeqExpression() throws Exception {
Object[] expected = new Object[] {"!=",new Object[]{"get", "hello"}, 1f};
Object[] actual = neq(get("hello"), 1).toArray();
assertTrue("expression should match", Arrays.deepEquals(expected, actual));
}

@Test
public void testGt() throws Exception {
Object[] expected = new Object[] {">", 0f, 1f};
Expand All @@ -166,6 +178,13 @@ public void testGtLiteral() throws Exception {
assertTrue("expression should match", Arrays.deepEquals(expected, actual));
}

@Test
public void testGtExpression() throws Exception {
Object[] expected = new Object[] {">", new Object[] {"get", "hello"}, 1f};
Object[] actual = gt(get("hello"), 1).toArray();
assertTrue("expression should match", Arrays.deepEquals(expected, actual));
}

@Test
public void testLt() throws Exception {
Object[] expected = new Object[] {"<", 1f, 0f};
Expand All @@ -180,6 +199,13 @@ public void testLtLiteral() throws Exception {
assertTrue("expression should match", Arrays.deepEquals(expected, actual));
}

@Test
public void testLtExpression() throws Exception {
Object[] expected = new Object[] {"<", new Object[] {"get", "hello"}, 1f};
Object[] actual = lt(get("hello"), 1).toArray();
assertTrue("expression should match", Arrays.deepEquals(expected, actual));
}

@Test
public void testGte() throws Exception {
Object[] expected = new Object[] {">=", 1f, 1f};
Expand All @@ -194,13 +220,27 @@ public void testGteLiteral() throws Exception {
assertTrue("expression should match", Arrays.deepEquals(expected, actual));
}

@Test
public void testGteExpression() throws Exception {
Object[] expected = new Object[] {">=", new Object[] {"get", "hello"}, 1f};
Object[] actual = gte(get("hello"), 1).toArray();
assertTrue("expression should match", Arrays.deepEquals(expected, actual));
}

@Test
public void testLte() throws Exception {
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 testLteExpression() throws Exception {
Object[] expected = new Object[] {"<=", new Object[] {"get", "hello"}, 1f};
Object[] actual = lte(get("hello"), 1).toArray();
assertTrue("expression should match", Arrays.deepEquals(expected, actual));
}

@Test
public void testLteLiteral() throws Exception {
Object[] expected = new Object[] {"<=", 1f, 1f};
Expand Down