mirror of
https://github.com/python/cpython.git
synced 2025-07-30 06:34:15 +00:00
Patch #1759: Backport of PEP 3129 class decorators
with some help from Georg
This commit is contained in:
parent
b12f0b581a
commit
5224d28d38
15 changed files with 1591 additions and 1465 deletions
|
@ -266,8 +266,44 @@ class TestDecorators(unittest.TestCase):
|
|||
self.assertEqual(bar(), 42)
|
||||
self.assertEqual(actions, expected_actions)
|
||||
|
||||
class TestClassDecorators(unittest.TestCase):
|
||||
|
||||
def test_simple(self):
|
||||
def plain(x):
|
||||
x.extra = 'Hello'
|
||||
return x
|
||||
@plain
|
||||
class C(object): pass
|
||||
self.assertEqual(C.extra, 'Hello')
|
||||
|
||||
def test_double(self):
|
||||
def ten(x):
|
||||
x.extra = 10
|
||||
return x
|
||||
def add_five(x):
|
||||
x.extra += 5
|
||||
return x
|
||||
|
||||
@add_five
|
||||
@ten
|
||||
class C(object): pass
|
||||
self.assertEqual(C.extra, 15)
|
||||
|
||||
def test_order(self):
|
||||
def applied_first(x):
|
||||
x.extra = 'first'
|
||||
return x
|
||||
def applied_second(x):
|
||||
x.extra = 'second'
|
||||
return x
|
||||
@applied_second
|
||||
@applied_first
|
||||
class C(object): pass
|
||||
self.assertEqual(C.extra, 'second')
|
||||
|
||||
def test_main():
|
||||
test_support.run_unittest(TestDecorators)
|
||||
test_support.run_unittest(TestClassDecorators)
|
||||
|
||||
if __name__=="__main__":
|
||||
test_main()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue