syntax_tests.py 1.73 KB
Newer Older
Johannes Bechberger's avatar
Johannes Bechberger committed
1 2 3
from mjtest.environment import Environment, TestMode
from mjtest.test.tests import TestCase, TestResult, BasicTestResult
from os import path
4
from mjtest.util.shell import execute
Johannes Bechberger's avatar
Johannes Bechberger committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20


class BasicSyntaxTest(TestCase):

    FILE_ENDINGS = [".invalid.mj", ".valid.mj", ".mj"]

    def __init__(self, env: Environment, type: str, file: str):
        super().__init__(env, type, file)
        self._should_succeed = not file.endswith(".invalid.mj")

    def should_succeed(self) -> bool:
        return self._should_succeed

    def short_name(self) -> str:
        return path.basename(self.file)[:-3]

21
    def run(self) -> BasicTestResult:
Johannes Bechberger's avatar
Johannes Bechberger committed
22 23 24 25 26
        out, err, rtcode = self.env.run_mj_command("--parsetest ", self.file)
        return BasicTestResult(self, rtcode, out.decode(), err.decode())

TestCase.TEST_CASE_CLASSES[TestMode.syntax].append(BasicSyntaxTest)

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

class JavaCompileTest(BasicSyntaxTest):
    """
    The MiniJava compiler should behave the same as javac
    """

    FILE_ENDINGS = [".java"]

    def __init__(self, env: Environment, type: str, file: str):
        super().__init__(env, type, file)
        tmp_dir = self.env.create_tmpdir()
        _, self.javac_err, self.javac_rtcode = \
            self.env.run_command("javac", file, "-d", tmp_dir)
        self._should_succeed = self.javac_rtcode == 0

    def short_name(self) -> str:
        return path.basename(self.file)

    def run(self) -> BasicTestResult:
        ret = super().run()
        ret.add_additional_text("javac error output", str(self.javac_err))
        ret.add_additional_text_line("javac return code", str(self.javac_rtcode))
        return ret

TestCase.TEST_CASE_CLASSES[TestMode.syntax].append(JavaCompileTest)
TestCase.TEST_CASE_CLASSES[TestMode.semantic].append(JavaCompileTest)