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

class Testresolve(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.global_task = 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_root, "globals", self.global_task])
        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_resolve_simple_local(self):
        """
        Test the resolution of a local variable
        """
        self.var = self.mvs.execute("CN", [])
        self.value = self.mvs.execute("CN", [])

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

        self.mvs.execute("CD", [self.task_frame, "IP", self.inst])
        self.mvs.execute("CD", [self.inst, "var", self.var])
        t = self.mvs.execute("CE", [self.symbols, self.value])
        self.mvs.execute("CE", [t, self.var])

        ### Execute rules
        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)

    def test_resolve_simple_global(self):
        """
        Test the resolution of a global variable
        """
        self.var = self.mvs.execute("CNV", ["glob"])
        self.value = self.mvs.execute("CN", [])

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

        self.mvs.execute("CD", [self.task_frame, "IP", self.inst])
        self.mvs.execute("CD", [self.inst, "var", self.var])
        t = self.mvs.execute("CE", [self.global_task, self.value])
        self.mvs.execute("CE", [t, self.var])

        ### Execute rules
        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)

    def test_resolve_simple_local_over_global(self):
        """
        Test the resolution of a local variable over a global
        """
        self.var = self.mvs.execute("CNV", ["local"])
        self.global_value = self.mvs.execute("CN", [])
        self.local_value = self.mvs.execute("CN", [])

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

        self.mvs.execute("CD", [self.task_frame, "IP", self.inst])
        self.mvs.execute("CD", [self.inst, "var", self.var])
        t = self.mvs.execute("CE", [self.global_task, self.global_value])
        self.mvs.execute("CE", [t, self.var])
        t = self.mvs.execute("CE", [self.symbols, self.local_value])
        self.mvs.execute("CE", [t, self.var])

        ### Execute rules
        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.local_value)
