summaryrefslogtreecommitdiff
path: root/test/style/filter.test.cpp
blob: 96de12594568e1fcd91552db44bfb370f6e1af62 (plain)
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
121
122
123
124
125
126
127
128
#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>
#include <mbgl/style/rapidjson_conversion.hpp>
#include <mbgl/style/conversion.hpp>
#include <mbgl/style/conversion/filter.hpp>

#include <rapidjson/document.h>

using namespace mbgl;
using namespace mbgl::style;

Filter parse(const char * expression) {
    rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> doc;
    doc.Parse<0>(expression);
    conversion::Error error;
    optional<Filter> filter = conversion::convert<Filter, JSValue>(doc, error);
    EXPECT_TRUE(bool(filter));
    return *filter;
}

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(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(feature({{}}, Point<double>())));
    ASSERT_TRUE(f(feature({{}}, LineString<double>())));
}

TEST(Filter, InType) {
    Filter f = parse(R"(["in", "$type", "LineString", "Polygon"])");
    ASSERT_FALSE(f(feature({{}}, Point<double>())));
    ASSERT_TRUE(f(feature({{}}, LineString<double>())));
    ASSERT_TRUE(f(feature({{}}, Polygon<double>())));
}

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) {
    Feature feature1 { Point<double>() };
    feature1.id = { uint64_t(1234) };

    ASSERT_TRUE(parse("[\"==\", \"$id\", 1234]")(feature1));
    ASSERT_FALSE(parse("[\"==\", \"$id\", \"1234\"]")(feature1));

    Feature feature2 { Point<double>() };
    feature2.properties["id"] = { uint64_t(1234) };

    ASSERT_FALSE(parse("[\"==\", \"$id\", 1234]")(feature2));
}