1
# Copyright (C) 2006, 2007 Canonical Ltd
2
# Authors: Robert Collins <robert.collins@canonical.com>
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
"""Symbol versioning tests."""
20
import bzrlib.symbol_versioning as symbol_versioning
21
from bzrlib.tests import TestCase
24
@symbol_versioning.deprecated_function(symbol_versioning.zero_seven)
25
def deprecated_function():
26
"""Deprecated function docstring."""
30
a_deprecated_list = symbol_versioning.deprecated_list(symbol_versioning.zero_nine,
31
'a_deprecated_list', ['one'], extra="Don't use me")
34
a_deprecated_dict = symbol_versioning.DeprecatedDict(
35
symbol_versioning.zero_fourteen,
38
advice='Pull the other one!',
42
class TestDeprecationWarnings(TestCase):
44
def capture_warning(self, message, category, stacklevel=None):
45
self._warnings.append((message, category, stacklevel))
48
super(TestDeprecationWarnings, self).setUp()
51
@symbol_versioning.deprecated_method(symbol_versioning.zero_seven)
52
def deprecated_method(self):
53
"""Deprecated method docstring.
55
This might explain stuff.
59
def test_deprecated_method(self):
61
"bzrlib.tests.test_symbol_versioning."
62
"TestDeprecationWarnings.deprecated_method "
63
"was deprecated in version 0.7.", DeprecationWarning, 2)
64
expected_docstring = ('Deprecated method docstring.\n'
66
' This might explain stuff.\n'
68
' This method was deprecated in version 0.7.\n'
70
self.check_deprecated_callable(expected_warning, expected_docstring,
72
"bzrlib.tests.test_symbol_versioning",
73
self.deprecated_method)
75
def test_deprecated_function(self):
77
"bzrlib.tests.test_symbol_versioning.deprecated_function "
78
"was deprecated in version 0.7.", DeprecationWarning, 2)
79
expected_docstring = ('Deprecated function docstring.\n'
81
'This function was deprecated in version 0.7.\n'
83
self.check_deprecated_callable(expected_warning, expected_docstring,
84
"deprecated_function",
85
"bzrlib.tests.test_symbol_versioning",
88
def test_deprecated_list(self):
90
"Modifying a_deprecated_list was deprecated in version 0.9."
91
" Don't use me", DeprecationWarning, 3)
92
old_warning_method = symbol_versioning.warn
94
symbol_versioning.set_warning_method(self.capture_warning)
95
self.assertEqual(['one'], a_deprecated_list)
96
self.assertEqual([], self._warnings)
98
a_deprecated_list.append('foo')
99
self.assertEqual([expected_warning], self._warnings)
100
self.assertEqual(['one', 'foo'], a_deprecated_list)
102
a_deprecated_list.extend(['bar', 'baz'])
103
self.assertEqual([expected_warning]*2, self._warnings)
104
self.assertEqual(['one', 'foo', 'bar', 'baz'], a_deprecated_list)
106
a_deprecated_list.insert(1, 'xxx')
107
self.assertEqual([expected_warning]*3, self._warnings)
108
self.assertEqual(['one', 'xxx', 'foo', 'bar', 'baz'], a_deprecated_list)
110
a_deprecated_list.remove('foo')
111
self.assertEqual([expected_warning]*4, self._warnings)
112
self.assertEqual(['one', 'xxx', 'bar', 'baz'], a_deprecated_list)
114
val = a_deprecated_list.pop()
115
self.assertEqual([expected_warning]*5, self._warnings)
116
self.assertEqual('baz', val)
117
self.assertEqual(['one', 'xxx', 'bar'], a_deprecated_list)
119
val = a_deprecated_list.pop(1)
120
self.assertEqual([expected_warning]*6, self._warnings)
121
self.assertEqual('xxx', val)
122
self.assertEqual(['one', 'bar'], a_deprecated_list)
124
symbol_versioning.set_warning_method(old_warning_method)
126
def test_deprecated_dict(self):
128
"access to a_deprecated_dict was deprecated in version 0.14."
129
" Pull the other one!", DeprecationWarning, 2)
130
old_warning_method = symbol_versioning.warn
132
symbol_versioning.set_warning_method(self.capture_warning)
133
self.assertEqual(len(a_deprecated_dict), 1)
134
self.assertEqual([expected_warning], self._warnings)
136
a_deprecated_dict['b'] = 42
137
self.assertEqual(a_deprecated_dict['b'], 42)
138
self.assertTrue('b' in a_deprecated_dict)
139
del a_deprecated_dict['b']
140
self.assertFalse('b' in a_deprecated_dict)
141
self.assertEqual([expected_warning] * 6, self._warnings)
143
symbol_versioning.set_warning_method(old_warning_method)
146
def check_deprecated_callable(self, expected_warning, expected_docstring,
147
expected_name, expected_module,
148
deprecated_callable):
149
old_warning_method = symbol_versioning.warn
151
symbol_versioning.set_warning_method(self.capture_warning)
152
self.assertEqual(1, deprecated_callable())
153
self.assertEqual([expected_warning], self._warnings)
154
deprecated_callable()
155
self.assertEqual([expected_warning, expected_warning],
157
self.assertEqualDiff(expected_docstring, deprecated_callable.__doc__)
158
self.assertEqualDiff(expected_name, deprecated_callable.__name__)
159
self.assertEqualDiff(expected_module, deprecated_callable.__module__)
160
self.assertTrue(deprecated_callable.is_deprecated)
162
symbol_versioning.set_warning_method(old_warning_method)
164
def test_deprecated_passed(self):
165
self.assertEqual(True, symbol_versioning.deprecated_passed(None))
166
self.assertEqual(True, symbol_versioning.deprecated_passed(True))
167
self.assertEqual(True, symbol_versioning.deprecated_passed(False))
168
self.assertEqual(False,
169
symbol_versioning.deprecated_passed(
170
symbol_versioning.DEPRECATED_PARAMETER))
172
def test_deprecation_string(self):
173
"""We can get a deprecation string for a method or function."""
174
self.assertEqual('bzrlib.tests.test_symbol_versioning.'
175
'TestDeprecationWarnings.test_deprecation_string was deprecated in '
177
symbol_versioning.deprecation_string(
178
self.test_deprecation_string, symbol_versioning.zero_eleven))
179
self.assertEqual('bzrlib.symbol_versioning.deprecated_function was '
180
'deprecated in version 0.11.',
181
symbol_versioning.deprecation_string(
182
symbol_versioning.deprecated_function,
183
symbol_versioning.zero_eleven))