File: utils.py

package info (click to toggle)
pcbasic 2.0.7-8
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 35,416 kB
  • sloc: python: 28,411; sh: 103; makefile: 10
file content (51 lines) | stat: -rw-r--r-- 1,484 bytes parent folder | download | duplicates (2)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
"""
PC-BASIC tests.utils
Shared testing utilities

(c) 2020--2023 Rob Hagemans
This file is released under the GNU GPL version 3 or later.
"""

import unittest
import os
import shutil
from unittest import main as run_tests


class TestCase(unittest.TestCase):
    """Base class for test cases."""

    tag = None

    def __init__(self, *args, **kwargs):
        """Define output dir name."""
        unittest.TestCase.__init__(self, *args, **kwargs)
        here = os.path.dirname(os.path.abspath(__file__))
        self._dir = os.path.join(here, u'output', self.tag)
        # does not need to exist
        self._model_dir = os.path.join(here, u'model', self.tag)

    def setUp(self):
        """Ensure output directory exists and is empty."""
        try:
            shutil.rmtree(self._dir)
        except EnvironmentError:
            pass
        if not os.path.isdir(self._dir):
            os.makedirs(self._dir)

    def output_path(self, *names):
        """Output file name."""
        return os.path.join(self._dir, *names)

    def model_path(self, *names):
        """Model file name."""
        return os.path.join(self._model_dir, *names)

    def get_text_stripped(self, s):
        """Get screen chars joined, stripped of trailing spaces."""
        return [_row.rstrip() for _row in self.get_text(s)]

    def get_text(self, s, as_type=bytes):
        """Get screen chars joined."""
        return [as_type().join(_row) for _row in s.get_chars(as_type=as_type)]