# Copyright 2013-2014 Simon Jagoe
import unittest

from . import _test_cases
from ..loader import Loader


class LoaderTestMixin(object):

    def setUp(self):
        self.loader = Loader()

    def tearDown(self):
        del self.loader


class TestLoadTest(LoaderTestMixin, unittest.TestCase):

    def test_creates_instance_for_valid_test(self):
        test = self.loader.load_test(_test_cases.TestCase, 'test_method')
        self.assertIsInstance(test, unittest.TestCase)
        self.assertIsInstance(test, _test_cases.TestCase)

    def test_raises_for_invalid_test(self):
        with self.assertRaises(TypeError):
            self.loader.load_test(_test_cases.NotTestCase, 'test_method')

    def test_raises_for_test_suite(self):
        with self.assertRaises(TypeError):
            self.loader.load_test(_test_cases.TestSuite, 'test_method')


class TestFindTestMethodNames(LoaderTestMixin, unittest.TestCase):

    def test_finds_valid_test_names(self):
        names = self.loader.find_test_method_names(_test_cases.TestCase)
        self.assertEqual(names, ['test_method'])


class TestLoadCase(LoaderTestMixin, unittest.TestCase):

    def test_creates_unittest_testsuite(self):
        suite = self.loader.load_case(_test_cases.TestCase)
        self.assertIsInstance(suite, unittest.TestSuite)

    def test_raises_for_invalid_test(self):
        with self.assertRaises(TypeError):
            self.loader.load_case(_test_cases.NotTestCase, 'test_method')

    def test_raises_for_test_suite(self):
        with self.assertRaises(TypeError):
            self.loader.load_case(_test_cases.TestSuite, 'test_method')


class TestLoadModule(LoaderTestMixin, unittest.TestCase):

    def test_find_all_cases_in_module(self):
        cases = self.loader.get_test_cases_from_module(_test_cases)
        self.assertEqual(cases, [_test_cases.TestCase])

    def test_load_all_cases_in_module(self):
        suite = self.loader.load_module(_test_cases)
        self.assertIsInstance(suite, unittest.TestSuite)
        sub_suites = list(suite)
        self.assertEqual(len(sub_suites), 1)
        cases = list(sub_suites[0])
        self.assertEqual(len(cases), 1)
        case = cases[0]
        self.assertIsInstance(case, _test_cases.TestCase)

@@ -231,24 +231,238 @@
 lf.loader%0A%0A%0A
+class TestSuiteSubclass(unittest.TestSuite):%0A%0A    pass%0A%0A%0Aclass TestSuiteNotSubclass(object):%0A%0A    def __init__(self, tests=()):%0A        self.tests = tests%0A%0A    def __iter__(self):%0A        return iter(self.tests)%0A%0A%0A
 class TestLo
@@ -497,32 +497,32 @@
 test.TestCase):%0A
-
 %0A    def test_cr
@@ -1520,24 +1520,603 @@
 TestSuite)%0A%0A
+    def test_creates_custom_testsuite_subclass(self):%0A        loader = Loader(test_suite_class=TestSuiteSubclass)%0A        suite = loader.load_case(_test_cases.TestCase)%0A        self.assertIsInstance(suite, unittest.TestSuite)%0A        self.assertIsInstance(suite, TestSuiteSubclass)%0A%0A    def test_creates_custom_testsuite_not_subclass(self):%0A        loader = Loader(test_suite_class=TestSuiteNotSubclass)%0A        suite = loader.load_case(_test_cases.TestCase)%0A        self.assertNotIsInstance(suite, unittest.TestSuite)%0A        self.assertIsInstance(suite, TestSuiteNotSubclass)%0A%0A
     def test
@@ -2475,32 +2475,255 @@
 est.TestCase):%0A%0A
+    def assertSuiteClasses(self, suite, klass):%0A        self.assertIsInstance(suite, klass)%0A        sub_suites = list(suite)%0A        self.assertEqual(len(sub_suites), 1)%0A        self.assertIsInstance(sub_suites%5B0%5D, klass)%0A%0A
     def test_fin
@@ -2989,34 +2989,36 @@
  self.assert
-IsInstance
+SuiteClasses
 (suite, unit
@@ -3211,16 +3211,16 @@
 ases%5B0%5D%0A
-
         
@@ -3249,28 +3249,480 @@
 case, _test_cases.TestCase)%0A
+%0A    def test_creates_custom_testsuite_subclass(self):%0A        loader = Loader(test_suite_class=TestSuiteSubclass)%0A        suite = loader.load_module(_test_cases)%0A        self.assertSuiteClasses(suite, TestSuiteSubclass)%0A%0A    def test_creates_custom_testsuite_not_subclass(self):%0A        loader = Loader(test_suite_class=TestSuiteNotSubclass)%0A        suite = loader.load_module(_test_cases)%0A        self.assertSuiteClasses(suite, TestSuiteNotSubclass)%0A

Add test case for custom test suite class