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

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

        self.task_root = self.mvs.execute("CN", [])[0]
        self.task_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, "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_assign_simple(self):
        """
        Test the code:
            a = True
        """
        self.var = self.mvs.execute("CN", [])[0]
        self.value = self.mvs.execute("CN", [])[0]
        self.previous = self.mvs.execute("CN", [])[0]
        self.const = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.const_value = self.mvs.execute("CNV", [True])[0]

        self.inst = self.mvs.execute("CNV", [{"value":"assign"}])[0]
        self.resolve = self.mvs.execute("CNV", [{"value":"resolve"}])[0]

        self.mvs.execute("CD", [self.task_frame, "IP", self.inst])
        self.mvs.execute("CD", [self.inst, "var", self.resolve])
        self.mvs.execute("CD", [self.inst, "value", self.const])
        self.mvs.execute("CD", [self.resolve, "var", self.var])
        self.mvs.execute("CD", [self.value, "value", self.previous])
        self.mvs.execute("CD", [self.const, "node", self.const_value])
        t = self.mvs.execute("CE", [self.symbols, self.value])[0]
        self.mvs.execute("CE", [t, self.var])

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

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

        self.assertEquals(get_inst(self.root, self.mvs), "resolve")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        self.assertEquals(get_returnvalue_ref(self.root, self.mvs), self.value)
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "assign")
        self.assertEquals(get_phase(self.root, self.mvs), "value")
        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")
        execute_until_finished(self.mvk, self.mvs)

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

        self.assertEquals(get_inst(self.root, self.mvs), "assign")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")

        # Check if the new value is really assigned
        t = self.mvs.execute("RD", [self.root, "task_1"])[0]
        t = self.mvs.execute("RD", [t, "frame"])[0]
        t = self.mvs.execute("RD", [t, "symbols"])[0]
        t = self.mvs.execute("RDN", [t, self.var])[0]
        t = self.mvs.execute("RD", [t, "value"])[0]
        t = self.mvs.execute("RV", [t])[0]
        self.assertEquals(t, True)
