summaryrefslogtreecommitdiff
path: root/test/miscellaneous/style_parser.cpp
blob: 3cef6ae6141d99a8dceda81ce5848051eef81b27 (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
#include "../util.hpp"

#include <mbgl/style/style.hpp>
#include <mbgl/util/io.hpp>

#include <rapidjson/document.h>

#include "../fixtures/fixture_log.hpp"

#include <iostream>
#include <fstream>

#include <dirent.h>

using namespace mbgl;

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

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

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

    const std::string style_path = base + ".style.json";
    const std::string info = util::read_file(base + ".info.json");

    // Parse settings.
    rapidjson::Document doc;
    doc.Parse<0>((const char *const)info.c_str());
    ASSERT_FALSE(doc.HasParseError());
    ASSERT_TRUE(doc.IsObject());

    std::ifstream stylefile(style_path);
    ASSERT_TRUE(stylefile.good());
    std::stringstream stylejson;
    stylejson << stylefile.rdbuf();

    const FixtureLogBackend &log = Log::Set<FixtureLogBackend>();

    Style style;
    style.loadJSON((const uint8_t *)stylejson.str().c_str());

    for (auto it = doc.MemberBegin(), end = doc.MemberEnd(); it != end; it++) {
        const std::string name { it->name.GetString(), it->name.GetStringLength() };
        const rapidjson::Value &value = it->value;
        ASSERT_EQ(true, value.IsObject());

        if (value.HasMember("log")) {
            const rapidjson::Value &js_log = value["log"];
            ASSERT_EQ(true, js_log.IsArray());
            for (rapidjson::SizeType i = 0; i < js_log.Size(); i++) {
                const rapidjson::Value &js_entry = js_log[i];
                ASSERT_EQ(true, js_entry.IsArray());

                const uint32_t count = js_entry[rapidjson::SizeType(0)].GetUint();
                const FixtureLogBackend::LogMessage message {
                    EventSeverityClass(js_entry[rapidjson::SizeType(1)].GetString()),
                    EventClass(js_entry[rapidjson::SizeType(2)].GetString()),
                    js_entry[rapidjson::SizeType(3)].GetString()
                };

                EXPECT_EQ(count, log.count(message)) << "Message: " << message << std::endl;
            }
        }

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

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

INSTANTIATE_TEST_CASE_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(), 0ul);
    return names;
}()));