diff options
author | kevin <kevin.li@mapbox.com> | 2020-02-11 19:34:58 +0800 |
---|---|---|
committer | kevin <kevin.li@mapbox.com> | 2020-02-15 08:52:23 +0800 |
commit | 6f6e577acff72caa2f540ccfe4844578f4b00a2e (patch) | |
tree | 19a097a295e00ff93cbff0f8f45f4f7e2a17ca40 | |
parent | 447b694e25997db7b5133b9376d94a4aa2f9ac5f (diff) | |
download | qtlocation-mapboxgl-6f6e577acff72caa2f540ccfe4844578f4b00a2e.tar.gz |
Add expression_equality test for 'in'
-rw-r--r-- | include/mbgl/style/expression/in.hpp | 3 | ||||
-rw-r--r-- | src/mbgl/style/expression/in.cpp | 41 | ||||
-rw-r--r-- | test/fixtures/expression_equality/in.a.json | 20 | ||||
-rw-r--r-- | test/fixtures/expression_equality/in.b.json | 20 |
4 files changed, 59 insertions, 25 deletions
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<Expression> needle_, std::unique_ptr<Expression> haystack_) - : Expression(Kind::In, type::Boolean), needle(std::move(needle_)), haystack(std::move(haystack_)) {} + In(std::unique_ptr<Expression> needle_, std::unique_ptr<Expression> 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::Array>() || type == type::Null; } +} // namespace + +In::In(std::unique_ptr<Expression> needle_, std::unique_ptr<Expression> 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<std::vector<Value>>(); - - bool result = false; - if (evaluatedNeedleType == type::Boolean) { - auto needleValue = evaluatedNeedle->get<bool>(); - result = find(haystackArray.begin(), haystackArray.end(), needleValue) != haystackArray.end(); - } else if (evaluatedNeedleType == type::String) { - auto needleValue = evaluatedNeedle->get<std::string>(); - result = find(haystackArray.begin(), haystackArray.end(), needleValue) != haystackArray.end(); - } else if (evaluatedNeedleType == type::Number) { - auto needleValue = evaluatedNeedle->get<double>(); - 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 |