import unittest
from modelverse_kernel.main import ModelverseKernel
from 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", [])[0]
        self.mvk = ModelverseKernel(self.root)

        self.user_root = self.mvs.execute("CN", [])[0]
        self.user_frame = self.mvs.execute("CN", [])[0]
        self.symbols = self.mvs.execute("CN", [])[0]
        self.evalstack = self.mvs.execute("CN", [])[0]
        self.returnvalue = self.mvs.execute("CN", [])[0]
        self.phase = self.mvs.execute("CNV", ["init"])[0]

        self.mvs.execute("CD", [self.root, "user_1", self.user_root])
        self.mvs.execute("CD", [self.user_root, "frame", self.user_frame])
        self.mvs.execute("CD", [self.user_frame, "symbols", self.symbols])
        self.mvs.execute("CD", [self.user_frame, "evalstack", self.evalstack])
        self.mvs.execute("CD", [self.user_frame, "phase", self.phase])
        self.mvs.execute("CD", [self.user_frame, "returnvalue", self.returnvalue])

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

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

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

        self.mvs.execute("CD", [self.user_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, "user_1"])[0]
        t = self.mvs.execute("RD", [t, "frame"])[0]
        self.assertEquals(t, prev_user_frame)
