~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_symbol_versioning.py

  • Committer: Alexander Belchenko
  • Date: 2007-09-22 17:29:16 UTC
  • mfrom: (2846 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2864.
  • Revision ID: bialix@ukr.net-20070922172916-yzl05wpf8ye852gw
Bug #140419 fixed by Robert Collins

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006, 2007 Canonical Ltd
 
2
#   Authors: Robert Collins <robert.collins@canonical.com>
 
3
#   and others
 
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
 
 
25
@symbol_versioning.deprecated_function(symbol_versioning.zero_seven)
 
26
def deprecated_function():
 
27
    """Deprecated function docstring."""
 
28
    return 1
 
29
 
 
30
 
 
31
a_deprecated_list = symbol_versioning.deprecated_list(symbol_versioning.zero_nine,
 
32
    'a_deprecated_list', ['one'], extra="Don't use me")
 
33
 
 
34
 
 
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
 
 
43
class TestDeprecationWarnings(TestCase):
 
44
 
 
45
    def capture_warning(self, message, category, stacklevel=None):
 
46
        self._warnings.append((message, category, stacklevel))
 
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):
 
54
        """Deprecated method docstring.
 
55
        
 
56
        This might explain stuff.
 
57
        """
 
58
        return 1
 
59
 
 
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):
 
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.
 
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
 
 
85
    def test_deprecated_method(self):
 
86
        expected_warning = (
 
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'
 
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,
 
97
                                       "deprecated_method",
 
98
                                       "bzrlib.tests.test_symbol_versioning",
 
99
                                       self.deprecated_method)
 
100
 
 
101
    def test_deprecated_function(self):
 
102
        expected_warning = (
 
103
            "bzrlib.tests.test_symbol_versioning.deprecated_function "
 
104
            "was deprecated in version 0.7.", DeprecationWarning, 2)
 
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,
 
110
                                       "deprecated_function",
 
111
                                       "bzrlib.tests.test_symbol_versioning",
 
112
                                       deprecated_function)
 
113
 
 
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
 
 
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
 
 
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
 
176
        try:
 
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],
 
182
                             self._warnings)
 
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)
 
187
        finally:
 
188
            symbol_versioning.set_warning_method(old_warning_method)
 
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(
 
196
                            symbol_versioning.DEPRECATED_PARAMETER))
 
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))