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

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

        self.task_root = self.mvs.execute("CN", [])
        self.task_frame = self.mvs.execute("CN", [])
        self.symbols = self.mvs.execute("CN", [])
        self.evalstack = self.mvs.execute("CN", [])
        self.returnvalue = self.mvs.execute("CN", [])
        self.phase = self.mvs.execute("CNV", ["init"])

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

    def test_return(self):
        """
        Test the code:
            return True
        """
        prev_task_frame = self.mvs.execute("CN", [])
        prev_symbols = self.mvs.execute("CN", [])
        prev_evalstack = self.mvs.execute("CN", [])
        prev_returnvalue = self.mvs.execute("CN", [])
        prev_phase = self.mvs.execute("CNV", ["init"])
        prev_IP = self.mvs.execute("CNV", [{"value":"constant"}])

        self.mvs.execute("CD", [self.task_frame, "prev", prev_task_frame])
        self.mvs.execute("CD", [prev_task_frame, "symbols", prev_symbols])
        self.mvs.execute("CD", [prev_task_frame, "evalstack", prev_evalstack])
        self.mvs.execute("CD", [prev_task_frame, "phase", prev_phase])
        self.mvs.execute("CD", [prev_task_frame, "IP", prev_IP])
        self.mvs.execute("CD", [prev_task_frame, "returnvalue", prev_returnvalue])

        self.inst = self.mvs.execute("CNV", [{"value":"return"}])
        self.const_true = self.mvs.execute("CNV", [{"value":"constant"}])
        self.value_true = self.mvs.execute("CNV", [True])

        self.mvs.execute("CD", [self.task_frame, "IP", self.inst])
        self.mvs.execute("CD", [self.inst, "value", self.const_true])
        self.mvs.execute("CD", [self.const_true, "node", self.value_true])

        ### Execute rules
        self.assertEquals(get_inst(self.root, self.mvs), "return")
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        self.assertEquals(get_returnvalue(self.root, self.mvs), True)
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "return")
        self.assertEquals(get_phase(self.root, self.mvs), "eval")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        self.assertEquals(get_returnvalue(self.root, self.mvs), True)

        t = self.mvs.execute("RD", [self.root, "task_1"])
        t = self.mvs.execute("RD", [t, "frame"])
        self.assertEquals(t, prev_task_frame)
