summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJean-Paul Calderone <exarkun@divmod.com>2010-04-10 19:59:28 (GMT)
committerJean-Paul Calderone <exarkun@divmod.com>2010-04-10 19:59:28 (GMT)
commitb60ee469cd1cd9c51f319fc32fa37987581605d1 (patch)
treec034aa60e83ea9dd1ba099e3451b748e7537d62a
parentbb66973afb4bb3056709181f320d97074e4db074 (diff)
downloadcpython-b60ee469cd1cd9c51f319fc32fa37987581605d1.zip
cpython-b60ee469cd1cd9c51f319fc32fa37987581605d1.tar.gz
cpython-b60ee469cd1cd9c51f319fc32fa37987581605d1.tar.bz2
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.
-rw-r--r--Lib/test/test_inspect.py88
1 files changed, 26 insertions, 62 deletions
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
index 23e1289..aaa15f8 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -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):