package org.mwg.core.task;

import org.junit.Assert;
import org.junit.Test;
import org.mwg.task.*;
import org.mwg.task.Task;
import org.mwg.task.TaskContext;

/**
 * Created by ludovicmouline on 26/04/16.
 */
public class ActionIfThenTest extends AbstractActionTest{

    @Test
    public void test() {
        boolean[] result = {false,false};

        Task modifyResult0 = graph.newTask()
                .then(new TaskAction() {
                    @Override
                    public void eval(TaskContext context) {
                        result[0] = true;
                    }
                });

        Task modifyResult1 = graph.newTask()
                .then(new TaskAction() {
                    @Override
                    public void eval(TaskContext context) {
                        result[0] = true;
                    }
                });


        graph.newTask()
                .ifThen(new TaskFunctionConditional() {
                    @Override
                    public boolean eval(TaskContext context) {
                        return true;
                    }
                },modifyResult0).execute();

        graph.newTask().ifThen(new TaskFunctionConditional() {
            @Override
            public boolean eval(TaskContext context) {
                return false;
            }
        },modifyResult0).execute();

        Assert.assertEquals(true,result[0]);
        Assert.assertEquals(false,result[1]);
    }

    @Test
    public void testChainAfterIfThen() {
        Task addVarInContext = graph.newTask().from(5).asVar("variable").then(new TaskAction() {
            @Override
            public void eval(TaskContext context) {
                //empty action
            }
        });

        graph.newTask().ifThen(new TaskFunctionConditional() {
            @Override
            public boolean eval(TaskContext context) {
                return true;
            }
        },addVarInContext).fromVar("variable").then(new TaskAction() {
            @Override
            public void eval(TaskContext context) {
                Integer val = (Integer) context.getPreviousResult();
                Assert.assertEquals(5,(int)val);
            }
        }).execute();
    }

    @Test
    public void accessContextVariableInThenTask() {
        Task accessVar = graph.newTask().then(new TaskAction() {
            @Override
            public void eval(TaskContext context) {
                Integer var = (Integer) context.getVariable("variable");
                Assert.assertEquals(5,(int)var);
            }
        });

        graph.newTask().from(5).asVar("variable").ifThen(new TaskFunctionConditional() {
            @Override
            public boolean eval(TaskContext context) {
                return true;
            }
        },accessVar).execute();
    }
}

@@ -2455,16 +2455,21 @@
 eger var
+iable
  = (Inte
@@ -2552,16 +2552,21 @@
 (int)var
+iable
 );%0A     

rename of var in test