|
1 | 1 | #!/usr/bin/env python |
2 | 2 | # -*- coding: utf-8 -*- |
3 | 3 |
|
4 | | -# The path to enlightenment starts with the following: |
| 4 | +''' |
| 5 | +Functions to load the test cases ("koans") that make up the |
| 6 | +Path to Enlightenment. |
| 7 | +''' |
5 | 8 |
|
| 9 | +import io |
6 | 10 | import unittest |
7 | 11 |
|
8 | | -from koans.about_asserts import AboutAsserts |
9 | | -from koans.about_strings import AboutStrings |
10 | | -from koans.about_none import AboutNone |
11 | | -from koans.about_lists import AboutLists |
12 | | -from koans.about_list_assignments import AboutListAssignments |
13 | | -from koans.about_dictionaries import AboutDictionaries |
14 | | -from koans.about_string_manipulation import AboutStringManipulation |
15 | | -from koans.about_tuples import AboutTuples |
16 | | -from koans.about_methods import AboutMethods |
17 | | -from koans.about_control_statements import AboutControlStatements |
18 | | -from koans.about_true_and_false import AboutTrueAndFalse |
19 | | -from koans.about_sets import AboutSets |
20 | | -from koans.about_triangle_project import AboutTriangleProject |
21 | | -from koans.about_exceptions import AboutExceptions |
22 | | -from koans.about_triangle_project2 import AboutTriangleProject2 |
23 | | -from koans.about_iteration import AboutIteration |
24 | | -from koans.about_comprehension import AboutComprehension |
25 | | -from koans.about_generators import AboutGenerators |
26 | | -from koans.about_lambdas import AboutLambdas |
27 | | -from koans.about_scoring_project import AboutScoringProject |
28 | | -from koans.about_classes import AboutClasses |
29 | | -from koans.about_new_style_classes import AboutNewStyleClasses |
30 | | -from koans.about_with_statements import AboutWithStatements |
31 | | -from koans.about_monkey_patching import AboutMonkeyPatching |
32 | | -from koans.about_dice_project import AboutDiceProject |
33 | | -from koans.about_method_bindings import AboutMethodBindings |
34 | | -from koans.about_decorating_with_functions import AboutDecoratingWithFunctions |
35 | | -from koans.about_decorating_with_classes import AboutDecoratingWithClasses |
36 | | -from koans.about_inheritance import AboutInheritance |
37 | | -from koans.about_multiple_inheritance import AboutMultipleInheritance |
38 | | -from koans.about_regex import AboutRegex |
39 | | -from koans.about_scope import AboutScope |
40 | | -from koans.about_modules import AboutModules |
41 | | -from koans.about_packages import AboutPackages |
42 | | -from koans.about_class_attributes import AboutClassAttributes |
43 | | -from koans.about_attribute_access import AboutAttributeAccess |
44 | | -from koans.about_deleting_objects import AboutDeletingObjects |
45 | | -from koans.about_proxy_object_project import * |
46 | | -from koans.about_extra_credit import AboutExtraCredit |
47 | 12 |
|
48 | | -def koans(): |
49 | | - loader = unittest.TestLoader() |
| 13 | +# The path to enlightenment starts with the following: |
| 14 | +KOANS_FILENAME = 'koans.txt' |
| 15 | + |
| 16 | + |
| 17 | +def filter_koan_names(lines): |
| 18 | + ''' |
| 19 | + Strips leading and trailing whitespace, then filters out blank |
| 20 | + lines and comment lines. |
| 21 | + ''' |
| 22 | + for line in lines: |
| 23 | + line = line.strip() |
| 24 | + if line.startswith('#'): |
| 25 | + continue |
| 26 | + if line: |
| 27 | + yield line |
| 28 | + return |
| 29 | + |
| 30 | + |
| 31 | +def names_from_file(filename): |
| 32 | + ''' |
| 33 | + Opens the given ``filename`` and yields the fully-qualified names |
| 34 | + of TestCases found inside (one per line). |
| 35 | + ''' |
| 36 | + with io.open(filename, 'rt', encoding='utf8') as names_file: |
| 37 | + for name in filter_koan_names(names_file): |
| 38 | + yield name |
| 39 | + return |
| 40 | + |
| 41 | + |
| 42 | +def koans_suite(names): |
| 43 | + ''' |
| 44 | + Returns a ``TestSuite`` loaded with all tests found in the given |
| 45 | + ``names``, preserving the order in which they are found. |
| 46 | + ''' |
50 | 47 | suite = unittest.TestSuite() |
| 48 | + loader = unittest.TestLoader() |
51 | 49 | loader.sortTestMethodsUsing = None |
52 | | - suite.addTests(loader.loadTestsFromTestCase(AboutAsserts)) |
53 | | - suite.addTests(loader.loadTestsFromTestCase(AboutStrings)) |
54 | | - suite.addTests(loader.loadTestsFromTestCase(AboutNone)) |
55 | | - suite.addTests(loader.loadTestsFromTestCase(AboutLists)) |
56 | | - suite.addTests(loader.loadTestsFromTestCase(AboutListAssignments)) |
57 | | - suite.addTests(loader.loadTestsFromTestCase(AboutDictionaries)) |
58 | | - suite.addTests(loader.loadTestsFromTestCase(AboutStringManipulation)) |
59 | | - suite.addTests(loader.loadTestsFromTestCase(AboutTuples)) |
60 | | - suite.addTests(loader.loadTestsFromTestCase(AboutMethods)) |
61 | | - suite.addTests(loader.loadTestsFromTestCase(AboutControlStatements)) |
62 | | - suite.addTests(loader.loadTestsFromTestCase(AboutTrueAndFalse)) |
63 | | - suite.addTests(loader.loadTestsFromTestCase(AboutSets)) |
64 | | - suite.addTests(loader.loadTestsFromTestCase(AboutTriangleProject)) |
65 | | - suite.addTests(loader.loadTestsFromTestCase(AboutExceptions)) |
66 | | - suite.addTests(loader.loadTestsFromTestCase(AboutTriangleProject2)) |
67 | | - suite.addTests(loader.loadTestsFromTestCase(AboutIteration)) |
68 | | - suite.addTests(loader.loadTestsFromTestCase(AboutComprehension)) |
69 | | - suite.addTests(loader.loadTestsFromTestCase(AboutGenerators)) |
70 | | - suite.addTests(loader.loadTestsFromTestCase(AboutLambdas)) |
71 | | - suite.addTests(loader.loadTestsFromTestCase(AboutScoringProject)) |
72 | | - suite.addTests(loader.loadTestsFromTestCase(AboutClasses)) |
73 | | - suite.addTests(loader.loadTestsFromTestCase(AboutNewStyleClasses)) |
74 | | - suite.addTests(loader.loadTestsFromTestCase(AboutWithStatements)) |
75 | | - suite.addTests(loader.loadTestsFromTestCase(AboutMonkeyPatching)) |
76 | | - suite.addTests(loader.loadTestsFromTestCase(AboutDiceProject)) |
77 | | - suite.addTests(loader.loadTestsFromTestCase(AboutMethodBindings)) |
78 | | - suite.addTests(loader.loadTestsFromTestCase(AboutDecoratingWithFunctions)) |
79 | | - suite.addTests(loader.loadTestsFromTestCase(AboutDecoratingWithClasses)) |
80 | | - suite.addTests(loader.loadTestsFromTestCase(AboutInheritance)) |
81 | | - suite.addTests(loader.loadTestsFromTestCase(AboutMultipleInheritance)) |
82 | | - suite.addTests(loader.loadTestsFromTestCase(AboutScope)) |
83 | | - suite.addTests(loader.loadTestsFromTestCase(AboutModules)) |
84 | | - suite.addTests(loader.loadTestsFromTestCase(AboutPackages)) |
85 | | - suite.addTests(loader.loadTestsFromTestCase(AboutClassAttributes)) |
86 | | - suite.addTests(loader.loadTestsFromTestCase(AboutAttributeAccess)) |
87 | | - suite.addTests(loader.loadTestsFromTestCase(AboutDeletingObjects)) |
88 | | - suite.addTests(loader.loadTestsFromTestCase(AboutProxyObjectProject)) |
89 | | - suite.addTests(loader.loadTestsFromTestCase(TelevisionTest)) |
90 | | - suite.addTests(loader.loadTestsFromTestCase(AboutExtraCredit)) |
91 | | - suite.addTests(loader.loadTestsFromTestCase(AboutRegex)) |
92 | | - |
| 50 | + for name in names: |
| 51 | + tests = loader.loadTestsFromName(name) |
| 52 | + suite.addTests(tests) |
93 | 53 | return suite |
| 54 | + |
| 55 | + |
| 56 | +def koans(filename=KOANS_FILENAME): |
| 57 | + ''' |
| 58 | + Returns a ``TestSuite`` loaded with all the koans (``TestCase``s) |
| 59 | + listed in ``filename``. |
| 60 | + ''' |
| 61 | + names = names_from_file(filename) |
| 62 | + return koans_suite(names) |
0 commit comments