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

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

    def test_resolve_init_local(self):
        root = self.root
        user_root = self.mvs.execute("CN", [])[0]
        user_frame = self.mvs.execute("CN", [])[0]
        phase = self.mvs.execute("CNV", ["init"])[0]
        inst = self.mvs.execute("CNV", [{"value":"resolve"}])[0]
        var_inst = self.mvs.execute("CN", [])[0]
        symbols = self.mvs.execute("CN", [])[0]
        returnvalue = self.mvs.execute("CN", [])[0]
        entry = self.mvs.execute("CN", [])[0]

        self.mvs.execute("CD", [root, "user_1", user_root])
        self.mvs.execute("CD", [user_root, "frame", user_frame])
        self.mvs.execute("CD", [user_frame, "phase", phase])
        self.mvs.execute("CD", [user_frame, "IP", inst])
        self.mvs.execute("CD", [inst, "var", var_inst])
        self.mvs.execute("CD", [user_frame, "symbols", symbols])
        self.mvs.execute("CD", [user_frame, "returnvalue", self.mvs.execute("CN", [])[0]])
        self.mvs.execute("CD", [user_frame, "evalstack", self.mvs.execute("CN", [])[0]])

        t1 = self.mvs.execute("CE", [symbols, entry])[0]
        self.mvs.execute("CE", [t1, var_inst])[0]

        # 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, "user_1"])[0], user_root)
        self.assertEquals(self.mvs.execute("RD", [user_root, "frame"])[0], user_frame)
        self.assertEquals(self.mvs.execute("RV", [self.mvs.execute("RD", [user_frame, "phase"])[0]])[0], "finish")
        self.assertEquals(self.mvs.execute("RD", [user_frame, "IP"])[0], inst)
        self.assertEquals(self.mvs.execute("RD", [user_frame, "returnvalue"])[0], entry)

    def test_resolve_init_global(self):
        root = self.root
        user_root = self.mvs.execute("CN", [])[0]
        user_frame = self.mvs.execute("CN", [])[0]
        _globals = self.mvs.execute("CN", [])[0]
        self.mvs.execute("CD", [user_root, "globals", _globals])
        global_user = self.mvs.execute("RD", [user_root, "globals"])[0]
        phase = self.mvs.execute("CNV", ["init"])[0]
        inst = self.mvs.execute("CNV", [{"value":"resolve"}])[0]
        var_inst = self.mvs.execute("CNV", ["var"])[0]
        symbols = self.mvs.execute("CN", [])[0]
        returnvalue = self.mvs.execute("CN", [])[0]
        entry = self.mvs.execute("CN", [])[0]

        self.mvs.execute("CD", [root, "user_1", user_root])
        self.mvs.execute("CD", [user_root, "frame", user_frame])
        self.mvs.execute("CD", [user_frame, "phase", phase])
        self.mvs.execute("CD", [user_frame, "IP", inst])
        self.mvs.execute("CD", [inst, "var", var_inst])
        self.mvs.execute("CD", [user_frame, "symbols", symbols])
        self.mvs.execute("CD", [user_frame, "returnvalue", self.mvs.execute("CN", [])[0]])
        self.mvs.execute("CD", [user_frame, "evalstack", self.mvs.execute("CN", [])[0]])

        t1 = self.mvs.execute("CE", [global_user, entry])[0]
        self.mvs.execute("CE", [t1, var_inst])[0]

        # 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, "user_1"])[0], user_root)
        self.assertEquals(self.mvs.execute("RD", [user_root, "frame"])[0], user_frame)
        self.assertEquals(self.mvs.execute("RV", [self.mvs.execute("RD", [user_frame, "phase"])[0]])[0], "finish")
        self.assertEquals(self.mvs.execute("RD", [user_frame, "IP"])[0], inst)
        self.assertEquals(self.mvs.execute("RD", [user_frame, "returnvalue"])[0], entry)

    def test_resolve_init_local_over_global(self):
        root = self.root
        user_root = self.mvs.execute("CN", [])[0]
        user_frame = self.mvs.execute("CN", [])[0]
        global_user = self.mvs.execute("RD", [user_root, "globals"])[0]
        phase = self.mvs.execute("CNV", ["init"])[0]
        inst = self.mvs.execute("CNV", [{"value":"resolve"}])[0]
        var_inst = self.mvs.execute("CNV", ["var"])[0]
        symbols = self.mvs.execute("CN", [])[0]
        returnvalue = self.mvs.execute("CN", [])[0]
        local_entry = self.mvs.execute("CN", [])[0]
        global_entry = self.mvs.execute("CN", [])[0]

        self.mvs.execute("CD", [root, "user_1", user_root])
        self.mvs.execute("CD", [user_root, "frame", user_frame])
        self.mvs.execute("CD", [user_frame, "phase", phase])
        self.mvs.execute("CD", [user_frame, "IP", inst])
        self.mvs.execute("CD", [inst, "var", var_inst])
        self.mvs.execute("CD", [user_frame, "symbols", symbols])
        self.mvs.execute("CD", [user_frame, "returnvalue", self.mvs.execute("CN", [])[0]])
        self.mvs.execute("CD", [user_frame, "evalstack", self.mvs.execute("CN", [])[0]])

        t1 = self.mvs.execute("CE", [global_user, global_entry])[0]
        self.mvs.execute("CE", [t1, var_inst])[0]
        t2 = self.mvs.execute("CE", [symbols, local_entry])[0]
        self.mvs.execute("CE", [t2, var_inst])[0]

        # 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, "user_1"])[0], user_root)
        self.assertEquals(self.mvs.execute("RD", [user_root, "frame"])[0], user_frame)
        self.assertEquals(self.mvs.execute("RV", [self.mvs.execute("RD", [user_frame, "phase"])[0]])[0], "finish")
        self.assertEquals(self.mvs.execute("RD", [user_frame, "IP"])[0], inst)
        self.assertEquals(self.mvs.execute("RD", [user_frame, "returnvalue"])[0], local_entry)
