Refactor test_util_inspect: Do not use unittest

This commit is contained in:
Takeshi KOMIYA 2017-06-11 21:32:40 +09:00
parent f6d0b07704
commit 94b31a2f07

View File

@ -8,8 +8,6 @@
:copyright: Copyright 2007-2017 by the Sphinx team, see AUTHORS. :copyright: Copyright 2007-2017 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details. :license: BSD, see LICENSE for details.
""" """
from unittest import TestCase
import sys import sys
from six import PY3 from six import PY3
import functools import functools
@ -19,114 +17,117 @@ import pytest
from sphinx.util import inspect from sphinx.util import inspect
class TestGetArgSpec(TestCase): def test_getargspec_builtin_type():
def test_getargspec_builtin_type(self): with pytest.raises(TypeError):
with pytest.raises(TypeError): inspect.getargspec(int)
inspect.getargspec(int)
def test_getargspec_partial(self):
def fun(a, b, c=1, d=2): def test_getargspec_partial():
def fun(a, b, c=1, d=2):
pass
p = functools.partial(fun, 10, c=11)
if PY3:
# Python 3's partial is rather cleverer than Python 2's, and we
# have to jump through some hoops to define an equivalent function
# in a way that won't confuse Python 2's parser:
ns = {}
exec(dedent("""
def f_expected(b, *, c=11, d=2):
pass
"""), ns)
f_expected = ns["f_expected"]
else:
def f_expected(b, d=2):
pass pass
p = functools.partial(fun, 10, c=11) expected = inspect.getargspec(f_expected)
if PY3: assert expected == inspect.getargspec(p)
# Python 3's partial is rather cleverer than Python 2's, and we
# have to jump through some hoops to define an equivalent function
# in a way that won't confuse Python 2's parser:
ns = {}
exec(dedent("""
def f_expected(b, *, c=11, d=2):
pass
"""), ns)
f_expected = ns["f_expected"]
else:
def f_expected(b, d=2):
pass
expected = inspect.getargspec(f_expected)
assert expected == inspect.getargspec(p)
def test_getargspec_bound_methods(self): def test_getargspec_bound_methods():
def f_expected_unbound(self, arg1, **kwargs): def f_expected_unbound(self, arg1, **kwargs):
pass pass
expected_unbound = inspect.getargspec(f_expected_unbound) expected_unbound = inspect.getargspec(f_expected_unbound)
def f_expected_bound(arg1, **kwargs): def f_expected_bound(arg1, **kwargs):
pass pass
expected_bound = inspect.getargspec(f_expected_bound) expected_bound = inspect.getargspec(f_expected_bound)
class Foo: class Foo:
def method(self, arg1, **kwargs): def method(self, arg1, **kwargs):
pass
bound_method = Foo().method
@functools.wraps(bound_method)
def wrapped_bound_method(*args, **kwargs):
pass pass
assert expected_unbound == inspect.getargspec(Foo.method) bound_method = Foo().method
if PY3 and sys.version_info >= (3, 4, 4):
# On py2, the inspect functions don't properly handle bound @functools.wraps(bound_method)
# methods (they include a spurious 'self' argument) def wrapped_bound_method(*args, **kwargs):
assert expected_bound == inspect.getargspec(bound_method) pass
# On py2, the inspect functions can't properly handle wrapped
# functions (no __wrapped__ support) assert expected_unbound == inspect.getargspec(Foo.method)
assert expected_bound == inspect.getargspec(wrapped_bound_method) if PY3 and sys.version_info >= (3, 4, 4):
# On py2, the inspect functions don't properly handle bound
# methods (they include a spurious 'self' argument)
assert expected_bound == inspect.getargspec(bound_method)
# On py2, the inspect functions can't properly handle wrapped
# functions (no __wrapped__ support)
assert expected_bound == inspect.getargspec(wrapped_bound_method)
class TestSafeGetAttr(TestCase): def test_safe_getattr_with_default():
def test_safe_getattr_with_default(self): class Foo(object):
class Foo(object): def __getattr__(self, item):
def __getattr__(self, item): raise Exception
raise Exception
obj = Foo() obj = Foo()
result = inspect.safe_getattr(obj, 'bar', 'baz') result = inspect.safe_getattr(obj, 'bar', 'baz')
assert result == 'baz' assert result == 'baz'
def test_safe_getattr_with_exception(self):
class Foo(object):
def __getattr__(self, item):
raise Exception
obj = Foo() def test_safe_getattr_with_exception():
class Foo(object):
def __getattr__(self, item):
raise Exception
try: obj = Foo()
inspect.safe_getattr(obj, 'bar')
except AttributeError as exc:
self.assertEqual(exc.args[0], 'bar')
else:
self.fail('AttributeError not raised')
def test_safe_getattr_with_property_exception(self): try:
class Foo(object): inspect.safe_getattr(obj, 'bar')
@property except AttributeError as exc:
def bar(self): assert exc.args[0] == 'bar'
raise Exception else:
pytest.fail('AttributeError not raised')
obj = Foo()
try: def test_safe_getattr_with_property_exception():
inspect.safe_getattr(obj, 'bar') class Foo(object):
except AttributeError as exc: @property
self.assertEqual(exc.args[0], 'bar') def bar(self):
else: raise Exception
self.fail('AttributeError not raised')
def test_safe_getattr_with___dict___override(self): obj = Foo()
class Foo(object):
@property
def __dict__(self):
raise Exception
obj = Foo() try:
inspect.safe_getattr(obj, 'bar')
except AttributeError as exc:
assert exc.args[0] == 'bar'
else:
pytest.fail('AttributeError not raised')
try:
inspect.safe_getattr(obj, 'bar') def test_safe_getattr_with___dict___override():
except AttributeError as exc: class Foo(object):
self.assertEqual(exc.args[0], 'bar') @property
else: def __dict__(self):
self.fail('AttributeError not raised') raise Exception
obj = Foo()
try:
inspect.safe_getattr(obj, 'bar')
except AttributeError as exc:
assert exc.args[0] == 'bar'
else:
pytest.fail('AttributeError not raised')