[core] Add support for $id key to filters
https://github.com/mapbox/mapbox-gl-style-spec/issues/391
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
#include <mbgl/test/util.hpp>
|
||||
#include <mbgl/util/feature.hpp>
|
||||
#include <mbgl/util/geometry.hpp>
|
||||
|
||||
#include <mbgl/style/filter.hpp>
|
||||
#include <mbgl/style/filter_evaluator.hpp>
|
||||
@@ -17,97 +19,107 @@ Filter parse(const char * expression) {
|
||||
return *conversion::convert<Filter>(doc);
|
||||
}
|
||||
|
||||
bool evaluate(const Filter& filter, const PropertyMap& properties, FeatureType type = FeatureType::Unknown) {
|
||||
return filter(type, [&] (const std::string& key) -> optional<Value> {
|
||||
auto it = properties.find(key);
|
||||
if (it == properties.end())
|
||||
return {};
|
||||
return it->second;
|
||||
});
|
||||
Feature feature(const PropertyMap& properties, const Geometry<double>& geometry = Point<double>()) {
|
||||
Feature result { geometry };
|
||||
result.properties = properties;
|
||||
return result;
|
||||
}
|
||||
|
||||
TEST(Filter, EqualsString) {
|
||||
Filter f = parse("[\"==\", \"foo\", \"bar\"]");
|
||||
ASSERT_TRUE(evaluate(f, {{ "foo", std::string("bar") }}));
|
||||
ASSERT_FALSE(evaluate(f, {{ "foo", std::string("baz") }}));
|
||||
ASSERT_TRUE(f(feature({{ "foo", std::string("bar") }})));
|
||||
ASSERT_FALSE(f(feature({{ "foo", std::string("baz") }})));
|
||||
}
|
||||
|
||||
TEST(Filter, EqualsNumber) {
|
||||
Filter f = parse("[\"==\", \"foo\", 0]");
|
||||
ASSERT_TRUE(evaluate(f, {{ "foo", int64_t(0) }}));
|
||||
ASSERT_TRUE(evaluate(f, {{ "foo", uint64_t(0) }}));
|
||||
ASSERT_TRUE(evaluate(f, {{ "foo", double(0) }}));
|
||||
ASSERT_FALSE(evaluate(f, {{ "foo", int64_t(1) }}));
|
||||
ASSERT_FALSE(evaluate(f, {{ "foo", uint64_t(1) }}));
|
||||
ASSERT_FALSE(evaluate(f, {{ "foo", double(1) }}));
|
||||
ASSERT_FALSE(evaluate(f, {{ "foo", std::string("0") }}));
|
||||
ASSERT_FALSE(evaluate(f, {{ "foo", false }}));
|
||||
ASSERT_FALSE(evaluate(f, {{ "foo", true }}));
|
||||
ASSERT_FALSE(evaluate(f, {{ "foo", nullptr }}));
|
||||
ASSERT_FALSE(evaluate(f, {{}}));
|
||||
ASSERT_TRUE(f(feature({{ "foo", int64_t(0) }})));
|
||||
ASSERT_TRUE(f(feature({{ "foo", uint64_t(0) }})));
|
||||
ASSERT_TRUE(f(feature({{ "foo", double(0) }})));
|
||||
ASSERT_FALSE(f(feature({{ "foo", int64_t(1) }})));
|
||||
ASSERT_FALSE(f(feature({{ "foo", uint64_t(1) }})));
|
||||
ASSERT_FALSE(f(feature({{ "foo", double(1) }})));
|
||||
ASSERT_FALSE(f(feature({{ "foo", std::string("0") }})));
|
||||
ASSERT_FALSE(f(feature({{ "foo", false }})));
|
||||
ASSERT_FALSE(f(feature({{ "foo", true }})));
|
||||
ASSERT_FALSE(f(feature({{ "foo", nullptr }})));
|
||||
ASSERT_FALSE(f(feature({{}})));
|
||||
}
|
||||
|
||||
TEST(Filter, EqualsType) {
|
||||
Filter f = parse("[\"==\", \"$type\", \"LineString\"]");
|
||||
ASSERT_FALSE(evaluate(f, {{}}, FeatureType::Point));
|
||||
ASSERT_TRUE(evaluate(f, {{}}, FeatureType::LineString));
|
||||
ASSERT_FALSE(f(feature({{}}, Point<double>())));
|
||||
ASSERT_TRUE(f(feature({{}}, LineString<double>())));
|
||||
}
|
||||
|
||||
TEST(Filter, InType) {
|
||||
Filter f = parse("[\"in\", \"$type\", \"LineString\", \"Polygon\"]");
|
||||
ASSERT_FALSE(evaluate(f, {{}}, FeatureType::Point));
|
||||
ASSERT_TRUE(evaluate(f, {{}}, FeatureType::LineString));
|
||||
ASSERT_TRUE(evaluate(f, {{}}, FeatureType::Polygon));
|
||||
ASSERT_FALSE(f(feature({{}}, Point<double>())));
|
||||
ASSERT_TRUE(f(feature({{}}, LineString<double>())));
|
||||
ASSERT_TRUE(f(feature({{}}, Polygon<double>())));
|
||||
}
|
||||
|
||||
TEST(Filter, Any) {
|
||||
ASSERT_FALSE(evaluate(parse("[\"any\"]"), {{}}));
|
||||
ASSERT_TRUE(evaluate(parse("[\"any\", [\"==\", \"foo\", 1]]"),
|
||||
{{ std::string("foo"), int64_t(1) }}));
|
||||
ASSERT_FALSE(evaluate(parse("[\"any\", [\"==\", \"foo\", 0]]"),
|
||||
{{ std::string("foo"), int64_t(1) }}));
|
||||
ASSERT_TRUE(evaluate(parse("[\"any\", [\"==\", \"foo\", 0], [\"==\", \"foo\", 1]]"),
|
||||
{{ std::string("foo"), int64_t(1) }}));
|
||||
ASSERT_FALSE(parse("[\"any\"]")(feature({{}})));
|
||||
ASSERT_TRUE(parse("[\"any\", [\"==\", \"foo\", 1]]")(
|
||||
feature({{ std::string("foo"), int64_t(1) }})));
|
||||
ASSERT_FALSE(parse("[\"any\", [\"==\", \"foo\", 0]]")(
|
||||
feature({{ std::string("foo"), int64_t(1) }})));
|
||||
ASSERT_TRUE(parse("[\"any\", [\"==\", \"foo\", 0], [\"==\", \"foo\", 1]]")(
|
||||
feature({{ std::string("foo"), int64_t(1) }})));
|
||||
}
|
||||
|
||||
TEST(Filter, All) {
|
||||
ASSERT_TRUE(evaluate(parse("[\"all\"]"), {{}}));
|
||||
ASSERT_TRUE(evaluate(parse("[\"all\", [\"==\", \"foo\", 1]]"),
|
||||
{{ std::string("foo"), int64_t(1) }}));
|
||||
ASSERT_FALSE(evaluate(parse("[\"all\", [\"==\", \"foo\", 0]]"),
|
||||
{{ std::string("foo"), int64_t(1) }}));
|
||||
ASSERT_FALSE(evaluate(parse("[\"all\", [\"==\", \"foo\", 0], [\"==\", \"foo\", 1]]"),
|
||||
{{ std::string("foo"), int64_t(1) }}));
|
||||
ASSERT_TRUE(parse("[\"all\"]")(feature({{}})));
|
||||
ASSERT_TRUE(parse("[\"all\", [\"==\", \"foo\", 1]]")(
|
||||
feature({{ std::string("foo"), int64_t(1) }})));
|
||||
ASSERT_FALSE(parse("[\"all\", [\"==\", \"foo\", 0]]")(
|
||||
feature({{ std::string("foo"), int64_t(1) }})));
|
||||
ASSERT_FALSE(parse("[\"all\", [\"==\", \"foo\", 0], [\"==\", \"foo\", 1]]")(
|
||||
feature({{ std::string("foo"), int64_t(1) }})));
|
||||
}
|
||||
|
||||
TEST(Filter, None) {
|
||||
ASSERT_TRUE(evaluate(parse("[\"none\"]"), {{}}));
|
||||
ASSERT_FALSE(evaluate(parse("[\"none\", [\"==\", \"foo\", 1]]"),
|
||||
{{ std::string("foo"), int64_t(1) }}));
|
||||
ASSERT_TRUE(evaluate(parse("[\"none\", [\"==\", \"foo\", 0]]"),
|
||||
{{ std::string("foo"), int64_t(1) }}));
|
||||
ASSERT_FALSE(evaluate(parse("[\"none\", [\"==\", \"foo\", 0], [\"==\", \"foo\", 1]]"),
|
||||
{{ std::string("foo"), int64_t(1) }}));
|
||||
ASSERT_TRUE(parse("[\"none\"]")(feature({{}})));
|
||||
ASSERT_FALSE(parse("[\"none\", [\"==\", \"foo\", 1]]")(
|
||||
feature({{ std::string("foo"), int64_t(1) }})));
|
||||
ASSERT_TRUE(parse("[\"none\", [\"==\", \"foo\", 0]]")(
|
||||
feature({{ std::string("foo"), int64_t(1) }})));
|
||||
ASSERT_FALSE(parse("[\"none\", [\"==\", \"foo\", 0], [\"==\", \"foo\", 1]]")(
|
||||
feature({{ std::string("foo"), int64_t(1) }})));
|
||||
}
|
||||
|
||||
TEST(Filter, Has) {
|
||||
ASSERT_TRUE(evaluate(parse("[\"has\", \"foo\"]"),
|
||||
{{ std::string("foo"), int64_t(1) }}));
|
||||
ASSERT_TRUE(evaluate(parse("[\"has\", \"foo\"]"),
|
||||
{{ std::string("foo"), int64_t(0) }}));
|
||||
ASSERT_TRUE(evaluate(parse("[\"has\", \"foo\"]"),
|
||||
{{ std::string("foo"), false }}));
|
||||
ASSERT_FALSE(evaluate(parse("[\"has\", \"foo\"]"),
|
||||
{{}}));
|
||||
ASSERT_TRUE(parse("[\"has\", \"foo\"]")(
|
||||
feature({{ std::string("foo"), int64_t(1) }})));
|
||||
ASSERT_TRUE(parse("[\"has\", \"foo\"]")(
|
||||
feature({{ std::string("foo"), int64_t(0) }})));
|
||||
ASSERT_TRUE(parse("[\"has\", \"foo\"]")(
|
||||
feature({{ std::string("foo"), false }})));
|
||||
ASSERT_FALSE(parse("[\"has\", \"foo\"]")(
|
||||
feature({{}})));
|
||||
}
|
||||
|
||||
TEST(Filter, NotHas) {
|
||||
ASSERT_FALSE(evaluate(parse("[\"!has\", \"foo\"]"),
|
||||
{{ std::string("foo"), int64_t(1) }}));
|
||||
ASSERT_FALSE(evaluate(parse("[\"!has\", \"foo\"]"),
|
||||
{{ std::string("foo"), int64_t(0) }}));
|
||||
ASSERT_FALSE(evaluate(parse("[\"!has\", \"foo\"]"),
|
||||
{{ std::string("foo"), false }}));
|
||||
ASSERT_TRUE(evaluate(parse("[\"!has\", \"foo\"]"),
|
||||
{{}}));
|
||||
ASSERT_FALSE(parse("[\"!has\", \"foo\"]")(
|
||||
feature({{ std::string("foo"), int64_t(1) }})));
|
||||
ASSERT_FALSE(parse("[\"!has\", \"foo\"]")(
|
||||
feature({{ std::string("foo"), int64_t(0) }})));
|
||||
ASSERT_FALSE(parse("[\"!has\", \"foo\"]")(
|
||||
feature({{ std::string("foo"), false }})));
|
||||
ASSERT_TRUE(parse("[\"!has\", \"foo\"]")(
|
||||
feature({{}})));
|
||||
}
|
||||
|
||||
TEST(Filter, ID) {
|
||||
Feature feature1 { Point<double>() };
|
||||
feature1.id = { 1234 };
|
||||
|
||||
ASSERT_TRUE(parse("[\"==\", \"$id\", 1234]")(feature1));
|
||||
ASSERT_FALSE(parse("[\"==\", \"$id\", \"1234\"]")(feature1));
|
||||
|
||||
Feature feature2 { Point<double>() };
|
||||
feature2.properties["id"] = { 1234 };
|
||||
|
||||
ASSERT_FALSE(parse("[\"==\", \"$id\", 1234]")(feature2));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user