1
# Copyright (C) 2006, 2007 Canonical Ltd
2
# Authors: Robert Collins <robert.collins@canonical.com>
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
"""Symbol versioning tests."""
21
import bzrlib.symbol_versioning as symbol_versioning
22
from bzrlib.tests import TestCase
25
@symbol_versioning.deprecated_function(symbol_versioning.zero_seven)
26
def deprecated_function():
27
"""Deprecated function docstring."""
31
a_deprecated_list = symbol_versioning.deprecated_list(symbol_versioning.zero_nine,
32
'a_deprecated_list', ['one'], extra="Don't use me")
35
a_deprecated_dict = symbol_versioning.DeprecatedDict(
36
symbol_versioning.zero_fourteen,
39
advice='Pull the other one!',
43
class TestDeprecationWarnings(TestCase):
45
def capture_warning(self, message, category, stacklevel=None):
46
self._warnings.append((message, category, stacklevel))
49
super(TestDeprecationWarnings, self).setUp()
52
@symbol_versioning.deprecated_method(symbol_versioning.zero_seven)
53
def deprecated_method(self):
54
"""Deprecated method docstring.
56
This might explain stuff.
61
@symbol_versioning.deprecated_function(symbol_versioning.zero_seven)
62
def deprecated_static():
63
"""Deprecated static."""
66
def test_deprecated_static(self):
67
# XXX: The results are not quite right because the class name is not
68
# shown - however it is enough to give people a good indication of
69
# where the problem is.
71
"bzrlib.tests.test_symbol_versioning."
73
"was deprecated in version 0.7.", DeprecationWarning, 2)
74
expected_docstring = (
75
'Deprecated static.\n'
77
'This function was deprecated in version 0.7.\n'
79
self.check_deprecated_callable(
80
expected_warning, expected_docstring,
82
"bzrlib.tests.test_symbol_versioning",
83
self.deprecated_static)
85
def test_deprecated_method(self):
87
"bzrlib.tests.test_symbol_versioning."
88
"TestDeprecationWarnings.deprecated_method "
89
"was deprecated in version 0.7.", DeprecationWarning, 2)
90
expected_docstring = ('Deprecated method docstring.\n'
92
' This might explain stuff.\n'
94
' This method was deprecated in version 0.7.\n'
96
self.check_deprecated_callable(expected_warning, expected_docstring,
98
"bzrlib.tests.test_symbol_versioning",
99
self.deprecated_method)
101
def test_deprecated_function(self):
103
"bzrlib.tests.test_symbol_versioning.deprecated_function "
104
"was deprecated in version 0.7.", DeprecationWarning, 2)
105
expected_docstring = ('Deprecated function docstring.\n'
107
'This function was deprecated in version 0.7.\n'
109
self.check_deprecated_callable(expected_warning, expected_docstring,
110
"deprecated_function",
111
"bzrlib.tests.test_symbol_versioning",
114
def test_deprecated_list(self):
116
"Modifying a_deprecated_list was deprecated in version 0.9."
117
" Don't use me", DeprecationWarning, 3)
118
old_warning_method = symbol_versioning.warn
120
symbol_versioning.set_warning_method(self.capture_warning)
121
self.assertEqual(['one'], a_deprecated_list)
122
self.assertEqual([], self._warnings)
124
a_deprecated_list.append('foo')
125
self.assertEqual([expected_warning], self._warnings)
126
self.assertEqual(['one', 'foo'], a_deprecated_list)
128
a_deprecated_list.extend(['bar', 'baz'])
129
self.assertEqual([expected_warning]*2, self._warnings)
130
self.assertEqual(['one', 'foo', 'bar', 'baz'], a_deprecated_list)
132
a_deprecated_list.insert(1, 'xxx')
133
self.assertEqual([expected_warning]*3, self._warnings)
134
self.assertEqual(['one', 'xxx', 'foo', 'bar', 'baz'], a_deprecated_list)
136
a_deprecated_list.remove('foo')
137
self.assertEqual([expected_warning]*4, self._warnings)
138
self.assertEqual(['one', 'xxx', 'bar', 'baz'], a_deprecated_list)
140
val = a_deprecated_list.pop()
141
self.assertEqual([expected_warning]*5, self._warnings)
142
self.assertEqual('baz', val)
143
self.assertEqual(['one', 'xxx', 'bar'], a_deprecated_list)
145
val = a_deprecated_list.pop(1)
146
self.assertEqual([expected_warning]*6, self._warnings)
147
self.assertEqual('xxx', val)
148
self.assertEqual(['one', 'bar'], a_deprecated_list)
150
symbol_versioning.set_warning_method(old_warning_method)
152
def test_deprecated_dict(self):
154
"access to a_deprecated_dict was deprecated in version 0.14."
155
" Pull the other one!", DeprecationWarning, 2)
156
old_warning_method = symbol_versioning.warn
158
symbol_versioning.set_warning_method(self.capture_warning)
159
self.assertEqual(len(a_deprecated_dict), 1)
160
self.assertEqual([expected_warning], self._warnings)
162
a_deprecated_dict['b'] = 42
163
self.assertEqual(a_deprecated_dict['b'], 42)
164
self.assertTrue('b' in a_deprecated_dict)
165
del a_deprecated_dict['b']
166
self.assertFalse('b' in a_deprecated_dict)
167
self.assertEqual([expected_warning] * 6, self._warnings)
169
symbol_versioning.set_warning_method(old_warning_method)
172
def check_deprecated_callable(self, expected_warning, expected_docstring,
173
expected_name, expected_module,
174
deprecated_callable):
175
old_warning_method = symbol_versioning.warn
177
symbol_versioning.set_warning_method(self.capture_warning)
178
self.assertEqual(1, deprecated_callable())
179
self.assertEqual([expected_warning], self._warnings)
180
deprecated_callable()
181
self.assertEqual([expected_warning, expected_warning],
183
self.assertEqualDiff(expected_docstring, deprecated_callable.__doc__)
184
self.assertEqualDiff(expected_name, deprecated_callable.__name__)
185
self.assertEqualDiff(expected_module, deprecated_callable.__module__)
186
self.assertTrue(deprecated_callable.is_deprecated)
188
symbol_versioning.set_warning_method(old_warning_method)
190
def test_deprecated_passed(self):
191
self.assertEqual(True, symbol_versioning.deprecated_passed(None))
192
self.assertEqual(True, symbol_versioning.deprecated_passed(True))
193
self.assertEqual(True, symbol_versioning.deprecated_passed(False))
194
self.assertEqual(False,
195
symbol_versioning.deprecated_passed(
196
symbol_versioning.DEPRECATED_PARAMETER))
198
def test_deprecation_string(self):
199
"""We can get a deprecation string for a method or function."""
200
self.assertEqual('bzrlib.tests.test_symbol_versioning.'
201
'TestDeprecationWarnings.test_deprecation_string was deprecated in '
203
symbol_versioning.deprecation_string(
204
self.test_deprecation_string, symbol_versioning.zero_eleven))
205
self.assertEqual('bzrlib.symbol_versioning.deprecated_function was '
206
'deprecated in version 0.11.',
207
symbol_versioning.deprecation_string(
208
symbol_versioning.deprecated_function,
209
symbol_versioning.zero_eleven))