From 54a81d0f9b9903213c55d89ee87eeacbadd5486c Mon Sep 17 00:00:00 2001 From: Mike Morris Date: Fri, 18 Jul 2014 14:09:33 -0400 Subject: [PATCH] migrate style_parser tests to use info and style json fixtures --- .gitignore | 1 + bin/{style.json => default.style.json} | 0 mapboxgl.gyp | 41 +------ test/fixtures/style_parser/default.info.json | 6 ++ test/style.cpp | 101 ------------------ test/style_parser.cpp | 106 +++++++++++++++++++ test/test.gyp | 10 +- 7 files changed, 123 insertions(+), 142 deletions(-) rename bin/{style.json => default.style.json} (100%) create mode 100644 test/fixtures/style_parser/default.info.json delete mode 100644 test/style.cpp create mode 100644 test/style_parser.cpp diff --git a/.gitignore b/.gitignore index c69b0331e..7eb63a125 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ /src/shader/shaders_gles2.cpp /bin/style.bin.js /test/fixtures/styles/index.html +/test/fixtures/style_parser/default.style.json diff --git a/bin/style.json b/bin/default.style.json similarity index 100% rename from bin/style.json rename to bin/default.style.json diff --git a/mapboxgl.gyp b/mapboxgl.gyp index e8a217a3f..0c08989ce 100644 --- a/mapboxgl.gyp +++ b/mapboxgl.gyp @@ -64,7 +64,7 @@ { 'action_name': 'Build Stylesheet', 'inputs': [ - 'bin/style.json', + 'bin/default.style.json', ], 'outputs': [ '<(SHARED_INTERMEDIATE_DIR)/bin/style.min.js', @@ -78,35 +78,6 @@ ], } }, - { - 'target_name': 'build_stylesheet_fixtures', - 'type': 'none', - 'hard_dependency': 1, - 'dependencies': [ - 'npm_install' - ], - 'actions': [ - { - 'action_name': 'Build Stylesheet Fixtures', - 'inputs': [ - 'bin/style.json', - ], - 'outputs': [ - '<(SHARED_INTERMEDIATE_DIR)/bin/fuzz-colors.min.js', - '<(SHARED_INTERMEDIATE_DIR)/bin/fuzz-functions.min.js', - '<(SHARED_INTERMEDIATE_DIR)/bin/fuzz-layers.min.js', - ], - 'action': ['<@(node)', 'bin/build-fixtures.js', '<@(_inputs)', '<(SHARED_INTERMEDIATE_DIR)/bin'] - } - ], - 'direct_dependent_settings': { - 'sources': [ - '<(SHARED_INTERMEDIATE_DIR)/bin/fuzz-colors.min.js', - '<(SHARED_INTERMEDIATE_DIR)/bin/fuzz-functions.min.js', - '<(SHARED_INTERMEDIATE_DIR)/bin/fuzz-layers.min.js', - ], - } - }, { 'target_name': 'copy_default_stylesheet', 'type': 'none', @@ -124,20 +95,18 @@ ] }, { - 'target_name': 'copy_stylesheet_fixtures', + 'target_name': 'copy_default_stylesheet_fixtures', 'type': 'none', 'hard_dependency': 1, 'dependencies': [ - 'build_stylesheet_fixtures' + 'build_stylesheet' ], 'copies': [ { 'files': [ - '<(SHARED_INTERMEDIATE_DIR)/bin/fuzz-colors.min.js', - '<(SHARED_INTERMEDIATE_DIR)/bin/fuzz-functions.min.js', - '<(SHARED_INTERMEDIATE_DIR)/bin/fuzz-layers.min.js', + 'bin/default.style.json', ], - 'destination': '<(PRODUCT_DIR)' + 'destination': 'test/fixtures/style_parser' } ] }, diff --git a/test/fixtures/style_parser/default.info.json b/test/fixtures/style_parser/default.info.json new file mode 100644 index 000000000..9c25a2f48 --- /dev/null +++ b/test/fixtures/style_parser/default.info.json @@ -0,0 +1,6 @@ +{ + "default": { + "log": [ + ] + } +} diff --git a/test/style.cpp b/test/style.cpp deleted file mode 100644 index e98740e40..000000000 --- a/test/style.cpp +++ /dev/null @@ -1,101 +0,0 @@ -#include "gtest/gtest.h" - -#include -#include "./fixtures/fixture_log.hpp" - -#include -#include - -using namespace mbgl; - -typedef std::vector> Messages; - -void checkMessages(const FixtureLogBackend &log, Messages messages) { - for (auto &it : messages) { - const FixtureLogBackend::LogMessage message { - EventSeverityClass("WARNING"), - EventClass("ParseStyle"), - it.second - }; - - EXPECT_EQ(it.first, log.count(message)) << "Message: " - << message << std::endl; - } - - const auto &unchecked = log.unchecked(); - if (unchecked.size()) { - std::cerr << "Unchecked Log Messages: " << std::endl - << unchecked; - } - - ASSERT_EQ(0, unchecked.size()); -} - -TEST(Style, Style) { - const FixtureLogBackend &log = Log::Set(); - - std::ifstream stylefile("./style.min.js"); - ASSERT_TRUE(stylefile.good()); - std::stringstream stylejson; - stylejson << stylefile.rdbuf(); - - Style style; - style.loadJSON((const uint8_t *)stylejson.str().c_str()); - - checkMessages(log, {}); -} - -TEST(Style, Colors) { - const FixtureLogBackend &log = Log::Set(); - - std::ifstream stylefile("./fuzz-colors.min.js"); - ASSERT_TRUE(stylefile.good()); - std::stringstream stylejson; - stylejson << stylefile.rdbuf(); - - Style style; - style.loadJSON((const uint8_t *)stylejson.str().c_str()); - - checkMessages(log, { - {84, "value of 'line-width' must be a number, or a number function"}, - {6, "value of 'line-opacity' must be a number, or a number function"}, - {12, "value of 'text-size' must be a number, or a number function"} - }); -} - -TEST(Style, Functions) { - const FixtureLogBackend &log = Log::Set(); - - std::ifstream stylefile("./fuzz-functions.min.js"); - ASSERT_TRUE(stylefile.good()); - std::stringstream stylejson; - stylejson << stylefile.rdbuf(); - - Style style; - style.loadJSON((const uint8_t *)stylejson.str().c_str()); - - checkMessages(log, { - {28, "stop must have zoom level and value specification"}, - {12, "function must specify a function type"} - }); -} - -TEST(Style, Layers) { - const FixtureLogBackend &log = Log::Set(); - - std::ifstream stylefile("./fuzz-layers.min.js"); - ASSERT_TRUE(stylefile.good()); - std::stringstream stylejson; - stylejson << stylefile.rdbuf(); - - Style style; - style.loadJSON((const uint8_t *)stylejson.str().c_str()); - - checkMessages(log, { - {20, "stop must have zoom level and value specification"}, - {6, "function must specify a function type"}, - {1, "value of 'line-width' must be a number, or a number function"}, - {1, "array value has unexpected number of elements"}, - {1, "value of 'line-opacity' must be a number, or a number function"} - }); -} diff --git a/test/style_parser.cpp b/test/style_parser.cpp new file mode 100644 index 000000000..ccc1ec535 --- /dev/null +++ b/test/style_parser.cpp @@ -0,0 +1,106 @@ +#include "gtest/gtest.h" + +#include +#include + +#include + +#include "./fixtures/fixture_log.hpp" + +#include +#include + +#include + +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 Message; +typedef std::vector Messages; + +class StyleParserTest : public ::testing::TestWithParam {}; + +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(); + + 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 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; +}())); diff --git a/test/test.gyp b/test/test.gyp index 8ecce6a32..f91f11808 100644 --- a/test/test.gyp +++ b/test/test.gyp @@ -107,22 +107,22 @@ ] }, { - "target_name": "style", - "product_name": "test_style", + "target_name": "style_parser", + "product_name": "test_style_parser", "type": "executable", "libraries": [ "-lpthread", ], "sources": [ "./main.cpp", - "./style.cpp", + "./style_parser.cpp", "./fixtures/fixture_log.hpp", "./fixtures/fixture_log.cpp", ], "dependencies": [ "../deps/gtest/gtest.gyp:gtest", "../mapboxgl.gyp:mapboxgl", - "../mapboxgl.gyp:copy_stylesheet_fixtures", + "../mapboxgl.gyp:copy_default_stylesheet_fixtures", ] }, { @@ -223,7 +223,7 @@ "tile", "functions", "headless", - "style", + "style_parser", "comparisons", ], }