Add expression in. (#171)

* Add expression in.

* Add test for expression in.
This commit is contained in:
Kevin Li
2020-02-19 08:52:39 +08:00
committed by GitHub
parent 6109d74b8a
commit 18264df98d
5 changed files with 97 additions and 0 deletions

View File

@@ -1536,6 +1536,42 @@ public class Expression {
return at(literal(number), expression);
}
/**
* Retrieves whether an item exists in an array or a substring exists in a string.
*
* @param needle the item expression
* @param haystack the array or string expression
* @return true if exists.
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-in">Style specification</a>
*/
public static Expression in(@NonNull Expression needle, @NonNull Expression haystack) {
return new Expression("in", needle, haystack);
}
/**
* Retrieves whether an item exists in an array or a substring exists in a string.
*
* @param needle the item expression
* @param haystack the array or string expression
* @return true if exists.
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-in">Style specification</a>
*/
public static Expression in(@NonNull Number needle, @NonNull Expression haystack) {
return new Expression("in", literal(needle), haystack);
}
/**
* Retrieves whether an item exists in an array or a substring exists in a string.
*
* @param needle the item expression
* @param haystack the array or string expression
* @return true if exists.
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-in">Style specification</a>
*/
public static Expression in(@NonNull String needle, @NonNull Expression haystack) {
return new Expression("in", literal(needle), haystack);
}
/**
* Retrieves a property value from the current feature's properties,
* or from another object if a second argument is provided.

View File

@@ -48,6 +48,7 @@ import static com.mapbox.mapboxsdk.style.expressions.Expression.gte;
import static com.mapbox.mapboxsdk.style.expressions.Expression.has;
import static com.mapbox.mapboxsdk.style.expressions.Expression.heatmapDensity;
import static com.mapbox.mapboxsdk.style.expressions.Expression.id;
import static com.mapbox.mapboxsdk.style.expressions.Expression.in;
import static com.mapbox.mapboxsdk.style.expressions.Expression.interpolate;
import static com.mapbox.mapboxsdk.style.expressions.Expression.isSupportedScript;
import static com.mapbox.mapboxsdk.style.expressions.Expression.length;
@@ -446,6 +447,27 @@ public class ExpressionTest {
assertTrue("expression should match", Arrays.deepEquals(expected, actual));
}
@Test
public void testInString() throws Exception {
Object[] expected = new Object[] {"in", "one", "onetwo"};
Object[] actual = in(literal("one"), literal("onetwo")).toArray();
assertTrue("expression should match", Arrays.deepEquals(expected, actual));
}
@Test
public void testInNumber() throws Exception {
Object[] expected = new Object[] {"in", 1f, new Object[] {"literal", new Object[] {1f, 2f}}};
Object[] actual = in(literal(1f), literal(new Object[] {1f, 2f})).toArray();
assertTrue("expression should match", Arrays.deepEquals(expected, actual));
}
@Test
public void testInArray() throws Exception {
Object[] expected = new Object[] {"in", "one", new Object[] {"literal", new Object[] {"one", "two"}}};
Object[] actual = in(literal("one"), 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", 3f, new Object[] {"literal", new Object[] {"one", "two"}}};