add none value and is operator to batch mode 75/207475/2
authorRadoslaw Cybulski <r.cybulski@partner.samsung.com>
Wed, 5 Jun 2019 07:37:08 +0000 (09:37 +0200)
committerRadoslaw Cybulski <r.cybulski@partner.samsung.com>
Wed, 5 Jun 2019 08:42:49 +0000 (10:42 +0200)
Change-Id: Ieb48eb43f5f81f9b9e7dbf3d5815b9e1f9f6106c

21 files changed:
src/batch/EvaluationValue.cpp
src/batch/EvaluationValue.hpp
src/batch/EvaluationValueBase.cpp
src/batch/EvaluationValueBase.hpp
src/batch/EvaluationValueBoolean.cpp
src/batch/EvaluationValueDict.cpp
src/batch/EvaluationValueDouble.cpp
src/batch/EvaluationValueFunction.cpp
src/batch/EvaluationValueInteger.cpp
src/batch/EvaluationValueIterator.cpp
src/batch/EvaluationValuePoint.cpp
src/batch/EvaluationValueSet.cpp
src/batch/EvaluationValueString.cpp
src/batch/EvaluationValueUIElement.cpp
src/batch/EvaluationValueVector.cpp
src/batch/EvaluationValueWait.cpp
src/batch/Evaluator.cpp
src/batch/Evaluator.hpp
src/batch/Lexer.cpp
src/batch/Parser.cpp
tests/no-ui-scenarios/BatchExecTests.cpp

index 93a36a7262467ec16129d791806889db6f56f25b..cc5a45d8183dc95d2c94549640b25d6d62307848 100644 (file)
@@ -141,6 +141,10 @@ bool EvaluationValue::contains(const EvaluationValue &val) const
 {
        return value->oper_contains(val.value);
 }
+bool EvaluationValue::is(const EvaluationValue &val) const
+{
+       return value->oper_is(val.value);
+}
 
 EvaluationValue::operator bool() const
 {
index 70e8c2eb37257fcd8f2bb6d01c37724c2765e6ed..da199aa3df737c736a747c2e264b399006221f16 100644 (file)
@@ -245,6 +245,7 @@ public:
 
        size_t hash() const;
        bool contains(const EvaluationValue &) const;
+       bool is(const EvaluationValue &) const;
        explicit operator bool() const;
 
        EvaluationValue operator()(const std::vector<EvaluationValue> &values, const std::unordered_map<std::string, EvaluationValue> &keyArgs);
index 15c12cfe5dee26a34ea2df25bb8d40677a52ce3d..03cf8a039ff304a21f932bc008e575425f7c8ea0 100644 (file)
@@ -267,6 +267,10 @@ public:
        {
                return other->isEmpty();
        }
+       bool oper_is(const EvaluationValuePtr &other) override
+       {
+               return other->isEmpty();
+       }
        bool oper_is_true() override
        {
                return false;
index 9188843945ea0ed56110a1b3a50a0544ad033b30..5337fb7586dac4a0c117376746e47c61c0c31755 100644 (file)
@@ -64,6 +64,7 @@ public:
        virtual int oper_order(const EvaluationValuePtr &);
        virtual size_t oper_hash();
        virtual bool oper_contains(const EvaluationValuePtr &value);
+       virtual bool oper_is(const EvaluationValuePtr &value) = 0;
        virtual bool oper_is_true() = 0;
        virtual EvaluationValuePtr oper_call(const std::vector<EvaluationValue> &args, const std::unordered_map<std::string, EvaluationValue> &keyArgs);
        virtual EvaluationValuePtr oper_attr_get(const std::string &name);
index 79f964027bdf101e092bb45c51553e70d9f6c676..be76c020bc5be296e6d5f96051cc1575e6e855bc 100644 (file)
@@ -50,6 +50,10 @@ public:
        {
                return other->isBoolean() && value == other->asBoolean();
        }
+       bool oper_is(const EvaluationValuePtr &v) override
+       {
+               return oper_equal(v);
+       }
        bool oper_is_true() override
        {
                return value;
index 74c468a22b33aebe8faa32c9655a04c6ac000b78..1165a015aa0cd6ad50b47fea14529d600ada4cb0 100644 (file)
@@ -69,6 +69,10 @@ public:
        {
                return value.find(v) != value.end();
        }
+       bool oper_is(const EvaluationValuePtr &v) override
+       {
+               return this == v.get();
+       }
        EvaluationValuePtr oper_bit_and(const EvaluationValuePtr &o) const override
        {
                auto &r = o->asDict();
index 5ea0f4f0195c2eb52271de3a4ff56237346a3ee2..861be100406a08bfcb4e516eee6e88fe7a866e42 100644 (file)
@@ -66,6 +66,10 @@ public:
                if (other->isInteger()) return value == static_cast<double>(other->asInteger());
                return false;
        }
+       bool oper_is(const EvaluationValuePtr &v) override
+       {
+               return v->isDouble() && oper_equal(v);
+       }
        int oper_order(const EvaluationValuePtr &other) override
        {
                if (other->isDouble()) return orderValues(value, other->asDouble());
index 8187903d2b8d2a5e000fc6c029a37b1991094694..d21edf5315c2171812e5824ae7a50dd1b62bd04d 100644 (file)
@@ -55,6 +55,10 @@ public:
        {
                throw EvaluationFailure{} << "unsupported operation, can't compare values " << typeName() << " and " << other->typeName();
        }
+       bool oper_is(const EvaluationValuePtr &v) override
+       {
+               return this == v.get();
+       }
        bool oper_is_true() override
        {
                return true;
index c38abce2b765347bcceb0ebeacacec3216be13d6..b4edc7ff2610dc39c456c3cd9f272fa0f12e413b 100644 (file)
@@ -64,6 +64,10 @@ public:
                if (other->isDouble()) return static_cast<double>(value) == other->asDouble();
                return false;
        }
+       bool oper_is(const EvaluationValuePtr &v) override
+       {
+               return v->isInteger() && oper_equal(v);
+       }
        int oper_order(const EvaluationValuePtr &other) override
        {
                if (other->isInteger()) return orderValues(value, other->asInteger());
index 4b66130801e73027d9fb93575786eb078e7c0e0b..09cc2c132665360330d20deaaf405b940ce5c8ec 100644 (file)
@@ -46,6 +46,10 @@ public:
        {
                return other->isIterator() && value == other->asIterator();
        }
+       bool oper_is(const EvaluationValuePtr &v) override
+       {
+               return this == v.get();
+       }
        bool oper_is_true() override
        {
                return bool(value->get());
index 5731130030ed6e1e46bbd6520dd9d030ad14e220..b6171a854657f96c34aaa94e8a96d2d3db934943 100644 (file)
@@ -60,6 +60,10 @@ public:
        {
                return other->isPoint() && value == other->asPoint();
        }
+       bool oper_is(const EvaluationValuePtr &v) override
+       {
+               return oper_equal(v);
+       }
        bool oper_is_true() override
        {
                return true;
index 3bde2076350b05b3d5782f7a7e514954c8b82042..429baf0aacc5dcab2bd138ab480e23d1e54d25d9 100644 (file)
@@ -104,6 +104,10 @@ public:
        {
                return other->isSet() && value == other->asSet();
        }
+       bool oper_is(const EvaluationValuePtr &v) override
+       {
+               return this == v.get();
+       }
        bool oper_is_true() override
        {
                return !value.empty();
index c14e970060efcee7f8f2154b719e894b77e71457..c74c88f811d011a0fc14def5c2d4824ffc9d7afb 100644 (file)
@@ -60,6 +60,10 @@ public:
        {
                return other->isString() && value == other->asString();
        }
+       bool oper_is(const EvaluationValuePtr &v) override
+       {
+               return oper_equal(v);
+       }
        bool oper_is_true() override
        {
                return !value.empty();
index 88aa28600201931560d1158c43efabf937c20c43..b545609ebd777add95fb2b77d737afe47f91fdbf 100644 (file)
@@ -64,6 +64,10 @@ public:
                auto otherId = EvaluationContext::getCurrentEvaluationContext().executionInterface().getUIElementUniqueId(other->asUIElement());
                return selfId == otherId;
        }
+       bool oper_is(const EvaluationValuePtr &v) override
+       {
+               return oper_equal(v);
+       }
        bool oper_is_true() override
        {
                return true;
index a0cbab6bd5603e6512645508137372ef774efa71..228347ce68f48a5009e3489a031e8cd09ea56081 100644 (file)
@@ -65,6 +65,10 @@ public:
        {
                return other->isVector() && value == other->asVector();
        }
+       bool oper_is(const EvaluationValuePtr &v) override
+       {
+               return this == v.get();
+       }
        int oper_order(const EvaluationValuePtr &other) override
        {
                if (!other->isVector())
index bd821177737a886fe1f866f47706476958b5a61c..0c015340f521069f11a8d3b18085e2c89fdf8b83 100644 (file)
@@ -49,6 +49,10 @@ public:
        {
                return other->isBoolean() && value == other->asWait();
        }
+       bool oper_is(const EvaluationValuePtr &v) override
+       {
+               return this == v.get();
+       }
        EvaluationValuePtr oper_call(const std::vector<EvaluationValue> &args, const std::unordered_map<std::string, EvaluationValue> &keyArgs) override
        {
                EvaluationValueFunction::validateArgumentCount(args.size(), 0, 0);
index f9b6beba4ce1e8032423cd8323514cd2c8b2ef61..15ab8ff921aa2d98432daefe607a4ca544757cd8 100644 (file)
@@ -345,6 +345,8 @@ EvaluationValue OperatorEvaluator::evaluateImpl() const
        switch (kind) {
        case Kind::IN:
                return vals[1].contains(vals[0]);
+       case Kind::IS:
+               return vals[1].is(vals[0]);
        case Kind::NOT:
                return !vals[0];
        case Kind::MINUS:
@@ -398,6 +400,9 @@ void OperatorEvaluator::printSelfInfo(unsigned int depth) const
                case Kind::IN:
                        desc = "in";
                        break;
+               case Kind::IS:
+                       desc = "is";
+                       break;
                case Kind::NOT:
                        desc = "not";
                        break;
@@ -547,6 +552,21 @@ void BooleanEvaluator::printSelfInfo(unsigned int depth) const
 
 
 
+EmptyEvaluator::EmptyEvaluator(TokenLocation tokenLocation) :
+       ExpressionEvaluator(std::move(tokenLocation)) { }
+
+EvaluationValue EmptyEvaluator::evaluateImpl() const
+{
+       return EvaluationValue{};
+}
+
+void EmptyEvaluator::printSelfInfo(unsigned int depth) const
+{
+       printLocationAndIndent(location(), depth) << "none";
+}
+
+
+
 CallEvaluator::CallEvaluator(TokenLocation tokenLocation, ExprPtr function, ExprPtrs args, ExprMapPtrs keywordArgs) :
        ExpressionEvaluator(tokenLocation), function(std::move(function)), args(std::move(args)), keywordArgs(std::move(keywordArgs)) { }
 
index 3cce13edc99a628fdb99c138d73dc8aa9d42a91a..a994d9c0e583f7e3f472cbb9f79455ab259773bf 100644 (file)
@@ -312,6 +312,19 @@ public:
        void printSelfInfo(unsigned int depth) const override;
 };
 
+/**
+ * @brief Type representing constructor of a empty value
+ */
+class EmptyEvaluator : public ExpressionEvaluator
+{
+protected:
+       EvaluationValue evaluateImpl() const override;
+public:
+       EmptyEvaluator(TokenLocation location_);
+
+       void printSelfInfo(unsigned int depth) const override;
+};
+
 /**
  * @brief Type representing constructor of a string
  */
@@ -382,7 +395,7 @@ protected:
        EvaluationValue evaluateImpl() const override;
 public:
        enum class Kind {
-               IN, GET, SET, MINUS, NOT, LOG_OR, LOG_AND, ADD, SUB, MUL, DIV, MOD, LSHIFT, RSHIFT, BIT_OR, BIT_NOT, BIT_AND, BIT_XOR
+               IN, GET, SET, MINUS, NOT, LOG_OR, LOG_AND, ADD, SUB, MUL, DIV, MOD, LSHIFT, RSHIFT, BIT_OR, BIT_NOT, BIT_AND, BIT_XOR, IS
        };
        OperatorEvaluator(TokenLocation location_, ExprPtrs args, Kind kind);
 
index e77f7a05b741c1ef71771eede4c40d0c0bb88be1..88876a331b8e7cfa309145d856f96c8f3e6582dc 100644 (file)
@@ -78,7 +78,7 @@ std::string toString(TokenType t)
  * If found, resulting token will be KEYWORD rather than IDENTIFIER.
  */
 static std::unordered_set<std::string> keywords {
-       "true", "false", "def", "return", "if", "else", "elif", "for", "in", "break", "continue", "while"
+       "true", "false", "def", "return", "if", "else", "elif", "for", "in", "break", "continue", "while", "none"
 };
 
 /**
index 2834aef940f10390c4e0a1bb12e37d0e0cc16285..a9f27b36be7114f5ec5e36e334600a31b894276d 100644 (file)
@@ -323,6 +323,7 @@ class Parser
                case TokenType::KEYWORD:
                        if (t.text() == "true") return std::make_shared<BooleanEvaluator>(t.location(), true);
                        if (t.text() == "false") return std::make_shared<BooleanEvaluator>(t.location(), false);
+                       if (t.text() == "none") return std::make_shared<EmptyEvaluator>(t.location());
                        if (t.text() == "def" && index < tokens.size() && tokens[index].text() == "(") return parseLambda();
                        break;
                case TokenType::IDENTIFIER:
@@ -606,6 +607,11 @@ class Parser
                                auto v = parseExprAboveComparisions();
                                if (!v) return {};
                                e = std::make_shared<OperatorEvaluator>(location, ExprPtrs{ std::move(e), std::move(v) }, OperatorEvaluator::Kind::IN);
+                       } else if (t == "is") {
+                               ++index;
+                               auto v = parseExprAboveComparisions();
+                               if (!v) return {};
+                               e = std::make_shared<OperatorEvaluator>(location, ExprPtrs{ std::move(e), std::move(v) }, OperatorEvaluator::Kind::IS);
                        } else if (t == "not") {
                                ++index;
                                if (!consume("in")) return {};
index dc32ec1d961787b7867060c0c787a0d934670762..22b19dcabb3b812bee3b3ce202733e57791121cb 100644 (file)
@@ -1125,6 +1125,33 @@ TEST_F(ScriptTest, range)
                , ec);
 }
 
+TEST_F(ScriptTest, operatorIs)
+{
+       TestBatchExecutor exec;
+       EvaluationContext ec{ exec };
+
+       executeCountAsserts(
+               R"_(
+                       assert(true is true)
+                       assert(not (true is false))
+                       assert(false is false)
+                       assert(not (false is true))
+                       assert(0 is 0)
+                       assert(not (0 is 0.0))
+                       assert(not (0.0 is 0))
+                       assert(1.0 is 1.0)
+                       assert(not (false is 0))
+                       assert(not (false is 0.0))
+                       assert(not (false is ""))
+                       assert("" is "")
+                       assert("qwerty" is "qwerty")
+                       assert(not ([] is []))
+                       assert(not ({} is {}))
+                       assert(not ({'':''} is {'':''}))
+               )_"
+               , 16);
+}
+
 int main(int argc, char *argv[])
 {
        try {