diff options
author | Mike Morris <michael.patrick.morris@gmail.com> | 2014-07-18 14:09:33 -0400 |
---|---|---|
committer | Mike Morris <michael.patrick.morris@gmail.com> | 2014-07-18 14:09:33 -0400 |
commit | 54a81d0f9b9903213c55d89ee87eeacbadd5486c (patch) | |
tree | 064e86dd0b806c4cfca0082e255edc989a17cfe6 /test/style_parser.cpp | |
parent | 4d4fd3155c9d649628f776ed298d3812f363b354 (diff) | |
download | qtlocation-mapboxgl-54a81d0f9b9903213c55d89ee87eeacbadd5486c.tar.gz |
migrate style_parser tests to use info and style json fixtures
Diffstat (limited to 'test/style_parser.cpp')
-rw-r--r-- | test/style_parser.cpp | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/test/style_parser.cpp b/test/style_parser.cpp new file mode 100644 index 0000000000..ccc1ec5350 --- /dev/null +++ b/test/style_parser.cpp @@ -0,0 +1,106 @@ +#include "gtest/gtest.h" + +#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> + +const std::string base_directory = []{ + std::string fn = __FILE__; + fn.erase(fn.find_last_of("/")); + return fn + "/fixtures/style_parser"; +}(); + +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, render) { + const std::string &base = base_directory + "/" + 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_EQ(false, doc.HasParseError()); + ASSERT_EQ(true, doc.IsObject()); + + std::ifstream stylefile(style_path); + ASSERT_TRUE(stylefile.good()); + std::stringstream stylejson; + stylejson << stylefile.rdbuf(); + + Style style; + style.loadJSON((const uint8_t *)stylejson.str().c_str()); + + for (auto it = doc.MemberBegin(), end = doc.MemberEnd(); it != end; it++) { + const FixtureLogBackend &log = Log::Set<FixtureLogBackend>(); + + 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()); + if (js_entry.Size() == 4) { + const uint32_t count = js_entry[rapidjson::SizeType(0)].GetUint(); + if (js_entry[rapidjson::SizeType(3)].IsString()) { + 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; + } + } else { + FAIL(); + } + } + } + + const auto &unchecked = log.unchecked(); + if (unchecked.size()) { + std::cerr << "Unchecked Log Messages (" << base << "): " + << std::endl << unchecked; + } + + ASSERT_EQ(0, unchecked.size()); + } +} + +INSTANTIATE_TEST_CASE_P(StyleParser, StyleParserTest, ::testing::ValuesIn([] { + std::vector<std::string> names; + const std::string ending = ".info.json"; + + DIR *dir = opendir(base_directory.c_str()); + if (dir == nullptr) { + return names; + } + + 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); + + return names; +}())); |