summaryrefslogtreecommitdiff
path: root/test/style/expression/expression.test.cpp
blob: 42a8fdd7262dc06d8ea7594dacd025c54c5e66a8 (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
#include <mbgl/test/util.hpp>
#include <mbgl/util/io.hpp>
#include <mbgl/style/conversion_impl.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;

        // TODO:   "feature-state": https://github.com/mapbox/mapbox-gl-native/issues/12613
        // TODO: "interpolate-hcl": https://github.com/mapbox/mapbox-gl-native/issues/8720
        // TODO: "interpolate-lab": https://github.com/mapbox/mapbox-gl-native/issues/8720
        // TODO: "number-format": https://github.com/mapbox/mapbox-gl-native/issues/13632
        // TODO: "accumulated": https://github.com/mapbox/mapbox-gl-native/issues/14043
        if (name == "feature-state" || name == "interpolate-hcl" || name == "interpolate-lab" || name == "number-format" ||
            name == "accumulated") {
            if (expression::isExpression(conversion::Convertible(expression))) {
                ASSERT_TRUE(false) << "Expression name" << name << "is implemented - please update Expression.IsExpression test.";
            }
            continue;
        }

        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.parseExpression(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;
}()));