~bzr-pqm/bzr/bzr.dev

2227.1.2 by mbp at sourcefrog
Add a simple DeprecatedDict class
1
# Copyright (C) 2006, 2007 Canonical Ltd
1534.2.1 by Robert Collins
Implement deprecated_method
2
#   Authors: Robert Collins <robert.collins@canonical.com>
2825.3.4 by Martin Pool
Better explanation of deprecation
3
#   and others
1534.2.1 by Robert Collins
Implement deprecated_method
4
#
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.
9
#
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.
14
#
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
18
19
"""Symbol versioning tests."""
20
21
import bzrlib.symbol_versioning as symbol_versioning
22
from bzrlib.tests import TestCase
23
24
1534.2.2 by Robert Collins
Implement deprecated_function.
25
@symbol_versioning.deprecated_function(symbol_versioning.zero_seven)
26
def deprecated_function():
1534.2.3 by Robert Collins
decorate docstrings in deprecated functions.
27
    """Deprecated function docstring."""
1534.2.2 by Robert Collins
Implement deprecated_function.
28
    return 1
29
1534.2.1 by Robert Collins
Implement deprecated_method
30
1836.1.12 by John Arbash Meinel
Move ignores into a file of their own, make DEFAULT_IGNORE a deprecated list. Create deprecated_list in symbol versioning.
31
a_deprecated_list = symbol_versioning.deprecated_list(symbol_versioning.zero_nine,
32
    'a_deprecated_list', ['one'], extra="Don't use me")
33
34
2227.1.2 by mbp at sourcefrog
Add a simple DeprecatedDict class
35
a_deprecated_dict = symbol_versioning.DeprecatedDict(
36
    symbol_versioning.zero_fourteen,
37
    'a_deprecated_dict',
38
    dict(a=42),
39
    advice='Pull the other one!',
40
    )
41
42
1534.2.1 by Robert Collins
Implement deprecated_method
43
class TestDeprecationWarnings(TestCase):
44
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
45
    def capture_warning(self, message, category, stacklevel=None):
46
        self._warnings.append((message, category, stacklevel))
1534.2.1 by Robert Collins
Implement deprecated_method
47
48
    def setUp(self):
49
        super(TestDeprecationWarnings, self).setUp()
50
        self._warnings = []
51
    
52
    @symbol_versioning.deprecated_method(symbol_versioning.zero_seven)
53
    def deprecated_method(self):
1534.2.3 by Robert Collins
decorate docstrings in deprecated functions.
54
        """Deprecated method docstring.
55
        
56
        This might explain stuff.
57
        """
1534.2.2 by Robert Collins
Implement deprecated_function.
58
        return 1
1534.2.1 by Robert Collins
Implement deprecated_method
59
2825.3.3 by Martin Pool
Add basic test for calling deprecated static methods
60
    @staticmethod
61
    @symbol_versioning.deprecated_function(symbol_versioning.zero_seven)
62
    def deprecated_static():
63
        """Deprecated static."""
64
        return 1
65
66
    def test_deprecated_static(self):
2825.3.4 by Martin Pool
Better explanation of deprecation
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.
2825.3.3 by Martin Pool
Add basic test for calling deprecated static methods
70
        expected_warning = (
71
            "bzrlib.tests.test_symbol_versioning."
72
            "deprecated_static "
73
            "was deprecated in version 0.7.", DeprecationWarning, 2)
74
        expected_docstring = (
75
            'Deprecated static.\n'
76
            '\n'
77
            'This function was deprecated in version 0.7.\n'
78
            )
79
        self.check_deprecated_callable(
80
            expected_warning, expected_docstring,
81
            "deprecated_static",
82
            "bzrlib.tests.test_symbol_versioning",
83
            self.deprecated_static)
84
1534.2.1 by Robert Collins
Implement deprecated_method
85
    def test_deprecated_method(self):
1534.2.2 by Robert Collins
Implement deprecated_function.
86
        expected_warning = (
87
            "bzrlib.tests.test_symbol_versioning."
88
            "TestDeprecationWarnings.deprecated_method "
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
89
            "was deprecated in version 0.7.", DeprecationWarning, 2)
1534.2.3 by Robert Collins
decorate docstrings in deprecated functions.
90
        expected_docstring = ('Deprecated method docstring.\n'
91
                              '        \n'
92
                              '        This might explain stuff.\n'
93
                              '        \n'
94
                              '        This method was deprecated in version 0.7.\n'
95
                              '        ')
96
        self.check_deprecated_callable(expected_warning, expected_docstring,
1534.2.5 by Robert Collins
Set the __name__ attribute on decorated methods and functions.
97
                                       "deprecated_method",
1534.1.3 by Robert Collins
Bugfix the symbol_versioning deprecation decorators to update the
98
                                       "bzrlib.tests.test_symbol_versioning",
1534.2.2 by Robert Collins
Implement deprecated_function.
99
                                       self.deprecated_method)
100
101
    def test_deprecated_function(self):
102
        expected_warning = (
103
            "bzrlib.tests.test_symbol_versioning.deprecated_function "
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
104
            "was deprecated in version 0.7.", DeprecationWarning, 2)
1534.2.3 by Robert Collins
decorate docstrings in deprecated functions.
105
        expected_docstring = ('Deprecated function docstring.\n'
106
                              '\n'
107
                              'This function was deprecated in version 0.7.\n'
108
                              )
109
        self.check_deprecated_callable(expected_warning, expected_docstring,
1534.2.5 by Robert Collins
Set the __name__ attribute on decorated methods and functions.
110
                                       "deprecated_function",
1534.1.3 by Robert Collins
Bugfix the symbol_versioning deprecation decorators to update the
111
                                       "bzrlib.tests.test_symbol_versioning",
1534.2.2 by Robert Collins
Implement deprecated_function.
112
                                       deprecated_function)
113
1836.1.12 by John Arbash Meinel
Move ignores into a file of their own, make DEFAULT_IGNORE a deprecated list. Create deprecated_list in symbol versioning.
114
    def test_deprecated_list(self):
115
        expected_warning = (
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
119
        try:
120
            symbol_versioning.set_warning_method(self.capture_warning)
121
            self.assertEqual(['one'], a_deprecated_list)
122
            self.assertEqual([], self._warnings)
123
124
            a_deprecated_list.append('foo')
125
            self.assertEqual([expected_warning], self._warnings)
126
            self.assertEqual(['one', 'foo'], a_deprecated_list)
127
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)
131
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)
135
136
            a_deprecated_list.remove('foo')
137
            self.assertEqual([expected_warning]*4, self._warnings)
138
            self.assertEqual(['one', 'xxx', 'bar', 'baz'], a_deprecated_list)
139
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)
144
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)
149
        finally:
150
            symbol_versioning.set_warning_method(old_warning_method)
151
2227.1.2 by mbp at sourcefrog
Add a simple DeprecatedDict class
152
    def test_deprecated_dict(self):
153
        expected_warning = (
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
157
        try:
158
            symbol_versioning.set_warning_method(self.capture_warning)
159
            self.assertEqual(len(a_deprecated_dict), 1)
160
            self.assertEqual([expected_warning], self._warnings)
161
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)
168
        finally:
169
            symbol_versioning.set_warning_method(old_warning_method)
170
171
1534.2.3 by Robert Collins
decorate docstrings in deprecated functions.
172
    def check_deprecated_callable(self, expected_warning, expected_docstring,
1534.1.3 by Robert Collins
Bugfix the symbol_versioning deprecation decorators to update the
173
                                  expected_name, expected_module,
1534.2.3 by Robert Collins
decorate docstrings in deprecated functions.
174
                                  deprecated_callable):
1534.2.1 by Robert Collins
Implement deprecated_method
175
        old_warning_method = symbol_versioning.warn
176
        try:
177
            symbol_versioning.set_warning_method(self.capture_warning)
1534.2.2 by Robert Collins
Implement deprecated_function.
178
            self.assertEqual(1, deprecated_callable())
1534.2.1 by Robert Collins
Implement deprecated_method
179
            self.assertEqual([expected_warning], self._warnings)
1534.2.2 by Robert Collins
Implement deprecated_function.
180
            deprecated_callable()
1534.2.1 by Robert Collins
Implement deprecated_method
181
            self.assertEqual([expected_warning, expected_warning],
182
                             self._warnings)
1534.2.3 by Robert Collins
decorate docstrings in deprecated functions.
183
            self.assertEqualDiff(expected_docstring, deprecated_callable.__doc__)
1534.2.5 by Robert Collins
Set the __name__ attribute on decorated methods and functions.
184
            self.assertEqualDiff(expected_name, deprecated_callable.__name__)
1534.1.3 by Robert Collins
Bugfix the symbol_versioning deprecation decorators to update the
185
            self.assertEqualDiff(expected_module, deprecated_callable.__module__)
1581.1.1 by Robert Collins
Bugfix aliases to be backwards compatible with plugins providing command.run_argv.
186
            self.assertTrue(deprecated_callable.is_deprecated)
1534.2.1 by Robert Collins
Implement deprecated_method
187
        finally:
188
            symbol_versioning.set_warning_method(old_warning_method)
1534.4.2 by Robert Collins
Introduce BranchFormats - factoring out intialisation of Branches.
189
    
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(
1534.4.32 by Robert Collins
Rename deprecated_nonce to DEPRECATED_PARAMETER
196
                            symbol_versioning.DEPRECATED_PARAMETER))
1982.3.1 by Robert Collins
New utility function symbol_versioning.deprecation_string. Returns the
197
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 '
202
            'version 0.11.',
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))