~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: 2005-10-02 22:47:02 UTC
  • mto: This revision was merged to the branch mainline in revision 1397.
  • Revision ID: robertc@robertcollins.net-20051002224701-8a8b20b90de559a6
support ghosts in commits

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 by 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
 
class TestDeprecationWarnings(TestCase):
35
 
 
36
 
    def capture_warning(self, message, category, stacklevel=None):
37
 
        self._warnings.append((message, category, stacklevel))
38
 
 
39
 
    def setUp(self):
40
 
        super(TestDeprecationWarnings, self).setUp()
41
 
        self._warnings = []
42
 
    
43
 
    @symbol_versioning.deprecated_method(symbol_versioning.zero_seven)
44
 
    def deprecated_method(self):
45
 
        """Deprecated method docstring.
46
 
        
47
 
        This might explain stuff.
48
 
        """
49
 
        return 1
50
 
 
51
 
    def test_deprecated_method(self):
52
 
        expected_warning = (
53
 
            "bzrlib.tests.test_symbol_versioning."
54
 
            "TestDeprecationWarnings.deprecated_method "
55
 
            "was deprecated in version 0.7.", DeprecationWarning, 2)
56
 
        expected_docstring = ('Deprecated method docstring.\n'
57
 
                              '        \n'
58
 
                              '        This might explain stuff.\n'
59
 
                              '        \n'
60
 
                              '        This method was deprecated in version 0.7.\n'
61
 
                              '        ')
62
 
        self.check_deprecated_callable(expected_warning, expected_docstring,
63
 
                                       "deprecated_method",
64
 
                                       "bzrlib.tests.test_symbol_versioning",
65
 
                                       self.deprecated_method)
66
 
 
67
 
    def test_deprecated_function(self):
68
 
        expected_warning = (
69
 
            "bzrlib.tests.test_symbol_versioning.deprecated_function "
70
 
            "was deprecated in version 0.7.", DeprecationWarning, 2)
71
 
        expected_docstring = ('Deprecated function docstring.\n'
72
 
                              '\n'
73
 
                              'This function was deprecated in version 0.7.\n'
74
 
                              )
75
 
        self.check_deprecated_callable(expected_warning, expected_docstring,
76
 
                                       "deprecated_function",
77
 
                                       "bzrlib.tests.test_symbol_versioning",
78
 
                                       deprecated_function)
79
 
 
80
 
    def test_deprecated_list(self):
81
 
        expected_warning = (
82
 
            "Modifying a_deprecated_list was deprecated in version 0.9."
83
 
            " Don't use me", DeprecationWarning, 3)
84
 
        expected_doctstring = ('appending to a_deprecated_list is deprecated')
85
 
 
86
 
        old_warning_method = symbol_versioning.warn
87
 
        try:
88
 
            symbol_versioning.set_warning_method(self.capture_warning)
89
 
            self.assertEqual(['one'], a_deprecated_list)
90
 
            self.assertEqual([], self._warnings)
91
 
 
92
 
            a_deprecated_list.append('foo')
93
 
            self.assertEqual([expected_warning], self._warnings)
94
 
            self.assertEqual(['one', 'foo'], a_deprecated_list)
95
 
 
96
 
            a_deprecated_list.extend(['bar', 'baz'])
97
 
            self.assertEqual([expected_warning]*2, self._warnings)
98
 
            self.assertEqual(['one', 'foo', 'bar', 'baz'], a_deprecated_list)
99
 
 
100
 
            a_deprecated_list.insert(1, 'xxx')
101
 
            self.assertEqual([expected_warning]*3, self._warnings)
102
 
            self.assertEqual(['one', 'xxx', 'foo', 'bar', 'baz'], a_deprecated_list)
103
 
 
104
 
            a_deprecated_list.remove('foo')
105
 
            self.assertEqual([expected_warning]*4, self._warnings)
106
 
            self.assertEqual(['one', 'xxx', 'bar', 'baz'], a_deprecated_list)
107
 
 
108
 
            val = a_deprecated_list.pop()
109
 
            self.assertEqual([expected_warning]*5, self._warnings)
110
 
            self.assertEqual('baz', val)
111
 
            self.assertEqual(['one', 'xxx', 'bar'], a_deprecated_list)
112
 
 
113
 
            val = a_deprecated_list.pop(1)
114
 
            self.assertEqual([expected_warning]*6, self._warnings)
115
 
            self.assertEqual('xxx', val)
116
 
            self.assertEqual(['one', 'bar'], a_deprecated_list)
117
 
        finally:
118
 
            symbol_versioning.set_warning_method(old_warning_method)
119
 
 
120
 
    def check_deprecated_callable(self, expected_warning, expected_docstring,
121
 
                                  expected_name, expected_module,
122
 
                                  deprecated_callable):
123
 
        old_warning_method = symbol_versioning.warn
124
 
        try:
125
 
            symbol_versioning.set_warning_method(self.capture_warning)
126
 
            self.assertEqual(1, deprecated_callable())
127
 
            self.assertEqual([expected_warning], self._warnings)
128
 
            deprecated_callable()
129
 
            self.assertEqual([expected_warning, expected_warning],
130
 
                             self._warnings)
131
 
            self.assertEqualDiff(expected_docstring, deprecated_callable.__doc__)
132
 
            self.assertEqualDiff(expected_name, deprecated_callable.__name__)
133
 
            self.assertEqualDiff(expected_module, deprecated_callable.__module__)
134
 
            self.assertTrue(deprecated_callable.is_deprecated)
135
 
        finally:
136
 
            symbol_versioning.set_warning_method(old_warning_method)
137
 
    
138
 
    def test_deprecated_passed(self):
139
 
        self.assertEqual(True, symbol_versioning.deprecated_passed(None))
140
 
        self.assertEqual(True, symbol_versioning.deprecated_passed(True))
141
 
        self.assertEqual(True, symbol_versioning.deprecated_passed(False))
142
 
        self.assertEqual(False,
143
 
                         symbol_versioning.deprecated_passed(
144
 
                            symbol_versioning.DEPRECATED_PARAMETER))