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

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

    def test_resolve_init_local(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":"resolve"}])
        var_inst = self.mvs.execute("CN", [])
        symbols = self.mvs.execute("CN", [])
        returnvalue = self.mvs.execute("CN", [])
        entry = 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", [inst, "var", var_inst])
        self.mvs.execute("CD", [task_frame, "symbols", symbols])
        self.mvs.execute("CD", [task_frame, "returnvalue", self.mvs.execute("CN", [])])
        self.mvs.execute("CD", [task_frame, "evalstack", self.mvs.execute("CN", [])])

        t1 = self.mvs.execute("CE", [symbols, entry])
        self.mvs.execute("CE", [t1, 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"])]), "finish")
        self.assertEquals(self.mvs.execute("RD", [task_frame, "IP"]), inst)
        self.assertEquals(self.mvs.execute("RD", [task_frame, "returnvalue"]), entry)

    def test_resolve_init_global(self):
        root = self.root
        task_root = self.mvs.execute("CN", [])
        task_frame = self.mvs.execute("CN", [])
        _globals = self.mvs.execute("CN", [])
        self.mvs.execute("CD", [task_root, "globals", _globals])
        global_task = self.mvs.execute("RD", [task_root, "globals"])
        phase = self.mvs.execute("CNV", ["init"])
        inst = self.mvs.execute("CNV", [{"value":"resolve"}])
        var_inst = self.mvs.execute("CNV", ["var"])
        symbols = self.mvs.execute("CN", [])
        returnvalue = self.mvs.execute("CN", [])
        entry = 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", [inst, "var", var_inst])
        self.mvs.execute("CD", [task_frame, "symbols", symbols])
        self.mvs.execute("CD", [task_frame, "returnvalue", self.mvs.execute("CN", [])])
        self.mvs.execute("CD", [task_frame, "evalstack", self.mvs.execute("CN", [])])

        t1 = self.mvs.execute("CE", [global_task, entry])
        self.mvs.execute("CE", [t1, 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"])]), "finish")
        self.assertEquals(self.mvs.execute("RD", [task_frame, "IP"]), inst)
        self.assertEquals(self.mvs.execute("RD", [task_frame, "returnvalue"]), entry)

    def test_resolve_init_local_over_global(self):
        root = self.root
        task_root = self.mvs.execute("CN", [])
        task_frame = self.mvs.execute("CN", [])
        global_task = self.mvs.execute("RD", [task_root, "globals"])
        phase = self.mvs.execute("CNV", ["init"])
        inst = self.mvs.execute("CNV", [{"value":"resolve"}])
        var_inst = self.mvs.execute("CNV", ["var"])
        symbols = self.mvs.execute("CN", [])
        returnvalue = self.mvs.execute("CN", [])
        local_entry = self.mvs.execute("CN", [])
        global_entry = 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", [inst, "var", var_inst])
        self.mvs.execute("CD", [task_frame, "symbols", symbols])
        self.mvs.execute("CD", [task_frame, "returnvalue", self.mvs.execute("CN", [])])
        self.mvs.execute("CD", [task_frame, "evalstack", self.mvs.execute("CN", [])])

        t1 = self.mvs.execute("CE", [global_task, global_entry])
        self.mvs.execute("CE", [t1, var_inst])
        t2 = self.mvs.execute("CE", [symbols, local_entry])
        self.mvs.execute("CE", [t2, 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"])]), "finish")
        self.assertEquals(self.mvs.execute("RD", [task_frame, "IP"]), inst)
        self.assertEquals(self.mvs.execute("RD", [task_frame, "returnvalue"]), local_entry)
