diff options
author | Anand Thakker <anandthakker@users.noreply.github.com> | 2017-11-08 12:34:02 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-08 12:34:02 -0500 |
commit | f648cfeef6544755fdb10c3cf8847e878d70e0ff (patch) | |
tree | 49800ebd34969b787681691f1219c6396ed58579 /test/style | |
parent | 9aac976104f4c6453cf9e79e03a002565720f213 (diff) | |
download | qtlocation-mapboxgl-f648cfeef6544755fdb10c3cf8847e878d70e0ff.tar.gz |
Implement Expressions (#9439)
Ports https://github.com/mapbox/mapbox-gl-js/pull/4777 (and its several follow-ups)
Diffstat (limited to 'test/style')
-rw-r--r-- | test/style/conversion/function.test.cpp | 28 | ||||
-rw-r--r-- | test/style/expression/expression.test.cpp | 91 | ||||
-rw-r--r-- | test/style/expression/util.test.cpp | 23 |
3 files changed, 142 insertions, 0 deletions
diff --git a/test/style/conversion/function.test.cpp b/test/style/conversion/function.test.cpp index 9e8a6b3a7f..a48be2c075 100644 --- a/test/style/conversion/function.test.cpp +++ b/test/style/conversion/function.test.cpp @@ -3,6 +3,8 @@ #include <mbgl/style/conversion/json.hpp> #include <mbgl/style/conversion/constant.hpp> #include <mbgl/style/conversion/function.hpp> +#include <mbgl/style/conversion/data_driven_property_value.hpp> +#include <mbgl/util/rapidjson.hpp> using namespace mbgl; using namespace mbgl::style; @@ -50,3 +52,29 @@ TEST(StyleConversion, Function) { ASSERT_FALSE(fn9); ASSERT_EQ("function base must be a number", error.message); } + +TEST(StyleConversion, CompositeFunctionExpression) { + Error error; + + auto parseFunction = [&](const std::string& src) { + JSDocument doc; + doc.Parse<0>(src); + return convert<DataDrivenPropertyValue<float>>(doc, error); + }; + + auto fn1 = parseFunction(R"(["interpolate", ["linear"], ["zoom"], 0, ["number", ["get", "x"]], 10, 10])"); + ASSERT_TRUE(fn1); + + auto fn2 = parseFunction(R"(["coalesce", ["interpolate", ["linear"], ["zoom"], 0, ["number", ["get", "x"]], 10, 10], 0])"); + ASSERT_TRUE(fn2); + + auto fn3 = parseFunction(R"(["let", "a", 0, ["interpolate", ["linear"], ["zoom"], 0, ["number", ["get", "x"]], 10, 10] ])"); + ASSERT_TRUE(fn3); + + auto fn4 = parseFunction(R"(["coalesce", ["let", "a", 0, ["interpolate", ["linear"], ["zoom"], 0, ["number", ["get", "x"]], 10, 10], 0 ])"); + ASSERT_TRUE(fn4); + + auto fn5 = parseFunction(R"(["coalesce", ["interpolate", ["linear"], ["number", ["get", "x"]], 0, ["zoom"], 10, 10], 0])"); + ASSERT_FALSE(fn5); + ASSERT_EQ(R"("zoom" expression may only be used as input to a top-level "step" or "interpolate" expression.)", error.message); +} diff --git a/test/style/expression/expression.test.cpp b/test/style/expression/expression.test.cpp new file mode 100644 index 0000000000..694569695c --- /dev/null +++ b/test/style/expression/expression.test.cpp @@ -0,0 +1,91 @@ +#include <mbgl/test/util.hpp> +#include <mbgl/util/io.hpp> +#include <mbgl/style/conversion.hpp> +#include <mbgl/util/rapidjson.hpp> +#include <mbgl/style/rapidjson_conversion.hpp> +#include <mbgl/style/expression/is_expression.hpp> + +#include <rapidjson/document.h> + +#include <iostream> +#include <fstream> +#include <dirent.h> + + +using namespace mbgl; +using namespace mbgl::style; + +TEST(Expression, IsExpression) { + rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> spec; + spec.Parse<0>(util::read_file("mapbox-gl-js/src/style-spec/reference/v8.json").c_str()); + ASSERT_FALSE(spec.HasParseError()); + ASSERT_TRUE(spec.IsObject() && + spec.HasMember("expression_name") && + spec["expression_name"].IsObject() && + spec["expression_name"].HasMember("values") && + spec["expression_name"]["values"].IsObject()); + + const auto& allExpressions = spec["expression_name"]["values"]; + + for(auto& entry : allExpressions.GetObject()) { + const std::string name { entry.name.GetString(), entry.name.GetStringLength() }; + JSDocument document; + document.Parse<0>(R"([")" + name + R"("])"); + + const JSValue* expression = &document; + EXPECT_TRUE(expression::isExpression(conversion::Convertible(expression))) << name; + } +} + +class ExpressionEqualityTest : public ::testing::TestWithParam<std::string> {}; + +TEST_P(ExpressionEqualityTest, ExpressionEquality) { + const std::string base = std::string("test/fixtures/expression_equality/") + GetParam(); + + std::string error; + auto parse = [&](std::string filename, std::string& error_) -> std::unique_ptr<expression::Expression> { + rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> document; + document.Parse<0>(util::read_file(filename).c_str()); + assert(!document.HasParseError()); + const JSValue* expression = &document; + expression::ParsingContext ctx; + expression::ParseResult parsed = ctx.parse(conversion::Convertible(expression)); + if (!parsed) { + error_ = ctx.getErrors().size() > 0 ? ctx.getErrors()[0].message : "failed to parse"; + }; + return std::move(*parsed); + }; + + std::unique_ptr<expression::Expression> expression_a1 = parse(base + ".a.json", error); + ASSERT_TRUE(expression_a1) << GetParam() << ": " << error; + + std::unique_ptr<expression::Expression> expression_a2 = parse(base + ".a.json", error); + ASSERT_TRUE(expression_a2) << GetParam() << ": " << error; + + std::unique_ptr<expression::Expression> expression_b = parse(base + ".b.json", error); + ASSERT_TRUE(expression_b) << GetParam() << ": " << error; + + + EXPECT_TRUE(*expression_a1 == *expression_a2); + EXPECT_TRUE(*expression_a1 != *expression_b); +} + +INSTANTIATE_TEST_CASE_P(Expression, ExpressionEqualityTest, ::testing::ValuesIn([] { + std::vector<std::string> names; + const std::string ending = ".a.json"; + + const std::string style_directory = "test/fixtures/expression_equality"; + DIR *dir = opendir(style_directory.c_str()); + if (dir != nullptr) { + for (dirent *dp = nullptr; (dp = readdir(dir)) != nullptr;) { + const std::string name = dp->d_name; + if (name.length() >= ending.length() && name.compare(name.length() - ending.length(), ending.length(), ending) == 0) { + names.push_back(name.substr(0, name.length() - ending.length())); + } + } + closedir(dir); + } + + EXPECT_GT(names.size(), 0u); + return names; +}())); diff --git a/test/style/expression/util.test.cpp b/test/style/expression/util.test.cpp new file mode 100644 index 0000000000..0337cd871f --- /dev/null +++ b/test/style/expression/util.test.cpp @@ -0,0 +1,23 @@ +#include <mbgl/test/util.hpp> +#include <mbgl/style/expression/util.hpp> + +using namespace mbgl; +using namespace mbgl::style::expression; + +TEST(Expression, Util_rgba) { + Result<Color> valid = rgba(0, 0, 0, 0); + ASSERT_TRUE(valid); + ASSERT_EQ(valid->r, 0); + ASSERT_EQ(valid->g, 0); + ASSERT_EQ(valid->b, 0); + ASSERT_EQ(valid->a, 0); + + ASSERT_FALSE(rgba(0, 0, 0, -0.1)); + ASSERT_FALSE(rgba(0, 0, 0, 1.1)); + ASSERT_FALSE(rgba(0, 0, -1, 1)); + ASSERT_FALSE(rgba(0, 0, 256, 1)); + ASSERT_FALSE(rgba(0, -1, 0, 1)); + ASSERT_FALSE(rgba(0, 256, 0, 1)); + ASSERT_FALSE(rgba(-1, 1, 0, 1)); + ASSERT_FALSE(rgba(-256, 1, 0, 1)); +} |