From 3b8e50a2a941d1786da4cbacff52734f0797c936 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Fri, 13 Jan 2017 16:20:57 +0100 Subject: [PATCH] [core] add tests for function parsing --- cmake/test-files.cmake | 1 + test/style/conversion/function.test.cpp | 54 +++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 test/style/conversion/function.test.cpp diff --git a/cmake/test-files.cmake b/cmake/test-files.cmake index cd40d177c..32ee23699 100644 --- a/cmake/test-files.cmake +++ b/cmake/test-files.cmake @@ -72,6 +72,7 @@ set(MBGL_TEST_FILES test/storage/resource.test.cpp # style/conversion + test/style/conversion/function.test.cpp test/style/conversion/geojson_options.test.cpp test/style/conversion/stringify.test.cpp diff --git a/test/style/conversion/function.test.cpp b/test/style/conversion/function.test.cpp new file mode 100644 index 000000000..e93207ea1 --- /dev/null +++ b/test/style/conversion/function.test.cpp @@ -0,0 +1,54 @@ +#include + +#include +#include +#include +#include +#include + +using namespace mbgl; +using namespace mbgl::style; +using namespace mbgl::style::conversion; + +auto parseFunction(const std::string& src) { + JSDocument doc; + doc.Parse<0>(src); + return convert>(doc); +} + +TEST(StyleConversion, Function) { + auto fn1 = parseFunction("{\"stops\":[]}"); + ASSERT_FALSE(fn1); + ASSERT_EQ("function must have at least one stop", fn1.error().message); + + auto fn2 = parseFunction("{\"stops\":[1]}"); + ASSERT_FALSE(fn2); + ASSERT_EQ("function stop must be an array", fn2.error().message); + + auto fn3 = parseFunction("{\"stops\":[[]]}"); + ASSERT_FALSE(fn3); + ASSERT_EQ("function stop must have two elements", fn3.error().message); + + auto fn4 = parseFunction("{\"stops\":[[-1,-1]]}"); + ASSERT_TRUE(bool(fn4)); + + auto fn5 = parseFunction("{\"stops\":[[0,1,2]]}"); + ASSERT_FALSE(fn5); + ASSERT_EQ("function stop must have two elements", fn5.error().message); + + auto fn6 = parseFunction("{\"stops\":[[0,\"x\"]]}"); + ASSERT_FALSE(fn6); + ASSERT_EQ("value must be a number", fn6.error().message); + + auto fn7 = parseFunction("{}"); + ASSERT_FALSE(fn7); + ASSERT_EQ("function value must specify stops", fn7.error().message); + + auto fn8 = parseFunction("[]"); + ASSERT_FALSE(fn8); + ASSERT_EQ("function must be an object", fn8.error().message); + + auto fn9 = parseFunction("{\"stops\":[[0,0]],\"base\":false}"); + ASSERT_FALSE(fn9); + ASSERT_EQ("function base must be a number", fn9.error().message); +}