4
from twisted.trial import unittest
6
from testdoc import split_name, find_tests, Documenter, WikiFormatter
9
class TestSplitName(unittest.TestCase):
11
def test_singleWord(self):
12
self.assertEqual(split_name('single'), ['single'])
14
def test_underscores(self):
15
self.assertEqual(split_name('split_name'), ['split', 'name'])
17
def test_camelCase(self):
18
self.assertEqual(split_name('splitName'), ['split', 'name'])
20
split_name('splitLongName'), ['split', 'long', 'name'])
21
self.assertEqual(split_name('splitAName'), ['split', 'a', 'name'])
23
def test_camelCaseWithCaps(self):
24
self.assertEqual(split_name('splitDNSName'), ['split', 'DNS', 'name'])
26
def test_singleUnderscore(self):
27
"""Single underscores are used for reflection prefixes, so we'd like to
31
split_name('test_splitName'), ['test', 'split', 'name'])
33
def test_multipleUnderscores(self):
34
"""If there are multiple underscores, but camel case, then someone is
35
probably referring to a camel-cased identifier in their name.
38
split_name('test_splitName_works'), ['test', 'splitName', 'works'])
40
def test_numbers(self):
42
split_name('test300Name'), ['test', '300', 'name'])
45
class MockFinder(object):
49
def gotModule(self, module):
50
self.log.append(('module', module))
52
def gotTestClass(self, klass):
53
self.log.append(('class', klass))
55
def gotTest(self, method):
56
self.log.append(('method', method))
59
class TestFinder(unittest.TestCase):
62
self.finder = MockFinder()
65
from testdoc.tests import empty
66
find_tests(self.finder, empty)
67
self.assertEqual(self.finder.log, [('module', empty)])
69
def test_hasemptycase(self):
70
from testdoc.tests import hasemptycase
71
find_tests(self.finder, hasemptycase)
74
('module', hasemptycase),
75
('class', hasemptycase.SomeTest)])
77
def test_hastests(self):
78
from testdoc.tests import hastests
79
find_tests(self.finder, hastests)
83
('class', hastests.SomeTest),
84
('method', hastests.SomeTest.test_foo_handles_qux),
85
('method', hastests.SomeTest.test_bar),
86
('class', hastests.AnotherTest),
87
('method', hastests.AnotherTest.test_baz)])
90
class MockFormatter(object):
94
def title(self, name):
95
self.log.append(('title', name))
97
def section(self, name):
98
self.log.append(('section', name))
100
def subsection(self, name):
101
self.log.append(('subsection', name))
103
def paragraph(self, text):
104
self.log.append(('para', text))
107
class TestDocumenter(unittest.TestCase):
109
self.formatter = MockFormatter()
110
self.documenter = Documenter(self.formatter)
112
def test_module(self):
113
from testdoc.tests import empty
114
self.documenter.gotModule(empty)
116
self.formatter.log, [
117
('title', 'testdoc.tests.empty')])
119
def test_emptyModuleWithDocstrings(self):
120
from testdoc.tests import hastests
121
self.documenter.gotModule(hastests)
123
self.formatter.log, [
124
('title', 'testdoc.tests.hastests'),
125
('para', self.documenter.getDocs(hastests))])
127
def test_emptyCase(self):
128
from testdoc.tests import hastests
129
self.documenter.gotTestClass(hastests.SomeTest)
131
self.formatter.log, [
132
('section', self.documenter.titleCase(['Some'])),
133
('para', self.documenter.getDocs(hastests.SomeTest))])
135
def test_method(self):
136
from testdoc.tests import hastests
137
self.documenter.gotTest(hastests.SomeTest.test_foo_handles_qux)
139
self.formatter.log, [
141
self.documenter.titleCase(['Foo', 'handles', 'qux'])),
142
('para', self.documenter.getDocs(
143
hastests.SomeTest.test_foo_handles_qux))])
145
def test_titleCase(self):
147
self.documenter.titleCase(['foo', 'BAR', 'a', 'In', 'Baz', '999']),
148
'Foo BAR a in Baz 999')
150
self.documenter.titleCase(['in', 'a', 'bind']),
153
# Docstrings first, then comments, then nothing
156
class WikiFormatterTest(unittest.TestCase):
158
self.stream = StringIO.StringIO()
159
self.formatter = WikiFormatter(self.stream)
161
def test_title(self):
162
self.formatter.title('foo')
163
self.assertEqual(self.stream.getvalue(), '= foo =\n\n')
165
def test_section(self):
166
self.formatter.section('foo')
167
self.assertEqual(self.stream.getvalue(), '== foo ==\n\n')
169
def test_subsection(self):
170
self.formatter.subsection('foo')
171
self.assertEqual(self.stream.getvalue(), '=== foo ===\n\n')
173
def test_paragraph(self):
174
self.formatter.paragraph('\nfoo\nbar\n')
175
self.assertEqual(self.stream.getvalue(), 'foo\nbar\n\n')