|
1 | 1 | """ Testing `deprecated` module
|
2 | 2 | """
|
3 | 3 |
|
| 4 | +import sys |
4 | 5 | import warnings
|
| 6 | +from functools import partial |
| 7 | +from distutils.version import LooseVersion |
5 | 8 |
|
6 | 9 | from nose.tools import (assert_true, assert_false, assert_raises,
|
7 | 10 | assert_equal, assert_not_equal)
|
8 | 11 |
|
9 |
| -from ..deprecated import ModuleProxy, FutureWarningMixin |
| 12 | +from .. import deprecated |
| 13 | +from ..deprecated import (ModuleProxy, FutureWarningMixin, _ensure_cr, |
| 14 | + _add_dep_doc, deprecate_with_version, |
| 15 | + ExpiredDeprecationError) |
| 16 | + |
| 17 | +from ..testing import clear_and_catch_warnings |
| 18 | + |
| 19 | + |
| 20 | +_ORIG_PKG_VERSION = deprecated.PKG_VERSION |
| 21 | +_OWN_MODULE = sys.modules[__name__] |
| 22 | + |
| 23 | + |
| 24 | +def setup(): |
| 25 | + deprecated.PKG_VERSION = LooseVersion('2.0') |
| 26 | + |
| 27 | + |
| 28 | +def teardown(): |
| 29 | + deprecated.PKG_VERSION = _ORIG_PKG_VERSION |
10 | 30 |
|
11 | 31 |
|
12 | 32 | def test_module_proxy():
|
@@ -47,3 +67,118 @@ class E(FutureWarningMixin, C):
|
47 | 67 | warn = warns.pop(0)
|
48 | 68 | assert_equal(warn.category, FutureWarning)
|
49 | 69 | assert_equal(str(warn.message), 'Oh no, not this one')
|
| 70 | + |
| 71 | + |
| 72 | +def test__ensure_cr(): |
| 73 | + # Make sure text ends with carriage return |
| 74 | + assert_equal(_ensure_cr(' foo'), ' foo\n') |
| 75 | + assert_equal(_ensure_cr(' foo\n'), ' foo\n') |
| 76 | + assert_equal(_ensure_cr(' foo '), ' foo\n') |
| 77 | + assert_equal(_ensure_cr('foo '), 'foo\n') |
| 78 | + assert_equal(_ensure_cr('foo \n bar'), 'foo \n bar\n') |
| 79 | + assert_equal(_ensure_cr('foo \n\n'), 'foo\n') |
| 80 | + |
| 81 | + |
| 82 | +def test__add_dep_doc(): |
| 83 | + # Test utility function to add deprecation message to docstring |
| 84 | + assert_equal(_add_dep_doc('', 'foo'), 'foo\n') |
| 85 | + assert_equal(_add_dep_doc('bar', 'foo'), 'bar\n\nfoo\n') |
| 86 | + assert_equal(_add_dep_doc(' bar', 'foo'), ' bar\n\nfoo\n') |
| 87 | + assert_equal(_add_dep_doc(' bar', 'foo\n'), ' bar\n\nfoo\n') |
| 88 | + assert_equal(_add_dep_doc('bar\n\n', 'foo'), 'bar\n\nfoo\n') |
| 89 | + assert_equal(_add_dep_doc('bar\n \n', 'foo'), 'bar\n\nfoo\n') |
| 90 | + assert_equal(_add_dep_doc(' bar\n\nSome explanation', 'foo\nbaz'), |
| 91 | + ' bar\n\nfoo\nbaz\n\nSome explanation\n') |
| 92 | + assert_equal(_add_dep_doc(' bar\n\n Some explanation', 'foo\nbaz'), |
| 93 | + ' bar\n \n foo\n baz\n \n Some explanation\n') |
| 94 | + |
| 95 | + |
| 96 | +def _make_version(version): |
| 97 | + return version if hasattr(version, 'version') else LooseVersion(version) |
| 98 | + |
| 99 | + |
| 100 | +def assert_deprecated(func, args=(), kwargs=None, |
| 101 | + warn_class=DeprecationWarning): |
| 102 | + """ Assert that `func` raises deprecation warning """ |
| 103 | + kwargs = {} if kwargs is None else kwargs |
| 104 | + func_module = sys.modules[func.__module__] |
| 105 | + with clear_and_catch_warnings(modules=[func_module]) as w: |
| 106 | + warnings.simplefilter('always', warn_class) |
| 107 | + func(*args, **kwargs) |
| 108 | + assert_equal(len(w), 1) |
| 109 | + |
| 110 | + |
| 111 | +def test_deprecate_with_version(): |
| 112 | + # Test function deprecation |
| 113 | + |
| 114 | + def func_no_doc(): pass |
| 115 | + |
| 116 | + def func_doc(i): "A docstring" |
| 117 | + |
| 118 | + def func_doc_long(i, j): "A docstring\n\n Some text" |
| 119 | + |
| 120 | + func = deprecate_with_version('foo')(func_no_doc) |
| 121 | + with clear_and_catch_warnings(modules=[_OWN_MODULE]) as w: |
| 122 | + warnings.simplefilter('always') |
| 123 | + assert_equal(func(), None) |
| 124 | + assert_equal(len(w), 1) |
| 125 | + assert_deprecated(func) |
| 126 | + assert_equal(func.__doc__, 'foo\n') |
| 127 | + func = deprecate_with_version('foo')(func_doc) |
| 128 | + with clear_and_catch_warnings(modules=[_OWN_MODULE]) as w: |
| 129 | + warnings.simplefilter('always') |
| 130 | + assert_equal(func(1), None) |
| 131 | + assert_equal(len(w), 1) |
| 132 | + assert_deprecated(func, (1,)) |
| 133 | + assert_equal(func.__doc__, 'A docstring\n\nfoo\n') |
| 134 | + func = deprecate_with_version('foo')(func_doc_long) |
| 135 | + with clear_and_catch_warnings(modules=[_OWN_MODULE]) as w: |
| 136 | + warnings.simplefilter('always') |
| 137 | + assert_equal(func(1, 2), None) |
| 138 | + assert_equal(len(w), 1) |
| 139 | + assert_deprecated(func, (1, 2)) |
| 140 | + assert_equal(func.__doc__, 'A docstring\n \n foo\n \n Some text\n') |
| 141 | + |
| 142 | + # Try some since and until versions |
| 143 | + func = deprecate_with_version('foo', '1.1')(func_no_doc) |
| 144 | + assert_equal(func.__doc__, 'foo\n\n* deprecated from version: 1.1\n') |
| 145 | + with clear_and_catch_warnings(modules=[_OWN_MODULE]) as w: |
| 146 | + warnings.simplefilter('always') |
| 147 | + assert_equal(func(), None) |
| 148 | + assert_equal(len(w), 1) |
| 149 | + assert_deprecated(func) |
| 150 | + func = deprecate_with_version('foo', until='2.4')(func_no_doc) |
| 151 | + with clear_and_catch_warnings(modules=[_OWN_MODULE]) as w: |
| 152 | + warnings.simplefilter('always') |
| 153 | + assert_equal(func(), None) |
| 154 | + assert_equal(len(w), 1) |
| 155 | + assert_deprecated(func) |
| 156 | + assert_equal(func.__doc__, |
| 157 | + 'foo\n\n* Will raise {} as of version: 2.4\n' |
| 158 | + .format(ExpiredDeprecationError)) |
| 159 | + func = deprecate_with_version('foo', until='1.8')(func_no_doc) |
| 160 | + assert_raises(ExpiredDeprecationError, func) |
| 161 | + assert_equal(func.__doc__, |
| 162 | + 'foo\n\n* Raises {} as of version: 1.8\n' |
| 163 | + .format(ExpiredDeprecationError)) |
| 164 | + func = deprecate_with_version('foo', '1.2', '1.8')(func_no_doc) |
| 165 | + assert_raises(ExpiredDeprecationError, func) |
| 166 | + assert_equal(func.__doc__, |
| 167 | + 'foo\n\n* deprecated from version: 1.2\n' |
| 168 | + '* Raises {} as of version: 1.8\n' |
| 169 | + .format(ExpiredDeprecationError)) |
| 170 | + func = deprecate_with_version('foo', '1.2', '1.8')(func_doc_long) |
| 171 | + assert_equal(func.__doc__, |
| 172 | + 'A docstring\n \n foo\n \n' |
| 173 | + ' * deprecated from version: 1.2\n' |
| 174 | + ' * Raises {} as of version: 1.8\n \n' |
| 175 | + ' Some text\n' |
| 176 | + .format(ExpiredDeprecationError)) |
| 177 | + assert_raises(ExpiredDeprecationError, func) |
| 178 | + # Check different warnings and errors |
| 179 | + func = deprecate_with_version('foo', warn_class=UserWarning)(func_no_doc) |
| 180 | + assert_deprecated(func, warn_class=UserWarning) |
| 181 | + func = deprecate_with_version('foo', |
| 182 | + expired_class=IOError, |
| 183 | + until='1.8')(func_no_doc) |
| 184 | + assert_raises(IOError, func) |
0 commit comments