From 647959a85327660a8d0b35da0b8a247820a36c15 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Fri, 29 Jun 2018 14:32:39 -0700 Subject: [PATCH] [core] Add assertions to expression constructors --- include/mbgl/style/expression/assertion.hpp | 5 +---- include/mbgl/style/expression/interpolate.hpp | 11 +++-------- include/mbgl/style/expression/step.hpp | 11 +++-------- src/mbgl/style/expression/assertion.cpp | 8 ++++++++ src/mbgl/style/expression/coercion.cpp | 1 + src/mbgl/style/expression/equals.cpp | 16 +++++++++------- src/mbgl/style/expression/interpolate.cpp | 11 +++++++++++ src/mbgl/style/expression/step.cpp | 10 ++++++++++ test/style/conversion/stringify.test.cpp | 2 +- 9 files changed, 47 insertions(+), 28 deletions(-) diff --git a/include/mbgl/style/expression/assertion.hpp b/include/mbgl/style/expression/assertion.hpp index d1e919b10..90da16b06 100644 --- a/include/mbgl/style/expression/assertion.hpp +++ b/include/mbgl/style/expression/assertion.hpp @@ -13,10 +13,7 @@ namespace expression { class Assertion : public Expression { public: - Assertion(type::Type type_, std::vector> inputs_) : - Expression(type_), - inputs(std::move(inputs_)) - {} + Assertion(type::Type type_, std::vector> inputs_); static ParseResult parse(const mbgl::style::conversion::Convertible& value, ParsingContext& ctx); diff --git a/include/mbgl/style/expression/interpolate.hpp b/include/mbgl/style/expression/interpolate.hpp index 0e7682007..0aca6909a 100644 --- a/include/mbgl/style/expression/interpolate.hpp +++ b/include/mbgl/style/expression/interpolate.hpp @@ -19,14 +19,9 @@ ParseResult parseInterpolate(const mbgl::style::conversion::Convertible& value, class Interpolate : public Expression { public: Interpolate(const type::Type& type_, - Interpolator interpolator_, - std::unique_ptr input_, - std::map> stops_ - ) : Expression(type_), - interpolator(std::move(interpolator_)), - input(std::move(input_)), - stops(std::move(stops_)) - {} + Interpolator interpolator_, + std::unique_ptr input_, + std::map> stops_); const std::unique_ptr& getInput() const { return input; } const Interpolator& getInterpolator() const { return interpolator; } diff --git a/include/mbgl/style/expression/step.hpp b/include/mbgl/style/expression/step.hpp index 2f9524a53..24e29b1a4 100644 --- a/include/mbgl/style/expression/step.hpp +++ b/include/mbgl/style/expression/step.hpp @@ -1,4 +1,3 @@ - #pragma once #include @@ -10,7 +9,6 @@ #include #include - namespace mbgl { namespace style { namespace expression { @@ -18,12 +16,8 @@ namespace expression { class Step : public Expression { public: Step(const type::Type& type_, - std::unique_ptr input_, - std::map> stops_ - ) : Expression(type_), - input(std::move(input_)), - stops(std::move(stops_)) - {} + std::unique_ptr input_, + std::map> stops_); EvaluationResult evaluate(const EvaluationContext& params) const override; void eachChild(const std::function& visit) const override; @@ -40,6 +34,7 @@ public: mbgl::Value serialize() const override; std::string getOperator() const override { return "step"; } + private: const std::unique_ptr input; const std::map> stops; diff --git a/src/mbgl/style/expression/assertion.cpp b/src/mbgl/style/expression/assertion.cpp index d6f3f1b58..eefc9e24f 100644 --- a/src/mbgl/style/expression/assertion.cpp +++ b/src/mbgl/style/expression/assertion.cpp @@ -6,6 +6,14 @@ namespace style { namespace expression { using namespace mbgl::style::conversion; + +Assertion::Assertion(type::Type type_, std::vector> inputs_) : + Expression(type_), + inputs(std::move(inputs_)) +{ + assert(!inputs.empty()); +} + ParseResult Assertion::parse(const Convertible& value, ParsingContext& ctx) { static std::unordered_map types { {"string", type::String}, diff --git a/src/mbgl/style/expression/coercion.cpp b/src/mbgl/style/expression/coercion.cpp index 0166d144e..11172a366 100644 --- a/src/mbgl/style/expression/coercion.cpp +++ b/src/mbgl/style/expression/coercion.cpp @@ -71,6 +71,7 @@ Coercion::Coercion(type::Type type_, std::vector> in Expression(std::move(type_)), inputs(std::move(inputs_)) { + assert(!inputs.empty()); type::Type t = getType(); if (t.is()) { coerceSingleValue = toNumber; diff --git a/src/mbgl/style/expression/equals.cpp b/src/mbgl/style/expression/equals.cpp index 6d963cc1d..f2f59e31e 100644 --- a/src/mbgl/style/expression/equals.cpp +++ b/src/mbgl/style/expression/equals.cpp @@ -4,11 +4,20 @@ namespace mbgl { namespace style { namespace expression { +static bool isComparableType(const type::Type& type) { + return type == type::String || + type == type::Number || + type == type::Boolean || + type == type::Null; +} + Equals::Equals(std::unique_ptr lhs_, std::unique_ptr rhs_, bool negate_) : Expression(type::Boolean), lhs(std::move(lhs_)), rhs(std::move(rhs_)), negate(negate_) { + assert(isComparableType(lhs->getType()) || isComparableType(rhs->getType())); + assert(lhs->getType() == rhs->getType() || lhs->getType() == type::Value || rhs->getType() == type::Value); } EvaluationResult Equals::evaluate(const EvaluationContext& params) const { @@ -41,13 +50,6 @@ std::vector> Equals::possibleOutputs() const { return {{ true }, { false }}; } -static bool isComparableType(const type::Type& type) { - return type == type::String || - type == type::Number || - type == type::Boolean || - type == type::Null; -} - using namespace mbgl::style::conversion; ParseResult Equals::parse(const Convertible& value, ParsingContext& ctx) { std::size_t length = arrayLength(value); diff --git a/src/mbgl/style/expression/interpolate.cpp b/src/mbgl/style/expression/interpolate.cpp index c3a1f0dc9..a9bb3bf05 100644 --- a/src/mbgl/style/expression/interpolate.cpp +++ b/src/mbgl/style/expression/interpolate.cpp @@ -260,6 +260,17 @@ ParseResult createInterpolate(type::Type type, ); } +Interpolate::Interpolate(const type::Type& type_, + Interpolator interpolator_, + std::unique_ptr input_, + std::map> stops_) + : Expression(type_), + interpolator(std::move(interpolator_)), + input(std::move(input_)), + stops(std::move(stops_)) { + assert(input->getType() == type::Number); +} + std::vector> Interpolate::possibleOutputs() const { std::vector> result; for (const auto& stop : stops) { diff --git a/src/mbgl/style/expression/step.cpp b/src/mbgl/style/expression/step.cpp index ddaf9417c..f42a2721a 100644 --- a/src/mbgl/style/expression/step.cpp +++ b/src/mbgl/style/expression/step.cpp @@ -8,6 +8,16 @@ namespace mbgl { namespace style { namespace expression { +Step::Step(const type::Type& type_, + std::unique_ptr input_, + std::map> stops_) + : Expression(type_), + input(std::move(input_)), + stops(std::move(stops_)) +{ + assert(input->getType() == type::Number); +} + EvaluationResult Step::evaluate(const EvaluationContext& params) const { const EvaluationResult evaluatedInput = input->evaluate(params); if (!evaluatedInput) { diff --git a/test/style/conversion/stringify.test.cpp b/test/style/conversion/stringify.test.cpp index 567c02203..bc73b0ced 100644 --- a/test/style/conversion/stringify.test.cpp +++ b/test/style/conversion/stringify.test.cpp @@ -79,7 +79,7 @@ TEST(Stringify, Value) { TEST(Stringify, Filter) { using namespace mbgl::style::expression::dsl; ASSERT_EQ(stringify(Filter()), "null"); - ASSERT_EQ(stringify(Filter(eq(literal("a"), literal(1.0)))), "[\"==\",\"a\",1.0]"); + ASSERT_EQ(stringify(Filter(eq(literal("a"), literal("b")))), "[\"==\",\"a\",\"b\"]"); } TEST(Stringify, CameraFunction) {