"""
Custom test suites to replace unittest's TestSuite class.
"""

__version__ = "$Revision: 11271 $"

from unittest import TestSuite as _TestSuite

__all__ = ["TestSuite"]


class TestSuite(_TestSuite):

    """Replacement for unittest's TestSuite class that provides a few
    convenience methods.

    """

    def add_tests_from_class(self, cls):
        "Add all tests of a TestCase class to this suite."
        from _testloader import TestLoader
        self.addTest(TestLoader().loadTestsFromTestCase(cls))

    def debug_with_teardown(self):
        """Run the tests without collecting errors in a TestResult.

        As opposed to debug, this ensures that all teardown methods are run.

        """
        for test in self._tests:
            test.debug_with_teardown()


class DefaultSuite(TestSuite):

    """Default test suite. All test cases listed in the tests attribute are
    exercised. All test suites listed in the test_suites attribute are called
    and the AllTests test suite from all packages listed in the test_packages
    attribute are called.

    Example:

      class AllTests(DefaultSuite):

          tests = [MyClassTest, AnotherClassTest]
          test_suites = [SomeTestSuite]
          test_packages = [subpackage_tests]

    """

    def __init__(self):
        TestSuite.__init__(self)
        cls = self.__class__
        if hasattr(cls, "tests"):
            for class_ in cls.tests:
                self.add_tests_from_class(class_)
        if hasattr(cls, "test_suites"):
            for suite in cls.test_suites:
                self.addTest(suite())
        if hasattr(cls, "test_packages"):
            for pkg in cls.test_packages:
                self.addTest(pkg.AllTests())

