[Python-checkins] r79935 - python/trunk/Lib/test/test_inspect.py

jean-paul.calderone python-checkins at python.org
Sat Apr 10 21:59:29 CEST 2010


Author: jean-paul.calderone
Date: Sat Apr 10 21:59:28 2010
New Revision: 79935

Log:
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.


Modified:
   python/trunk/Lib/test/test_inspect.py

Modified: python/trunk/Lib/test/test_inspect.py
==============================================================================
--- python/trunk/Lib/test/test_inspect.py	(original)
+++ python/trunk/Lib/test/test_inspect.py	Sat Apr 10 21:59:28 2010
@@ -435,8 +435,18 @@
             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 @@
 
         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 m(self): pass
-            def c(self): pass
+    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)
 
-        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):
 


More information about the Python-checkins mailing list