~testdoc-dev/testdoc/trunk.git

« back to all changes in this revision

Viewing changes to testdoc/tests/test_testdoc.py

  • Committer: jml@canonical.com
  • Date: 2007-03-25 09:47:54 UTC
  • Revision ID: git-v1:efaa91a66979f582c7692974477fb9b0716ba2d7
First draft after a day of hacking on a crazy python test -> doc tool.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import inspect
 
2
import StringIO
 
3
 
 
4
from twisted.trial import unittest
 
5
 
 
6
from testdoc import split_name, find_tests, Documenter, WikiFormatter
 
7
 
 
8
 
 
9
class TestSplitName(unittest.TestCase):
 
10
 
 
11
    def test_singleWord(self):
 
12
        self.assertEqual(split_name('single'), ['single'])
 
13
 
 
14
    def test_underscores(self):
 
15
        self.assertEqual(split_name('split_name'), ['split', 'name'])
 
16
 
 
17
    def test_camelCase(self):
 
18
        self.assertEqual(split_name('splitName'), ['split', 'name'])
 
19
        self.assertEqual(
 
20
            split_name('splitLongName'), ['split', 'long', 'name'])
 
21
        self.assertEqual(split_name('splitAName'), ['split', 'a', 'name'])
 
22
 
 
23
    def test_camelCaseWithCaps(self):
 
24
        self.assertEqual(split_name('splitDNSName'), ['split', 'DNS', 'name'])
 
25
 
 
26
    def test_singleUnderscore(self):
 
27
        """Single underscores are used for reflection prefixes, so we'd like to
 
28
        ignore them.
 
29
        """
 
30
        self.assertEqual(
 
31
            split_name('test_splitName'), ['test', 'split', 'name'])
 
32
 
 
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.
 
36
        """
 
37
        self.assertEqual(
 
38
            split_name('test_splitName_works'), ['test', 'splitName', 'works'])
 
39
 
 
40
    def test_numbers(self):
 
41
        self.assertEqual(
 
42
            split_name('test300Name'), ['test', '300', 'name'])
 
43
 
 
44
 
 
45
class MockFinder(object):
 
46
    def __init__(self):
 
47
        self.log = []
 
48
 
 
49
    def gotModule(self, module):
 
50
        self.log.append(('module', module))
 
51
 
 
52
    def gotTestClass(self, klass):
 
53
        self.log.append(('class', klass))
 
54
 
 
55
    def gotTest(self, method):
 
56
        self.log.append(('method', method))
 
57
 
 
58
 
 
59
class TestFinder(unittest.TestCase):
 
60
 
 
61
    def setUp(self):
 
62
        self.finder = MockFinder()
 
63
 
 
64
    def test_empty(self):
 
65
        from testdoc.tests import empty
 
66
        find_tests(self.finder, empty)
 
67
        self.assertEqual(self.finder.log, [('module', empty)])
 
68
 
 
69
    def test_hasemptycase(self):
 
70
        from testdoc.tests import hasemptycase
 
71
        find_tests(self.finder, hasemptycase)
 
72
        self.assertEqual(
 
73
            self.finder.log, [
 
74
                ('module', hasemptycase),
 
75
                ('class', hasemptycase.SomeTest)])
 
76
 
 
77
    def test_hastests(self):
 
78
        from testdoc.tests import hastests
 
79
        find_tests(self.finder, hastests)
 
80
        self.assertEqual(
 
81
            self.finder.log, [
 
82
                ('module', 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)])
 
88
 
 
89
 
 
90
class MockFormatter(object):
 
91
    def __init__(self):
 
92
        self.log = []
 
93
 
 
94
    def title(self, name):
 
95
        self.log.append(('title', name))
 
96
 
 
97
    def section(self, name):
 
98
        self.log.append(('section', name))
 
99
 
 
100
    def subsection(self, name):
 
101
        self.log.append(('subsection', name))
 
102
 
 
103
    def paragraph(self, text):
 
104
        self.log.append(('para', text))
 
105
 
 
106
 
 
107
class TestDocumenter(unittest.TestCase):
 
108
    def setUp(self):
 
109
        self.formatter = MockFormatter()
 
110
        self.documenter = Documenter(self.formatter)
 
111
 
 
112
    def test_module(self):
 
113
        from testdoc.tests import empty
 
114
        self.documenter.gotModule(empty)
 
115
        self.assertEqual(
 
116
            self.formatter.log, [
 
117
                ('title', 'testdoc.tests.empty')])
 
118
 
 
119
    def test_emptyModuleWithDocstrings(self):
 
120
        from testdoc.tests import hastests
 
121
        self.documenter.gotModule(hastests)
 
122
        self.assertEqual(
 
123
            self.formatter.log, [
 
124
                ('title', 'testdoc.tests.hastests'),
 
125
                ('para', self.documenter.getDocs(hastests))])
 
126
 
 
127
    def test_emptyCase(self):
 
128
        from testdoc.tests import hastests
 
129
        self.documenter.gotTestClass(hastests.SomeTest)
 
130
        self.assertEqual(
 
131
            self.formatter.log, [
 
132
                ('section', self.documenter.titleCase(['Some'])),
 
133
                ('para', self.documenter.getDocs(hastests.SomeTest))])
 
134
 
 
135
    def test_method(self):
 
136
        from testdoc.tests import hastests
 
137
        self.documenter.gotTest(hastests.SomeTest.test_foo_handles_qux)
 
138
        self.assertEqual(
 
139
            self.formatter.log, [
 
140
                ('subsection',
 
141
                 self.documenter.titleCase(['Foo', 'handles', 'qux'])),
 
142
                ('para', self.documenter.getDocs(
 
143
                    hastests.SomeTest.test_foo_handles_qux))])
 
144
 
 
145
    def test_titleCase(self):
 
146
        self.assertEqual(
 
147
            self.documenter.titleCase(['foo', 'BAR', 'a', 'In', 'Baz', '999']),
 
148
            'Foo BAR a in Baz 999')
 
149
        self.assertEqual(
 
150
            self.documenter.titleCase(['in', 'a', 'bind']),
 
151
            'In a Bind')
 
152
 
 
153
    # Docstrings first, then comments, then nothing
 
154
 
 
155
 
 
156
class WikiFormatterTest(unittest.TestCase):
 
157
    def setUp(self):
 
158
        self.stream = StringIO.StringIO()
 
159
        self.formatter = WikiFormatter(self.stream)
 
160
 
 
161
    def test_title(self):
 
162
        self.formatter.title('foo')
 
163
        self.assertEqual(self.stream.getvalue(), '= foo =\n\n')
 
164
 
 
165
    def test_section(self):
 
166
        self.formatter.section('foo')
 
167
        self.assertEqual(self.stream.getvalue(), '== foo ==\n\n')
 
168
 
 
169
    def test_subsection(self):
 
170
        self.formatter.subsection('foo')
 
171
        self.assertEqual(self.stream.getvalue(), '=== foo ===\n\n')
 
172
 
 
173
    def test_paragraph(self):
 
174
        self.formatter.paragraph('\nfoo\nbar\n')
 
175
        self.assertEqual(self.stream.getvalue(), 'foo\nbar\n\n')