1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
#include <mbgl/test/util.hpp>
#include <mbgl/util/feature.hpp>
#include <mbgl/util/geometry.hpp>
#include <mbgl/test/stub_geometry_tile_feature.hpp>
#include <mbgl/style/filter.hpp>
#include <mbgl/style/filter_evaluator.hpp>
#include <mbgl/style/conversion/json.hpp>
#include <mbgl/style/conversion/filter.hpp>
using namespace mbgl;
using namespace mbgl::style;
Filter parse(const char * expression) {
conversion::Error error;
optional<Filter> filter = conversion::convertJSON<Filter>(expression, error);
EXPECT_TRUE(bool(filter));
return *filter;
}
StubGeometryTileFeature feature(const PropertyMap& properties) {
return StubGeometryTileFeature { properties };
}
TEST(Filter, EqualsString) {
Filter f = parse(R"(["==", "foo", "bar"])");
ASSERT_TRUE(f(feature({{ "foo", std::string("bar") }})));
ASSERT_FALSE(f(feature({{ "foo", std::string("baz") }})));
}
TEST(Filter, EqualsNumber) {
Filter f = parse(R"(["==", "foo", 0])");
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", mapbox::geometry::null_value }})));
ASSERT_FALSE(f(feature({{}})));
}
TEST(Filter, EqualsType) {
Filter f = parse(R"(["==", "$type", "LineString"])");
ASSERT_FALSE(f(StubGeometryTileFeature({}, FeatureType::Point, {}, {})));
ASSERT_TRUE(f(StubGeometryTileFeature({}, FeatureType::LineString, {}, {})));
}
TEST(Filter, InType) {
Filter f = parse(R"(["in", "$type", "LineString", "Polygon"])");
ASSERT_FALSE(f(StubGeometryTileFeature({}, FeatureType::Point, {}, {})));
ASSERT_TRUE(f(StubGeometryTileFeature({}, FeatureType::LineString, {}, {})));
ASSERT_TRUE(f(StubGeometryTileFeature({}, FeatureType::Polygon, {}, {})));
}
TEST(Filter, Any) {
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(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(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(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(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) {
StubGeometryTileFeature feature1 { FeatureIdentifier{ int64_t{ 1234 } }, {}, {}, {} };
ASSERT_TRUE(parse("[\"==\", \"$id\", 1234]")(feature1));
ASSERT_FALSE(parse("[\"==\", \"$id\", \"1234\"]")(feature1));
StubGeometryTileFeature feature2 { {{ "id", uint64_t(1234) }} };
ASSERT_FALSE(parse("[\"==\", \"$id\", 1234]")(feature2));
}
|