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

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

        self.task_root = self.mvs.execute("CN", [])[0]
        self.task_frame = self.mvs.execute("CN", [])[0]
        self.task_input = 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, "task_1", self.task_root])
        self.mvs.execute("CD", [self.task_root, "input", self.task_input])
        self.mvs.execute("CD", [self.task_root, "frame", self.task_frame])
        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_input(self):
        self.input_value = self.mvs.execute("CN", [])[0]
        self.inst = self.mvs.execute("CNV", [{"value":"input"}])[0]

        self.value_a = self.mvs.execute("CNV", ["a"])[0]

        self.mvs.execute("CD", [self.task_frame, "IP", self.inst])
        self.mvs.execute("CD", [self.task_input, "next", self.input_value])
        self.mvs.execute("CD", [self.task_input, "value", self.value_a])

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

        self.assertEquals(get_inst(self.root, self.mvs), "input")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        self.assertEquals(get_returnvalue_ref(self.root, self.mvs), self.value_a)
