syntax_tests.py 2.25 KB
Newer Older
Johannes Bechberger's avatar
Johannes Bechberger committed
1
import shutil
2
from mjtest.environment import Environment, TestMode, TEST_MODES
Johannes Bechberger's avatar
Johannes Bechberger committed
3
from mjtest.test.tests import TestCase, BasicTestResult
Johannes Bechberger's avatar
Johannes Bechberger committed
4 5 6 7 8 9
from os import path


class BasicSyntaxTest(TestCase):

    FILE_ENDINGS = [".invalid.mj", ".valid.mj", ".mj"]
Johannes Bechberger's avatar
Johannes Bechberger committed
10
    MODE = TestMode.syntax
Johannes Bechberger's avatar
Johannes Bechberger committed
11 12 13

    def __init__(self, env: Environment, type: str, file: str):
        super().__init__(env, type, file)
14 15 16 17
        if type != self.MODE and TEST_MODES.index(type) > TEST_MODES.index(self.MODE):
            self._should_succeed = True
        else:
            self._should_succeed = not file.endswith(".invalid.mj")
Johannes Bechberger's avatar
Johannes Bechberger committed
18 19 20 21
    def should_succeed(self) -> bool:
        return self._should_succeed

    def short_name(self) -> str:
Johannes Bechberger's avatar
Johannes Bechberger committed
22
        return path.basename(self.file)
Johannes Bechberger's avatar
Johannes Bechberger committed
23

24
    def run(self) -> BasicTestResult:
Johannes Bechberger's avatar
Johannes Bechberger committed
25
        out, err, rtcode = self.env.run_mj_command(self.MODE, self.file)
Johannes Bechberger's avatar
Johannes Bechberger committed
26 27 28 29
        return BasicTestResult(self, rtcode, out.decode(), err.decode())

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

30 31 32 33 34 35 36

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

    FILE_ENDINGS = [".java"]
Johannes Bechberger's avatar
Johannes Bechberger committed
37
    SYNTAX_TEST = True
38 39 40 41 42

    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 = \
Johannes Bechberger's avatar
Johannes Bechberger committed
43 44
            self.env.run_command("javac", path.relpath(file), "-d", tmp_dir, "-verbose")
        self.javac_err = self.javac_err.decode()  # type: str
Johannes Bechberger's avatar
Johannes Bechberger committed
45
        shutil.rmtree(tmp_dir, ignore_errors=True)
Johannes Bechberger's avatar
Johannes Bechberger committed
46
        self._should_succeed = self._is_file_syntactically_correct() if self.SYNTAX_TEST else self.javac_rtcode == 0
47

Johannes Bechberger's avatar
Johannes Bechberger committed
48 49 50 51 52
    def _is_file_syntactically_correct(self):
        return self.javac_rtcode == 0 or "META-INF" in self.javac_err

    def _is_file_semantically_correct(self):
        return self.javac_rtcode == 0
53 54 55

    def run(self) -> BasicTestResult:
        ret = super().run()
Johannes Bechberger's avatar
Johannes Bechberger committed
56
        ret.add_additional_text("javac (verbose) error output", self.javac_err)
57
        ret.add_additional_text_line("javac return code", str(self.javac_rtcode))
Johannes Bechberger's avatar
Johannes Bechberger committed
58
        ret.add_additional_text_line("Is syntax correct? ", str(self._is_file_syntactically_correct()))
59 60 61
        return ret

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