ast_tests.py 1.58 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
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():
31
            if self._has_expected_output_file and self.type == self.MODE and self.env.mode == self.MODE:
Johannes Bechberger's avatar
Johannes Bechberger committed
32 33
                with open(self._expected_output_file, "r") as f:
                    exp_out = f.read()
34 35
            #else:
            #    _LOG.error("Expected output file for test case {}:{} is missing.".format(self.MODE, self.short_name()))
Johannes Bechberger's avatar
Johannes Bechberger committed
36 37
        return BasicDiffTestResult(self, rtcode, out.decode(), err.decode(), exp_out)

38 39 40 41 42 43 44

class LexerDiffTest(ASTDiffTest):

    MODE = TestMode.lexer

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