tests.py 22.8 KB
Newer Older
Johannes Bechberger's avatar
Johannes Bechberger committed
1
2
from collections import namedtuple
import shutil
3
from typing import Optional, List, Tuple, Union, Dict
4
import collections
5
from mjtest.environment import Environment, TestMode, TEST_MODES, get_test_dirname
Johannes Bechberger's avatar
Johannes Bechberger committed
6
7
8
9
10
from os.path import join, exists, basename
import logging
import os
import multiprocessing
from mjtest.util.parallelism import available_cpu_count
11
from mjtest.util.utils import cprint, colored, decode
Johannes Bechberger's avatar
Johannes Bechberger committed
12
13
from pprint import pprint
import shutil
Johannes Bechberger's avatar
Johannes Bechberger committed
14
import difflib
Johannes Bechberger's avatar
Johannes Bechberger committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

_LOG = logging.getLogger("tests")


RunResult = namedtuple("RunResult", ['count', 'failed'])


class TestSuite:
    """
    The whole set of tests.
    """

    def __init__(self, env: Environment):
        self.env = env
        self.test_cases = {} # type: Dict[str, List[TestCase]]
30
        self.correct_test_cases = collections.defaultdict(set) # type: Dict[str, Set[str]]
Johannes Bechberger's avatar
Johannes Bechberger committed
31
32
33
        self._load_test_cases()

    def _load_test_cases(self):
Johannes Bechberger's avatar
Johannes Bechberger committed
34
35
36
37
38
        types = [self.env.mode]#TEST_MODES#TEST_MODES[TEST_MODES.index(self.env.mode):]
        if self.env.ci_testing:
            types = TEST_MODES
        elif self.env.mode in TestMode.USE_TESTS_OF_OTHER:
            types += TestMode.USE_TESTS_OF_OTHER[self.env.mode]
Johannes Bechberger's avatar
Johannes Bechberger committed
39
40
41
42
        for type in types:
            self._load_test_case_type(type)

    def _load_test_case_type(self, type: str):
43
        dir = join(self.env.test_dir, get_test_dirname(type))
Johannes Bechberger's avatar
Johannes Bechberger committed
44
45
46
        if exists(dir):
            self._load_test_case_dir(type, dir)
        else:
Johannes Bechberger's avatar
Johannes Bechberger committed
47
            _LOG.info("Test folder {} doesn't exist".format(dir))
Johannes Bechberger's avatar
Johannes Bechberger committed
48
49
50
51
52

    def _load_test_case_dir(self, mode: str, dir: str):
        correct_test_cases = set()
        log_file = self._log_file_for_type(mode)
        if exists(log_file):
53
            with open(log_file, errors="backslashreplace") as f:
54
55
56
57
58
59
                correct_test_cases = set()
                for t in f.readlines():
                    t = t.strip()
                    if len(t) > 0:
                        self.correct_test_cases[mode].add(t)
                        correct_test_cases.add(t)
Johannes Bechberger's avatar
Johannes Bechberger committed
60
61
62
63
        m = mode
        if m != self.env.mode and self.env.mode in TestMode.USE_TESTS_OF_OTHER and \
            m in TestMode.USE_TESTS_OF_OTHER[self.env.mode]:
            m = self.env.mode
64
65
66
        file_names = []
        for root, dirs, files in os.walk(dir):
            base = os.path.relpath(root, dir)
67
            dirname = os.path.dirname(base)
68
69
            if root.startswith("."):
                continue
70
71
            if dir == root:
                file_names.extend(files)
72
            elif ".preprocessed" in base or (dirname.startswith(".") and dirname != "."):
73
                continue
74
75
            elif base.startswith("big") and get_test_dirname(mode) == "exec" and self.env.only_small_exec_tests:
                continue
76
            elif base.startswith("."):
77
78
79
80
81
                continue
            for file in files:
                p = join(base, file)
                if not p.startswith("."):
                    file_names.append(p)
82
        for file in sorted(file_names):
83
            if not TestCase.has_valid_file_ending(self.env.mode, join(dir, file)):
Johannes Bechberger's avatar
Johannes Bechberger committed
84
85
86
87
                _LOG.debug("Skip file " + file)
            elif self.env.only_incorrect_tests and file in correct_test_cases:
                _LOG.info("Skip file {} as its test case was executed correctly the last run")
            else:
88
89
90
91
                file_path = join(dir, file)
                if self.env.has_to_preprocess(file_path) and self.env.is_lib_file(file_path):
                    _LOG.debug("Skip lib file '{}'".format(file))
                    continue
92
                preprocessed = self.env.preprocess(join(dir, TestCase.get_test_case_file(self.env, join(dir, file))))
93
                test_case = TestCase.create_from_file(self.env, m, join(dir, file), preprocessed, mode)
Johannes Bechberger's avatar
Johannes Bechberger committed
94
95
96
                if not test_case:
                    pass
                elif not test_case.can_run():
Johannes Bechberger's avatar
Johannes Bechberger committed
97
98
                    _LOG.debug("Skip test case '{}' because it isn't suited".format(test_case.name()))
                else:
99
100
101
102
103
                    if mode not in self.test_cases:
                        self.test_cases[mode] = []
                    self.test_cases[mode].append(test_case)
        if mode in self.test_cases and len(self.test_cases[mode]) == 0:
            del self.test_cases[mode]
Johannes Bechberger's avatar
Johannes Bechberger committed
104
105

    def _log_file_for_type(self, type: str):
106
        return join(self.env.test_dir, get_test_dirname(type), ".mjtest_correct_testcases_" + self.env.mode)
Johannes Bechberger's avatar
Johannes Bechberger committed
107
108

    def _add_correct_test_case(self, test_case: 'TestCase'):
109
110
111
112
113
114
115
116
117
118
119
120
121
122
        test_dir = None
        modes = [test_case.MODE]
        if test_case.MODE in TestMode.USE_TESTS_OF_OTHER:
            modes += TestMode.USE_TESTS_OF_OTHER[test_case.MODE]
        for mode in modes:
            subdir = os.path.join(self.env.test_dir, get_test_dirname(mode))
            if test_case.file.startswith(subdir):
                test_dir = subdir
                break
        basefile = None
        if test_dir is None:
            basefile = os.path.relpath(test_case.file, self.env.test_dir)
        basefile = os.path.relpath(test_case.file, test_dir)
        self.correct_test_cases[test_case.log_file_mode].add(basefile)
Johannes Bechberger's avatar
Johannes Bechberger committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142

    def run(self) -> RunResult:
        ret = RunResult(0, 0)
        try:
            for mode in self.test_cases.keys():
                single_ret = RunResult(0, 0)
                if self.env.parallel:
                    single_ret = self._run_parallel(mode, available_cpu_count())
                else:
                    single_ret = self._run_sequential(mode)
                ret = RunResult(ret.count + single_ret.count, ret.failed + single_ret.failed)
        except BaseException:
            logging.exception("")
        finally:
            print("-" * 40)
            if ret.failed > 0:  # some tests failed
                print(colored("Ran {} tests, of which ".format(ret.count), "red") +
                      colored("{} failed.".format(ret.failed), "red", attrs=["bold"]))
            else:
                cprint("All {} run tests succeeded".format(ret.count), "green")
143
            if self.env.produce_reports and (self.env.produce_all_reports or ret.failed > 0):
144
                report_dir = self.env.report_dir + "." + ("successful" if ret.failed == 0 else "failed")
145
146
147
148
                try:
                    os.rename(self.env.report_dir, report_dir)
                except IOError:
                    pass
149
150
                print("A full report for each test can be found at {}".format(
                    os.path.relpath(report_dir)))
Johannes Bechberger's avatar
Johannes Bechberger committed
151
152
153
154
155
156
157

            return ret

    def _run_sequential(self, mode: str) -> RunResult:
        failed = 0
        count = 0
        for test_case in self.test_cases[mode]:
Johannes Bechberger's avatar
Johannes Bechberger committed
158
159
160
161
162
163
164
165
166
            try:
                ret = self._run_test_case(test_case)
                if ret is False or not ret.is_correct():
                    failed += 1
                else:
                    self._add_correct_test_case(test_case)
                count += 1
            except KeyboardInterrupt:
                return RunResult(count, failed)
Johannes Bechberger's avatar
Johannes Bechberger committed
167
168
169
170
        return RunResult(count, failed)

    def _func(self, test_case: 'TestCase'):
        ret = self._run_test_case(test_case)
Johannes Bechberger's avatar
Johannes Bechberger committed
171
        if ret is not False and ret.is_correct():
172
173
            return 0, test_case
        return 1, test_case
Johannes Bechberger's avatar
Johannes Bechberger committed
174
175
176
177

    def _run_parallel(self, mode: str, parallel_jobs: int) -> RunResult:
        pool = multiprocessing.Pool(parallel_jobs)
        rets = pool.map(self._func, self.test_cases[mode])
178
179
180
181
182
183
        result = RunResult(len(rets), sum(map(lambda x: x[0], rets)))
        for (suc, test_case) in rets:
            if suc == 0:
                self._add_correct_test_case(test_case)
        return result

Johannes Bechberger's avatar
Johannes Bechberger committed
184
185
186
187
188
189
190
    def _run_test_case(self, test_case: 'TestCase') -> Optional['TestResult']:
        try:
            ret = test_case.run()
            color = "green" if ret.is_correct() else "red"
            print(colored("[{result:7s}] {tc:40s}".format(
                result="SUCCESS" if ret.is_correct() else "FAIL",
                tc=test_case.name()), color, attrs=["bold"]) +
191
                  colored(ret.short_message(), color))
Johannes Bechberger's avatar
Johannes Bechberger committed
192
            try:
193
                if self.env.produce_reports and (self.env.produce_all_reports or not ret.is_correct()):
194
195
196
197
                    if not exists(self.env.report_dir):
                        os.mkdir(self.env.report_dir)
                    rep_dir = join(self.env.report_dir, test_case.type)
                    if not exists(rep_dir):
198
199
200
201
                        try:
                            os.mkdir(rep_dir)
                        except IOError:
                            pass
202
203
                    suffix = ".correct" if ret.is_correct() else ".incorrect"
                    ret.store_at(join(rep_dir, test_case.short_name() + suffix))
204
205
206
                if self.env.output_incorrect_reports and not ret.is_correct():
                    print(colored("Report for failing test case {}".format(test_case.short_name()), "red", attrs=["bold"]))
                    print(colored(ret.long_message(), "red"))
Johannes Bechberger's avatar
Johannes Bechberger committed
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
                return ret
            except IOError:
                _LOG.exception("Caught i/o error while trying to store the report for '{}'"
                               .format(test_case.name()))
                return False
        except KeyboardInterrupt:
            raise
        except BaseException:
            _LOG.exception("At test case '{}'".format(test_case.short_name()))
            return False

    def store(self):
        for mode in self.correct_test_cases.keys():
            log_file = self._log_file_for_type(mode)
            try:
Johannes Bechberger's avatar
Johannes Bechberger committed
222
223
224
225
                try:
                    os.mkdir(os.path.dirname(log_file))
                except IOError:
                    pass
226
                with open(log_file, "w", errors="backslashreplace") as f:
Johannes Bechberger's avatar
Johannes Bechberger committed
227
228
229
230
231
232
233
234
235
236
                    f.write("\n".join(self.correct_test_cases[mode]))
            except IOError as e:
                _LOG.exception("Caught i/o error while storing {}".format(log_file))


class TestCase:
    """
    A single test case.
    """

Johannes Bechberger's avatar
Johannes Bechberger committed
237
    TEST_CASE_CLASSES = dict((k, []) for k in TEST_MODES)
Johannes Bechberger's avatar
Johannes Bechberger committed
238
    FILE_ENDINGS = []
Johannes Bechberger's avatar
Johannes Bechberger committed
239
    INVALID_FILE_ENDINGS = []
Johannes Bechberger's avatar
Johannes Bechberger committed
240

241
    def __init__(self, env: Environment, type: str, file: str, preprocessed_file: str, log_file_mode: str):
Johannes Bechberger's avatar
Johannes Bechberger committed
242
243
244
        self.env = env
        self.type = type
        self.file = file
245
        self.preprocessed_file = preprocessed_file
246
        self.log_file_mode = log_file_mode
Johannes Bechberger's avatar
Johannes Bechberger committed
247
248
249
250
251
252

    def should_succeed(self) -> bool:
        raise NotImplementedError()

    def can_run(self, mode: str = "") -> bool:
        mode = mode or self.env.mode
Johannes Bechberger's avatar
Johannes Bechberger committed
253
        same_mode = self.type == mode
254
        types = TEST_MODES[TEST_MODES.index(self.env.mode):]
255
        if self.env.ci_testing:
Johannes Bechberger's avatar
Johannes Bechberger committed
256
            return same_mode or \
257
258
259
                   (self.type in types and self.should_succeed()) or \
                   (self.type not in types and not self.should_succeed())
        else:
Johannes Bechberger's avatar
Johannes Bechberger committed
260
            return same_mode
Johannes Bechberger's avatar
Johannes Bechberger committed
261
262
263
264
265

    def run(self) -> 'TestResult':
        raise NotImplementedError()

    @classmethod
266
    def create_from_file(cls, env: Environment, mode: str, file: str, preprocessed_file: str, log_file_mode: str) -> Optional['TestCase']:
267
        if cls.has_valid_file_ending(env.mode, file):
268
            return cls._test_case_class_for_file(env.mode, file)(env, mode, file, preprocessed_file, log_file_mode)
Johannes Bechberger's avatar
Johannes Bechberger committed
269
270
271
272
273
274
275
276
277
278
279
        return None

    def name(self):
        return "{}:{}".format(self.type, self.short_name())

    def short_name(self) -> str:
        raise NotImplementedError()

    @classmethod
    def _test_case_class_for_file(cls, type: str, file: str):
        for t in cls.TEST_CASE_CLASSES[type]:
280
            if t.is_file_ending_valid(t, file):
Johannes Bechberger's avatar
Johannes Bechberger committed
281
282
283
284
285
286
287
288
                return t
        return False


    @classmethod
    def has_valid_file_ending(cls, type: str, file: str):
        return cls._test_case_class_for_file(type, file) != False

289
290
291
292
293
294
295
296
297
298
299
300
301
    @staticmethod
    def is_file_ending_valid(cls, file: str):
        return any(file.endswith(e) for e in cls.FILE_ENDINGS) and \
               not any(file.endswith(e) for e in cls.INVALID_FILE_ENDINGS)


    @staticmethod
    def _get_test_case_file(cls, file: str) -> Optional[str]:
        return file

    @classmethod
    def get_test_case_file(cls,  env: Environment, file: str) -> Optional[str]:
        return cls._test_case_class_for_file(env.mode, file)._get_test_case_file(cls, file)
Johannes Bechberger's avatar
Johannes Bechberger committed
302
303
304
305
306
307
308
309
310
311
312
313
314
315

class TestResult:

    def __init__(self, test_case: TestCase, error_code: int):
        self.test_case = test_case
        self.error_code = error_code

    def is_correct(self) -> bool:
        return self.succeeded() == self.test_case.should_succeed()

    def succeeded(self) -> bool:
        return self.error_code == 0

    def store_at(self, file: str):
316
        os.makedirs(os.path.dirname(file), exist_ok=True)
317
        with open(file, "w", errors="backslashreplace") as f:
Johannes Bechberger's avatar
Johannes Bechberger committed
318
319
320
321
322
323
324
325
326
            print(self.long_message(), file=f)

    def short_message(self) -> str:
        raise NotImplementedError()

    def long_message(self) -> str:
        raise NotImplementedError()


327
328
329
330
331
332
333
334
class ExtensibleTestResult(TestResult):

    def __init__(self, test_case: TestCase):
        super().__init__(test_case, None)
        self.messages = []  # type: List[TestResultMessage]
        self.incorrect_msg = None  # type: Optional[str]
        self.has_succeeded = True  # type: bool
        self._contains_error_str = True  # type: bool
335
        self.correct_msg = "correct" # type: str
336
337
338
339
340
341

    def add_error_output(self, title: str, error_output: str):
        """
        Checks for "error" string
        """
        self._contains_error_str = self._contains_error_str and error_output is not None and "error" in error_output
Johannes Bechberger's avatar
Johannes Bechberger committed
342
        self.messages.append(TestResultMessage(title, self._contains_error_str))
343
344
345
346
347
348
349
350

    def add_long_text(self, title: str, content: str, with_line_numbers: bool = True):
        self.messages.append(TestResultMessage(title, content, multiline=True, with_line_numbers=with_line_numbers))

    def add_short_text(self, title, content: str):
        self.messages.append(TestResultMessage(title, content, multiline=False, with_line_numbers=False))

    def add_file(self, title: str, file_name: str, with_line_numbers: bool = True):
351
352
        with open(file_name, "r", errors="backslashreplace") as f:
            file_content = os.linesep.join([line.rstrip() for line in f.read()])
353
354
355
356
357
358
359
360
361
362
363
364
365
        self.add_long_text(title, file_content, with_line_numbers)

    def succeeded(self):
        return self.has_succeeded

    def is_correct(self):
        if self.succeeded():
            return super().is_correct()
        else:
            return super().is_correct() and self._contains_error_str

    def short_message(self) -> str:
        if self.is_correct():
366
            return self.correct_msg
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
        else:
            if not self.succeeded() and not self.test_case.should_succeed() and not self._contains_error_str:
                return "the error output doesn't contain the word \"error\""
            return self.incorrect_msg

    def long_message(self) -> str:
        texts = [self.short_message().capitalize()]
        if self.error_code is not None:
            texts.append("Error code: {}".format(self.error_code))
        for msg in self.messages:
            if msg.multiline:
                texts.append(msg.title + ":")
                texts.append("")
                if msg.with_line_numbers:
                    texts.append(self._ident(msg.content))
                else:
                    texts.append(msg.content)
Johannes Bechberger's avatar
Johannes Bechberger committed
384
                texts.append("")
385
386
            else:
                texts.append("{}: {}".format(msg.title, msg.content))
387
        return "\n".join(texts)
388
389
390
391
392
393
394
395
396
397
398
399

    def _ident(self, text: Union[str,List[str]]) -> str:
        arr = text if isinstance(text, list) else text.split("\n")
        if len(arr) == 0 or text == "":
            return ""
        arr = ["[{:04d}]    {:s}".format(i + 1, l) for (i, l) in enumerate(arr)]
        return "\n".join(arr)

    def add_diff(self, title: str, first: str, second: str, with_line_numbers: bool):
        self.add_long_text(title, "".join(difflib.Differ().compare(first.splitlines(True), second.splitlines(True))),
                           with_line_numbers=with_line_numbers)

400
401
402
    def set_error_code(self, error_code: int):
        self.has_succeeded = error_code == 0
        self.error_code = error_code
403

404

405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
class TestResultMessage:

    def __init__(self, title: str, content: str, multiline: bool, with_line_numbers: bool):
        self.title = title
        self.content = content
        self.multiline = multiline
        self.with_line_numbers = with_line_numbers


class TestResultFactory:
    def __init__(self):
        self._texts = []  # type: [str, str, bool]
        self.return_code = 0
        self.short_error_message = None  # type: str

    def add_short_texts(self, title: str, content: str):
        self._texts.append((title, content, True))

    def add_long_message(self, title: str, content: str):
        self._texts.append((title, content, True))


Johannes Bechberger's avatar
Johannes Bechberger committed
427
428
class BasicTestResult(TestResult):

Johannes Bechberger's avatar
Johannes Bechberger committed
429
430
    def __init__(self, test_case: TestCase, error_code: int, output: str = None, error_output: str = None,
                 incorrect_msg: str = "incorrect return code"):
Johannes Bechberger's avatar
Johannes Bechberger committed
431
        super().__init__(test_case, error_code)
Johannes Bechberger's avatar
Johannes Bechberger committed
432
        self._incorrect_msg = incorrect_msg
433
        self._contains_error_str = error_output is not None and "error" in error_output
Johannes Bechberger's avatar
Johannes Bechberger committed
434
435
        self.error_output = error_output
        self.output = output
436
        self.other_texts = [] # type: List[Tuple[str, str, bool]]
Johannes Bechberger's avatar
Johannes Bechberger committed
437
438
439
440
        if output:
            self.add_additional_text("Output", output)
        if error_output:
            self.add_additional_text("Error output", error_output)
441
        self.has_succeeded = error_code == 0
442
        self.require_error_string_in_error_case = True
443
444
445

    def succeeded(self):
        return self.has_succeeded
Johannes Bechberger's avatar
Johannes Bechberger committed
446
447
448
449
450

    def is_correct(self):
        if self.succeeded():
            return super().is_correct()
        else:
451
            return super().is_correct() and (self._contains_error_str or not self.require_error_string_in_error_case)
Johannes Bechberger's avatar
Johannes Bechberger committed
452
453
454
455
456

    def short_message(self) -> str:
        if self.is_correct():
            return "correct"
        else:
457
458
            if not self.succeeded() and not self.test_case.should_succeed() and not self._contains_error_str and \
               not self.require_error_string_in_error_case:
Johannes Bechberger's avatar
Johannes Bechberger committed
459
                return "the error output doesn't contain the word \"error\""
Johannes Bechberger's avatar
Johannes Bechberger committed
460
            return self._incorrect_msg
Johannes Bechberger's avatar
Johannes Bechberger committed
461
462

    def long_message(self) -> str:
463
        file_content = []
464
        with open(self.test_case.preprocessed_file, "r", errors="backslashreplace") as f:
465
            file_content = [line.rstrip() for line in f]
466
467
468
469
470
471
472
473
474
475
476
        others = []
        for title, content, long_text in self.other_texts:
            if long_text:
                others.append("""
{}:

{}

""".format(title, self._ident(content)))
            else:
                others.append("""{}: {}\n""".format(title, content))
Johannes Bechberger's avatar
Johannes Bechberger committed
477
478
479
480
481
482
483
        return """{}

Source file:

{}

Return code: {}
484
485

{}
Johannes Bechberger's avatar
Johannes Bechberger committed
486
""".format(self.short_message().capitalize(), self._ident(file_content), self.error_code,
487
488
489
490
491
492
493
           "\n".join(others))

    def add_additional_text(self, title: str, content: str):
        self.other_texts.append((title, content, True))

    def add_additional_text_line(self, title: str, content: str):
        self.other_texts.append((title, content, False))
Johannes Bechberger's avatar
Johannes Bechberger committed
494
495
496

    def _ident(self, text: Union[str,List[str]]) -> str:
        arr = text if isinstance(text, list) else text.split("\n")
497
498
499
500
        if len(arr) == 0 or text == "":
            return ""
        arr = ["[{:04d}]    {:s}".format(i + 1, l) for (i, l) in enumerate(arr)]
        return "\n".join(arr)
Johannes Bechberger's avatar
Johannes Bechberger committed
501

Johannes Bechberger's avatar
Johannes Bechberger committed
502
503
504

class BasicDiffTestResult(BasicTestResult):

505
506
    def __init__(self, test_case: TestCase, error_code: int, output: str, error_output: str, expected_output: str,
                 short_error_message: str = None):
Johannes Bechberger's avatar
Johannes Bechberger committed
507
508
        super().__init__(test_case, error_code, output, error_output)
        self.expected_output = expected_output
509
        self._is_output_correct = self.expected_output.strip() == self.output.strip()
Johannes Bechberger's avatar
Johannes Bechberger committed
510
511
512
513
514
        if self.is_correct():
            self.add_additional_text("Expected and actual output", self.output)
        elif self.succeeded() and self.test_case.should_succeed():
            self.add_additional_text("Diff[expected output, actual output]", self._output_diff())
            self.add_additional_text("Expected output", self.expected_output)
515
        self.short_error_message = short_error_message
516
            #self.add_additional_text("Actual output", self.output)
Johannes Bechberger's avatar
Johannes Bechberger committed
517
518
519
520
521
522
523
524

    def is_correct(self):
        if self.succeeded():
            return super().is_correct() and self.is_output_correct()
        else:
            return super().is_correct() and self._contains_error_str

    def _output_diff(self) -> str:
525
        return "".join(difflib.Differ().compare(self.expected_output.splitlines(True), self.output.splitlines(True)))
Johannes Bechberger's avatar
Johannes Bechberger committed
526
527
528
529
530
531
532
533

    def is_output_correct(self) -> str:
        return self._is_output_correct

    def short_message(self) -> str:
        if self.is_correct():
            return "correct"
        else:
534
535
            if self.short_error_message:
                return self.short_error_message
Johannes Bechberger's avatar
Johannes Bechberger committed
536
537
538
539
540
541
542
            if not self.succeeded() and not self.test_case.should_succeed() and not self._contains_error_str:
                return "the error output doesn't contain the word \"error\""
            if self.succeeded() and self.test_case.should_succeed():
                return "the actual output differs from the expected"
            return "incorrect return code"


Johannes Bechberger's avatar
Johannes Bechberger committed
543
544
545
546
547
548
class DiffTest(TestCase):

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

Johannes Bechberger's avatar
Johannes Bechberger committed
549
550
    def __init__(self, env: Environment, type: str, file: str, preprocessed_file: str, log_file_mode):
        super().__init__(env, type, file, preprocessed_file, log_file_mode)
Johannes Bechberger's avatar
Johannes Bechberger committed
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
        self._should_succeed = not file.endswith(".invalid.mj")
        self._expected_output_file = file + self.OUTPUT_FILE_ENDING
        self._has_expected_output_file = exists(self._expected_output_file)

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

    def short_name(self) -> str:
        return 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 and self.type == self.MODE and self.env.mode == self.MODE:
566
                with open(self._expected_output_file, "r", errors="backslashreplace") as f:
Johannes Bechberger's avatar
Johannes Bechberger committed
567
568
569
570
                    exp_out = f.read()
            #else:
            #    _LOG.error("Expected output file for test case {}:{} is missing.".format(self.MODE, self.short_name()))
        if self.type == self.MODE and self.env.mode == self.MODE:
571
572
            return BasicDiffTestResult(self, rtcode, decode(out), decode(err), exp_out)
        return BasicTestResult(self, rtcode, decode(out), decode(err))
Johannes Bechberger's avatar
Johannes Bechberger committed
573
574
575
576
577
578
579


class LexerDiffTest(DiffTest):

    MODE = TestMode.lexer

TestCase.TEST_CASE_CLASSES[TestMode.lexer].append(LexerDiffTest)
Johannes Bechberger's avatar
Johannes Bechberger committed
580
581

import mjtest.test.syntax_tests
582
import mjtest.test.ast_tests
Johannes Bechberger's avatar
Johannes Bechberger committed
583
import mjtest.test.semantic_tests
584
585
import mjtest.test.exec_tests
import mjtest.test.bench