summaryrefslogtreecommitdiff
path: root/test/style/style_parser.test.cpp
blob: 7607dbd0777b0b85e4b222252742e8b146c045e0 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <mbgl/test/util.hpp>
#include <mbgl/test/fixture_log_observer.hpp>

#include <mbgl/style/parser.hpp>
#include <mbgl/util/io.hpp>
#include <mbgl/util/enum.hpp>
#include <mbgl/util/string.hpp>
#include <mbgl/util/tileset.hpp>

#include <rapidjson/document.h>

#include <iostream>
#include <fstream>
#include <set>

#include <dirent.h>

using namespace mbgl;

using Message = std::pair<uint32_t, std::string>;
using Messages = std::vector<Message>;

class StyleParserTest : public ::testing::TestWithParam<std::string> {};

TEST_P(StyleParserTest, ParseStyle) {
    const std::string base = std::string("test/fixtures/style_parser/") + GetParam();

    rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> infoDoc;
    infoDoc.Parse<0>(util::read_file(base + ".info.json").c_str());
    ASSERT_FALSE(infoDoc.HasParseError());
    ASSERT_TRUE(infoDoc.IsObject());

    auto observer = new FixtureLogObserver();
    Log::setObserver(std::unique_ptr<Log::Observer>(observer));

    style::Parser parser;
    auto error = parser.parse(util::read_file(base + ".style.json"));

    if (error) {
        Log::Error(Event::ParseStyle, "Failed to parse style: %s", util::toString(error).c_str());
    }

    ASSERT_TRUE(infoDoc.IsObject());
    for (const auto& property : infoDoc.GetObject()) {
        const std::string name { property.name.GetString(), property.name.GetStringLength() };
        const JSValue &value = property.value;
        ASSERT_EQ(true, value.IsObject());

        if (value.HasMember("log")) {
            const JSValue &js_log = value["log"];
            ASSERT_EQ(true, js_log.IsArray());
            for (auto& js_entry : js_log.GetArray()) {
                ASSERT_EQ(true, js_entry.IsArray());
                ASSERT_GE(4u, js_entry.Size());

                const uint32_t count = js_entry[rapidjson::SizeType(0)].GetUint();
                const FixtureLogObserver::LogMessage message {
                    *Enum<EventSeverity>::toEnum(js_entry[rapidjson::SizeType(1)].GetString()),
                    *Enum<Event>::toEnum(js_entry[rapidjson::SizeType(2)].GetString()),
                    int64_t(-1),
                    js_entry[rapidjson::SizeType(3)].GetString()
                };

                EXPECT_EQ(count, observer->count(message)) << "Message: " << message << std::endl;
            }
        }

        const auto &unchecked = observer->unchecked();
        if (unchecked.size()) {
            std::cerr << "Unchecked Log Messages (" << base << "/" << name << "): " << std::endl << unchecked;
        }

        ASSERT_EQ(0u, unchecked.size());
    }
}

INSTANTIATE_TEST_SUITE_P(StyleParser, StyleParserTest, ::testing::ValuesIn([] {
                             std::vector<std::string> names;
                             const std::string ending = ".info.json";

                             const std::string style_directory = "test/fixtures/style_parser";
                             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;
                         }()));

TEST(StyleParser, FontStacks) {
    style::Parser parser;
    parser.parse(util::read_file("test/fixtures/style_parser/font_stacks.json"));
    std::set<mbgl::FontStack> expected;
    expected.insert(FontStack({"a"}));
    expected.insert(FontStack({"a", "b"}));
    expected.insert(FontStack({"a", "b", "c"}));
    std::set<mbgl::FontStack> result = parser.fontStacks();
    ASSERT_EQ(expected, result);
}

TEST(StyleParser, FontStacksNoTextField) {
    style::Parser parser;
    parser.parse(R"({
        "version": 8,
        "layers": [{
            "id": "symbol",
            "type": "symbol",
            "source": "vector",
            "layout": {
                "text-font": ["a"]
            }
        }]
    })");
    auto result = parser.fontStacks();
    ASSERT_EQ(0u, result.size());
}

TEST(StyleParser, FontStacksCaseExpression) {
    style::Parser parser;
    parser.parse(R"({
        "version": 8,
        "layers": [{
            "id": "symbol",
            "type": "symbol",
            "source": "vector",
            "layout": {
                "text-field": "a",
                "text-font": ["case", ["==", "a", ["string", ["get", "text-font"]]], ["literal", ["Arial"]], ["literal", ["Helvetica"]]]
            }
        }]
    })");
    std::set<mbgl::FontStack> expected;
    expected.insert(FontStack({"Arial"}));
    expected.insert(FontStack({"Helvetica"}));
    std::set<mbgl::FontStack> result = parser.fontStacks();
    ASSERT_EQ(expected, result);
}

TEST(StyleParser, FontStacksMatchExpression) {
    style::Parser parser;
    parser.parse(R"({
        "version": 8,
        "layers": [{
            "id": "symbol",
            "type": "symbol",
            "source": "vector",
            "layout": {
                "text-field": "a",
                "text-font": ["match", ["get", "text-font"], "a", ["literal", ["Arial"]], ["literal", ["Helvetica"]]]
            }
        }]
    })");
    std::set<mbgl::FontStack> expected;
    expected.insert(FontStack({"Arial"}));
    expected.insert(FontStack({"Helvetica"}));
    std::set<mbgl::FontStack> result = parser.fontStacks();
    ASSERT_EQ(expected, result);
}

TEST(StyleParser, FontStacksStepExpression) {
    style::Parser parser;
    parser.parse(R"({
        "version": 8,
        "layers": [{
            "id": "symbol",
            "type": "symbol",
            "source": "vector",
            "layout": {
                "text-field": "a",
                "text-font": ["array", "string", ["step", ["get", "text-font"], ["literal", ["Arial"]], 0, ["literal", ["Helvetica"]]]]
            }
        }]
    })");
    std::set<mbgl::FontStack> expected;
    expected.insert(FontStack({"Arial"}));
    expected.insert(FontStack({"Helvetica"}));
    std::set<mbgl::FontStack> result = parser.fontStacks();
    ASSERT_EQ(expected, result);
}

TEST(StyleParser, FontStacksGetExpression) {
    // Invalid style, but not currently validated.
    style::Parser parser;
    parser.parse(R"({
        "version": 8,
        "layers": [{
            "id": "symbol",
            "type": "symbol",
            "source": "vector",
            "layout": {
                "text-field": "a",
                "text-font": ["array", "string", ["get", "text-font"]]
            }
        }]
    })");
    auto result = parser.fontStacks();
    ASSERT_EQ(0u, result.size());
}