~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_symbol_versioning.py

 * Deprecated functions and methods can now be marked as such using the 
   bzrlib.symbol_versioning module. Marked method have their docstring
   updated and will issue a DeprecationWarning using the warnings module
   when they are used. (Robert Collins)

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
class TestDeprecationWarnings(TestCase):
 
31
 
 
32
    def capture_warning(self, message, category):
 
33
        self._warnings.append((message, category))
 
34
 
 
35
    def setUp(self):
 
36
        super(TestDeprecationWarnings, self).setUp()
 
37
        self._warnings = []
 
38
    
 
39
    @symbol_versioning.deprecated_method(symbol_versioning.zero_seven)
 
40
    def deprecated_method(self):
 
41
        """Deprecated method docstring.
 
42
        
 
43
        This might explain stuff.
 
44
        """
 
45
        return 1
 
46
 
 
47
    def test_deprecated_method(self):
 
48
        expected_warning = (
 
49
            "bzrlib.tests.test_symbol_versioning."
 
50
            "TestDeprecationWarnings.deprecated_method "
 
51
            "was deprecated in version 0.7.", DeprecationWarning)
 
52
        expected_docstring = ('Deprecated method docstring.\n'
 
53
                              '        \n'
 
54
                              '        This might explain stuff.\n'
 
55
                              '        \n'
 
56
                              '        This method was deprecated in version 0.7.\n'
 
57
                              '        ')
 
58
        self.check_deprecated_callable(expected_warning, expected_docstring,
 
59
                                       "deprecated_method",
 
60
                                       self.deprecated_method)
 
61
 
 
62
    def test_deprecated_function(self):
 
63
        expected_warning = (
 
64
            "bzrlib.tests.test_symbol_versioning.deprecated_function "
 
65
            "was deprecated in version 0.7.", DeprecationWarning)
 
66
        expected_docstring = ('Deprecated function docstring.\n'
 
67
                              '\n'
 
68
                              'This function was deprecated in version 0.7.\n'
 
69
                              )
 
70
        self.check_deprecated_callable(expected_warning, expected_docstring,
 
71
                                       "deprecated_function",
 
72
                                       deprecated_function)
 
73
 
 
74
    def check_deprecated_callable(self, expected_warning, expected_docstring,
 
75
                                  expected_name,
 
76
                                  deprecated_callable):
 
77
        old_warning_method = symbol_versioning.warn
 
78
        try:
 
79
            symbol_versioning.set_warning_method(self.capture_warning)
 
80
            self.assertEqual(1, deprecated_callable())
 
81
            self.assertEqual([expected_warning], self._warnings)
 
82
            deprecated_callable()
 
83
            self.assertEqual([expected_warning, expected_warning],
 
84
                             self._warnings)
 
85
            self.assertEqualDiff(expected_docstring, deprecated_callable.__doc__)
 
86
            self.assertEqualDiff(expected_name, deprecated_callable.__name__)
 
87
        finally:
 
88
            symbol_versioning.set_warning_method(old_warning_method)