From 1d8664a2ae3822ba4d45973640c0068c65a237e7 Mon Sep 17 00:00:00 2001 From: Asheem Mamoowala Date: Fri, 29 Jun 2018 12:56:44 -0700 Subject: Keep original input for filters using legacy syntax. --- test/style/filter.test.cpp | 55 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) (limited to 'test/style/filter.test.cpp') 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 #include #include +#include + +#include +#include using namespace mbgl; using namespace mbgl::style; @@ -39,6 +43,42 @@ void invalidFilter(const char * json) { EXPECT_NE(error.message, ""); } +void writeJSON(rapidjson::Writer& 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& 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 = conversion::convertJSON(json, error); + EXPECT_TRUE(bool(filter)); + EXPECT_EQ(error.message, ""); + + auto value = filter->serialize(); + + rapidjson::StringBuffer s; + rapidjson::Writer 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 = conversion::convertJSON(R"(["any", ["all", ["==", ["geometry-type"], "LineString"]], ["==", "x", 1]])", error); -- cgit v1.2.1