Commit b60ee469 authored by Jean-Paul Calderone's avatar Jean-Paul Calderone

Refactor a couple inspect module tests to remove duplicate code

The test_classify_oldstyle and test_classify_newstyle methods of
test.test_inspect.TestClassesAndFunctions were previously almost
identical (aside from irrelevant whitespace and one semantic
difference).  They now share a single helper.

Fixes issue #8363.
parent bb66973a
......@@ -435,8 +435,18 @@ class TestClassesAndFunctions(unittest.TestCase):
exec 'def fakeSublistOfOne((foo)): return 1'
self.assertArgSpecEquals(fakeSublistOfOne, ['foo'])
def test_classify_oldstyle(self):
class A:
def _classify_test(self, newstyle):
"""Helper for testing that classify_class_attrs finds a bunch of
different kinds of attributes on a given class.
"""
if newstyle:
base = object
else:
class base:
pass
class A(base):
def s(): pass
s = staticmethod(s)
......@@ -489,76 +499,30 @@ class TestClassesAndFunctions(unittest.TestCase):
attrs = attrs_wo_objs(D)
self.assertIn(('s', 'static method', A), attrs, 'missing static method')
self.assertIn(('c', 'class method', A), attrs, 'missing class method')
if newstyle:
self.assertIn(('c', 'method', C), attrs, 'missing plain method')
else:
self.assertIn(('c', 'class method', A), attrs, 'missing class method')
self.assertIn(('p', 'property', A), attrs, 'missing property')
self.assertIn(('m', 'method', B), attrs, 'missing plain method')
self.assertIn(('m1', 'method', D), attrs, 'missing plain method')
self.assertIn(('datablob', 'data', A), attrs, 'missing data')
# Repeat all that, but w/ new-style classes.
def test_classify_newstyle(self):
class A(object):
def s(): pass
s = staticmethod(s)
def c(cls): pass
c = classmethod(c)
def getp(self): pass
p = property(getp)
def m(self): pass
def m1(self): pass
datablob = '1'
attrs = attrs_wo_objs(A)
self.assertIn(('s', 'static method', A), attrs, 'missing static method')
self.assertIn(('c', 'class method', A), attrs, 'missing class method')
self.assertIn(('p', 'property', A), attrs, 'missing property')
self.assertIn(('m', 'method', A), attrs, 'missing plain method')
self.assertIn(('m1', 'method', A), attrs, 'missing plain method')
self.assertIn(('datablob', 'data', A), attrs, 'missing data')
class B(A):
def m(self): pass
attrs = attrs_wo_objs(B)
self.assertIn(('s', 'static method', A), attrs, 'missing static method')
self.assertIn(('c', 'class method', A), attrs, 'missing class method')
self.assertIn(('p', 'property', A), attrs, 'missing property')
self.assertIn(('m', 'method', B), attrs, 'missing plain method')
self.assertIn(('m1', 'method', A), attrs, 'missing plain method')
self.assertIn(('datablob', 'data', A), attrs, 'missing data')
class C(A):
def test_classify_oldstyle(self):
"""classify_class_attrs finds static methods, class methods,
properties, normal methods, and data attributes on an old-style
class.
"""
self._classify_test(False)
def m(self): pass
def c(self): pass
attrs = attrs_wo_objs(C)
self.assertIn(('s', 'static method', A), attrs, 'missing static method')
self.assertIn(('c', 'method', C), attrs, 'missing plain method')
self.assertIn(('p', 'property', A), attrs, 'missing property')
self.assertIn(('m', 'method', C), attrs, 'missing plain method')
self.assertIn(('m1', 'method', A), attrs, 'missing plain method')
self.assertIn(('datablob', 'data', A), attrs, 'missing data')
class D(B, C):
def test_classify_newstyle(self):
"""Just like test_classify_oldstyle, but for a new-style class.
"""
self._classify_test(True)
def m1(self): pass
attrs = attrs_wo_objs(D)
self.assertIn(('s', 'static method', A), attrs, 'missing static method')
self.assertIn(('c', 'method', C), attrs, 'missing plain method')
self.assertIn(('p', 'property', A), attrs, 'missing property')
self.assertIn(('m', 'method', B), attrs, 'missing plain method')
self.assertIn(('m1', 'method', D), attrs, 'missing plain method')
self.assertIn(('datablob', 'data', A), attrs, 'missing data')
class TestGetcallargsFunctions(unittest.TestCase):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment