~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>
3
#
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.
8
#
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.
13
#
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
17
18
"""Symbol versioning tests."""
19
20
import bzrlib.symbol_versioning as symbol_versioning
21
from bzrlib.tests import TestCase
22
23
1534.2.2 by Robert Collins
Implement deprecated_function.
24
@symbol_versioning.deprecated_function(symbol_versioning.zero_seven)
25
def deprecated_function():
1534.2.3 by Robert Collins
decorate docstrings in deprecated functions.
26
    """Deprecated function docstring."""
1534.2.2 by Robert Collins
Implement deprecated_function.
27
    return 1
28
1534.2.1 by Robert Collins
Implement deprecated_method
29
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.
30
a_deprecated_list = symbol_versioning.deprecated_list(symbol_versioning.zero_nine,
31
    'a_deprecated_list', ['one'], extra="Don't use me")
32
33
2227.1.2 by mbp at sourcefrog
Add a simple DeprecatedDict class
34
a_deprecated_dict = symbol_versioning.DeprecatedDict(
35
    symbol_versioning.zero_fourteen,
36
    'a_deprecated_dict',
37
    dict(a=42),
38
    advice='Pull the other one!',
39
    )
40
41
1534.2.1 by Robert Collins
Implement deprecated_method
42
class TestDeprecationWarnings(TestCase):
43
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
44
    def capture_warning(self, message, category, stacklevel=None):
45
        self._warnings.append((message, category, stacklevel))
1534.2.1 by Robert Collins
Implement deprecated_method
46
47
    def setUp(self):
48
        super(TestDeprecationWarnings, self).setUp()
49
        self._warnings = []
50
    
51
    @symbol_versioning.deprecated_method(symbol_versioning.zero_seven)
52
    def deprecated_method(self):
1534.2.3 by Robert Collins
decorate docstrings in deprecated functions.
53
        """Deprecated method docstring.
54
        
55
        This might explain stuff.
56
        """
1534.2.2 by Robert Collins
Implement deprecated_function.
57
        return 1
1534.2.1 by Robert Collins
Implement deprecated_method
58
59
    def test_deprecated_method(self):
1534.2.2 by Robert Collins
Implement deprecated_function.
60
        expected_warning = (
61
            "bzrlib.tests.test_symbol_versioning."
62
            "TestDeprecationWarnings.deprecated_method "
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
63
            "was deprecated in version 0.7.", DeprecationWarning, 2)
1534.2.3 by Robert Collins
decorate docstrings in deprecated functions.
64
        expected_docstring = ('Deprecated method docstring.\n'
65
                              '        \n'
66
                              '        This might explain stuff.\n'
67
                              '        \n'
68
                              '        This method was deprecated in version 0.7.\n'
69
                              '        ')
70
        self.check_deprecated_callable(expected_warning, expected_docstring,
1534.2.5 by Robert Collins
Set the __name__ attribute on decorated methods and functions.
71
                                       "deprecated_method",
1534.1.3 by Robert Collins
Bugfix the symbol_versioning deprecation decorators to update the
72
                                       "bzrlib.tests.test_symbol_versioning",
1534.2.2 by Robert Collins
Implement deprecated_function.
73
                                       self.deprecated_method)
74
75
    def test_deprecated_function(self):
76
        expected_warning = (
77
            "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.
78
            "was deprecated in version 0.7.", DeprecationWarning, 2)
1534.2.3 by Robert Collins
decorate docstrings in deprecated functions.
79
        expected_docstring = ('Deprecated function docstring.\n'
80
                              '\n'
81
                              'This function was deprecated in version 0.7.\n'
82
                              )
83
        self.check_deprecated_callable(expected_warning, expected_docstring,
1534.2.5 by Robert Collins
Set the __name__ attribute on decorated methods and functions.
84
                                       "deprecated_function",
1534.1.3 by Robert Collins
Bugfix the symbol_versioning deprecation decorators to update the
85
                                       "bzrlib.tests.test_symbol_versioning",
1534.2.2 by Robert Collins
Implement deprecated_function.
86
                                       deprecated_function)
87
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.
88
    def test_deprecated_list(self):
89
        expected_warning = (
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
93
        try:
94
            symbol_versioning.set_warning_method(self.capture_warning)
95
            self.assertEqual(['one'], a_deprecated_list)
96
            self.assertEqual([], self._warnings)
97
98
            a_deprecated_list.append('foo')
99
            self.assertEqual([expected_warning], self._warnings)
100
            self.assertEqual(['one', 'foo'], a_deprecated_list)
101
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)
105
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)
109
110
            a_deprecated_list.remove('foo')
111
            self.assertEqual([expected_warning]*4, self._warnings)
112
            self.assertEqual(['one', 'xxx', 'bar', 'baz'], a_deprecated_list)
113
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)
118
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)
123
        finally:
124
            symbol_versioning.set_warning_method(old_warning_method)
125
2227.1.2 by mbp at sourcefrog
Add a simple DeprecatedDict class
126
    def test_deprecated_dict(self):
127
        expected_warning = (
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
131
        try:
132
            symbol_versioning.set_warning_method(self.capture_warning)
133
            self.assertEqual(len(a_deprecated_dict), 1)
134
            self.assertEqual([expected_warning], self._warnings)
135
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)
142
        finally:
143
            symbol_versioning.set_warning_method(old_warning_method)
144
145
1534.2.3 by Robert Collins
decorate docstrings in deprecated functions.
146
    def check_deprecated_callable(self, expected_warning, expected_docstring,
1534.1.3 by Robert Collins
Bugfix the symbol_versioning deprecation decorators to update the
147
                                  expected_name, expected_module,
1534.2.3 by Robert Collins
decorate docstrings in deprecated functions.
148
                                  deprecated_callable):
1534.2.1 by Robert Collins
Implement deprecated_method
149
        old_warning_method = symbol_versioning.warn
150
        try:
151
            symbol_versioning.set_warning_method(self.capture_warning)
1534.2.2 by Robert Collins
Implement deprecated_function.
152
            self.assertEqual(1, deprecated_callable())
1534.2.1 by Robert Collins
Implement deprecated_method
153
            self.assertEqual([expected_warning], self._warnings)
1534.2.2 by Robert Collins
Implement deprecated_function.
154
            deprecated_callable()
1534.2.1 by Robert Collins
Implement deprecated_method
155
            self.assertEqual([expected_warning, expected_warning],
156
                             self._warnings)
1534.2.3 by Robert Collins
decorate docstrings in deprecated functions.
157
            self.assertEqualDiff(expected_docstring, deprecated_callable.__doc__)
1534.2.5 by Robert Collins
Set the __name__ attribute on decorated methods and functions.
158
            self.assertEqualDiff(expected_name, deprecated_callable.__name__)
1534.1.3 by Robert Collins
Bugfix the symbol_versioning deprecation decorators to update the
159
            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.
160
            self.assertTrue(deprecated_callable.is_deprecated)
1534.2.1 by Robert Collins
Implement deprecated_method
161
        finally:
162
            symbol_versioning.set_warning_method(old_warning_method)
1534.4.2 by Robert Collins
Introduce BranchFormats - factoring out intialisation of Branches.
163
    
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(
1534.4.32 by Robert Collins
Rename deprecated_nonce to DEPRECATED_PARAMETER
170
                            symbol_versioning.DEPRECATED_PARAMETER))
1982.3.1 by Robert Collins
New utility function symbol_versioning.deprecation_string. Returns the
171
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 '
176
            'version 0.11.',
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))