diff options
author | Asheem Mamoowala <asheem.mamoowala@mapbox.com> | 2018-06-29 12:56:44 -0700 |
---|---|---|
committer | Asheem Mamoowala <asheem.mamoowala@mapbox.com> | 2018-07-23 09:12:26 -0700 |
commit | 1d8664a2ae3822ba4d45973640c0068c65a237e7 (patch) | |
tree | 62160340b72f987ac340c5807552a7033e9269ac /test | |
parent | bfe714eebe1fd5a8563db92e46220775aa82e3e0 (diff) | |
download | qtlocation-mapboxgl-1d8664a2ae3822ba4d45973640c0068c65a237e7.tar.gz |
Keep original input for filters using legacy syntax.
Diffstat (limited to 'test')
-rw-r--r-- | test/style/filter.test.cpp | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/test/style/filter.test.cpp b/test/style/filter.test.cpp index bac3801223..e04a569203 100644 --- a/test/style/filter.test.cpp +++ b/test/style/filter.test.cpp @@ -11,6 +11,10 @@ #include <mbgl/style/filter.hpp> #include <mbgl/style/conversion/json.hpp> #include <mbgl/style/conversion/filter.hpp> +#include <mbgl/util/rapidjson.hpp> + +#include <rapidjson/writer.h> +#include <rapidjson/stringbuffer.h> using namespace mbgl; using namespace mbgl::style; @@ -39,6 +43,42 @@ void invalidFilter(const char * json) { EXPECT_NE(error.message, ""); } +void writeJSON(rapidjson::Writer<rapidjson::StringBuffer>& writer, const Value& value) { + value.match( + [&] (const NullValue&) { writer.Null(); }, + [&] (bool b) { writer.Bool(b); }, + [&] (uint64_t t) { writer.Uint64(t); }, + [&] (int64_t t) { writer.Int64(t); }, + [&] (double f) { + // make sure integer values are stringified without trailing ".0". + f == std::floor(f) ? writer.Int(f) : writer.Double(f); + }, + [&] (const std::string& s) { writer.String(s); }, + [&] (const std::vector<Value>& arr) { + writer.StartArray(); + for(const auto& item : arr) { + writeJSON(writer, item); + } + writer.EndArray(); + }, + [](const auto&) { + }); +} + +std::string stringifyFilter(const char * json) { + conversion::Error error; + optional<Filter> filter = conversion::convertJSON<Filter>(json, error); + EXPECT_TRUE(bool(filter)); + EXPECT_EQ(error.message, ""); + + auto value = filter->serialize(); + + rapidjson::StringBuffer s; + rapidjson::Writer<rapidjson::StringBuffer> writer(s); + writeJSON(writer, value); + return s.GetString(); +} + TEST(Filter, EqualsNull) { auto f = R"(["==", "foo", null])"; ASSERT_TRUE(filter(f, {{ "foo", mapbox::geometry::null_value }})); @@ -181,6 +221,21 @@ TEST(Filter, LegacyProperty) { ASSERT_FALSE(filter("[\"==\", \"two\", 4]", {{"two", std::string("2")}})); } +TEST(Filter, Serialize) { + std::string json = R"(["any",["==","foo",0],["==","foo",1]])"; + EXPECT_EQ(stringifyFilter(json.c_str()), std::string(json)); + + json = R"(["<=","two",2])"; + EXPECT_EQ(stringifyFilter(json.c_str()), std::string(json)); + + //Constant folding for Expression filters + json = R"(["==",["+",1,1],2])"; + EXPECT_EQ(stringifyFilter(json.c_str()), std::string("true")); + + json = R"(["all",["==",["get","foo"],0],["==",["get","foo"],1]])"; + EXPECT_EQ(stringifyFilter(json.c_str()), std::string(json)); +} + TEST(Filter, ExpressionLegacyMix) { conversion::Error error; optional<Filter> filter = conversion::convertJSON<Filter>(R"(["any", ["all", ["==", ["geometry-type"], "LineString"]], ["==", "x", 1]])", error); |