Add range python-like function to batch mode 47/206947/3
authorRadoslaw Cybulski <r.cybulski@partner.samsung.com>
Tue, 28 May 2019 12:00:55 +0000 (14:00 +0200)
committerRadoslaw Cybulski <r.cybulski@partner.samsung.com>
Wed, 29 May 2019 11:59:45 +0000 (13:59 +0200)
Change-Id: I328c423249bc99baab6d1fadde48d144ae7507f3

src/batch/BatchRunner.cpp
tests/no-ui-scenarios/BatchExecTests.cpp

index 4e3492b..0d4bff4 100644 (file)
@@ -29,6 +29,7 @@
 #include "../DBus.hpp"
 #include "Dlog.hpp"
 #include "ReturnValue.hpp"
+#include "EvaluationValueBase.hpp"
 
 #include <mutex>
 #include <chrono>
@@ -832,6 +833,34 @@ void BatchExecutor::insertMethods()
                        return {};
                },  { {"text"} } };
 
+       variables["range"] = EvaluationValueFunction{ [&](EvaluationValue min, EvaluationValue max, EvaluationValue step) -> EvaluationValue {
+                       if (max.isEmpty() && step.isEmpty())
+                       {
+                               max = min;
+                               min = 0;
+                               step = 1;
+                       }
+                       auto mn = min.convertToInteger();
+                       auto mx = max.convertToInteger();
+                       if (step.isEmpty()) step = mn <= mx ? 1 : -1;
+                       auto st = step.convertToInteger();
+                       struct Iterator : public EvaluationValueIteratorInterface {
+                               EvaluationValueInteger min, max, step;
+
+                               Iterator(EvaluationValueInteger min, EvaluationValueInteger max, EvaluationValueInteger step) :
+                                       min(min), max(max), step(step) { }
+
+                               Optional<EvaluationValue> get() const override {
+                                       if ((step > 0 && min < max) || (step < 0 && min > max)) return min;
+                                       return {};
+                               }
+                               void next() override {
+                                       if ((step > 0 && min < max) || (step < 0 && min > max)) min += step;
+                               }
+                       };
+                       return EvaluationValueBase::create(std::make_shared<Iterator>(mn, mx, st));
+               },  { {"min"}, { "max", EvaluationValue{} }, { "step", EvaluationValue{} } } };
+
        variables["str"] = EvaluationValueFunction{ [&](EvaluationValue e) -> EvaluationValue {
                        std::ostringstream tmp;
                        tmp << e;
index 7f667d1..4dac1be 100644 (file)
@@ -1059,6 +1059,67 @@ TEST_F(ScriptTest, functionRecursiveCall)
                , EvaluationFailure);
 }
 
+TEST_F(ScriptTest, range)
+{
+       TestBatchExecutor exec;
+       EvaluationContext ec{ exec };
+
+       executeWithInfo(
+               R"_(
+                       v = []
+                       for(x in range(5)) {
+                               v.append(x)
+                       }
+                       assert(v == [0, 1, 2, 3, 4])
+               )_"
+               , ec);
+       executeWithInfo(
+               R"_(
+                       v = []
+                       for(x in range(-5)) {
+                               v.append(x)
+                       }
+                       assert(v == [])
+               )_"
+               , ec);
+       executeWithInfo(
+               R"_(
+                       v = []
+                       for(x in range(1, 5)) {
+                               v.append(x)
+                       }
+                       assert(v == [1, 2, 3, 4])
+               )_"
+               , ec);
+       executeWithInfo(
+               R"_(
+                       v = []
+                       for(x in range(5, 1)) {
+                               v.append(x)
+                       }
+                       assert(v == [5, 4, 3, 2])
+               )_"
+               , ec);
+       executeWithInfo(
+               R"_(
+                       v = []
+                       for(x in range(1, 5, 3)) {
+                               v.append(x)
+                       }
+                       assert(v == [1, 4])
+               )_"
+               , ec);
+       executeWithInfo(
+               R"_(
+                       v = []
+                       for(x in range(1, 5, -3)) {
+                               v.append(x)
+                       }
+                       assert(v == [])
+               )_"
+               , ec);
+}
+
 int main(int argc, char *argv[])
 {
        try {