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

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

        self.mvs.execute("CD", [self.user_frame, "IP", self.inst])
        self.mvs.execute("CD", [self.inst, "var", self.var])
        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), "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"])[0]
        self.value = self.mvs.execute("CN", [])[0]

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

        self.mvs.execute("CD", [self.user_frame, "IP", self.inst])
        self.mvs.execute("CD", [self.inst, "var", self.var])
        t = self.mvs.execute("CE", [self.global_user, self.value])[0]
        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"])[0]
        self.global_value = self.mvs.execute("CN", [])[0]
        self.local_value = self.mvs.execute("CN", [])[0]

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

        self.mvs.execute("CD", [self.user_frame, "IP", self.inst])
        self.mvs.execute("CD", [self.inst, "var", self.var])
        t = self.mvs.execute("CE", [self.global_user, self.global_value])[0]
        self.mvs.execute("CE", [t, self.var])
        t = self.mvs.execute("CE", [self.symbols, self.local_value])[0]
        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)
