#include "../DBus.hpp"
#include "Dlog.hpp"
#include "ReturnValue.hpp"
+#include "EvaluationValueBase.hpp"
#include <mutex>
#include <chrono>
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;
, 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 {