Debian Codegraph @

lupa/tests/__init__.py

raw symbols SCIP
1 from __future__ import absolute_import
2
3 import unittest
4 import doctest
5 import os
6 import os.path as os_path
7 import sys
8
9 import lupa
10
11
12 class LupaTestCase(unittest.TestCase):
13 """
14 Subclasses can use 'self.lupa' to get the test module, which build_suite_for_module() below will vary.
15 """
16 lupa = lupa
17
18 if sys.version_info < (3, 4):
19 from contextlib import contextmanager
20
21 @contextmanager
22 def subTest(self, message=None, **parameters):
23 """Dummy implementation"""
24 yield
25
26
27 def find_lua_modules():
28 modules = [lupa]
29 imported = set()
30 for filename in os.listdir(os.path.dirname(os.path.dirname(__file__))):
31 if not filename.startswith('lua'):
32 continue
33 module_name = "lupa." + filename.partition('.')[0]
34 if module_name in imported:
35 continue
36 try:
37 module = __import__(module_name, fromlist='*', level=0)
38 except ImportError:
39 pass
40 else:
41 imported.add(module_name)
42 modules.append(module)
43
44 return modules
45
46
47 def build_suite_for_modules(loader, test_module_globals):
48 suite = unittest.TestSuite()
49 all_lua_modules = find_lua_modules()
50
51 for module in all_lua_modules[1:]:
52 suite.addTests(doctest.DocTestSuite(module))
53
54 def add_tests(cls):
55 tests = loader.loadTestsFromTestCase(cls)
56 suite.addTests(tests)
57
58 for name, test_class in test_module_globals.items():
59 if (not isinstance(test_class, type) or
60 not name.startswith('Test') or
61 not issubclass(test_class, unittest.TestCase)):
62 continue
63
64 if issubclass(test_class, LupaTestCase):
65 prefix = test_class.__name__ + "_"
66 qprefix = getattr(test_class, '__qualname__', test_class.__name__) + "_"
67
68 for module in all_lua_modules:
69 class TestClass(test_class):
70 lupa = module
71
72 module_name = module.__name__.rpartition('.')[2]
73 TestClass.__name__ = prefix + module_name
74 TestClass.__qualname__ = qprefix + module_name
75 add_tests(TestClass)
76 else:
77 add_tests(test_class)
78
79 return suite
80
81
82 def suite():
83 test_dir = os.path.abspath(os.path.dirname(__file__))
84
85 tests = []
86 for filename in os.listdir(test_dir):
87 if filename.endswith('.py') and not filename.startswith('_'):
88 tests.append('lupa.tests.' + filename[:-3])
89
90 suite = unittest.defaultTestLoader.loadTestsFromNames(tests)
91
92 # Long version of
93 # suite.addTest(doctest.DocFileSuite('../../README.rst'))
94 # to remove some platform specific tests.
95 readme_filename = 'README.rst'
96 readme_file = os_path.join(os_path.dirname(__file__), '..', '..', readme_filename)
97 with open(readme_file) as f:
98 readme = f.read()
99 if sys.platform != 'linux2':
100 # Exclude last section, which is Linux specific.
101 readme = readme.split('Importing Lua binary modules\n----------------------------\n', 1)[0]
102
103 parser = doctest.DocTestParser()
104 test = parser.get_doctest(readme, {'__file__': readme_file}, 'README.rst', readme_file, 0)
105 suite.addTest(doctest.DocFileCase(test))
106
107 return suite
108
109
110 if __name__ == '__main__':
111 unittest.TextTestRunner(verbosity=2).run(suite())