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

6 7
from mjtest.util.utils import decode

Johannes Bechberger's avatar
Johannes Bechberger committed
8 9 10

class BasicSyntaxTest(TestCase):

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

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

Johannes Bechberger's avatar
Johannes Bechberger committed
21 22 23 24
    def should_succeed(self) -> bool:
        return self._should_succeed

    def short_name(self) -> str:
25 26 27 28 29 30 31 32 33
        test_dir = None
        modes = [self.MODE]
        if self.MODE in TestMode.USE_TESTS_OF_OTHER:
            modes += TestMode.USE_TESTS_OF_OTHER[self.MODE]
        for mode in modes:
            subdir = path.join(self.env.test_dir, get_test_dirname(mode))
            if self.file.startswith(subdir):
                test_dir = subdir
                break
34 35
        if test_dir is None:
            return path.relpath(self.file, self.env.test_dir)
36
        return path.relpath(self.file, test_dir)
Johannes Bechberger's avatar
Johannes Bechberger committed
37

38
    def run(self) -> BasicTestResult:
39
        out, err, rtcode = self.env.run_mj_command(self.MODE, self.preprocessed_file)
40
        return BasicTestResult(self, rtcode, decode(out), decode(err))
Johannes Bechberger's avatar
Johannes Bechberger committed
41 42 43

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

44 45 46 47 48 49 50

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

    FILE_ENDINGS = [".java"]
Johannes Bechberger's avatar
Johannes Bechberger committed
51
    SYNTAX_TEST = True
52

53 54
    def __init__(self, env: Environment, type: str, file: str, preprocessed_file: str, log_file_mode: str):
        super().__init__(env, type, file, preprocessed_file, log_file_mode)
55
        tmp_dir = self.env.create_pid_local_tmpdir()
56
        _, self.javac_err, self.javac_rtcode = \
57
            self.env.run_command("javac", path.relpath(preprocessed_file), "-d", tmp_dir, "-verbose")
58
        self.javac_err = decode(self.javac_err)  # type: str
Johannes Bechberger's avatar
Johannes Bechberger committed
59
        self._should_succeed = self._is_file_syntactically_correct() if self.SYNTAX_TEST else self.javac_rtcode == 0
60

Johannes Bechberger's avatar
Johannes Bechberger committed
61 62 63 64 65
    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
66 67 68

    def run(self) -> BasicTestResult:
        ret = super().run()
Johannes Bechberger's avatar
Johannes Bechberger committed
69
        ret.add_additional_text("javac (verbose) error output", self.javac_err)
70
        ret.add_additional_text_line("javac return code", str(self.javac_rtcode))
Johannes Bechberger's avatar
Johannes Bechberger committed
71
        ret.add_additional_text_line("Is syntax correct? ", str(self._is_file_syntactically_correct()))
72 73
        return ret

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