import unittest
from modelverse_kernel.main import ModelverseKernel
from utils import execute_until_finished, MvSWrapper

class Testif(unittest.TestCase):
    def setUp(self):
        self.mvs = MvSWrapper()
        self.root = self.mvs.execute("RR", [])[0]
        self.mvk = ModelverseKernel(self.root)

    def test_if_evaluate_condition(self):
        root = self.root
        task_root = self.mvs.execute("CN", [])[0]
        task_frame = self.mvs.execute("CN", [])[0]
        phase = self.mvs.execute("CNV", ["init"])[0]
        inst = self.mvs.execute("CNV", [{"value":"if"}])[0]
        evalstack = self.mvs.execute("CN", [])[0]
        cond = self.mvs.execute("CN", [])[0]

        self.mvs.execute("CD", [root, "task_1", task_root])
        self.mvs.execute("CD", [task_root, "frame", task_frame])
        self.mvs.execute("CD", [task_frame, "phase", phase])
        self.mvs.execute("CD", [task_frame, "IP", inst])
        self.mvs.execute("CD", [inst, "cond", cond])
        self.mvs.execute("CD", [task_frame, "evalstack", evalstack])

        # Do the invocation until it returns None
        execute_until_finished(self.mvk, self.mvs)

        # Execution of the command finished, so the MvS should be in the correct new state right now
        self.assertEquals(self.mvs.execute("RD", [root, "task_1"])[0], task_root)
        self.assertEquals(self.mvs.execute("RD", [task_root, "frame"])[0], task_frame)
        self.assertEquals(self.mvs.execute("RV", [self.mvs.execute("RD", [task_frame, "phase"])[0]])[0], "init")
        self.assertEquals(self.mvs.execute("RD", [task_frame, "IP"])[0], cond)
        new_evalstack = self.mvs.execute("RD", [task_frame, "evalstack"])[0]
        self.assertNotEquals(evalstack, new_evalstack)
        self.assertEquals(self.mvs.execute("RD", [new_evalstack, "prev"])[0], evalstack)
        self.assertEquals(self.mvs.execute("RD", [evalstack, "inst"])[0], inst)
        self.assertEquals(self.mvs.execute("RV", [self.mvs.execute("RD", [evalstack, "phase"])[0]])[0], "cond")

    def test_if_true(self):
        root = self.root
        task_root = self.mvs.execute("CN", [])[0]
        task_frame = self.mvs.execute("CN", [])[0]
        phase = self.mvs.execute("CNV", ["cond"])[0]
        inst = self.mvs.execute("CNV", [{"value":"if"}])[0]
        evalstack = self.mvs.execute("CN", [])[0]
        returnvalue = self.mvs.execute("CNV", [True])[0]
        then = self.mvs.execute("CN", [])[0]

        self.mvs.execute("CD", [root, "task_1", task_root])
        self.mvs.execute("CD", [task_root, "frame", task_frame])
        self.mvs.execute("CD", [task_frame, "phase", phase])
        self.mvs.execute("CD", [task_frame, "IP", inst])
        self.mvs.execute("CD", [task_frame, "evalstack", evalstack])
        self.mvs.execute("CD", [task_frame, "returnvalue", returnvalue])
        self.mvs.execute("CD", [inst, "then", then])

        # Do the invocation until it returns None
        execute_until_finished(self.mvk, self.mvs)

        # Execution of the command finished, so the MvS should be in the correct new state right now
        self.assertEquals(self.mvs.execute("RD", [root, "task_1"])[0], task_root)
        self.assertEquals(self.mvs.execute("RD", [task_root, "frame"])[0], task_frame)
        self.assertEquals(self.mvs.execute("RV", [self.mvs.execute("RD", [task_frame, "phase"])[0]])[0], "init")
        self.assertEquals(self.mvs.execute("RD", [task_frame, "IP"])[0], then)
        new_evalstack = self.mvs.execute("RD", [task_frame, "evalstack"])[0]
        self.assertNotEquals(evalstack, new_evalstack)
        self.assertEquals(self.mvs.execute("RD", [new_evalstack, "prev"])[0], evalstack)
        self.assertEquals(self.mvs.execute("RD", [evalstack, "inst"])[0], inst)
        self.assertEquals(self.mvs.execute("RV", [self.mvs.execute("RD", [evalstack, "phase"])[0]])[0], "finish")

    def test_if_false_else(self):
        root = self.root
        task_root = self.mvs.execute("CN", [])[0]
        task_frame = self.mvs.execute("CN", [])[0]
        phase = self.mvs.execute("CNV", ["cond"])[0]
        inst = self.mvs.execute("CNV", [{"value":"if"}])[0]
        evalstack = self.mvs.execute("CN", [])[0]
        returnvalue = self.mvs.execute("CNV", [False])[0]
        else_inst = self.mvs.execute("CN", [])[0]

        self.mvs.execute("CD", [root, "task_1", task_root])
        self.mvs.execute("CD", [task_root, "frame", task_frame])
        self.mvs.execute("CD", [task_frame, "phase", phase])
        self.mvs.execute("CD", [task_frame, "IP", inst])
        self.mvs.execute("CD", [task_frame, "evalstack", evalstack])
        self.mvs.execute("CD", [task_frame, "returnvalue", returnvalue])
        self.mvs.execute("CD", [inst, "else", else_inst])

        # Do the invocation until it returns None
        execute_until_finished(self.mvk, self.mvs)

        # Execution of the command finished, so the MvS should be in the correct new state right now
        self.assertEquals(self.mvs.execute("RD", [root, "task_1"])[0], task_root)
        self.assertEquals(self.mvs.execute("RD", [task_root, "frame"])[0], task_frame)
        self.assertEquals(self.mvs.execute("RV", [self.mvs.execute("RD", [task_frame, "phase"])[0]])[0], "init")
        self.assertEquals(self.mvs.execute("RD", [task_frame, "IP"])[0], else_inst)
        new_evalstack = self.mvs.execute("RD", [task_frame, "evalstack"])[0]
        self.assertNotEquals(evalstack, new_evalstack)
        self.assertEquals(self.mvs.execute("RD", [new_evalstack, "prev"])[0], evalstack)
        self.assertEquals(self.mvs.execute("RD", [evalstack, "inst"])[0], inst)
        self.assertEquals(self.mvs.execute("RV", [self.mvs.execute("RD", [evalstack, "phase"])[0]])[0], "finish")

    def test_if_false_no_else(self):
        root = self.root
        task_root = self.mvs.execute("CN", [])[0]
        task_frame = self.mvs.execute("CN", [])[0]
        phase = self.mvs.execute("CNV", ["cond"])[0]
        inst = self.mvs.execute("CNV", [{"value":"if"}])[0]
        evalstack = self.mvs.execute("CN", [])[0]
        returnvalue = self.mvs.execute("CNV", [False])[0]

        self.mvs.execute("CD", [root, "task_1", task_root])
        self.mvs.execute("CD", [task_root, "frame", task_frame])
        self.mvs.execute("CD", [task_frame, "phase", phase])
        self.mvs.execute("CD", [task_frame, "IP", inst])
        self.mvs.execute("CD", [task_frame, "evalstack", evalstack])
        self.mvs.execute("CD", [task_frame, "returnvalue", returnvalue])

        # Do the invocation until it returns None
        execute_until_finished(self.mvk, self.mvs)

        # Execution of the command finished, so the MvS should be in the correct new state right now
        self.assertEquals(self.mvs.execute("RD", [root, "task_1"])[0], task_root)
        self.assertEquals(self.mvs.execute("RD", [task_root, "frame"])[0], task_frame)
        self.assertEquals(self.mvs.execute("RV", [self.mvs.execute("RD", [task_frame, "phase"])[0]])[0], "finish")
        self.assertEquals(self.mvs.execute("RD", [task_frame, "IP"])[0], inst)
