Ticket #3909: misnamed-method-2.patch

File misnamed-method-2.patch, 9.8 KB (added by kelly, 10 years ago)

Updated version of the patch which is slightly more conformant to the style guidelines.

  • misnamed-method/twisted/trial/test/test_loader.py

     
    149149
    150150
    151151class LoaderTest(packages.SysPathManglingTest):
     152    """
     153    Tests for L{trial.TestLoader}.
     154    """
    152155
    153156    def setUp(self):
    154157        self.loader = runner.TestLoader()
    155158        packages.SysPathManglingTest.setUp(self)
    156159
     160
    157161    def test_sortCases(self):
    158162        import sample
    159163        suite = self.loader.loadClass(sample.AlphabetTest)
     
    166170        self.failUnlessEqual(newOrder,
    167171                             [test._testMethodName for test in suite._tests])
    168172
     173
    169174    def test_loadMethod(self):
    170175        import sample
    171176        suite = self.loader.loadMethod(sample.FooTest.test_foo)
    172177        self.failUnlessEqual(1, suite.countTestCases())
    173178        self.failUnlessEqual('test_foo', suite._testMethodName)
    174179
     180
    175181    def test_loadFailingMethod(self):
    176182        # test added for issue1353
    177183        import erroneous
     
    181187        self.failUnlessEqual(result.testsRun, 1)
    182188        self.failUnlessEqual(len(result.failures), 1)
    183189
     190
    184191    def test_loadNonMethod(self):
    185192        import sample
    186193        self.failUnlessRaises(TypeError, self.loader.loadMethod, sample)
     
    190197        self.failUnlessRaises(TypeError,
    191198                              self.loader.loadMethod, ('foo', 'bar'))
    192199
     200
     201    def test_loadBadDecorator(self):
     202        """
     203        Load a decorated test method for which the decorator has failed to set
     204        the method's __name__ correctly.
     205        """
     206        import sample
     207        suite = self.loader.loadMethod(sample.DecorationTest.test_bad_decorator)
     208        self.failUnlessEqual(1, suite.countTestCases())
     209        self.failUnlessEqual('test_bad_decorator', suite._testMethodName)
     210
     211
     212    def test_loadGoodDecorator(self):
     213        """
     214        Load a decorated test method for which the decorator has set the
     215        method's __name__ correctly.
     216        """
     217        import sample
     218        suite = self.loader.loadMethod(
     219            sample.DecorationTest.test_good_decorator)
     220        self.failUnlessEqual(1, suite.countTestCases())
     221        self.failUnlessEqual('test_good_decorator', suite._testMethodName)
     222
     223
     224    def test_loadRenamedDecorator(self):
     225        """
     226        Load a decorated method which has been copied to a new name inside the
     227        class.  Thus its __name__ and its key in the class's __dict__ no
     228        longer match.
     229        """
     230        import sample
     231        suite = self.loader.loadMethod(
     232            sample.DecorationTest.test_renamed_decorator)
     233        self.failUnlessEqual(1, suite.countTestCases())
     234        self.failUnlessEqual('test_renamed_decorator', suite._testMethodName)
     235
     236
    193237    def test_loadClass(self):
    194238        import sample
    195239        suite = self.loader.loadClass(sample.FooTest)
     
    207251        self.failUnlessRaises(TypeError,
    208252                              self.loader.loadClass, ('foo', 'bar'))
    209253
     254
    210255    def test_loadNonTestCase(self):
    211256        import sample
    212257        self.failUnlessRaises(ValueError, self.loader.loadClass,
    213258                              sample.NotATest)
    214259
     260
    215261    def test_loadModule(self):
    216262        import sample
    217263        suite = self.loader.loadModule(sample)
    218         self.failUnlessEqual(7, suite.countTestCases())
     264        self.failUnlessEqual(10, suite.countTestCases())
    219265
     266
    220267    def test_loadNonModule(self):
    221268        import sample
    222269        self.failUnlessRaises(TypeError,
     
    227274        self.failUnlessRaises(TypeError,
    228275                              self.loader.loadModule, ('foo', 'bar'))
    229276
     277
    230278    def test_loadPackage(self):
    231279        import goodpackage
    232280        suite = self.loader.loadPackage(goodpackage)
    233281        self.failUnlessEqual(7, suite.countTestCases())
    234282
     283
    235284    def test_loadNonPackage(self):
    236285        import sample
    237286        self.failUnlessRaises(TypeError,
     
    242291        self.failUnlessRaises(TypeError,
    243292                              self.loader.loadPackage, ('foo', 'bar'))
    244293
     294
    245295    def test_loadModuleAsPackage(self):
    246296        import sample
    247297        ## XXX -- should this instead raise a ValueError? -- jml
    248298        self.failUnlessRaises(TypeError, self.loader.loadPackage, sample)
    249299
     300
    250301    def test_loadPackageRecursive(self):
    251302        import goodpackage
    252303        suite = self.loader.loadPackage(goodpackage, recurse=True)
    253304        self.failUnlessEqual(14, suite.countTestCases())
    254305
     306
    255307    def test_loadAnythingOnModule(self):
    256308        import sample
    257309        suite = self.loader.loadAnything(sample)
    258310        self.failUnlessEqual(sample.__name__,
    259311                             suite._tests[0]._tests[0].__class__.__module__)
    260312
     313
    261314    def test_loadAnythingOnClass(self):
    262315        import sample
    263316        suite = self.loader.loadAnything(sample.FooTest)
    264317        self.failUnlessEqual(2, suite.countTestCases())
    265318
     319
    266320    def test_loadAnythingOnMethod(self):
    267321        import sample
    268322        suite = self.loader.loadAnything(sample.FooTest.test_foo)
    269323        self.failUnlessEqual(1, suite.countTestCases())
    270324
     325
    271326    def test_loadAnythingOnPackage(self):
    272327        import goodpackage
    273328        suite = self.loader.loadAnything(goodpackage)
    274329        self.failUnless(isinstance(suite, self.loader.suiteFactory))
    275330        self.failUnlessEqual(7, suite.countTestCases())
    276331
     332
    277333    def test_loadAnythingOnPackageRecursive(self):
    278334        import goodpackage
    279335        suite = self.loader.loadAnything(goodpackage, recurse=True)
    280336        self.failUnless(isinstance(suite, self.loader.suiteFactory))
    281337        self.failUnlessEqual(14, suite.countTestCases())
    282338
     339
    283340    def test_loadAnythingOnString(self):
    284341        # the important thing about this test is not the string-iness
    285342        # but the non-handledness.
    286343        self.failUnlessRaises(TypeError,
    287344                              self.loader.loadAnything, "goodpackage")
    288345
     346
    289347    def test_importErrors(self):
    290348        import package
    291349        suite = self.loader.loadPackage(package, recurse=True)
     
    357415        names2.sort()
    358416        self.assertEqual(names1, names2)
    359417
     418
    360419    def test_loadByNamesDuplicate(self):
    361420        """
    362421        Check that loadByNames ignores duplicate names
     
    366425        suite2 = self.loader.loadByName(module, True)
    367426        self.assertSuitesEqual(suite1, suite2)
    368427
     428
    369429    def test_loadDifferentNames(self):
    370430        """
    371431        Check that loadByNames loads all the names that it is given
  • 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
     23
     24def good_decorator(fn):
     25    def name_collision(*args, **kwargs):
     26        return fn(*args, **kwargs)
     27    return mergeFunctionMetadata(fn, name_collision)
     28   
     29
     30class DecorationTest(unittest.TestCase):
     31    def test_bad_decorator(self):
     32        pass
     33    test_bad_decorator = bad_decorator(test_bad_decorator)
     34
     35    def test_good_decorator(self):
     36        pass
     37    test_good_decorator = good_decorator(test_good_decorator)
     38
     39    def renamed_decorator(self):
     40        pass
     41    test_renamed_decorator = good_decorator(renamed_decorator)
     42
     43    def name_collision(self):
     44        # not a test
     45        pass
     46
     47
    1748class PyunitTest(pyunit.TestCase):
    1849    def test_foo(self):
    1950        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)