diff genshi/template/tests/eval.py @ 586:3670ea49c65a trunk

Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
author cmlenz
date Wed, 08 Aug 2007 22:21:21 +0000
parents 0dc024715f93
children 37ff75bb4301
line wrap: on
line diff
--- a/genshi/template/tests/eval.py
+++ b/genshi/template/tests/eval.py
@@ -449,7 +449,8 @@
         self.assertEqual(None, data['donothing']())
 
     def test_def_with_multiple_statements(self):
-        suite = Suite("""def donothing():
+        suite = Suite("""
+def donothing():
     if True:
         return foo
 """)
@@ -458,6 +459,35 @@
         assert 'donothing' in data
         self.assertEqual('bar', data['donothing']())
 
+    def test_def_using_nonlocal(self):
+        suite = Suite("""
+values = []
+def add(value):
+    if value not in values:
+        values.append(value)
+add('foo')
+add('bar')
+""")
+        data = {}
+        suite.execute(data)
+        self.assertEqual(['foo', 'bar'], data['values'])
+
+    def test_def_nested(self):
+        suite = Suite("""
+def doit():
+    values = []
+    def add(value):
+        if value not in values:
+            values.append(value)
+    add('foo')
+    add('bar')
+    return values
+x = doit()
+""")
+        data = {}
+        suite.execute(data)
+        self.assertEqual(['foo', 'bar'], data['x'])
+
     def test_delete(self):
         suite = Suite("""foo = 42
 del foo
@@ -472,6 +502,19 @@
         suite.execute(data)
         assert 'plain' in data
 
+    def test_class_in_def(self):
+        suite = Suite("""
+def create():
+    class Foobar(object):
+        def __str__(self):
+            return 'foobar'
+    return Foobar()
+x = create()
+""")
+        data = {}
+        suite.execute(data)
+        self.assertEqual('foobar', str(data['x']))
+
     def test_class_with_methods(self):
         suite = Suite("""class plain(object):
     def donothing():
Copyright (C) 2012-2017 Edgewall Software