package org.mwg.core.task;

import org.junit.Assert;
import org.junit.Test;
import org.mwg.Callback;
import org.mwg.Graph;
import org.mwg.GraphBuilder;
import org.mwg.task.Action;
import org.mwg.task.TaskContext;

import static org.mwg.task.Actions.newTask;

public class CoreTaskContextTests {

    @Test
    public void testArrayInTemplate() {
        Graph graph = new GraphBuilder().build();
        graph.connect(new Callback<Boolean>() {
            @Override
            public void on(Boolean result) {
                newTask()
                        .setVar("i",4)
                        .setVar("array",new int[]{1,2,3,4,5,6,7,8,9})
                        .fromVar("array")
                        .then(new Action() {
                            @Override
                            public void eval(TaskContext context) {
                                Assert.assertEquals("5",context.template("{{array[4]}}"));
                                Assert.assertEquals("9",context.template("{{result[8]}}"));
                                Assert.assertEquals("[1,2,3,4,5,6,7,8,9]",context.template("{{result}}"));
                                Assert.assertEquals("[1,2,3,4,5,6,7,8,9]",context.template("{{array}}"));

                                boolean exceptionCaught = false;
                                try {
                                    context.template("{{result[]}}");
                                } catch (RuntimeException e) {
                                    exceptionCaught = true;
                                }
                                Assert.assertTrue(exceptionCaught);

                                exceptionCaught = false;
                                try {
                                    System.out.println(context.template("{{result[9]}}"));;
                                } catch (RuntimeException e) {
                                    exceptionCaught = true;
                                }
                                Assert.assertTrue(exceptionCaught);

                                Assert.assertEquals("9.0",context.template("{{=(1 + 2) + (array[6] - 4) * 2 }}"));
                            }
                        })
                        .execute(graph,null);
            }
        });
    }
}

@@ -2100,9 +2100,9 @@
 (%229.
-0
+1
 %22,co
@@ -2121,16 +2121,17 @@
 te(%22%7B%7B=(
+(
 1 + 2) +
@@ -2149,16 +2149,23 @@
 - 4) * 2
+) + 0.1
  %7D%7D%22));%0A

fix js test