aeidon/unittest.py

Source code for module aeidon.unittest from file aeidon/unittest.py.

  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
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
# -*- coding: utf-8 -*-

# Copyright (C) 2005 Osmo Salomaa
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Base class for unit test cases."""

import aeidon
import os

__all__ = ("TestCase",)


class TestCase:

    """
    Base class for unit test cases.

    Unit tests are designed to be run with ``py.test``, ``nose`` or something
    compatible. Tests should use plain ``assert`` statements to allow multiple
    different tools to be used to run the tests.
    """

    def assert_raises(self, exception, function, *args, **kwargs):
        """Assert that calling `function` raises `exception`."""
        try:
            function(*args, **kwargs)
        except exception:
            return
        raise AssertionError("{} failed to raise {}"
                             .format(repr(function),
                                     repr(exception)))

    @aeidon.deco.memoize(100)
    def get_sample_text(self, format, name=None):
        """
        Return sample text for subtitle file `format`.

        `name` can be specified if different from ``format.name.lower()``.
        This is useful for variants of a format, e.g. "subrip-extended".
        """
        name = name or format.name.lower()
        basename = "".join((name, format.extension))
        path = os.path.join(aeidon.DATA_DIR, "samples", basename)
        with open(path, "r", encoding="ascii") as f:
            return f.read().strip()

    def new_microdvd_file(self):
        """Return path to a new temporary ``MicroDVD`` file."""
        return self.new_temp_file(aeidon.formats.MICRODVD)

    def new_project(self):
        """Return a new project with both main and translation files."""
        project = aeidon.Project()
        project.open_main(self.new_subrip_file(), "ascii")
        project.open_translation(self.new_microdvd_file(), "ascii")
        return project

    def new_subrip_file(self):
        """Return path to a new temporary ``SubRip`` file."""
        return self.new_temp_file(aeidon.formats.SUBRIP)

    def new_temp_file(self, format, name=None):
        """Return path to a new temporary subtitle file."""
        text = self.get_sample_text(format, name)
        path = aeidon.temp.create(format.extension)
        with open(path, "w", encoding="ascii") as f:
            f.write(text)
        return path

    def setUp(self):
        """Compatibility alias for :meth:`setup_method`."""
        self.setup_method(None)

    def setup_method(self, method):
        """Set state for executing tests in `method`."""
        pass

    def tearDown(self):
        """Compatibility alias for :meth:`teardown_method`."""
        self.teardown_method(None)

    def teardown_method(self, method):
        """Remove state set for executing tests in `method`."""
        pass

    def test___init__(self):
        """Make sure that :meth:`setup_method` is always run."""
        pass