summaryrefslogtreecommitdiff
path: root/Lib/test
diff options
context:
space:
mode:
authorChristian Heimes <christian@cheimes.de>2008-02-23 15:01:05 +0000
committerChristian Heimes <christian@cheimes.de>2008-02-23 15:01:05 +0000
commit5224d28d38eb784f17c2fed3f48368285df6d17a (patch)
treeb258202efc39ae5239d91dcf5b1898c6ce713f16 /Lib/test
parentb12f0b581a6f268d0611c87012d1273aeca220b8 (diff)
downloadcpython-git-5224d28d38eb784f17c2fed3f48368285df6d17a.tar.gz
Patch #1759: Backport of PEP 3129 class decorators
with some help from Georg
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_ast.py2
-rw-r--r--Lib/test/test_decorators.py36
-rw-r--r--Lib/test/test_grammar.py10
3 files changed, 47 insertions, 1 deletions
diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py
index 14fc01026f..03dd5b5193 100644
--- a/Lib/test/test_ast.py
+++ b/Lib/test/test_ast.py
@@ -156,7 +156,7 @@ def run_tests():
#### EVERYTHING BELOW IS GENERATED #####
exec_results = [
('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, None, []), [('Pass', (1, 9))], [])]),
-('Module', [('ClassDef', (1, 0), 'C', [], [('Pass', (1, 8))])]),
+('Module', [('ClassDef', (1, 0), 'C', [], [('Pass', (1, 8))], [])]),
('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, None, []), [('Return', (1, 8), ('Num', (1, 15), 1))], [])]),
('Module', [('Delete', (1, 0), [('Name', (1, 4), 'v', ('Del',))])]),
('Module', [('Assign', (1, 0), [('Name', (1, 0), 'v', ('Store',))], ('Num', (1, 4), 1))]),
diff --git a/Lib/test/test_decorators.py b/Lib/test/test_decorators.py
index 56aa5e1323..cf2bc3000c 100644
--- a/Lib/test/test_decorators.py
+++ b/Lib/test/test_decorators.py
@@ -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()
diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py
index d4bcfda017..e2bb3eb9e6 100644
--- a/Lib/test/test_grammar.py
+++ b/Lib/test/test_grammar.py
@@ -779,6 +779,16 @@ hello world
def meth1(self): pass
def meth2(self, arg): pass
def meth3(self, a1, a2): pass
+ # decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
+ # decorators: decorator+
+ # decorated: decorators (classdef | funcdef)
+ def class_decorator(x):
+ x.decorated = True
+ return x
+ @class_decorator
+ class G:
+ pass
+ self.assertEqual(G.decorated, True)
def testListcomps(self):
# list comprehension tests