Ticket #3909: misnamed-method.patch

File misnamed-method.patch, 5.3 KB (added by kelly, 10 years ago)
  • misnamed-method/twisted/trial/test/test_loader.py

     
    190190        self.failUnlessRaises(TypeError,
    191191                              self.loader.loadMethod, ('foo', 'bar'))
    192192
     193    def test_loadBadDecorator(self):
     194        import sample
     195        suite = self.loader.loadMethod(sample.DecorationTest.test_bad_decorator)
     196        self.failUnlessEqual(1, suite.countTestCases())
     197        self.failUnlessEqual('test_bad_decorator', suite._testMethodName)
     198
     199    def test_loadGoodDecorator(self):
     200        import sample
     201        suite = self.loader.loadMethod(sample.DecorationTest.test_good_decorator)
     202        self.failUnlessEqual(1, suite.countTestCases())
     203        self.failUnlessEqual('test_good_decorator', suite._testMethodName)
     204
     205    def test_loadRenamedDecorator(self):
     206        import sample
     207        suite = self.loader.loadMethod(sample.DecorationTest.test_renamed_decorator)
     208        self.failUnlessEqual(1, suite.countTestCases())
     209        self.failUnlessEqual('test_renamed_decorator', suite._testMethodName)
     210
    193211    def test_loadClass(self):
    194212        import sample
    195213        suite = self.loader.loadClass(sample.FooTest)
     
    197215        self.failUnlessEqual(['test_bar', 'test_foo'],
    198216                             [test._testMethodName for test in suite._tests])
    199217
    200 
    201218    def test_loadNonClass(self):
    202219        import sample
    203220        self.failUnlessRaises(TypeError, self.loader.loadClass, sample)
     
    215232    def test_loadModule(self):
    216233        import sample
    217234        suite = self.loader.loadModule(sample)
    218         self.failUnlessEqual(7, suite.countTestCases())
     235        self.failUnlessEqual(10, suite.countTestCases())
    219236
    220237    def test_loadNonModule(self):
    221238        import sample
  • misnamed-method/twisted/trial/test/sample.py

     
    55
    66import unittest as pyunit
    77from twisted.trial import unittest
     8from twisted.python.util import mergeFunctionMetadata
    89
    910class FooTest(unittest.TestCase):
    1011    def test_foo(self):
     
    1415        pass
    1516
    1617
     18def bad_decorator(fn):
     19    def name_collision(*args, **kwargs):
     20        return fn(*args, **kwargs)
     21    return name_collision
     22
     23def good_decorator(fn):
     24    def name_collision(*args, **kwargs):
     25        return fn(*args, **kwargs)
     26    return mergeFunctionMetadata(fn, name_collision)
     27   
     28
     29class DecorationTest(unittest.TestCase):
     30    def test_bad_decorator(self):
     31        pass
     32    test_bad_decorator = bad_decorator(test_bad_decorator)
     33
     34    def test_good_decorator(self):
     35        pass
     36    test_good_decorator = good_decorator(test_good_decorator)
     37
     38    def renamed_decorator(self):
     39        pass
     40    test_renamed_decorator = good_decorator(renamed_decorator)
     41
     42    def name_collision(self):
     43        # not a test
     44        pass
     45
     46
    1747class PyunitTest(pyunit.TestCase):
    1848    def test_foo(self):
    1949        pass
  • misnamed-method/twisted/trial/test/test_runner.py

     
    297297                               'startTest', 'addSuccess', 'stopTest',
    298298                               'startTest', 'addSuccess', 'stopTest',
    299299                               'startTest', 'addSuccess', 'stopTest',
     300                               'startTest', 'addSuccess', 'stopTest',
     301                               'startTest', 'addSuccess', 'stopTest',
     302                               'startTest', 'addSuccess', 'stopTest',
    300303                               'startTest', 'addSuccess', 'stopTest']
    301304
    302305
  • misnamed-method/twisted/trial/runner.py

     
    130130    return fn
    131131
    132132
     133def _getMethodNameInClass(method):
     134    """
     135    Find the attribute name on the method's class which refers to the method.
    133136
     137    For some methods, notably decorators which have not had __name__ set correctly:
     138
     139    getattr(method.im_class, method.__name__) != method
     140    """
     141    if getattr(method.im_class, method.__name__, object()) != method:
     142        for alias in dir(method.im_class):
     143            if (hasattr(method.im_class, alias) and
     144                getattr(method.im_class, alias) == method):
     145                return alias
     146    return method.__name__
     147
     148
    134149class DestructiveTestSuite(TestSuite):
    135150    """
    136151    A test suite which remove the tests once run, to minimize memory usage.
     
    521536        """
    522537        if not isinstance(method, types.MethodType):
    523538            raise TypeError("%r not a method" % (method,))
    524         return self._makeCase(method.im_class, method.__name__)
     539        return self._makeCase(method.im_class, _getMethodNameInClass(method))
    525540
    526541    def _makeCase(self, klass, methodName):
    527542        return klass(methodName)