-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathpath_to_enlightenment.py
62 lines (50 loc) · 1.45 KB
/
path_to_enlightenment.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Functions to load the test cases ("koans") that make up the
Path to Enlightenment.
'''
import io
import unittest
# The path to enlightenment starts with the following:
KOANS_FILENAME = 'koans.txt'
def filter_koan_names(lines):
'''
Strips leading and trailing whitespace, then filters out blank
lines and comment lines.
'''
for line in lines:
line = line.strip()
if line.startswith('#'):
continue
if line:
yield line
return
def names_from_file(filename):
'''
Opens the given ``filename`` and yields the fully-qualified names
of TestCases found inside (one per line).
'''
with io.open(filename, 'rt', encoding='utf8') as names_file:
for name in filter_koan_names(names_file):
yield name
return
def koans_suite(names):
'''
Returns a ``TestSuite`` loaded with all tests found in the given
``names``, preserving the order in which they are found.
'''
suite = unittest.TestSuite()
loader = unittest.TestLoader()
loader.sortTestMethodsUsing = None
for name in names:
tests = loader.loadTestsFromName(name)
suite.addTests(tests)
return suite
def koans(filename=KOANS_FILENAME):
'''
Returns a ``TestSuite`` loaded with all the koans (``TestCase``s)
listed in ``filename``.
'''
names = names_from_file(filename)
return koans_suite(names)