annotate genshi/template/tests/eval.py @ 917:bcaa91c42b97 experimental-py3k

add support for python 3 to genshi.template expression evaluator: * add support for python 3 AST: * AST for raise has changed in Python 3. * Python 3 adds AST nodes for individual arguments and Bytes. * use genshi.compat functions for dealing with code objects. * do not coerce byte strings to unicode in Python 3 ASTTransformer. * replace doctests that reply on exception names with uglier but more compatible try:.. except:.. doctest * handle filename preferences of Python 2 and 3 (2 prefers bytes, 3 prefers unicode). * ifilter is gone from itertools in Python 3 so use repeat for tests instead.
author hodgestar
date Sun, 24 Oct 2010 22:39:08 +0000
parents 85e4678337cf
children
rev   line source
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
2 #
897
85e4678337cf Update changelog and copyright years.
cmlenz
parents: 896
diff changeset
3 # Copyright (C) 2006-2010 Edgewall Software
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
4 # All rights reserved.
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
5 #
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
8 # are also available at http://genshi.edgewall.org/wiki/License.
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
9 #
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
12 # history and logs, available at http://genshi.edgewall.org/log/.
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
13
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
14 import doctest
867
efc03d0719f3 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
15 import os
715
09715f868a73 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 702
diff changeset
16 import pickle
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
17 import sys
868
9309530ee279 Fix the unit test for execution of statements containing with statements so that it also works on Windows. Thanks to cboos for the patch.
cmlenz
parents: 867
diff changeset
18 from tempfile import mkstemp
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
19 import unittest
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
20
401
9582328f82e5 Make the `Markup` class available by default in template expressions. Closes #67.
cmlenz
parents: 396
diff changeset
21 from genshi.core import Markup
750
d007a0d7ba81 Remove some cruft for supporting Python 2.3.
cmlenz
parents: 739
diff changeset
22 from genshi.template.base import Context
442
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
23 from genshi.template.eval import Expression, Suite, Undefined, UndefinedError, \
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
24 UNDEFINED
917
bcaa91c42b97 add support for python 3 to genshi.template expression evaluator:
hodgestar
parents: 897
diff changeset
25 from genshi.compat import BytesIO, IS_PYTHON2, wrapped_bytes
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
26
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
27
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
28 class ExpressionTestCase(unittest.TestCase):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
29
340
6c8b7a1fb50d Make expressions hashable.
cmlenz
parents: 336
diff changeset
30 def test_eq(self):
6c8b7a1fb50d Make expressions hashable.
cmlenz
parents: 336
diff changeset
31 expr = Expression('x,y')
6c8b7a1fb50d Make expressions hashable.
cmlenz
parents: 336
diff changeset
32 self.assertEqual(expr, Expression('x,y'))
6c8b7a1fb50d Make expressions hashable.
cmlenz
parents: 336
diff changeset
33 self.assertNotEqual(expr, Expression('y, x'))
6c8b7a1fb50d Make expressions hashable.
cmlenz
parents: 336
diff changeset
34
6c8b7a1fb50d Make expressions hashable.
cmlenz
parents: 336
diff changeset
35 def test_hash(self):
6c8b7a1fb50d Make expressions hashable.
cmlenz
parents: 336
diff changeset
36 expr = Expression('x,y')
6c8b7a1fb50d Make expressions hashable.
cmlenz
parents: 336
diff changeset
37 self.assertEqual(hash(expr), hash(Expression('x,y')))
6c8b7a1fb50d Make expressions hashable.
cmlenz
parents: 336
diff changeset
38 self.assertNotEqual(hash(expr), hash(Expression('y, x')))
6c8b7a1fb50d Make expressions hashable.
cmlenz
parents: 336
diff changeset
39
715
09715f868a73 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 702
diff changeset
40 def test_pickle(self):
09715f868a73 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 702
diff changeset
41 expr = Expression('1 < 2')
917
bcaa91c42b97 add support for python 3 to genshi.template expression evaluator:
hodgestar
parents: 897
diff changeset
42 buf = BytesIO()
715
09715f868a73 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 702
diff changeset
43 pickle.dump(expr, buf, 2)
09715f868a73 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 702
diff changeset
44 buf.seek(0)
09715f868a73 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 702
diff changeset
45 unpickled = pickle.load(buf)
09715f868a73 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 702
diff changeset
46 assert unpickled.evaluate({}) is True
09715f868a73 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 702
diff changeset
47
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
48 def test_name_lookup(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
49 self.assertEqual('bar', Expression('foo').evaluate({'foo': 'bar'}))
343
4ff2338e89cd Remove automatic calling of expression evaluation results if they are callable. See [http://groups.google.com/group/genshi/browse_thread/thread/f515986760918d41 this mailing list thread].
cmlenz
parents: 340
diff changeset
50 self.assertEqual(id, Expression('id').evaluate({}))
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
51 self.assertEqual('bar', Expression('id').evaluate({'id': 'bar'}))
343
4ff2338e89cd Remove automatic calling of expression evaluation results if they are callable. See [http://groups.google.com/group/genshi/browse_thread/thread/f515986760918d41 this mailing list thread].
cmlenz
parents: 340
diff changeset
52 self.assertEqual(None, Expression('id').evaluate({'id': None}))
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
53
401
9582328f82e5 Make the `Markup` class available by default in template expressions. Closes #67.
cmlenz
parents: 396
diff changeset
54 def test_builtins(self):
9582328f82e5 Make the `Markup` class available by default in template expressions. Closes #67.
cmlenz
parents: 396
diff changeset
55 expr = Expression('Markup')
9582328f82e5 Make the `Markup` class available by default in template expressions. Closes #67.
cmlenz
parents: 396
diff changeset
56 self.assertEqual(expr.evaluate({}), Markup)
9582328f82e5 Make the `Markup` class available by default in template expressions. Closes #67.
cmlenz
parents: 396
diff changeset
57
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
58 def test_str_literal(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
59 self.assertEqual('foo', Expression('"foo"').evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
60 self.assertEqual('foo', Expression('"""foo"""').evaluate({}))
917
bcaa91c42b97 add support for python 3 to genshi.template expression evaluator:
hodgestar
parents: 897
diff changeset
61 self.assertEqual(u'foo'.encode('utf-8'),
bcaa91c42b97 add support for python 3 to genshi.template expression evaluator:
hodgestar
parents: 897
diff changeset
62 Expression(wrapped_bytes("b'foo'")).evaluate({}))
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
63 self.assertEqual('foo', Expression("'''foo'''").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
64 self.assertEqual('foo', Expression("u'foo'").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
65 self.assertEqual('foo', Expression("r'foo'").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
66
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
67 def test_str_literal_non_ascii(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
68 expr = Expression(u"u'\xfe'")
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
69 self.assertEqual(u'þ', expr.evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
70 expr = Expression("u'\xfe'")
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
71 self.assertEqual(u'þ', expr.evaluate({}))
917
bcaa91c42b97 add support for python 3 to genshi.template expression evaluator:
hodgestar
parents: 897
diff changeset
72 # On Python2 strings are converted to unicode if they contained
bcaa91c42b97 add support for python 3 to genshi.template expression evaluator:
hodgestar
parents: 897
diff changeset
73 # non-ASCII characters.
bcaa91c42b97 add support for python 3 to genshi.template expression evaluator:
hodgestar
parents: 897
diff changeset
74 # On Py3k, we have no need to do this as non-prefixed strings aren't
bcaa91c42b97 add support for python 3 to genshi.template expression evaluator:
hodgestar
parents: 897
diff changeset
75 # raw.
bcaa91c42b97 add support for python 3 to genshi.template expression evaluator:
hodgestar
parents: 897
diff changeset
76 expr = Expression(wrapped_bytes(r"b'\xc3\xbe'"))
bcaa91c42b97 add support for python 3 to genshi.template expression evaluator:
hodgestar
parents: 897
diff changeset
77 if IS_PYTHON2:
bcaa91c42b97 add support for python 3 to genshi.template expression evaluator:
hodgestar
parents: 897
diff changeset
78 self.assertEqual(u'þ', expr.evaluate({}))
bcaa91c42b97 add support for python 3 to genshi.template expression evaluator:
hodgestar
parents: 897
diff changeset
79 else:
bcaa91c42b97 add support for python 3 to genshi.template expression evaluator:
hodgestar
parents: 897
diff changeset
80 self.assertEqual(u'þ'.encode('utf-8'), expr.evaluate({}))
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
81
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
82 def test_num_literal(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
83 self.assertEqual(42, Expression("42").evaluate({}))
917
bcaa91c42b97 add support for python 3 to genshi.template expression evaluator:
hodgestar
parents: 897
diff changeset
84 if IS_PYTHON2:
bcaa91c42b97 add support for python 3 to genshi.template expression evaluator:
hodgestar
parents: 897
diff changeset
85 self.assertEqual(42L, Expression("42L").evaluate({}))
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
86 self.assertEqual(.42, Expression(".42").evaluate({}))
917
bcaa91c42b97 add support for python 3 to genshi.template expression evaluator:
hodgestar
parents: 897
diff changeset
87 if IS_PYTHON2:
bcaa91c42b97 add support for python 3 to genshi.template expression evaluator:
hodgestar
parents: 897
diff changeset
88 self.assertEqual(07, Expression("07").evaluate({}))
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
89 self.assertEqual(0xF2, Expression("0xF2").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
90 self.assertEqual(0XF2, Expression("0XF2").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
91
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
92 def test_dict_literal(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
93 self.assertEqual({}, Expression("{}").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
94 self.assertEqual({'key': True},
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
95 Expression("{'key': value}").evaluate({'value': True}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
96
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
97 def test_list_literal(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
98 self.assertEqual([], Expression("[]").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
99 self.assertEqual([1, 2, 3], Expression("[1, 2, 3]").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
100 self.assertEqual([True],
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
101 Expression("[value]").evaluate({'value': True}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
102
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
103 def test_tuple_literal(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
104 self.assertEqual((), Expression("()").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
105 self.assertEqual((1, 2, 3), Expression("(1, 2, 3)").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
106 self.assertEqual((True,),
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
107 Expression("(value,)").evaluate({'value': True}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
108
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
109 def test_unaryop_pos(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
110 self.assertEqual(1, Expression("+1").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
111 self.assertEqual(1, Expression("+x").evaluate({'x': 1}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
112
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
113 def test_unaryop_neg(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
114 self.assertEqual(-1, Expression("-1").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
115 self.assertEqual(-1, Expression("-x").evaluate({'x': 1}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
116
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
117 def test_unaryop_not(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
118 self.assertEqual(False, Expression("not True").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
119 self.assertEqual(False, Expression("not x").evaluate({'x': True}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
120
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
121 def test_unaryop_inv(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
122 self.assertEqual(-2, Expression("~1").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
123 self.assertEqual(-2, Expression("~x").evaluate({'x': 1}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
124
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
125 def test_binop_add(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
126 self.assertEqual(3, Expression("2 + 1").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
127 self.assertEqual(3, Expression("x + y").evaluate({'x': 2, 'y': 1}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
128
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
129 def test_binop_sub(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
130 self.assertEqual(1, Expression("2 - 1").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
131 self.assertEqual(1, Expression("x - y").evaluate({'x': 1, 'y': 1}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
132
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
133 def test_binop_sub(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
134 self.assertEqual(1, Expression("2 - 1").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
135 self.assertEqual(1, Expression("x - y").evaluate({'x': 2, 'y': 1}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
136
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
137 def test_binop_mul(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
138 self.assertEqual(4, Expression("2 * 2").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
139 self.assertEqual(4, Expression("x * y").evaluate({'x': 2, 'y': 2}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
140
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
141 def test_binop_pow(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
142 self.assertEqual(4, Expression("2 ** 2").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
143 self.assertEqual(4, Expression("x ** y").evaluate({'x': 2, 'y': 2}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
144
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
145 def test_binop_div(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
146 self.assertEqual(2, Expression("4 / 2").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
147 self.assertEqual(2, Expression("x / y").evaluate({'x': 4, 'y': 2}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
148
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
149 def test_binop_floordiv(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
150 self.assertEqual(1, Expression("3 // 2").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
151 self.assertEqual(1, Expression("x // y").evaluate({'x': 3, 'y': 2}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
152
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
153 def test_binop_mod(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
154 self.assertEqual(1, Expression("3 % 2").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
155 self.assertEqual(1, Expression("x % y").evaluate({'x': 3, 'y': 2}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
156
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
157 def test_binop_and(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
158 self.assertEqual(0, Expression("1 & 0").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
159 self.assertEqual(0, Expression("x & y").evaluate({'x': 1, 'y': 0}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
160
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
161 def test_binop_or(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
162 self.assertEqual(1, Expression("1 | 0").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
163 self.assertEqual(1, Expression("x | y").evaluate({'x': 1, 'y': 0}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
164
396
22a581cfa537 add visitor for xor operator
mgood
parents: 393
diff changeset
165 def test_binop_xor(self):
22a581cfa537 add visitor for xor operator
mgood
parents: 393
diff changeset
166 self.assertEqual(1, Expression("1 ^ 0").evaluate({}))
22a581cfa537 add visitor for xor operator
mgood
parents: 393
diff changeset
167 self.assertEqual(1, Expression("x ^ y").evaluate({'x': 1, 'y': 0}))
22a581cfa537 add visitor for xor operator
mgood
parents: 393
diff changeset
168
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
169 def test_binop_contains(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
170 self.assertEqual(True, Expression("1 in (1, 2, 3)").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
171 self.assertEqual(True, Expression("x in y").evaluate({'x': 1,
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
172 'y': (1, 2, 3)}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
173
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
174 def test_binop_not_contains(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
175 self.assertEqual(True, Expression("4 not in (1, 2, 3)").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
176 self.assertEqual(True, Expression("x not in y").evaluate({'x': 4,
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
177 'y': (1, 2, 3)}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
178
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
179 def test_binop_is(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
180 self.assertEqual(True, Expression("1 is 1").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
181 self.assertEqual(True, Expression("x is y").evaluate({'x': 1, 'y': 1}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
182 self.assertEqual(False, Expression("1 is 2").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
183 self.assertEqual(False, Expression("x is y").evaluate({'x': 1, 'y': 2}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
184
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
185 def test_binop_is_not(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
186 self.assertEqual(True, Expression("1 is not 2").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
187 self.assertEqual(True, Expression("x is not y").evaluate({'x': 1,
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
188 'y': 2}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
189 self.assertEqual(False, Expression("1 is not 1").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
190 self.assertEqual(False, Expression("x is not y").evaluate({'x': 1,
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
191 'y': 1}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
192
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
193 def test_boolop_and(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
194 self.assertEqual(False, Expression("True and False").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
195 self.assertEqual(False, Expression("x and y").evaluate({'x': True,
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
196 'y': False}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
197
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
198 def test_boolop_or(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
199 self.assertEqual(True, Expression("True or False").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
200 self.assertEqual(True, Expression("x or y").evaluate({'x': True,
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
201 'y': False}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
202
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
203 def test_compare_eq(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
204 self.assertEqual(True, Expression("1 == 1").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
205 self.assertEqual(True, Expression("x == y").evaluate({'x': 1, 'y': 1}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
206
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
207 def test_compare_ne(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
208 self.assertEqual(False, Expression("1 != 1").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
209 self.assertEqual(False, Expression("x != y").evaluate({'x': 1, 'y': 1}))
793
7cf2407671c2 Get rid of a couple more -3 warnings.
cmlenz
parents: 750
diff changeset
210 if sys.version < '3':
7cf2407671c2 Get rid of a couple more -3 warnings.
cmlenz
parents: 750
diff changeset
211 self.assertEqual(False, Expression("1 <> 1").evaluate({}))
7cf2407671c2 Get rid of a couple more -3 warnings.
cmlenz
parents: 750
diff changeset
212 self.assertEqual(False, Expression("x <> y").evaluate({'x': 1, 'y': 1}))
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
213
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
214 def test_compare_lt(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
215 self.assertEqual(True, Expression("1 < 2").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
216 self.assertEqual(True, Expression("x < y").evaluate({'x': 1, 'y': 2}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
217
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
218 def test_compare_le(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
219 self.assertEqual(True, Expression("1 <= 1").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
220 self.assertEqual(True, Expression("x <= y").evaluate({'x': 1, 'y': 1}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
221
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
222 def test_compare_gt(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
223 self.assertEqual(True, Expression("2 > 1").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
224 self.assertEqual(True, Expression("x > y").evaluate({'x': 2, 'y': 1}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
225
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
226 def test_compare_ge(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
227 self.assertEqual(True, Expression("1 >= 1").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
228 self.assertEqual(True, Expression("x >= y").evaluate({'x': 1, 'y': 1}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
229
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
230 def test_compare_multi(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
231 self.assertEqual(True, Expression("1 != 3 == 3").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
232 self.assertEqual(True, Expression("x != y == y").evaluate({'x': 1,
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
233 'y': 3}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
234
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
235 def test_call_function(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
236 self.assertEqual(42, Expression("foo()").evaluate({'foo': lambda: 42}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
237 data = {'foo': 'bar'}
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
238 self.assertEqual('BAR', Expression("foo.upper()").evaluate(data))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
239 data = {'foo': {'bar': range(42)}}
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
240 self.assertEqual(42, Expression("len(foo.bar)").evaluate(data))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
241
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
242 def test_call_keywords(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
243 self.assertEqual(42, Expression("foo(x=bar)").evaluate({'foo': lambda x: x,
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
244 'bar': 42}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
245
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
246 def test_call_star_args(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
247 self.assertEqual(42, Expression("foo(*bar)").evaluate({'foo': lambda x: x,
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
248 'bar': [42]}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
249
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
250 def test_call_dstar_args(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
251 def foo(x):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
252 return x
343
4ff2338e89cd Remove automatic calling of expression evaluation results if they are callable. See [http://groups.google.com/group/genshi/browse_thread/thread/f515986760918d41 this mailing list thread].
cmlenz
parents: 340
diff changeset
253 expr = Expression("foo(**bar)")
4ff2338e89cd Remove automatic calling of expression evaluation results if they are callable. See [http://groups.google.com/group/genshi/browse_thread/thread/f515986760918d41 this mailing list thread].
cmlenz
parents: 340
diff changeset
254 self.assertEqual(42, expr.evaluate({'foo': foo, 'bar': {"x": 42}}))
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
255
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
256 def test_lambda(self):
793
7cf2407671c2 Get rid of a couple more -3 warnings.
cmlenz
parents: 750
diff changeset
257 data = {'items': range(5)}
7cf2407671c2 Get rid of a couple more -3 warnings.
cmlenz
parents: 750
diff changeset
258 expr = Expression("filter(lambda x: x > 2, items)")
917
bcaa91c42b97 add support for python 3 to genshi.template expression evaluator:
hodgestar
parents: 897
diff changeset
259 self.assertEqual([3, 4], list(expr.evaluate(data)))
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
260
896
248d6671a7e1 Fix for infinite recursion when parsing argument names from tuples. Closes #383.
cmlenz
parents: 879
diff changeset
261 def test_lambda_tuple_arg(self):
917
bcaa91c42b97 add support for python 3 to genshi.template expression evaluator:
hodgestar
parents: 897
diff changeset
262 # This syntax goes away in Python 3
bcaa91c42b97 add support for python 3 to genshi.template expression evaluator:
hodgestar
parents: 897
diff changeset
263 if not IS_PYTHON2:
bcaa91c42b97 add support for python 3 to genshi.template expression evaluator:
hodgestar
parents: 897
diff changeset
264 return
896
248d6671a7e1 Fix for infinite recursion when parsing argument names from tuples. Closes #383.
cmlenz
parents: 879
diff changeset
265 data = {'items': [(1, 2), (2, 1)]}
248d6671a7e1 Fix for infinite recursion when parsing argument names from tuples. Closes #383.
cmlenz
parents: 879
diff changeset
266 expr = Expression("filter(lambda (x, y): x > y, items)")
917
bcaa91c42b97 add support for python 3 to genshi.template expression evaluator:
hodgestar
parents: 897
diff changeset
267 self.assertEqual([(2, 1)], list(expr.evaluate(data)))
896
248d6671a7e1 Fix for infinite recursion when parsing argument names from tuples. Closes #383.
cmlenz
parents: 879
diff changeset
268
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
269 def test_list_comprehension(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
270 expr = Expression("[n for n in numbers if n < 2]")
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
271 self.assertEqual([0, 1], expr.evaluate({'numbers': range(5)}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
272
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
273 expr = Expression("[(i, n + 1) for i, n in enumerate(numbers)]")
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
274 self.assertEqual([(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)],
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
275 expr.evaluate({'numbers': range(5)}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
276
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
277 expr = Expression("[offset + n for n in numbers]")
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
278 self.assertEqual([2, 3, 4, 5, 6],
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
279 expr.evaluate({'numbers': range(5), 'offset': 2}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
280
845
9cabfbc4ac5c Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents: 807
diff changeset
281 expr = Expression("[n for group in groups for n in group]")
9cabfbc4ac5c Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents: 807
diff changeset
282 self.assertEqual([0, 1, 0, 1, 2],
9cabfbc4ac5c Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents: 807
diff changeset
283 expr.evaluate({'groups': [range(2), range(3)]}))
9cabfbc4ac5c Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents: 807
diff changeset
284
9cabfbc4ac5c Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents: 807
diff changeset
285 expr = Expression("[(a, b) for a in x for b in y]")
9cabfbc4ac5c Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents: 807
diff changeset
286 self.assertEqual([('x0', 'y0'), ('x0', 'y1'), ('x1', 'y0'), ('x1', 'y1')],
9cabfbc4ac5c Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents: 807
diff changeset
287 expr.evaluate({'x': ['x0', 'x1'], 'y': ['y0', 'y1']}))
9cabfbc4ac5c Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents: 807
diff changeset
288
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
289 def test_list_comprehension_with_getattr(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
290 items = [{'name': 'a', 'value': 1}, {'name': 'b', 'value': 2}]
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
291 expr = Expression("[i.name for i in items if i.value > 1]")
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
292 self.assertEqual(['b'], expr.evaluate({'items': items}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
293
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
294 def test_list_comprehension_with_getitem(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
295 items = [{'name': 'a', 'value': 1}, {'name': 'b', 'value': 2}]
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
296 expr = Expression("[i['name'] for i in items if i['value'] > 1]")
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
297 self.assertEqual(['b'], expr.evaluate({'items': items}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
298
750
d007a0d7ba81 Remove some cruft for supporting Python 2.3.
cmlenz
parents: 739
diff changeset
299 def test_generator_expression(self):
d007a0d7ba81 Remove some cruft for supporting Python 2.3.
cmlenz
parents: 739
diff changeset
300 expr = Expression("list(n for n in numbers if n < 2)")
d007a0d7ba81 Remove some cruft for supporting Python 2.3.
cmlenz
parents: 739
diff changeset
301 self.assertEqual([0, 1], expr.evaluate({'numbers': range(5)}))
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
302
750
d007a0d7ba81 Remove some cruft for supporting Python 2.3.
cmlenz
parents: 739
diff changeset
303 expr = Expression("list((i, n + 1) for i, n in enumerate(numbers))")
d007a0d7ba81 Remove some cruft for supporting Python 2.3.
cmlenz
parents: 739
diff changeset
304 self.assertEqual([(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)],
d007a0d7ba81 Remove some cruft for supporting Python 2.3.
cmlenz
parents: 739
diff changeset
305 expr.evaluate({'numbers': range(5)}))
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
306
750
d007a0d7ba81 Remove some cruft for supporting Python 2.3.
cmlenz
parents: 739
diff changeset
307 expr = Expression("list(offset + n for n in numbers)")
d007a0d7ba81 Remove some cruft for supporting Python 2.3.
cmlenz
parents: 739
diff changeset
308 self.assertEqual([2, 3, 4, 5, 6],
d007a0d7ba81 Remove some cruft for supporting Python 2.3.
cmlenz
parents: 739
diff changeset
309 expr.evaluate({'numbers': range(5), 'offset': 2}))
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
310
845
9cabfbc4ac5c Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents: 807
diff changeset
311 expr = Expression("list(n for group in groups for n in group)")
9cabfbc4ac5c Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents: 807
diff changeset
312 self.assertEqual([0, 1, 0, 1, 2],
9cabfbc4ac5c Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents: 807
diff changeset
313 expr.evaluate({'groups': [range(2), range(3)]}))
9cabfbc4ac5c Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents: 807
diff changeset
314
9cabfbc4ac5c Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents: 807
diff changeset
315 expr = Expression("list((a, b) for a in x for b in y)")
9cabfbc4ac5c Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents: 807
diff changeset
316 self.assertEqual([('x0', 'y0'), ('x0', 'y1'), ('x1', 'y0'), ('x1', 'y1')],
9cabfbc4ac5c Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents: 807
diff changeset
317 expr.evaluate({'x': ['x0', 'x1'], 'y': ['y0', 'y1']}))
9cabfbc4ac5c Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents: 807
diff changeset
318
750
d007a0d7ba81 Remove some cruft for supporting Python 2.3.
cmlenz
parents: 739
diff changeset
319 def test_generator_expression_with_getattr(self):
d007a0d7ba81 Remove some cruft for supporting Python 2.3.
cmlenz
parents: 739
diff changeset
320 items = [{'name': 'a', 'value': 1}, {'name': 'b', 'value': 2}]
d007a0d7ba81 Remove some cruft for supporting Python 2.3.
cmlenz
parents: 739
diff changeset
321 expr = Expression("list(i.name for i in items if i.value > 1)")
d007a0d7ba81 Remove some cruft for supporting Python 2.3.
cmlenz
parents: 739
diff changeset
322 self.assertEqual(['b'], expr.evaluate({'items': items}))
d007a0d7ba81 Remove some cruft for supporting Python 2.3.
cmlenz
parents: 739
diff changeset
323
d007a0d7ba81 Remove some cruft for supporting Python 2.3.
cmlenz
parents: 739
diff changeset
324 def test_generator_expression_with_getitem(self):
d007a0d7ba81 Remove some cruft for supporting Python 2.3.
cmlenz
parents: 739
diff changeset
325 items = [{'name': 'a', 'value': 1}, {'name': 'b', 'value': 2}]
d007a0d7ba81 Remove some cruft for supporting Python 2.3.
cmlenz
parents: 739
diff changeset
326 expr = Expression("list(i['name'] for i in items if i['value'] > 1)")
d007a0d7ba81 Remove some cruft for supporting Python 2.3.
cmlenz
parents: 739
diff changeset
327 self.assertEqual(['b'], expr.evaluate({'items': items}))
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
328
393
a056613621ab add support for Python 2.5 conditional expressions (fixes #74)
mgood
parents: 343
diff changeset
329 if sys.version_info >= (2, 5):
a056613621ab add support for Python 2.5 conditional expressions (fixes #74)
mgood
parents: 343
diff changeset
330 def test_conditional_expression(self):
a056613621ab add support for Python 2.5 conditional expressions (fixes #74)
mgood
parents: 343
diff changeset
331 expr = Expression("'T' if foo else 'F'")
a056613621ab add support for Python 2.5 conditional expressions (fixes #74)
mgood
parents: 343
diff changeset
332 self.assertEqual('T', expr.evaluate({'foo': True}))
a056613621ab add support for Python 2.5 conditional expressions (fixes #74)
mgood
parents: 343
diff changeset
333 self.assertEqual('F', expr.evaluate({'foo': False}))
a056613621ab add support for Python 2.5 conditional expressions (fixes #74)
mgood
parents: 343
diff changeset
334
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
335 def test_slice(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
336 expr = Expression("numbers[0:2]")
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
337 self.assertEqual([0, 1], expr.evaluate({'numbers': range(5)}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
338
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
339 def test_slice_with_vars(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
340 expr = Expression("numbers[start:end]")
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
341 self.assertEqual([0, 1], expr.evaluate({'numbers': range(5), 'start': 0,
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
342 'end': 2}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
343
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
344 def test_slice_copy(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
345 expr = Expression("numbers[:]")
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
346 self.assertEqual([0, 1, 2, 3, 4], expr.evaluate({'numbers': range(5)}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
347
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
348 def test_slice_stride(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
349 expr = Expression("numbers[::stride]")
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
350 self.assertEqual([0, 2, 4], expr.evaluate({'numbers': range(5),
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
351 'stride': 2}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
352
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
353 def test_slice_negative_start(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
354 expr = Expression("numbers[-1:]")
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
355 self.assertEqual([4], expr.evaluate({'numbers': range(5)}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
356
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
357 def test_slice_negative_end(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
358 expr = Expression("numbers[:-1]")
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
359 self.assertEqual([0, 1, 2, 3], expr.evaluate({'numbers': range(5)}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
360
442
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
361 def test_access_undefined(self):
606
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 586
diff changeset
362 expr = Expression("nothing", filename='index.html', lineno=50,
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 586
diff changeset
363 lookup='lenient')
442
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
364 retval = expr.evaluate({})
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
365 assert isinstance(retval, Undefined)
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
366 self.assertEqual('nothing', retval._name)
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
367 assert retval._owner is UNDEFINED
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
368
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
369 def test_getattr_undefined(self):
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
370 class Something(object):
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
371 def __repr__(self):
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
372 return '<Something>'
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
373 something = Something()
606
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 586
diff changeset
374 expr = Expression('something.nil', filename='index.html', lineno=50,
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 586
diff changeset
375 lookup='lenient')
442
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
376 retval = expr.evaluate({'something': something})
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
377 assert isinstance(retval, Undefined)
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
378 self.assertEqual('nil', retval._name)
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
379 assert retval._owner is something
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
380
569
4cbd8031ed76 Attribute access in template expressions no longer silently ignores exceptions other than `AttributeError` raised in the attribute accessor.
cmlenz
parents: 568
diff changeset
381 def test_getattr_exception(self):
4cbd8031ed76 Attribute access in template expressions no longer silently ignores exceptions other than `AttributeError` raised in the attribute accessor.
cmlenz
parents: 568
diff changeset
382 class Something(object):
702
ebae1774d188 Improve error reporting when accessing an attribute in a Python expression raises an `AttributeError`. Closes #191. Thanks to Michele Cella for the patch!
cmlenz
parents: 682
diff changeset
383 def prop_a(self):
569
4cbd8031ed76 Attribute access in template expressions no longer silently ignores exceptions other than `AttributeError` raised in the attribute accessor.
cmlenz
parents: 568
diff changeset
384 raise NotImplementedError
702
ebae1774d188 Improve error reporting when accessing an attribute in a Python expression raises an `AttributeError`. Closes #191. Thanks to Michele Cella for the patch!
cmlenz
parents: 682
diff changeset
385 prop_a = property(prop_a)
ebae1774d188 Improve error reporting when accessing an attribute in a Python expression raises an `AttributeError`. Closes #191. Thanks to Michele Cella for the patch!
cmlenz
parents: 682
diff changeset
386 def prop_b(self):
ebae1774d188 Improve error reporting when accessing an attribute in a Python expression raises an `AttributeError`. Closes #191. Thanks to Michele Cella for the patch!
cmlenz
parents: 682
diff changeset
387 raise AttributeError
ebae1774d188 Improve error reporting when accessing an attribute in a Python expression raises an `AttributeError`. Closes #191. Thanks to Michele Cella for the patch!
cmlenz
parents: 682
diff changeset
388 prop_b = property(prop_b)
569
4cbd8031ed76 Attribute access in template expressions no longer silently ignores exceptions other than `AttributeError` raised in the attribute accessor.
cmlenz
parents: 568
diff changeset
389 self.assertRaises(NotImplementedError,
702
ebae1774d188 Improve error reporting when accessing an attribute in a Python expression raises an `AttributeError`. Closes #191. Thanks to Michele Cella for the patch!
cmlenz
parents: 682
diff changeset
390 Expression('s.prop_a').evaluate, {'s': Something()})
ebae1774d188 Improve error reporting when accessing an attribute in a Python expression raises an `AttributeError`. Closes #191. Thanks to Michele Cella for the patch!
cmlenz
parents: 682
diff changeset
391 self.assertRaises(AttributeError,
ebae1774d188 Improve error reporting when accessing an attribute in a Python expression raises an `AttributeError`. Closes #191. Thanks to Michele Cella for the patch!
cmlenz
parents: 682
diff changeset
392 Expression('s.prop_b').evaluate, {'s': Something()})
569
4cbd8031ed76 Attribute access in template expressions no longer silently ignores exceptions other than `AttributeError` raised in the attribute accessor.
cmlenz
parents: 568
diff changeset
393
442
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
394 def test_getitem_undefined_string(self):
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
395 class Something(object):
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
396 def __repr__(self):
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
397 return '<Something>'
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
398 something = Something()
606
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 586
diff changeset
399 expr = Expression('something["nil"]', filename='index.html', lineno=50,
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 586
diff changeset
400 lookup='lenient')
442
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
401 retval = expr.evaluate({'something': something})
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
402 assert isinstance(retval, Undefined)
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
403 self.assertEqual('nil', retval._name)
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
404 assert retval._owner is something
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
405
569
4cbd8031ed76 Attribute access in template expressions no longer silently ignores exceptions other than `AttributeError` raised in the attribute accessor.
cmlenz
parents: 568
diff changeset
406 def test_getitem_exception(self):
4cbd8031ed76 Attribute access in template expressions no longer silently ignores exceptions other than `AttributeError` raised in the attribute accessor.
cmlenz
parents: 568
diff changeset
407 class Something(object):
4cbd8031ed76 Attribute access in template expressions no longer silently ignores exceptions other than `AttributeError` raised in the attribute accessor.
cmlenz
parents: 568
diff changeset
408 def __getitem__(self, key):
4cbd8031ed76 Attribute access in template expressions no longer silently ignores exceptions other than `AttributeError` raised in the attribute accessor.
cmlenz
parents: 568
diff changeset
409 raise NotImplementedError
4cbd8031ed76 Attribute access in template expressions no longer silently ignores exceptions other than `AttributeError` raised in the attribute accessor.
cmlenz
parents: 568
diff changeset
410 self.assertRaises(NotImplementedError,
4cbd8031ed76 Attribute access in template expressions no longer silently ignores exceptions other than `AttributeError` raised in the attribute accessor.
cmlenz
parents: 568
diff changeset
411 Expression('s["foo"]').evaluate, {'s': Something()})
4cbd8031ed76 Attribute access in template expressions no longer silently ignores exceptions other than `AttributeError` raised in the attribute accessor.
cmlenz
parents: 568
diff changeset
412
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
413 def test_error_access_undefined(self):
442
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
414 expr = Expression("nothing", filename='index.html', lineno=50,
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
415 lookup='strict')
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
416 try:
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
417 expr.evaluate({})
418
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
418 self.fail('Expected UndefinedError')
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
419 except UndefinedError, e:
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
420 exc_type, exc_value, exc_traceback = sys.exc_info()
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
421 frame = exc_traceback.tb_next
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
422 frames = []
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
423 while frame.tb_next:
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
424 frame = frame.tb_next
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
425 frames.append(frame)
418
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
426 self.assertEqual('"nothing" not defined', str(e))
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
427 self.assertEqual("<Expression 'nothing'>",
442
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
428 frames[-3].tb_frame.f_code.co_name)
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
429 self.assertEqual('index.html',
442
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
430 frames[-3].tb_frame.f_code.co_filename)
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
431 self.assertEqual(50, frames[-3].tb_lineno)
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
432
418
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
433 def test_error_getattr_undefined(self):
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
434 class Something(object):
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
435 def __repr__(self):
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
436 return '<Something>'
442
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
437 expr = Expression('something.nil', filename='index.html', lineno=50,
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
438 lookup='strict')
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
439 try:
418
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
440 expr.evaluate({'something': Something()})
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
441 self.fail('Expected UndefinedError')
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
442 except UndefinedError, e:
473
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
443 self.assertEqual('<Something> has no member named "nil"', str(e))
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
444 exc_type, exc_value, exc_traceback = sys.exc_info()
473
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
445 search_string = "<Expression 'something.nil'>"
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
446 frame = exc_traceback.tb_next
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
447 while frame.tb_next:
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
448 frame = frame.tb_next
473
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
449 code = frame.tb_frame.f_code
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
450 if code.co_name == search_string:
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
451 break
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
452 else:
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
453 self.fail("never found the frame I was looking for")
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
454 self.assertEqual('index.html', code.co_filename)
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
455 self.assertEqual(50, frame.tb_lineno)
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
456
418
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
457 def test_error_getitem_undefined_string(self):
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
458 class Something(object):
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
459 def __repr__(self):
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
460 return '<Something>'
442
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
461 expr = Expression('something["nil"]', filename='index.html', lineno=50,
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
462 lookup='strict')
418
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
463 try:
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
464 expr.evaluate({'something': Something()})
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
465 self.fail('Expected UndefinedError')
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
466 except UndefinedError, e:
473
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
467 self.assertEqual('<Something> has no member named "nil"', str(e))
418
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
468 exc_type, exc_value, exc_traceback = sys.exc_info()
473
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
469 search_string = '''<Expression 'something["nil"]'>'''
418
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
470 frame = exc_traceback.tb_next
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
471 while frame.tb_next:
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
472 frame = frame.tb_next
473
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
473 code = frame.tb_frame.f_code
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
474 if code.co_name == search_string:
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
475 break
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
476 else:
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
477 self.fail("never found the frame I was looking for")
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
478 self.assertEqual('index.html', code.co_filename)
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
479 self.assertEqual(50, frame.tb_lineno)
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
480
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
481
405
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
482 class SuiteTestCase(unittest.TestCase):
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
483
715
09715f868a73 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 702
diff changeset
484 def test_pickle(self):
09715f868a73 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 702
diff changeset
485 suite = Suite('foo = 42')
917
bcaa91c42b97 add support for python 3 to genshi.template expression evaluator:
hodgestar
parents: 897
diff changeset
486 buf = BytesIO()
715
09715f868a73 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 702
diff changeset
487 pickle.dump(suite, buf, 2)
09715f868a73 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 702
diff changeset
488 buf.seek(0)
09715f868a73 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 702
diff changeset
489 unpickled = pickle.load(buf)
09715f868a73 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 702
diff changeset
490 data = {}
09715f868a73 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 702
diff changeset
491 unpickled.execute(data)
09715f868a73 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 702
diff changeset
492 self.assertEqual(42, data['foo'])
09715f868a73 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 702
diff changeset
493
682
75795aadf920 Assigning to a variable named `data` in a Python code block no longer breaks context lookup. We now use the name `__data__` for internal data, hoping that that name is not as commonly used in templates.
cmlenz
parents: 606
diff changeset
494 def test_internal_shadowing(self):
75795aadf920 Assigning to a variable named `data` in a Python code block no longer breaks context lookup. We now use the name `__data__` for internal data, hoping that that name is not as commonly used in templates.
cmlenz
parents: 606
diff changeset
495 # The context itself is stored in the global execution scope of a suite
75795aadf920 Assigning to a variable named `data` in a Python code block no longer breaks context lookup. We now use the name `__data__` for internal data, hoping that that name is not as commonly used in templates.
cmlenz
parents: 606
diff changeset
496 # It used to get stored under the name 'data', which meant the
75795aadf920 Assigning to a variable named `data` in a Python code block no longer breaks context lookup. We now use the name `__data__` for internal data, hoping that that name is not as commonly used in templates.
cmlenz
parents: 606
diff changeset
497 # following test would fail, as the user defined 'data' variable
75795aadf920 Assigning to a variable named `data` in a Python code block no longer breaks context lookup. We now use the name `__data__` for internal data, hoping that that name is not as commonly used in templates.
cmlenz
parents: 606
diff changeset
498 # shadowed the Genshi one. We now use the name '__data__' to avoid
75795aadf920 Assigning to a variable named `data` in a Python code block no longer breaks context lookup. We now use the name `__data__` for internal data, hoping that that name is not as commonly used in templates.
cmlenz
parents: 606
diff changeset
499 # conflicts
75795aadf920 Assigning to a variable named `data` in a Python code block no longer breaks context lookup. We now use the name `__data__` for internal data, hoping that that name is not as commonly used in templates.
cmlenz
parents: 606
diff changeset
500 suite = Suite("""data = []
75795aadf920 Assigning to a variable named `data` in a Python code block no longer breaks context lookup. We now use the name `__data__` for internal data, hoping that that name is not as commonly used in templates.
cmlenz
parents: 606
diff changeset
501 bar = foo
75795aadf920 Assigning to a variable named `data` in a Python code block no longer breaks context lookup. We now use the name `__data__` for internal data, hoping that that name is not as commonly used in templates.
cmlenz
parents: 606
diff changeset
502 """)
75795aadf920 Assigning to a variable named `data` in a Python code block no longer breaks context lookup. We now use the name `__data__` for internal data, hoping that that name is not as commonly used in templates.
cmlenz
parents: 606
diff changeset
503 data = {'foo': 42}
75795aadf920 Assigning to a variable named `data` in a Python code block no longer breaks context lookup. We now use the name `__data__` for internal data, hoping that that name is not as commonly used in templates.
cmlenz
parents: 606
diff changeset
504 suite.execute(data)
75795aadf920 Assigning to a variable named `data` in a Python code block no longer breaks context lookup. We now use the name `__data__` for internal data, hoping that that name is not as commonly used in templates.
cmlenz
parents: 606
diff changeset
505 self.assertEqual(42, data['bar'])
75795aadf920 Assigning to a variable named `data` in a Python code block no longer breaks context lookup. We now use the name `__data__` for internal data, hoping that that name is not as commonly used in templates.
cmlenz
parents: 606
diff changeset
506
405
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
507 def test_assign(self):
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
508 suite = Suite("foo = 42")
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
509 data = {}
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
510 suite.execute(data)
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
511 self.assertEqual(42, data['foo'])
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
512
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
513 def test_def(self):
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
514 suite = Suite("def donothing(): pass")
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
515 data = {}
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
516 suite.execute(data)
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
517 assert 'donothing' in data
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
518 self.assertEqual(None, data['donothing']())
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
519
473
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
520 def test_def_with_multiple_statements(self):
586
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
521 suite = Suite("""
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
522 def donothing():
473
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
523 if True:
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
524 return foo
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
525 """)
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
526 data = {'foo': 'bar'}
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
527 suite.execute(data)
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
528 assert 'donothing' in data
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
529 self.assertEqual('bar', data['donothing']())
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
530
586
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
531 def test_def_using_nonlocal(self):
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
532 suite = Suite("""
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
533 values = []
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
534 def add(value):
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
535 if value not in values:
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
536 values.append(value)
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
537 add('foo')
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
538 add('bar')
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
539 """)
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
540 data = {}
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
541 suite.execute(data)
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
542 self.assertEqual(['foo', 'bar'], data['values'])
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
543
802
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
544 def test_def_some_defaults(self):
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
545 suite = Suite("""
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
546 def difference(v1, v2=10):
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
547 return v1 - v2
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
548 x = difference(20, 19)
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
549 y = difference(20)
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
550 """)
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
551 data = {}
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
552 suite.execute(data)
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
553 self.assertEqual(1, data['x'])
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
554 self.assertEqual(10, data['y'])
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
555
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
556 def test_def_all_defaults(self):
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
557 suite = Suite("""
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
558 def difference(v1=100, v2=10):
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
559 return v1 - v2
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
560 x = difference(20, 19)
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
561 y = difference(20)
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
562 z = difference()
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
563 """)
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
564 data = {}
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
565 suite.execute(data)
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
566 self.assertEqual(1, data['x'])
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
567 self.assertEqual(10, data['y'])
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
568 self.assertEqual(90, data['z'])
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
569
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
570 def test_def_vararg(self):
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
571 suite = Suite("""
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
572 def mysum(*others):
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
573 rv = 0
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
574 for n in others:
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
575 rv = rv + n
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
576 return rv
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
577 x = mysum(1, 2, 3)
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
578 """)
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
579 data = {}
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
580 suite.execute(data)
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
581 self.assertEqual(6, data['x'])
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
582
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
583 def test_def_kwargs(self):
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
584 suite = Suite("""
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
585 def smash(**kw):
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
586 return [''.join(i) for i in kw.items()]
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
587 x = smash(foo='abc', bar='def')
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
588 """)
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
589 data = {}
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
590 suite.execute(data)
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
591 self.assertEqual(['fooabc', 'bardef'], data['x'])
aa274188b77a Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents: 793
diff changeset
592
586
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
593 def test_def_nested(self):
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
594 suite = Suite("""
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
595 def doit():
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
596 values = []
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
597 def add(value):
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
598 if value not in values:
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
599 values.append(value)
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
600 add('foo')
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
601 add('bar')
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
602 return values
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
603 x = doit()
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
604 """)
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
605 data = {}
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
606 suite.execute(data)
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
607 self.assertEqual(['foo', 'bar'], data['x'])
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
608
879
27d210790d27 Looks like the 'decorators' field in the AST was renamed to 'decorator_list' in Python 2.6, so look for both. See #379.
cmlenz
parents: 868
diff changeset
609 def test_def_with_decorator(self):
27d210790d27 Looks like the 'decorators' field in the AST was renamed to 'decorator_list' in Python 2.6, so look for both. See #379.
cmlenz
parents: 868
diff changeset
610 suite = Suite("""
27d210790d27 Looks like the 'decorators' field in the AST was renamed to 'decorator_list' in Python 2.6, so look for both. See #379.
cmlenz
parents: 868
diff changeset
611 def lower(fun):
27d210790d27 Looks like the 'decorators' field in the AST was renamed to 'decorator_list' in Python 2.6, so look for both. See #379.
cmlenz
parents: 868
diff changeset
612 return lambda: fun().lower()
27d210790d27 Looks like the 'decorators' field in the AST was renamed to 'decorator_list' in Python 2.6, so look for both. See #379.
cmlenz
parents: 868
diff changeset
613
27d210790d27 Looks like the 'decorators' field in the AST was renamed to 'decorator_list' in Python 2.6, so look for both. See #379.
cmlenz
parents: 868
diff changeset
614 @lower
27d210790d27 Looks like the 'decorators' field in the AST was renamed to 'decorator_list' in Python 2.6, so look for both. See #379.
cmlenz
parents: 868
diff changeset
615 def say_hi():
27d210790d27 Looks like the 'decorators' field in the AST was renamed to 'decorator_list' in Python 2.6, so look for both. See #379.
cmlenz
parents: 868
diff changeset
616 return 'Hi!'
27d210790d27 Looks like the 'decorators' field in the AST was renamed to 'decorator_list' in Python 2.6, so look for both. See #379.
cmlenz
parents: 868
diff changeset
617
27d210790d27 Looks like the 'decorators' field in the AST was renamed to 'decorator_list' in Python 2.6, so look for both. See #379.
cmlenz
parents: 868
diff changeset
618 result = say_hi()
27d210790d27 Looks like the 'decorators' field in the AST was renamed to 'decorator_list' in Python 2.6, so look for both. See #379.
cmlenz
parents: 868
diff changeset
619 """)
27d210790d27 Looks like the 'decorators' field in the AST was renamed to 'decorator_list' in Python 2.6, so look for both. See #379.
cmlenz
parents: 868
diff changeset
620 data = {}
27d210790d27 Looks like the 'decorators' field in the AST was renamed to 'decorator_list' in Python 2.6, so look for both. See #379.
cmlenz
parents: 868
diff changeset
621 suite.execute(data)
27d210790d27 Looks like the 'decorators' field in the AST was renamed to 'decorator_list' in Python 2.6, so look for both. See #379.
cmlenz
parents: 868
diff changeset
622 self.assertEqual('hi!', data['result'])
27d210790d27 Looks like the 'decorators' field in the AST was renamed to 'decorator_list' in Python 2.6, so look for both. See #379.
cmlenz
parents: 868
diff changeset
623
405
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
624 def test_delete(self):
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
625 suite = Suite("""foo = 42
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
626 del foo
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
627 """)
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
628 data = {}
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
629 suite.execute(data)
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
630 assert 'foo' not in data
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
631
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
632 def test_class(self):
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
633 suite = Suite("class plain(object): pass")
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
634 data = {}
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
635 suite.execute(data)
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
636 assert 'plain' in data
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
637
586
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
638 def test_class_in_def(self):
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
639 suite = Suite("""
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
640 def create():
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
641 class Foobar(object):
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
642 def __str__(self):
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
643 return 'foobar'
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
644 return Foobar()
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
645 x = create()
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
646 """)
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
647 data = {}
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
648 suite.execute(data)
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
649 self.assertEqual('foobar', str(data['x']))
5413c9d95db1 Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
650
568
8f0d601afc0c AST transformer needs to also handle the class body.
cmlenz
parents: 473
diff changeset
651 def test_class_with_methods(self):
8f0d601afc0c AST transformer needs to also handle the class body.
cmlenz
parents: 473
diff changeset
652 suite = Suite("""class plain(object):
8f0d601afc0c AST transformer needs to also handle the class body.
cmlenz
parents: 473
diff changeset
653 def donothing():
8f0d601afc0c AST transformer needs to also handle the class body.
cmlenz
parents: 473
diff changeset
654 pass
8f0d601afc0c AST transformer needs to also handle the class body.
cmlenz
parents: 473
diff changeset
655 """)
8f0d601afc0c AST transformer needs to also handle the class body.
cmlenz
parents: 473
diff changeset
656 data = {}
8f0d601afc0c AST transformer needs to also handle the class body.
cmlenz
parents: 473
diff changeset
657 suite.execute(data)
8f0d601afc0c AST transformer needs to also handle the class body.
cmlenz
parents: 473
diff changeset
658 assert 'plain' in data
8f0d601afc0c AST transformer needs to also handle the class body.
cmlenz
parents: 473
diff changeset
659
405
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
660 def test_import(self):
917
bcaa91c42b97 add support for python 3 to genshi.template expression evaluator:
hodgestar
parents: 897
diff changeset
661 suite = Suite("from itertools import repeat")
405
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
662 data = {}
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
663 suite.execute(data)
917
bcaa91c42b97 add support for python 3 to genshi.template expression evaluator:
hodgestar
parents: 897
diff changeset
664 assert 'repeat' in data
405
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
665
731
6514d9889ac8 Workaround for a Python 2.4 bug that broke star imports in template code blocks. Closes #221. Many thanks to Armin Ronacher for the patch.
cmlenz
parents: 719
diff changeset
666 def test_import_star(self):
6514d9889ac8 Workaround for a Python 2.4 bug that broke star imports in template code blocks. Closes #221. Many thanks to Armin Ronacher for the patch.
cmlenz
parents: 719
diff changeset
667 suite = Suite("from itertools import *")
736
62e816c3ced8 Yet another followup fix for #221.
cmlenz
parents: 731
diff changeset
668 data = Context()
750
d007a0d7ba81 Remove some cruft for supporting Python 2.3.
cmlenz
parents: 739
diff changeset
669 suite.execute(data)
917
bcaa91c42b97 add support for python 3 to genshi.template expression evaluator:
hodgestar
parents: 897
diff changeset
670 assert 'repeat' in data
731
6514d9889ac8 Workaround for a Python 2.4 bug that broke star imports in template code blocks. Closes #221. Many thanks to Armin Ronacher for the patch.
cmlenz
parents: 719
diff changeset
671
807
46816d77cab7 Added test for [1008].
cmlenz
parents: 803
diff changeset
672 def test_import_in_def(self):
46816d77cab7 Added test for [1008].
cmlenz
parents: 803
diff changeset
673 suite = Suite("""def fun():
917
bcaa91c42b97 add support for python 3 to genshi.template expression evaluator:
hodgestar
parents: 897
diff changeset
674 from itertools import repeat
bcaa91c42b97 add support for python 3 to genshi.template expression evaluator:
hodgestar
parents: 897
diff changeset
675 return repeat(1, 3)
807
46816d77cab7 Added test for [1008].
cmlenz
parents: 803
diff changeset
676 """)
46816d77cab7 Added test for [1008].
cmlenz
parents: 803
diff changeset
677 data = Context()
46816d77cab7 Added test for [1008].
cmlenz
parents: 803
diff changeset
678 suite.execute(data)
917
bcaa91c42b97 add support for python 3 to genshi.template expression evaluator:
hodgestar
parents: 897
diff changeset
679 assert 'repeat' not in data
bcaa91c42b97 add support for python 3 to genshi.template expression evaluator:
hodgestar
parents: 897
diff changeset
680 self.assertEqual([1, 1, 1], list(data['fun']()))
807
46816d77cab7 Added test for [1008].
cmlenz
parents: 803
diff changeset
681
405
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
682 def test_for(self):
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
683 suite = Suite("""x = []
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
684 for i in range(3):
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
685 x.append(i**2)
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
686 """)
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
687 data = {}
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
688 suite.execute(data)
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
689 self.assertEqual([0, 1, 4], data['x'])
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
690
803
92b9b647e1fe `for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents: 802
diff changeset
691 def test_for_in_def(self):
92b9b647e1fe `for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents: 802
diff changeset
692 suite = Suite("""def loop():
92b9b647e1fe `for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents: 802
diff changeset
693 for i in range(10):
92b9b647e1fe `for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents: 802
diff changeset
694 if i == 5:
92b9b647e1fe `for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents: 802
diff changeset
695 break
92b9b647e1fe `for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents: 802
diff changeset
696 return i
92b9b647e1fe `for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents: 802
diff changeset
697 """)
92b9b647e1fe `for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents: 802
diff changeset
698 data = {}
92b9b647e1fe `for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents: 802
diff changeset
699 suite.execute(data)
92b9b647e1fe `for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents: 802
diff changeset
700 assert 'loop' in data
92b9b647e1fe `for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents: 802
diff changeset
701 self.assertEqual(5, data['loop']())
92b9b647e1fe `for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents: 802
diff changeset
702
405
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
703 def test_if(self):
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
704 suite = Suite("""if foo == 42:
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
705 x = True
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
706 """)
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
707 data = {'foo': 42}
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
708 suite.execute(data)
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
709 self.assertEqual(True, data['x'])
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
710
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
711 def test_raise(self):
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
712 suite = Suite("""raise NotImplementedError""")
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
713 self.assertRaises(NotImplementedError, suite.execute, {})
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
714
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
715 def test_try_except(self):
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
716 suite = Suite("""try:
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
717 import somemod
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
718 except ImportError:
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
719 somemod = None
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
720 else:
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
721 somemod.dosth()""")
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
722 data = {}
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
723 suite.execute(data)
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
724 self.assertEqual(None, data['somemod'])
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
725
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
726 def test_finally(self):
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
727 suite = Suite("""try:
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
728 x = 2
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
729 finally:
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
730 x = None
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
731 """)
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
732 data = {}
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
733 suite.execute(data)
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
734 self.assertEqual(None, data['x'])
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
735
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
736 def test_while_break(self):
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
737 suite = Suite("""x = 0
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
738 while x < 5:
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
739 x += step
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
740 if x == 4:
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
741 break
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
742 """)
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
743 data = {'step': 2}
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
744 suite.execute(data)
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
745 self.assertEqual(4, data['x'])
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
746
473
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
747 def test_augmented_attribute_assignment(self):
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
748 suite = Suite("d['k'] += 42")
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
749 d = {"k": 1}
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
750 suite.execute({"d": d})
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
751 self.assertEqual(43, d["k"])
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
752
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
753 def test_local_augmented_assign(self):
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
754 Suite("x = 1; x += 42; assert x == 43").execute({})
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
755
579
71e3205925e6 Fix for augmented assignments to local variables. Thanks to Erik Bray for reporting the problem.
cmlenz
parents: 569
diff changeset
756 def test_augmented_assign_in_def(self):
71e3205925e6 Fix for augmented assignments to local variables. Thanks to Erik Bray for reporting the problem.
cmlenz
parents: 569
diff changeset
757 d = {}
71e3205925e6 Fix for augmented assignments to local variables. Thanks to Erik Bray for reporting the problem.
cmlenz
parents: 569
diff changeset
758 Suite("""def foo():
71e3205925e6 Fix for augmented assignments to local variables. Thanks to Erik Bray for reporting the problem.
cmlenz
parents: 569
diff changeset
759 i = 1
71e3205925e6 Fix for augmented assignments to local variables. Thanks to Erik Bray for reporting the problem.
cmlenz
parents: 569
diff changeset
760 i += 1
71e3205925e6 Fix for augmented assignments to local variables. Thanks to Erik Bray for reporting the problem.
cmlenz
parents: 569
diff changeset
761 return i
71e3205925e6 Fix for augmented assignments to local variables. Thanks to Erik Bray for reporting the problem.
cmlenz
parents: 569
diff changeset
762 x = foo()""").execute(d)
71e3205925e6 Fix for augmented assignments to local variables. Thanks to Erik Bray for reporting the problem.
cmlenz
parents: 569
diff changeset
763 self.assertEqual(2, d['x'])
71e3205925e6 Fix for augmented assignments to local variables. Thanks to Erik Bray for reporting the problem.
cmlenz
parents: 569
diff changeset
764
582
b21da79c9bde Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents: 579
diff changeset
765 def test_augmented_assign_in_loop_in_def(self):
b21da79c9bde Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents: 579
diff changeset
766 d = {}
b21da79c9bde Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents: 579
diff changeset
767 Suite("""def foo():
b21da79c9bde Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents: 579
diff changeset
768 i = 0
b21da79c9bde Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents: 579
diff changeset
769 for n in range(5):
b21da79c9bde Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents: 579
diff changeset
770 i += n
b21da79c9bde Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents: 579
diff changeset
771 return i
b21da79c9bde Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents: 579
diff changeset
772 x = foo()""").execute(d)
b21da79c9bde Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents: 579
diff changeset
773 self.assertEqual(10, d['x'])
b21da79c9bde Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents: 579
diff changeset
774
473
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
775 def test_assign_in_list(self):
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
776 suite = Suite("[d['k']] = 'foo',; assert d['k'] == 'foo'")
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
777 d = {"k": "bar"}
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
778 suite.execute({"d": d})
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
779 self.assertEqual("foo", d["k"])
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
780
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
781 def test_exec(self):
917
bcaa91c42b97 add support for python 3 to genshi.template expression evaluator:
hodgestar
parents: 897
diff changeset
782 suite = Suite("x = 1; exec(d['k']); assert x == 42, x")
473
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
783 suite.execute({"d": {"k": "x = 42"}})
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
784
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
785 def test_return(self):
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
786 suite = Suite("""
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
787 def f():
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
788 return v
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
789
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
790 assert f() == 42
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
791 """)
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
792 suite.execute({"v": 42})
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
793
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
794 def test_assign_to_dict_item(self):
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
795 suite = Suite("d['k'] = 'foo'")
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
796 data = {'d': {}}
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
797 suite.execute(data)
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
798 self.assertEqual('foo', data['d']['k'])
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
799
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
800 def test_assign_to_attribute(self):
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
801 class Something(object): pass
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
802 something = Something()
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
803 suite = Suite("obj.attr = 'foo'")
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
804 data = {"obj": something}
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
805 suite.execute(data)
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
806 self.assertEqual('foo', something.attr)
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
807
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
808 def test_delattr(self):
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
809 class Something(object):
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
810 def __init__(self):
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
811 self.attr = 'foo'
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
812 obj = Something()
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
813 Suite("del obj.attr").execute({'obj': obj})
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
814 self.failIf(hasattr(obj, 'attr'))
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
815
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
816 def test_delitem(self):
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
817 d = {'k': 'foo'}
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
818 Suite("del d['k']").execute({'d': d})
793
7cf2407671c2 Get rid of a couple more -3 warnings.
cmlenz
parents: 750
diff changeset
819 self.failIf('k' in d, repr(d))
473
5870bdf03fca Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
820
867
efc03d0719f3 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
821 if sys.version_info >= (2, 5):
efc03d0719f3 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
822 def test_with_statement(self):
868
9309530ee279 Fix the unit test for execution of statements containing with statements so that it also works on Windows. Thanks to cboos for the patch.
cmlenz
parents: 867
diff changeset
823 fd, path = mkstemp()
9309530ee279 Fix the unit test for execution of statements containing with statements so that it also works on Windows. Thanks to cboos for the patch.
cmlenz
parents: 867
diff changeset
824 f = os.fdopen(fd, "w")
9309530ee279 Fix the unit test for execution of statements containing with statements so that it also works on Windows. Thanks to cboos for the patch.
cmlenz
parents: 867
diff changeset
825 try:
9309530ee279 Fix the unit test for execution of statements containing with statements so that it also works on Windows. Thanks to cboos for the patch.
cmlenz
parents: 867
diff changeset
826 f.write('foo\nbar\n')
9309530ee279 Fix the unit test for execution of statements containing with statements so that it also works on Windows. Thanks to cboos for the patch.
cmlenz
parents: 867
diff changeset
827 f.seek(0)
9309530ee279 Fix the unit test for execution of statements containing with statements so that it also works on Windows. Thanks to cboos for the patch.
cmlenz
parents: 867
diff changeset
828 f.close()
867
efc03d0719f3 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
829
868
9309530ee279 Fix the unit test for execution of statements containing with statements so that it also works on Windows. Thanks to cboos for the patch.
cmlenz
parents: 867
diff changeset
830 d = {'path': path}
9309530ee279 Fix the unit test for execution of statements containing with statements so that it also works on Windows. Thanks to cboos for the patch.
cmlenz
parents: 867
diff changeset
831 suite = Suite("""from __future__ import with_statement
867
efc03d0719f3 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
832 lines = []
efc03d0719f3 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
833 with open(path) as file:
efc03d0719f3 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
834 for line in file:
efc03d0719f3 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
835 lines.append(line)
efc03d0719f3 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
836 """)
868
9309530ee279 Fix the unit test for execution of statements containing with statements so that it also works on Windows. Thanks to cboos for the patch.
cmlenz
parents: 867
diff changeset
837 suite.execute(d)
9309530ee279 Fix the unit test for execution of statements containing with statements so that it also works on Windows. Thanks to cboos for the patch.
cmlenz
parents: 867
diff changeset
838 self.assertEqual(['foo\n', 'bar\n'], d['lines'])
9309530ee279 Fix the unit test for execution of statements containing with statements so that it also works on Windows. Thanks to cboos for the patch.
cmlenz
parents: 867
diff changeset
839 finally:
9309530ee279 Fix the unit test for execution of statements containing with statements so that it also works on Windows. Thanks to cboos for the patch.
cmlenz
parents: 867
diff changeset
840 os.remove(path)
867
efc03d0719f3 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
841
efc03d0719f3 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
842 def test_yield_expression(self):
efc03d0719f3 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
843 d = {}
917
bcaa91c42b97 add support for python 3 to genshi.template expression evaluator:
hodgestar
parents: 897
diff changeset
844 suite = Suite("""from genshi.compat import next
bcaa91c42b97 add support for python 3 to genshi.template expression evaluator:
hodgestar
parents: 897
diff changeset
845 results = []
867
efc03d0719f3 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
846 def counter(maximum):
efc03d0719f3 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
847 i = 0
efc03d0719f3 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
848 while i < maximum:
efc03d0719f3 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
849 val = (yield i)
efc03d0719f3 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
850 if val is not None:
efc03d0719f3 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
851 i = val
efc03d0719f3 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
852 else:
efc03d0719f3 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
853 i += 1
efc03d0719f3 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
854 it = counter(5)
917
bcaa91c42b97 add support for python 3 to genshi.template expression evaluator:
hodgestar
parents: 897
diff changeset
855 results.append(next(it))
867
efc03d0719f3 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
856 results.append(it.send(3))
917
bcaa91c42b97 add support for python 3 to genshi.template expression evaluator:
hodgestar
parents: 897
diff changeset
857 results.append(next(it))
867
efc03d0719f3 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
858 """)
efc03d0719f3 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
859 suite.execute(d)
efc03d0719f3 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
860 self.assertEqual([0, 3, 4], d['results'])
efc03d0719f3 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
861
405
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
862
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
863 def suite():
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
864 suite = unittest.TestSuite()
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
865 suite.addTest(doctest.DocTestSuite(Expression.__module__))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
866 suite.addTest(unittest.makeSuite(ExpressionTestCase, 'test'))
405
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
867 suite.addTest(unittest.makeSuite(SuiteTestCase, 'test'))
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
868 return suite
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
869
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
870 if __name__ == '__main__':
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
871 unittest.main(defaultTest='suite')
Copyright (C) 2012-2017 Edgewall Software