~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_symbol_versioning.py

  • Committer: Robert Collins
  • Date: 2007-03-11 23:34:27 UTC
  • mto: This revision was merged to the branch mainline in revision 2442.
  • Revision ID: robertc@robertcollins.net-20070311233427-z8skxqrx8shqy1de
Fix building of C modules without pyrex installed.

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
#
 
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
 
 
24
@symbol_versioning.deprecated_function(symbol_versioning.zero_seven)
 
25
def deprecated_function():
 
26
    """Deprecated function docstring."""
 
27
    return 1
 
28
 
 
29
 
 
30
a_deprecated_list = symbol_versioning.deprecated_list(symbol_versioning.zero_nine,
 
31
    'a_deprecated_list', ['one'], extra="Don't use me")
 
32
 
 
33
 
 
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
 
 
42
class TestDeprecationWarnings(TestCase):
 
43
 
 
44
    def capture_warning(self, message, category, stacklevel=None):
 
45
        self._warnings.append((message, category, stacklevel))
 
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):
 
53
        """Deprecated method docstring.
 
54
        
 
55
        This might explain stuff.
 
56
        """
 
57
        return 1
 
58
 
 
59
    def test_deprecated_method(self):
 
60
        expected_warning = (
 
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'
 
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,
 
71
                                       "deprecated_method",
 
72
                                       "bzrlib.tests.test_symbol_versioning",
 
73
                                       self.deprecated_method)
 
74
 
 
75
    def test_deprecated_function(self):
 
76
        expected_warning = (
 
77
            "bzrlib.tests.test_symbol_versioning.deprecated_function "
 
78
            "was deprecated in version 0.7.", DeprecationWarning, 2)
 
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,
 
84
                                       "deprecated_function",
 
85
                                       "bzrlib.tests.test_symbol_versioning",
 
86
                                       deprecated_function)
 
87
 
 
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
 
 
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
 
 
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
 
150
        try:
 
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],
 
156
                             self._warnings)
 
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)
 
161
        finally:
 
162
            symbol_versioning.set_warning_method(old_warning_method)
 
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(
 
170
                            symbol_versioning.DEPRECATED_PARAMETER))
 
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))