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

class Testcall(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.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_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_no_params(self):
        """
        Test the code:
            def function_A():
                return

            function_A()
            a
        """
        self.params = self.mvs.execute("CN", [])[0]
        self.var_function = self.mvs.execute("CN", [])[0]
        self.signature = self.mvs.execute("CN", [])[0]
        self.val_function = self.mvs.execute("CN", [])[0]

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

        self.const_a = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.value_a = self.mvs.execute("CNV", ["a"])[0]

        self.mvs.execute("CD", [self.user_frame, "IP", self.inst])
        self.mvs.execute("CD", [self.inst, "func", self.access])
        self.mvs.execute("CD", [self.inst, "next", self.const_a])
        self.mvs.execute("CD", [self.access, "var", self.resolve])
        self.mvs.execute("CD", [self.resolve, "var", self.var_function])
        self.mvs.execute("CD", [self.val_function, "value", self.signature])
        self.mvs.execute("CD", [self.signature, "body", self.ret])
        self.mvs.execute("CD", [self.signature, "params", self.params])
        self.mvs.execute("CD", [self.const_a, "node", self.value_a])
        t = self.mvs.execute("CE", [self.symbols, self.val_function])[0]
        self.mvs.execute("CE", [t, self.var_function])

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

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

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

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

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

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

        self.assertEquals(get_inst(self.root, self.mvs), "call")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        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")
        self.assertEquals(get_returnvalue(self.root, self.mvs), "a")

    def test_single_param(self):
        """
        Test the code:
            def function_A(abc):
                return

            function_A(abc="def")
            a
        """
        self.params = self.mvs.execute("CN", [])[0]
        self.var_function = self.mvs.execute("CN", [])[0]
        self.signature = self.mvs.execute("CN", [])[0]
        self.val_function = self.mvs.execute("CN", [])[0]
        self.var_abc = self.mvs.execute("CN", [])[0]
        self.actual_arg = self.mvs.execute("CN", [])[0]

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

        self.const_a = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.const_def = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.value_a = self.mvs.execute("CNV", ["a"])[0]
        self.value_abc = self.mvs.execute("CNV", ["abc"])[0]
        self.value_def = self.mvs.execute("CNV", ["def"])[0]

        self.mvs.execute("CD", [self.user_frame, "IP", self.inst])
        self.mvs.execute("CD", [self.inst, "func", self.access])
        self.mvs.execute("CD", [self.inst, "next", self.const_a])
        self.mvs.execute("CD", [self.inst, "params", self.actual_arg])
        self.mvs.execute("CD", [self.inst, "last_param", self.actual_arg])
        self.mvs.execute("CD", [self.actual_arg, "value", self.const_def])
        self.mvs.execute("CD", [self.actual_arg, "name", self.mvs.execute("CNV", ["abc"])[0]])
        self.mvs.execute("CD", [self.const_def, "node", self.value_def])
        self.mvs.execute("CD", [self.access, "var", self.resolve])
        self.mvs.execute("CD", [self.resolve, "var", self.var_function])
        self.mvs.execute("CD", [self.val_function, "value", self.signature])
        self.mvs.execute("CD", [self.signature, "body", self.ret])
        self.mvs.execute("CD", [self.signature, "params", self.params])
        self.mvs.execute("CD", [self.const_a, "node", self.value_a])
        t = self.mvs.execute("CE", [self.symbols, self.val_function])[0]
        self.mvs.execute("CE", [t, self.var_function])
        self.mvs.execute("CD", [self.params, "abc", self.var_abc])

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

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

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

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

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

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

        self.assertEquals(get_inst(self.root, self.mvs), "call")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        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")
        self.assertEquals(get_returnvalue(self.root, self.mvs), "a")

    def test_two_params(self):
        """
        Test the code:
            def function_A(a, b):
                return

            function_A(a="d", b="e")
            f
        """
        self.params = self.mvs.execute("CN", [])[0]
        self.var_function = self.mvs.execute("CN", [])[0]
        self.signature = self.mvs.execute("CN", [])[0]
        self.val_function = self.mvs.execute("CN", [])[0]
        self.var_a = self.mvs.execute("CN", [])[0]
        self.var_b = self.mvs.execute("CN", [])[0]
        self.actual_arg_a = self.mvs.execute("CN", [])[0]
        self.actual_arg_b = self.mvs.execute("CN", [])[0]

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

        self.const_a = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.const_b = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.const_d = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.const_e = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.const_f = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.value_a = self.mvs.execute("CNV", ["a"])[0]
        self.value_b = self.mvs.execute("CNV", ["b"])[0]
        self.value_d = self.mvs.execute("CNV", ["d"])[0]
        self.value_e = self.mvs.execute("CNV", ["e"])[0]
        self.value_f = self.mvs.execute("CNV", ["f"])[0]

        self.mvs.execute("CD", [self.user_frame, "IP", self.inst])
        self.mvs.execute("CD", [self.inst, "func", self.access])
        self.mvs.execute("CD", [self.inst, "next", self.const_a])
        self.mvs.execute("CD", [self.inst, "params", self.actual_arg_a])
        self.mvs.execute("CD", [self.actual_arg_a, "next_param", self.actual_arg_b])
        self.mvs.execute("CD", [self.inst, "last_param", self.actual_arg_b])
        self.mvs.execute("CD", [self.actual_arg_a, "value", self.const_d])
        self.mvs.execute("CD", [self.actual_arg_a, "name", self.mvs.execute("CNV", ["a"])[0]])
        self.mvs.execute("CD", [self.actual_arg_b, "value", self.const_e])
        self.mvs.execute("CD", [self.actual_arg_b, "name", self.mvs.execute("CNV", ["b"])[0]])
        self.mvs.execute("CD", [self.const_a, "node", self.value_a])
        self.mvs.execute("CD", [self.const_b, "node", self.value_b])
        self.mvs.execute("CD", [self.const_d, "node", self.value_d])
        self.mvs.execute("CD", [self.const_e, "node", self.value_e])
        self.mvs.execute("CD", [self.const_f, "node", self.value_f])
        self.mvs.execute("CD", [self.access, "var", self.resolve])
        self.mvs.execute("CD", [self.resolve, "var", self.var_function])
        self.mvs.execute("CD", [self.val_function, "value", self.signature])
        self.mvs.execute("CD", [self.signature, "body", self.ret])
        self.mvs.execute("CD", [self.signature, "params", self.params])
        t = self.mvs.execute("CE", [self.symbols, self.val_function])[0]
        self.mvs.execute("CE", [t, self.var_function])
        self.mvs.execute("CD", [self.params, "a", self.var_a])
        self.mvs.execute("CD", [self.params, "b", self.var_b])

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

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

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

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

        self.assertEquals(get_inst(self.root, self.mvs), "call")
        self.assertEquals(get_phase_ref(self.root, self.mvs), self.actual_arg_a)
        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), "call")
        self.assertEquals(get_phase_ref(self.root, self.mvs), self.actual_arg_b)
        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), "call")
        self.assertEquals(get_phase(self.root, self.mvs), "call")
        execute_until_finished(self.mvk, self.mvs)

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

        self.assertEquals(get_inst(self.root, self.mvs), "call")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        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")
        self.assertEquals(get_returnvalue(self.root, self.mvs), "a")

    def test_three_params(self):
        """
        Test the code:
            def function_A(a, b, c):
                return

            function_A(a="d", b="e", c="f")
            f
        """
        self.params = self.mvs.execute("CN", [])[0]
        self.var_function = self.mvs.execute("CN", [])[0]
        self.signature = self.mvs.execute("CN", [])[0]
        self.val_function = self.mvs.execute("CN", [])[0]
        self.var_a = self.mvs.execute("CN", [])[0]
        self.var_b = self.mvs.execute("CN", [])[0]
        self.var_c = self.mvs.execute("CN", [])[0]
        self.actual_arg_a = self.mvs.execute("CN", [])[0]
        self.actual_arg_b = self.mvs.execute("CN", [])[0]
        self.actual_arg_c = self.mvs.execute("CN", [])[0]

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

        self.const_a = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.const_b = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.const_c = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.const_d = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.const_e = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.const_f = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.value_a = self.mvs.execute("CNV", ["a"])[0]
        self.value_b = self.mvs.execute("CNV", ["b"])[0]
        self.value_c = self.mvs.execute("CNV", ["c"])[0]
        self.value_d = self.mvs.execute("CNV", ["d"])[0]
        self.value_e = self.mvs.execute("CNV", ["e"])[0]
        self.value_f = self.mvs.execute("CNV", ["f"])[0]

        self.mvs.execute("CD", [self.user_frame, "IP", self.inst])
        self.mvs.execute("CD", [self.inst, "func", self.access])
        self.mvs.execute("CD", [self.inst, "next", self.const_a])
        self.mvs.execute("CD", [self.inst, "params", self.actual_arg_a])
        self.mvs.execute("CD", [self.actual_arg_a, "next_param", self.actual_arg_b])
        self.mvs.execute("CD", [self.actual_arg_b, "next_param", self.actual_arg_c])
        self.mvs.execute("CD", [self.inst, "last_param", self.actual_arg_c])
        self.mvs.execute("CD", [self.actual_arg_a, "value", self.const_d])
        self.mvs.execute("CD", [self.actual_arg_a, "name", self.mvs.execute("CNV", ["a"])[0]])
        self.mvs.execute("CD", [self.actual_arg_b, "value", self.const_e])
        self.mvs.execute("CD", [self.actual_arg_b, "name", self.mvs.execute("CNV", ["b"])[0]])
        self.mvs.execute("CD", [self.actual_arg_c, "value", self.const_f])
        self.mvs.execute("CD", [self.actual_arg_c, "name", self.mvs.execute("CNV", ["c"])[0]])
        self.mvs.execute("CD", [self.const_a, "node", self.value_a])
        self.mvs.execute("CD", [self.const_b, "node", self.value_b])
        self.mvs.execute("CD", [self.const_c, "node", self.value_c])
        self.mvs.execute("CD", [self.const_d, "node", self.value_d])
        self.mvs.execute("CD", [self.const_e, "node", self.value_e])
        self.mvs.execute("CD", [self.const_f, "node", self.value_f])
        self.mvs.execute("CD", [self.access, "var", self.resolve])
        self.mvs.execute("CD", [self.resolve, "var", self.var_function])
        self.mvs.execute("CD", [self.val_function, "value", self.signature])
        self.mvs.execute("CD", [self.signature, "body", self.ret])
        self.mvs.execute("CD", [self.signature, "params", self.params])
        t = self.mvs.execute("CE", [self.symbols, self.val_function])[0]
        self.mvs.execute("CE", [t, self.var_function])
        self.mvs.execute("CD", [self.params, "a", self.var_a])
        self.mvs.execute("CD", [self.params, "b", self.var_b])
        self.mvs.execute("CD", [self.params, "c", self.var_c])

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

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

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

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

        self.assertEquals(get_inst(self.root, self.mvs), "call")
        self.assertEquals(get_phase_ref(self.root, self.mvs), self.actual_arg_a)
        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), "call")
        self.assertEquals(get_phase_ref(self.root, self.mvs), self.actual_arg_b)
        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), "call")
        self.assertEquals(get_phase_ref(self.root, self.mvs), self.actual_arg_c)
        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), "call")
        self.assertEquals(get_phase(self.root, self.mvs), "call")
        execute_until_finished(self.mvk, self.mvs)

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

        self.assertEquals(get_inst(self.root, self.mvs), "call")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        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")
        self.assertEquals(get_returnvalue(self.root, self.mvs), "a")
