ast_tests.py 1.39 KB
Newer Older
Johannes Bechberger's avatar
Johannes Bechberger committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import shutil, logging
from mjtest.environment import Environment, TestMode
from mjtest.test.tests import TestCase, BasicDiffTestResult
from os import path

_LOG = logging.getLogger("tests")


class ASTDiffTest(TestCase):

    FILE_ENDINGS = [".invalid.mj", ".valid.mj", ".mj"]
    OUTPUT_FILE_ENDING = ".out"
    MODE = TestMode.ast

    def __init__(self, env: Environment, type: str, file: str):
        super().__init__(env, type, file)
        self._should_succeed = not file.endswith(".invalid.mj")
        self._expected_output_file = file + self.OUTPUT_FILE_ENDING
        self._has_expected_output_file = path.exists(self._expected_output_file)

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

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

    def run(self) -> BasicDiffTestResult:
        out, err, rtcode = self.env.run_mj_command(self.MODE, self.file)
        exp_out = ""
        if rtcode > 0 and self.should_succeed():
            if self._has_expected_output_file:
                with open(self._expected_output_file, "r") as f:
                    exp_out = f.read()
            else:
                _LOG.error("Expected output file for test case {}:{} is missing.".format(self.MODE, self.short_name()))
        return BasicDiffTestResult(self, rtcode, out.decode(), err.decode(), exp_out)

TestCase.TEST_CASE_CLASSES[TestMode.ast].append(ASTDiffTest)