OLD | NEW |
| (Empty) |
1 # Copyright (c) 2008 Twisted Matrix Laboratories. | |
2 # See LICENSE for details. | |
3 | |
4 | |
5 """ | |
6 Tests for Twisted's deprecation framework. | |
7 """ | |
8 | |
9 | |
10 from twisted.trial.unittest import TestCase | |
11 | |
12 from twisted.python.deprecate import _appendToDocstring | |
13 from twisted.python.deprecate import _getDeprecationDocstring | |
14 from twisted.python.deprecate import deprecated, getDeprecationWarningString | |
15 from twisted.python.reflect import qual | |
16 from twisted.python.versions import getVersionString, Version | |
17 | |
18 | |
19 | |
20 def dummyCallable(): | |
21 """ | |
22 Do nothing. | |
23 | |
24 This is used to test the deprecation decorators. | |
25 """ | |
26 | |
27 | |
28 | |
29 class TestDeprecationWarnings(TestCase): | |
30 | |
31 def test_getDeprecationWarningString(self): | |
32 """ | |
33 L{getDeprecationWarningString} returns a string that tells us that a | |
34 callable was deprecated at a certain released version of Twisted. | |
35 """ | |
36 version = Version('Twisted', 8, 0, 0) | |
37 self.assertEqual( | |
38 getDeprecationWarningString(self.test_getDeprecationWarningString, v
ersion), | |
39 "%s was deprecated in Twisted 8.0.0" % ( | |
40 qual(self.test_getDeprecationWarningString))) | |
41 | |
42 | |
43 def test_deprecateEmitsWarning(self): | |
44 """ | |
45 Decorating a callable with L{deprecated} emits a warning. | |
46 """ | |
47 version = Version('Twisted', 8, 0, 0) | |
48 dummy = deprecated(version)(dummyCallable) | |
49 def add_a_stack_level(): | |
50 dummy() | |
51 self.assertWarns( | |
52 DeprecationWarning, | |
53 getDeprecationWarningString(dummyCallable, version), | |
54 __file__, | |
55 add_a_stack_level) | |
56 | |
57 | |
58 def test_deprecatedPreservesName(self): | |
59 """ | |
60 The decorated function has the same name as the original. | |
61 """ | |
62 version = Version('Twisted', 8, 0, 0) | |
63 dummy = deprecated(version)(dummyCallable) | |
64 self.assertEqual(dummyCallable.__name__, dummy.__name__) | |
65 self.assertEqual(qual(dummyCallable), qual(dummy)) | |
66 | |
67 | |
68 def test_getDeprecationDocstring(self): | |
69 """ | |
70 L{_getDeprecationDocstring} returns a note about the deprecation to go | |
71 into a docstring. | |
72 """ | |
73 version = Version('Twisted', 8, 0, 0) | |
74 self.assertEqual( | |
75 "Deprecated in Twisted 8.0.0.", _getDeprecationDocstring(version)) | |
76 | |
77 | |
78 def test_deprecatedUpdatesDocstring(self): | |
79 """ | |
80 The docstring of the deprecated function is appended with information | |
81 about the deprecation. | |
82 """ | |
83 | |
84 version = Version('Twisted', 8, 0, 0) | |
85 dummy = deprecated(version)(dummyCallable) | |
86 | |
87 _appendToDocstring( | |
88 dummyCallable, | |
89 _getDeprecationDocstring(version)) | |
90 | |
91 self.assertEqual(dummyCallable.__doc__, dummy.__doc__) | |
92 | |
93 | |
94 def test_versionMetadata(self): | |
95 """ | |
96 Deprecating a function adds version information to the decorated | |
97 version of that function. | |
98 """ | |
99 # XXX - Is this a YAGNI? | |
100 | |
101 version = Version('Twisted', 8, 0, 0) | |
102 dummy = deprecated(version)(dummyCallable) | |
103 | |
104 self.assertEqual(version, dummy.deprecatedVersion) | |
105 | |
106 | |
107 | |
108 class TestAppendToDocstring(TestCase): | |
109 """ | |
110 Test the _appendToDocstring function. | |
111 | |
112 _appendToDocstring is used to add text to a docstring. | |
113 """ | |
114 | |
115 def test_appendToEmptyDocstring(self): | |
116 """ | |
117 Appending to an empty docstring simply replaces the docstring. | |
118 """ | |
119 | |
120 def noDocstring(): | |
121 pass | |
122 | |
123 _appendToDocstring(noDocstring, "Appended text.") | |
124 self.assertEqual("Appended text.", noDocstring.__doc__) | |
125 | |
126 | |
127 def test_appendToSingleLineDocstring(self): | |
128 """ | |
129 Appending to a single line docstring places the message on a new line, | |
130 with a blank line separating it from the rest of the docstring. | |
131 | |
132 The docstring ends with a newline, conforming to Twisted and PEP 8 | |
133 standards. Unfortunately, the indentation is incorrect, since the | |
134 existing docstring doesn't have enough info to help us indent | |
135 properly. | |
136 """ | |
137 | |
138 def singleLineDocstring(): | |
139 """This doesn't comply with standards, but is here for a test.""" | |
140 | |
141 _appendToDocstring(singleLineDocstring, "Appended text.") | |
142 self.assertEqual( | |
143 ["This doesn't comply with standards, but is here for a test.", | |
144 "", | |
145 "Appended text."], | |
146 singleLineDocstring.__doc__.splitlines()) | |
147 self.assertTrue(singleLineDocstring.__doc__.endswith('\n')) | |
148 | |
149 | |
150 def test_appendToMultilineDocstring(self): | |
151 """ | |
152 Appending to a multi-line docstring places the messade on a new line, | |
153 with a blank line separating it from the rest of the docstring. | |
154 | |
155 Because we have multiple lines, we have enough information to do | |
156 indentation. | |
157 """ | |
158 | |
159 def multiLineDocstring(): | |
160 """ | |
161 This is a multi-line docstring. | |
162 """ | |
163 | |
164 def expectedDocstring(): | |
165 """ | |
166 This is a multi-line docstring. | |
167 | |
168 Appended text. | |
169 """ | |
170 | |
171 _appendToDocstring(multiLineDocstring, "Appended text.") | |
172 self.assertEqual( | |
173 expectedDocstring.__doc__, multiLineDocstring.__doc__) | |
OLD | NEW |