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

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

    def test_basic(self):
        # Create a continue construct
        root = self.root
        task_root = self.mvs.execute("CN", [])[0]
        task_frame = self.mvs.execute("CN", [])[0]
        phase = self.mvs.execute("CNV", ["init"])[0]
        inst = self.mvs.execute("CNV", [{"value":"continue"}])[0]
        while_inst = self.mvs.execute("CNV", ["while"])[0]
        evalstack_top = self.mvs.execute("CN", [])[0]
        evalstack_bottom = self.mvs.execute("CN", [])[0]
        init_phase = self.mvs.execute("CNV", ["finish"])[0]

        self.mvs.execute("CD", [task_frame, "evalstack", evalstack_top])
        self.mvs.execute("CD", [evalstack_top, "prev", evalstack_bottom])
        self.mvs.execute("CD", [evalstack_bottom, "inst", while_inst])
        self.mvs.execute("CD", [evalstack_bottom, "phase", init_phase])
        self.mvs.execute("CD", [root, "task_1", task_root])
        self.mvs.execute("CD", [task_root, "frame", task_frame])
        self.mvs.execute("CD", [task_frame, "phase", phase])
        self.mvs.execute("CD", [task_frame, "IP", inst])
        self.mvs.execute("CD", [inst, "while", while_inst])

        # Do the invocation until it returns None
        execute_until_finished(self.mvk, self.mvs)

        # Execution of the command inited, so the MvS should be in the correct new state right now
        self.assertEquals(self.mvs.execute("RD", [root, "task_1"])[0], task_root)
        self.assertEquals(self.mvs.execute("RD", [task_root, "frame"])[0], task_frame)
        self.assertEquals(self.mvs.execute("RV", [self.mvs.execute("RD", [task_frame, "phase"])[0]])[0], "finish")
        self.assertEquals(self.mvs.execute("RD", [task_frame, "IP"])[0], inst)
        self.assertEquals(self.mvs.execute("RD", [inst, "while"])[0], while_inst)
        self.assertNotEquals(self.mvs.execute("RD", [task_frame, "evalstack"])[0], evalstack_top)
