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

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

    def test_assign_init(self):
        root = self.root
        task_root = self.mvs.execute("CN", [])
        task_frame = self.mvs.execute("CN", [])
        phase = self.mvs.execute("CNV", ["init"])
        inst = self.mvs.execute("CNV", [{"value":"assign"}])
        evalstack = self.mvs.execute("CN", [])
        var_inst = self.mvs.execute("CN", [])

        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", [inst, "var", var_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"]), task_root)
        self.assertEquals(self.mvs.execute("RD", [task_root, "frame"]), task_frame)
        self.assertEquals(self.mvs.execute("RV", [self.mvs.execute("RD", [task_frame, "phase"])]), "init")
        self.assertEquals(self.mvs.execute("RD", [task_frame, "IP"]), var_inst)
        new_evalstack = self.mvs.execute("RD", [task_frame, "evalstack"])
        self.assertNotEquals(evalstack, new_evalstack)
        self.assertEquals(self.mvs.execute("RD", [new_evalstack, "prev"]), evalstack)
        self.assertEquals(self.mvs.execute("RD", [evalstack, "inst"]), inst)
        self.assertEquals(self.mvs.execute("RV", [self.mvs.execute("RD", [evalstack, "phase"])]), "value")

    def test_assign_value(self):
        root = self.root
        task_root = self.mvs.execute("CN", [])
        task_frame = self.mvs.execute("CN", [])
        phase = self.mvs.execute("CNV", ["value"])
        inst = self.mvs.execute("CNV", [{"value":"assign"}])
        evalstack = self.mvs.execute("CN", [])
        returnvalue = self.mvs.execute("CN", [])
        value_inst = self.mvs.execute("CN", [])

        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, "value", value_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"]), task_root)
        self.assertEquals(self.mvs.execute("RD", [task_root, "frame"]), task_frame)
        self.assertEquals(self.mvs.execute("RV", [self.mvs.execute("RD", [task_frame, "phase"])]), "init")
        self.assertEquals(self.mvs.execute("RD", [task_frame, "IP"]), value_inst)
        new_evalstack = self.mvs.execute("RD", [task_frame, "evalstack"])
        self.assertNotEquals(evalstack, new_evalstack)
        self.assertEquals(self.mvs.execute("RD", [new_evalstack, "prev"]), evalstack)
        self.assertEquals(self.mvs.execute("RD", [evalstack, "inst"]), inst)
        self.assertEquals(self.mvs.execute("RD", [task_frame, "returnvalue"]), returnvalue)
        self.assertEquals(self.mvs.execute("RD", [task_frame, "variable"]), returnvalue)
        self.assertEquals(self.mvs.execute("RV", [self.mvs.execute("RD", [evalstack, "phase"])]), "assign")

    def test_assign_assign(self):
        root = self.root
        task_root = self.mvs.execute("CN", [])
        task_frame = self.mvs.execute("CN", [])
        phase = self.mvs.execute("CNV", ["assign"])
        inst = self.mvs.execute("CNV", [{"value":"assign"}])
        evalstack = self.mvs.execute("CN", [])
        returnvalue = self.mvs.execute("CN", [])
        variable = self.mvs.execute("CN", [])
        value_inst = self.mvs.execute("CN", [])

        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", [task_frame, "variable", variable])
        self.mvs.execute("CD", [variable, "value", value_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"]), task_root)
        self.assertEquals(self.mvs.execute("RD", [task_root, "frame"]), task_frame)
        self.assertEquals(self.mvs.execute("RV", [self.mvs.execute("RD", [task_frame, "phase"])]), "finish")
        self.assertEquals(self.mvs.execute("RD", [task_frame, "IP"]), inst)
        self.assertEquals(self.mvs.execute("RD", [task_frame, "variable"]), None)
        self.assertEquals(self.mvs.execute("RD", [variable, "value"]), returnvalue)
        self.assertEquals(self.mvs.execute("RD", [task_frame, "returnvalue"]), returnvalue)
