From 6f6e577acff72caa2f540ccfe4844578f4b00a2e Mon Sep 17 00:00:00 2001 From: kevin Date: Tue, 11 Feb 2020 19:34:58 +0800 Subject: Add expression_equality test for 'in' --- include/mbgl/style/expression/in.hpp | 3 +-- src/mbgl/style/expression/in.cpp | 41 +++++++++++++---------------- test/fixtures/expression_equality/in.a.json | 20 ++++++++++++++ test/fixtures/expression_equality/in.b.json | 20 ++++++++++++++ 4 files changed, 59 insertions(+), 25 deletions(-) create mode 100644 test/fixtures/expression_equality/in.a.json create mode 100644 test/fixtures/expression_equality/in.b.json diff --git a/include/mbgl/style/expression/in.hpp b/include/mbgl/style/expression/in.hpp index 6bd57e2833..f3e7551914 100644 --- a/include/mbgl/style/expression/in.hpp +++ b/include/mbgl/style/expression/in.hpp @@ -10,8 +10,7 @@ namespace expression { class In final : public Expression { public: - In(std::unique_ptr needle_, std::unique_ptr haystack_) - : Expression(Kind::In, type::Boolean), needle(std::move(needle_)), haystack(std::move(haystack_)) {} + In(std::unique_ptr needle_, std::unique_ptr haystack_); static ParseResult parse(const mbgl::style::conversion::Convertible& value, ParsingContext& ctx); diff --git a/src/mbgl/style/expression/in.cpp b/src/mbgl/style/expression/in.cpp index 28658b4a31..e0cdc9f260 100644 --- a/src/mbgl/style/expression/in.cpp +++ b/src/mbgl/style/expression/in.cpp @@ -11,37 +11,43 @@ namespace expression { namespace { bool isComparableType(type::Type type) { return type == type::Boolean || type == type::String || type == type::Number || type == type::Null || - type == type::Value; + type == type::Value; } -bool isComparableRuntimeValue(type::Type type) { +bool isComparableRuntimeType(type::Type type) { return type == type::Boolean || type == type::String || type == type::Number || type == type::Null; } -bool isSearchableRuntimeValue(type::Type type) { +bool isSearchableRuntimeType(type::Type type) { return type == type::String || type.is() || type == type::Null; } +} // namespace + +In::In(std::unique_ptr needle_, std::unique_ptr haystack_) + : Expression(Kind::In, type::Boolean), needle(std::move(needle_)), haystack(std::move(haystack_)) { + assert(isComparableType(needle->getType())); + assert(isSearchableRuntimeType(haystack->getType()) || haystack->getType() == type::Value); } EvaluationResult In::evaluate(const EvaluationContext& params) const { - const EvaluationResult evaluatedNeedle = needle->evaluate(params); - if (!evaluatedNeedle) { - return evaluatedNeedle.error(); - } - const EvaluationResult evaluatedHaystack = haystack->evaluate(params); if (!evaluatedHaystack) { return evaluatedHaystack.error(); } + const EvaluationResult evaluatedNeedle = needle->evaluate(params); + if (!evaluatedNeedle) { + return evaluatedNeedle.error(); + } + type::Type evaluatedNeedleType = typeOf(*evaluatedNeedle); - if (!isComparableRuntimeValue(evaluatedNeedleType)) { + if (!isComparableRuntimeType(evaluatedNeedleType)) { return EvaluationError{"Expected first argument to be of type boolean, string or number, but found " + toString(evaluatedNeedleType) + " instead."}; } type::Type evaluatedHaystackType = typeOf(*evaluatedHaystack); - if (!isSearchableRuntimeValue(evaluatedHaystackType)) { + if (!isSearchableRuntimeType(evaluatedHaystackType)) { return EvaluationError{"Expected second argument to be of type array or string, but found " + toString(evaluatedHaystackType) + " instead."}; } @@ -65,19 +71,8 @@ EvaluationResult In::evaluate(const EvaluationContext& params) const { return EvaluationResult(haystackString.find(needleValue) != std::string::npos); } else { const auto haystackArray = evaluatedHaystack->get>(); - - bool result = false; - if (evaluatedNeedleType == type::Boolean) { - auto needleValue = evaluatedNeedle->get(); - result = find(haystackArray.begin(), haystackArray.end(), needleValue) != haystackArray.end(); - } else if (evaluatedNeedleType == type::String) { - auto needleValue = evaluatedNeedle->get(); - result = find(haystackArray.begin(), haystackArray.end(), needleValue) != haystackArray.end(); - } else if (evaluatedNeedleType == type::Number) { - auto needleValue = evaluatedNeedle->get(); - result = find(haystackArray.begin(), haystackArray.end(), needleValue) != haystackArray.end(); - } - return EvaluationResult(result); + return EvaluationResult(std::find(haystackArray.begin(), haystackArray.end(), *evaluatedNeedle) != + haystackArray.end()); } } diff --git a/test/fixtures/expression_equality/in.a.json b/test/fixtures/expression_equality/in.a.json new file mode 100644 index 0000000000..2632695c6f --- /dev/null +++ b/test/fixtures/expression_equality/in.a.json @@ -0,0 +1,20 @@ +[ + "number", + [ + "in", + [ + "number", + [ + "get", + "i" + ] + ], + [ + "array", + [ + "get", + "arr" + ] + ] + ] +] \ No newline at end of file diff --git a/test/fixtures/expression_equality/in.b.json b/test/fixtures/expression_equality/in.b.json new file mode 100644 index 0000000000..a63a94fb00 --- /dev/null +++ b/test/fixtures/expression_equality/in.b.json @@ -0,0 +1,20 @@ +[ + "number", + [ + "in", + [ + "number", + [ + "get", + "i" + ] + ], + [ + "array", + [ + "get", + "arr_other" + ] + ] + ] +] \ No newline at end of file -- cgit v1.2.1