annotate genshi/template/tests/eval.py @ 1024:a5e09a7ba12d trunk

Add support for kwonlyargs and kw_defaults attributes of AST argument nodes.
author hodgestar
date Sun, 16 Feb 2014 19:36:21 +0000
parents 2ed9827596a4
children
rev   line source
336
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
2 #
897
64f04a2c5e66 Update changelog and copyright years.
cmlenz
parents: 896
diff changeset
3 # Copyright (C) 2006-2010 Edgewall Software
336
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
4 # All rights reserved.
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
5 #
7763f7aec949 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
7763f7aec949 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
7763f7aec949 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.
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
9 #
7763f7aec949 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
7763f7aec949 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
7763f7aec949 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/.
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
13
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
14 import doctest
867
82ba3198d913 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
b5bd8c109209 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 702
diff changeset
16 import pickle
336
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
17 import sys
868
eb80cbd8e4fe 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
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
19 import unittest
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
20
401
68772732c896 Make the `Markup` class available by default in template expressions. Closes #67.
cmlenz
parents: 396
diff changeset
21 from genshi.core import Markup
750
52219748e5c1 Remove some cruft for supporting Python 2.3.
cmlenz
parents: 739
diff changeset
22 from genshi.template.base import Context
442
97544725bb7f 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, \
97544725bb7f 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
934
31bbb6f9e87b Merge r1142 from py3k:
hodgestar
parents: 897
diff changeset
25 from genshi.compat import BytesIO, IS_PYTHON2, wrapped_bytes
336
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
26
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
27
7763f7aec949 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):
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
29
340
b517929eb334 Make expressions hashable.
cmlenz
parents: 336
diff changeset
30 def test_eq(self):
b517929eb334 Make expressions hashable.
cmlenz
parents: 336
diff changeset
31 expr = Expression('x,y')
b517929eb334 Make expressions hashable.
cmlenz
parents: 336
diff changeset
32 self.assertEqual(expr, Expression('x,y'))
b517929eb334 Make expressions hashable.
cmlenz
parents: 336
diff changeset
33 self.assertNotEqual(expr, Expression('y, x'))
b517929eb334 Make expressions hashable.
cmlenz
parents: 336
diff changeset
34
b517929eb334 Make expressions hashable.
cmlenz
parents: 336
diff changeset
35 def test_hash(self):
b517929eb334 Make expressions hashable.
cmlenz
parents: 336
diff changeset
36 expr = Expression('x,y')
b517929eb334 Make expressions hashable.
cmlenz
parents: 336
diff changeset
37 self.assertEqual(hash(expr), hash(Expression('x,y')))
b517929eb334 Make expressions hashable.
cmlenz
parents: 336
diff changeset
38 self.assertNotEqual(hash(expr), hash(Expression('y, x')))
b517929eb334 Make expressions hashable.
cmlenz
parents: 336
diff changeset
39
715
b5bd8c109209 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 702
diff changeset
40 def test_pickle(self):
b5bd8c109209 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 702
diff changeset
41 expr = Expression('1 < 2')
934
31bbb6f9e87b Merge r1142 from py3k:
hodgestar
parents: 897
diff changeset
42 buf = BytesIO()
715
b5bd8c109209 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 702
diff changeset
43 pickle.dump(expr, buf, 2)
b5bd8c109209 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 702
diff changeset
44 buf.seek(0)
b5bd8c109209 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 702
diff changeset
45 unpickled = pickle.load(buf)
b5bd8c109209 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 702
diff changeset
46 assert unpickled.evaluate({}) is True
b5bd8c109209 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 702
diff changeset
47
336
7763f7aec949 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):
7763f7aec949 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
35189e960252 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
7763f7aec949 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
35189e960252 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
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
53
401
68772732c896 Make the `Markup` class available by default in template expressions. Closes #67.
cmlenz
parents: 396
diff changeset
54 def test_builtins(self):
68772732c896 Make the `Markup` class available by default in template expressions. Closes #67.
cmlenz
parents: 396
diff changeset
55 expr = Expression('Markup')
68772732c896 Make the `Markup` class available by default in template expressions. Closes #67.
cmlenz
parents: 396
diff changeset
56 self.assertEqual(expr.evaluate({}), Markup)
68772732c896 Make the `Markup` class available by default in template expressions. Closes #67.
cmlenz
parents: 396
diff changeset
57
336
7763f7aec949 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):
7763f7aec949 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({}))
7763f7aec949 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({}))
934
31bbb6f9e87b Merge r1142 from py3k:
hodgestar
parents: 897
diff changeset
61 self.assertEqual(u'foo'.encode('utf-8'),
31bbb6f9e87b Merge r1142 from py3k:
hodgestar
parents: 897
diff changeset
62 Expression(wrapped_bytes("b'foo'")).evaluate({}))
336
7763f7aec949 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({}))
7763f7aec949 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({}))
7763f7aec949 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({}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
66
7763f7aec949 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):
7763f7aec949 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'")
7763f7aec949 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({}))
7763f7aec949 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'")
7763f7aec949 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({}))
934
31bbb6f9e87b Merge r1142 from py3k:
hodgestar
parents: 897
diff changeset
72 # On Python2 strings are converted to unicode if they contained
31bbb6f9e87b Merge r1142 from py3k:
hodgestar
parents: 897
diff changeset
73 # non-ASCII characters.
31bbb6f9e87b Merge r1142 from py3k:
hodgestar
parents: 897
diff changeset
74 # On Py3k, we have no need to do this as non-prefixed strings aren't
31bbb6f9e87b Merge r1142 from py3k:
hodgestar
parents: 897
diff changeset
75 # raw.
31bbb6f9e87b Merge r1142 from py3k:
hodgestar
parents: 897
diff changeset
76 expr = Expression(wrapped_bytes(r"b'\xc3\xbe'"))
31bbb6f9e87b Merge r1142 from py3k:
hodgestar
parents: 897
diff changeset
77 if IS_PYTHON2:
31bbb6f9e87b Merge r1142 from py3k:
hodgestar
parents: 897
diff changeset
78 self.assertEqual(u'þ', expr.evaluate({}))
31bbb6f9e87b Merge r1142 from py3k:
hodgestar
parents: 897
diff changeset
79 else:
31bbb6f9e87b Merge r1142 from py3k:
hodgestar
parents: 897
diff changeset
80 self.assertEqual(u'þ'.encode('utf-8'), expr.evaluate({}))
336
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
81
7763f7aec949 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):
7763f7aec949 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({}))
934
31bbb6f9e87b Merge r1142 from py3k:
hodgestar
parents: 897
diff changeset
84 if IS_PYTHON2:
31bbb6f9e87b Merge r1142 from py3k:
hodgestar
parents: 897
diff changeset
85 self.assertEqual(42L, Expression("42L").evaluate({}))
336
7763f7aec949 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({}))
934
31bbb6f9e87b Merge r1142 from py3k:
hodgestar
parents: 897
diff changeset
87 if IS_PYTHON2:
31bbb6f9e87b Merge r1142 from py3k:
hodgestar
parents: 897
diff changeset
88 self.assertEqual(07, Expression("07").evaluate({}))
336
7763f7aec949 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({}))
7763f7aec949 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({}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
91
7763f7aec949 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):
7763f7aec949 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({}))
7763f7aec949 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},
7763f7aec949 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}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
96
7763f7aec949 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):
7763f7aec949 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({}))
7763f7aec949 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({}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
100 self.assertEqual([True],
7763f7aec949 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}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
102
7763f7aec949 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):
7763f7aec949 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({}))
7763f7aec949 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({}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
106 self.assertEqual((True,),
7763f7aec949 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}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
108
7763f7aec949 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):
7763f7aec949 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({}))
7763f7aec949 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}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
112
7763f7aec949 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):
7763f7aec949 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({}))
7763f7aec949 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}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
116
7763f7aec949 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):
7763f7aec949 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({}))
7763f7aec949 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}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
120
7763f7aec949 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):
7763f7aec949 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({}))
7763f7aec949 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}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
124
7763f7aec949 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):
7763f7aec949 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({}))
7763f7aec949 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}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
128
7763f7aec949 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):
7763f7aec949 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({}))
7763f7aec949 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}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
132
7763f7aec949 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):
7763f7aec949 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({}))
7763f7aec949 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}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
136
7763f7aec949 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):
7763f7aec949 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({}))
7763f7aec949 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}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
140
7763f7aec949 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):
7763f7aec949 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({}))
7763f7aec949 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}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
144
7763f7aec949 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):
7763f7aec949 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({}))
7763f7aec949 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}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
148
7763f7aec949 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):
7763f7aec949 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({}))
7763f7aec949 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}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
152
7763f7aec949 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):
7763f7aec949 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({}))
7763f7aec949 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}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
156
7763f7aec949 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):
7763f7aec949 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({}))
7763f7aec949 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}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
160
7763f7aec949 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):
7763f7aec949 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({}))
7763f7aec949 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}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
164
396
7016f404b915 add visitor for xor operator
mgood
parents: 393
diff changeset
165 def test_binop_xor(self):
7016f404b915 add visitor for xor operator
mgood
parents: 393
diff changeset
166 self.assertEqual(1, Expression("1 ^ 0").evaluate({}))
7016f404b915 add visitor for xor operator
mgood
parents: 393
diff changeset
167 self.assertEqual(1, Expression("x ^ y").evaluate({'x': 1, 'y': 0}))
7016f404b915 add visitor for xor operator
mgood
parents: 393
diff changeset
168
336
7763f7aec949 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):
7763f7aec949 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({}))
7763f7aec949 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,
7763f7aec949 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)}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
173
7763f7aec949 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):
7763f7aec949 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({}))
7763f7aec949 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,
7763f7aec949 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)}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
178
7763f7aec949 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):
7763f7aec949 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({}))
7763f7aec949 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}))
7763f7aec949 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({}))
7763f7aec949 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}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
184
7763f7aec949 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):
7763f7aec949 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({}))
7763f7aec949 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,
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
188 'y': 2}))
7763f7aec949 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({}))
7763f7aec949 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,
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
191 'y': 1}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
192
7763f7aec949 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):
7763f7aec949 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({}))
7763f7aec949 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,
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
196 'y': False}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
197
7763f7aec949 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):
7763f7aec949 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({}))
7763f7aec949 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,
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
201 'y': False}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
202
7763f7aec949 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):
7763f7aec949 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({}))
7763f7aec949 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}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
206
7763f7aec949 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):
7763f7aec949 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({}))
7763f7aec949 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
be88c77839fc Get rid of a couple more -3 warnings.
cmlenz
parents: 750
diff changeset
210 if sys.version < '3':
be88c77839fc Get rid of a couple more -3 warnings.
cmlenz
parents: 750
diff changeset
211 self.assertEqual(False, Expression("1 <> 1").evaluate({}))
be88c77839fc 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
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
213
7763f7aec949 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):
7763f7aec949 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({}))
7763f7aec949 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}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
217
7763f7aec949 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):
7763f7aec949 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({}))
7763f7aec949 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}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
221
7763f7aec949 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):
7763f7aec949 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({}))
7763f7aec949 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}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
225
7763f7aec949 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):
7763f7aec949 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({}))
7763f7aec949 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}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
229
7763f7aec949 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):
7763f7aec949 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({}))
7763f7aec949 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,
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
233 'y': 3}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
234
7763f7aec949 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):
7763f7aec949 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}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
237 data = {'foo': 'bar'}
7763f7aec949 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))
7763f7aec949 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)}}
7763f7aec949 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))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
241
7763f7aec949 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):
7763f7aec949 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,
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
244 'bar': 42}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
245
7763f7aec949 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):
7763f7aec949 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,
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
248 'bar': [42]}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
249
7763f7aec949 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):
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
251 def foo(x):
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
252 return x
343
35189e960252 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)")
35189e960252 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
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
255
7763f7aec949 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
be88c77839fc Get rid of a couple more -3 warnings.
cmlenz
parents: 750
diff changeset
257 data = {'items': range(5)}
be88c77839fc Get rid of a couple more -3 warnings.
cmlenz
parents: 750
diff changeset
258 expr = Expression("filter(lambda x: x > 2, items)")
934
31bbb6f9e87b Merge r1142 from py3k:
hodgestar
parents: 897
diff changeset
259 self.assertEqual([3, 4], list(expr.evaluate(data)))
336
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
260
896
8ae2c1765068 Fix for infinite recursion when parsing argument names from tuples. Closes #383.
cmlenz
parents: 879
diff changeset
261 def test_lambda_tuple_arg(self):
934
31bbb6f9e87b Merge r1142 from py3k:
hodgestar
parents: 897
diff changeset
262 # This syntax goes away in Python 3
31bbb6f9e87b Merge r1142 from py3k:
hodgestar
parents: 897
diff changeset
263 if not IS_PYTHON2:
31bbb6f9e87b Merge r1142 from py3k:
hodgestar
parents: 897
diff changeset
264 return
896
8ae2c1765068 Fix for infinite recursion when parsing argument names from tuples. Closes #383.
cmlenz
parents: 879
diff changeset
265 data = {'items': [(1, 2), (2, 1)]}
8ae2c1765068 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)")
934
31bbb6f9e87b Merge r1142 from py3k:
hodgestar
parents: 897
diff changeset
267 self.assertEqual([(2, 1)], list(expr.evaluate(data)))
896
8ae2c1765068 Fix for infinite recursion when parsing argument names from tuples. Closes #383.
cmlenz
parents: 879
diff changeset
268
336
7763f7aec949 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):
7763f7aec949 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]")
7763f7aec949 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)}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
272
7763f7aec949 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)]")
7763f7aec949 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)],
7763f7aec949 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)}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
276
7763f7aec949 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]")
7763f7aec949 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],
7763f7aec949 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}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
280
845
4ec4377f49ee 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]")
4ec4377f49ee Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents: 807
diff changeset
282 self.assertEqual([0, 1, 0, 1, 2],
4ec4377f49ee Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents: 807
diff changeset
283 expr.evaluate({'groups': [range(2), range(3)]}))
4ec4377f49ee Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents: 807
diff changeset
284
4ec4377f49ee 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]")
4ec4377f49ee Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents: 807
diff changeset
286 self.assertEqual([('x0', 'y0'), ('x0', 'y1'), ('x1', 'y0'), ('x1', 'y1')],
4ec4377f49ee Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents: 807
diff changeset
287 expr.evaluate({'x': ['x0', 'x1'], 'y': ['y0', 'y1']}))
4ec4377f49ee Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents: 807
diff changeset
288
336
7763f7aec949 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):
7763f7aec949 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}]
7763f7aec949 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]")
7763f7aec949 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}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
293
7763f7aec949 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):
7763f7aec949 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}]
7763f7aec949 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]")
7763f7aec949 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}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
298
750
52219748e5c1 Remove some cruft for supporting Python 2.3.
cmlenz
parents: 739
diff changeset
299 def test_generator_expression(self):
52219748e5c1 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)")
52219748e5c1 Remove some cruft for supporting Python 2.3.
cmlenz
parents: 739
diff changeset
301 self.assertEqual([0, 1], expr.evaluate({'numbers': range(5)}))
336
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
302
750
52219748e5c1 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))")
52219748e5c1 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)],
52219748e5c1 Remove some cruft for supporting Python 2.3.
cmlenz
parents: 739
diff changeset
305 expr.evaluate({'numbers': range(5)}))
336
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
306
750
52219748e5c1 Remove some cruft for supporting Python 2.3.
cmlenz
parents: 739
diff changeset
307 expr = Expression("list(offset + n for n in numbers)")
52219748e5c1 Remove some cruft for supporting Python 2.3.
cmlenz
parents: 739
diff changeset
308 self.assertEqual([2, 3, 4, 5, 6],
52219748e5c1 Remove some cruft for supporting Python 2.3.
cmlenz
parents: 739
diff changeset
309 expr.evaluate({'numbers': range(5), 'offset': 2}))
336
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
310
845
4ec4377f49ee 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)")
4ec4377f49ee Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents: 807
diff changeset
312 self.assertEqual([0, 1, 0, 1, 2],
4ec4377f49ee Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents: 807
diff changeset
313 expr.evaluate({'groups': [range(2), range(3)]}))
4ec4377f49ee Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents: 807
diff changeset
314
4ec4377f49ee 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)")
4ec4377f49ee Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents: 807
diff changeset
316 self.assertEqual([('x0', 'y0'), ('x0', 'y1'), ('x1', 'y0'), ('x1', 'y1')],
4ec4377f49ee Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents: 807
diff changeset
317 expr.evaluate({'x': ['x0', 'x1'], 'y': ['y0', 'y1']}))
4ec4377f49ee Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents: 807
diff changeset
318
750
52219748e5c1 Remove some cruft for supporting Python 2.3.
cmlenz
parents: 739
diff changeset
319 def test_generator_expression_with_getattr(self):
52219748e5c1 Remove some cruft for supporting Python 2.3.
cmlenz
parents: 739
diff changeset
320 items = [{'name': 'a', 'value': 1}, {'name': 'b', 'value': 2}]
52219748e5c1 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)")
52219748e5c1 Remove some cruft for supporting Python 2.3.
cmlenz
parents: 739
diff changeset
322 self.assertEqual(['b'], expr.evaluate({'items': items}))
52219748e5c1 Remove some cruft for supporting Python 2.3.
cmlenz
parents: 739
diff changeset
323
52219748e5c1 Remove some cruft for supporting Python 2.3.
cmlenz
parents: 739
diff changeset
324 def test_generator_expression_with_getitem(self):
52219748e5c1 Remove some cruft for supporting Python 2.3.
cmlenz
parents: 739
diff changeset
325 items = [{'name': 'a', 'value': 1}, {'name': 'b', 'value': 2}]
52219748e5c1 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)")
52219748e5c1 Remove some cruft for supporting Python 2.3.
cmlenz
parents: 739
diff changeset
327 self.assertEqual(['b'], expr.evaluate({'items': items}))
336
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
328
393
1dc3d3df4a85 add support for Python 2.5 conditional expressions (fixes #74)
mgood
parents: 343
diff changeset
329 if sys.version_info >= (2, 5):
1dc3d3df4a85 add support for Python 2.5 conditional expressions (fixes #74)
mgood
parents: 343
diff changeset
330 def test_conditional_expression(self):
1dc3d3df4a85 add support for Python 2.5 conditional expressions (fixes #74)
mgood
parents: 343
diff changeset
331 expr = Expression("'T' if foo else 'F'")
1dc3d3df4a85 add support for Python 2.5 conditional expressions (fixes #74)
mgood
parents: 343
diff changeset
332 self.assertEqual('T', expr.evaluate({'foo': True}))
1dc3d3df4a85 add support for Python 2.5 conditional expressions (fixes #74)
mgood
parents: 343
diff changeset
333 self.assertEqual('F', expr.evaluate({'foo': False}))
1dc3d3df4a85 add support for Python 2.5 conditional expressions (fixes #74)
mgood
parents: 343
diff changeset
334
336
7763f7aec949 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):
7763f7aec949 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]")
7763f7aec949 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)}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
338
7763f7aec949 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):
7763f7aec949 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]")
7763f7aec949 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,
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
342 'end': 2}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
343
7763f7aec949 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):
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
345 expr = Expression("numbers[:]")
7763f7aec949 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)}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
347
7763f7aec949 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):
7763f7aec949 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]")
7763f7aec949 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),
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
351 'stride': 2}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
352
7763f7aec949 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):
7763f7aec949 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:]")
7763f7aec949 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)}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
356
7763f7aec949 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):
7763f7aec949 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]")
7763f7aec949 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)}))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
360
442
97544725bb7f 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
37ff75bb4301 Changed the default error handling mode to "strict".
cmlenz
parents: 586
diff changeset
362 expr = Expression("nothing", filename='index.html', lineno=50,
37ff75bb4301 Changed the default error handling mode to "strict".
cmlenz
parents: 586
diff changeset
363 lookup='lenient')
442
97544725bb7f 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({})
97544725bb7f 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)
97544725bb7f 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)
97544725bb7f 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
97544725bb7f 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
97544725bb7f 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):
97544725bb7f 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):
97544725bb7f 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):
97544725bb7f 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>'
97544725bb7f 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
37ff75bb4301 Changed the default error handling mode to "strict".
cmlenz
parents: 586
diff changeset
374 expr = Expression('something.nil', filename='index.html', lineno=50,
37ff75bb4301 Changed the default error handling mode to "strict".
cmlenz
parents: 586
diff changeset
375 lookup='lenient')
442
97544725bb7f 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})
97544725bb7f 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)
97544725bb7f 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)
97544725bb7f 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
97544725bb7f 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
c17342ef9efb 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):
c17342ef9efb 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
e5b0d9d6b406 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
c17342ef9efb 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
e5b0d9d6b406 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)
e5b0d9d6b406 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):
e5b0d9d6b406 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
e5b0d9d6b406 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
c17342ef9efb 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
e5b0d9d6b406 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()})
e5b0d9d6b406 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,
e5b0d9d6b406 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
c17342ef9efb 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
97544725bb7f 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):
97544725bb7f 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):
97544725bb7f 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):
97544725bb7f 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>'
97544725bb7f 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
37ff75bb4301 Changed the default error handling mode to "strict".
cmlenz
parents: 586
diff changeset
399 expr = Expression('something["nil"]', filename='index.html', lineno=50,
37ff75bb4301 Changed the default error handling mode to "strict".
cmlenz
parents: 586
diff changeset
400 lookup='lenient')
442
97544725bb7f 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})
97544725bb7f 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)
97544725bb7f 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)
97544725bb7f 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
97544725bb7f 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
c17342ef9efb 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):
c17342ef9efb 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):
c17342ef9efb 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):
c17342ef9efb 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
c17342ef9efb 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,
c17342ef9efb 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()})
c17342ef9efb 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
7763f7aec949 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
97544725bb7f 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,
97544725bb7f 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
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
416 try:
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
417 expr.evaluate({})
418
c478a6fa9e77 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
418 self.fail('Expected UndefinedError')
c478a6fa9e77 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
419 except UndefinedError, e:
336
7763f7aec949 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()
7763f7aec949 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
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
422 frames = []
7763f7aec949 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:
7763f7aec949 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
7763f7aec949 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
c478a6fa9e77 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
426 self.assertEqual('"nothing" not defined', str(e))
c478a6fa9e77 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
427 self.assertEqual("<Expression 'nothing'>",
442
97544725bb7f 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
7763f7aec949 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
97544725bb7f 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)
97544725bb7f 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
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
432
418
c478a6fa9e77 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
433 def test_error_getattr_undefined(self):
c478a6fa9e77 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
434 class Something(object):
c478a6fa9e77 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
435 def __repr__(self):
c478a6fa9e77 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
436 return '<Something>'
442
97544725bb7f 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,
97544725bb7f 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
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
439 try:
418
c478a6fa9e77 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
440 expr.evaluate({'something': Something()})
c478a6fa9e77 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
441 self.fail('Expected UndefinedError')
c478a6fa9e77 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
442 except UndefinedError, e:
473
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
443 self.assertEqual('<Something> has no member named "nil"', str(e))
336
7763f7aec949 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
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
445 search_string = "<Expression 'something.nil'>"
336
7763f7aec949 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
7763f7aec949 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:
7763f7aec949 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
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
449 code = frame.tb_frame.f_code
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
450 if code.co_name == search_string:
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
451 break
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
452 else:
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
453 self.fail("never found the frame I was looking for")
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
454 self.assertEqual('index.html', code.co_filename)
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
455 self.assertEqual(50, frame.tb_lineno)
336
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
456
418
c478a6fa9e77 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
457 def test_error_getitem_undefined_string(self):
c478a6fa9e77 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
458 class Something(object):
c478a6fa9e77 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
459 def __repr__(self):
c478a6fa9e77 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
460 return '<Something>'
442
97544725bb7f 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,
97544725bb7f 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
c478a6fa9e77 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
463 try:
c478a6fa9e77 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
464 expr.evaluate({'something': Something()})
c478a6fa9e77 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
465 self.fail('Expected UndefinedError')
c478a6fa9e77 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
466 except UndefinedError, e:
473
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
467 self.assertEqual('<Something> has no member named "nil"', str(e))
418
c478a6fa9e77 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
468 exc_type, exc_value, exc_traceback = sys.exc_info()
473
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
469 search_string = '''<Expression 'something["nil"]'>'''
418
c478a6fa9e77 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
470 frame = exc_traceback.tb_next
c478a6fa9e77 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
471 while frame.tb_next:
c478a6fa9e77 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
472 frame = frame.tb_next
473
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
473 code = frame.tb_frame.f_code
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
474 if code.co_name == search_string:
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
475 break
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
476 else:
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
477 self.fail("never found the frame I was looking for")
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
478 self.assertEqual('index.html', code.co_filename)
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
479 self.assertEqual(50, frame.tb_lineno)
336
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
480
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
481
405
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
482 class SuiteTestCase(unittest.TestCase):
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
483
715
b5bd8c109209 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 702
diff changeset
484 def test_pickle(self):
b5bd8c109209 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 702
diff changeset
485 suite = Suite('foo = 42')
934
31bbb6f9e87b Merge r1142 from py3k:
hodgestar
parents: 897
diff changeset
486 buf = BytesIO()
715
b5bd8c109209 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 702
diff changeset
487 pickle.dump(suite, buf, 2)
b5bd8c109209 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 702
diff changeset
488 buf.seek(0)
b5bd8c109209 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 702
diff changeset
489 unpickled = pickle.load(buf)
b5bd8c109209 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 702
diff changeset
490 data = {}
b5bd8c109209 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 702
diff changeset
491 unpickled.execute(data)
b5bd8c109209 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 702
diff changeset
492 self.assertEqual(42, data['foo'])
b5bd8c109209 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 702
diff changeset
493
682
0653f6c1ffdf 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):
0653f6c1ffdf 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
0653f6c1ffdf 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
0653f6c1ffdf 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
0653f6c1ffdf 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
0653f6c1ffdf 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
0653f6c1ffdf 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 = []
0653f6c1ffdf 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
0653f6c1ffdf 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 """)
0653f6c1ffdf 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}
0653f6c1ffdf 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)
0653f6c1ffdf 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'])
0653f6c1ffdf 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
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
507 def test_assign(self):
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
508 suite = Suite("foo = 42")
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
509 data = {}
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
510 suite.execute(data)
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
511 self.assertEqual(42, data['foo'])
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
512
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
513 def test_def(self):
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
514 suite = Suite("def donothing(): pass")
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
515 data = {}
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
516 suite.execute(data)
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
517 assert 'donothing' in data
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
518 self.assertEqual(None, data['donothing']())
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
519
473
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
520 def test_def_with_multiple_statements(self):
586
3670ea49c65a Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
521 suite = Suite("""
3670ea49c65a 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
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
523 if True:
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
524 return foo
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
525 """)
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
526 data = {'foo': 'bar'}
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
527 suite.execute(data)
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
528 assert 'donothing' in data
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
529 self.assertEqual('bar', data['donothing']())
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
530
586
3670ea49c65a 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):
3670ea49c65a Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
532 suite = Suite("""
3670ea49c65a Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
533 values = []
3670ea49c65a 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):
3670ea49c65a 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:
3670ea49c65a 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)
3670ea49c65a Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
537 add('foo')
3670ea49c65a Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
538 add('bar')
3670ea49c65a Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
539 """)
3670ea49c65a Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
540 data = {}
3670ea49c65a 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)
3670ea49c65a 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'])
3670ea49c65a Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
543
802
47f715774b12 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):
47f715774b12 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("""
47f715774b12 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):
47f715774b12 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
47f715774b12 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)
47f715774b12 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)
47f715774b12 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 """)
47f715774b12 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 = {}
47f715774b12 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)
47f715774b12 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'])
47f715774b12 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'])
47f715774b12 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
47f715774b12 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):
47f715774b12 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("""
47f715774b12 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):
47f715774b12 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
47f715774b12 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)
47f715774b12 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)
47f715774b12 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()
47f715774b12 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 """)
47f715774b12 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 = {}
47f715774b12 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)
47f715774b12 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'])
47f715774b12 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'])
47f715774b12 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'])
47f715774b12 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
47f715774b12 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):
47f715774b12 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("""
47f715774b12 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):
47f715774b12 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
47f715774b12 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:
47f715774b12 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
47f715774b12 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
47f715774b12 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)
47f715774b12 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 """)
47f715774b12 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 = {}
47f715774b12 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)
47f715774b12 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'])
47f715774b12 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
47f715774b12 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):
47f715774b12 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("""
47f715774b12 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):
47f715774b12 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()]
47f715774b12 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')
47f715774b12 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 """)
47f715774b12 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 = {}
47f715774b12 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)
967
19ac5d8fd96c Fix a number of tests which Python's new hash randomization is causing to fail randomly.
hodgestar
parents: 934
diff changeset
591 self.assertEqual(['bardef', 'fooabc'], sorted(data['x']))
802
47f715774b12 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
1024
a5e09a7ba12d Add support for kwonlyargs and kw_defaults attributes of AST argument nodes.
hodgestar
parents: 968
diff changeset
593 if not IS_PYTHON2:
a5e09a7ba12d Add support for kwonlyargs and kw_defaults attributes of AST argument nodes.
hodgestar
parents: 968
diff changeset
594 def test_def_kwonlyarg(self):
a5e09a7ba12d Add support for kwonlyargs and kw_defaults attributes of AST argument nodes.
hodgestar
parents: 968
diff changeset
595 suite = Suite("""
a5e09a7ba12d Add support for kwonlyargs and kw_defaults attributes of AST argument nodes.
hodgestar
parents: 968
diff changeset
596 def kwonly(*args, k):
a5e09a7ba12d Add support for kwonlyargs and kw_defaults attributes of AST argument nodes.
hodgestar
parents: 968
diff changeset
597 return k
a5e09a7ba12d Add support for kwonlyargs and kw_defaults attributes of AST argument nodes.
hodgestar
parents: 968
diff changeset
598 x = kwonly(k="foo")
a5e09a7ba12d Add support for kwonlyargs and kw_defaults attributes of AST argument nodes.
hodgestar
parents: 968
diff changeset
599 """)
a5e09a7ba12d Add support for kwonlyargs and kw_defaults attributes of AST argument nodes.
hodgestar
parents: 968
diff changeset
600 data = {}
a5e09a7ba12d Add support for kwonlyargs and kw_defaults attributes of AST argument nodes.
hodgestar
parents: 968
diff changeset
601 suite.execute(data)
a5e09a7ba12d Add support for kwonlyargs and kw_defaults attributes of AST argument nodes.
hodgestar
parents: 968
diff changeset
602 self.assertEqual("foo", data['x'])
a5e09a7ba12d Add support for kwonlyargs and kw_defaults attributes of AST argument nodes.
hodgestar
parents: 968
diff changeset
603
a5e09a7ba12d Add support for kwonlyargs and kw_defaults attributes of AST argument nodes.
hodgestar
parents: 968
diff changeset
604 def test_def_kwonlyarg_with_default(self):
a5e09a7ba12d Add support for kwonlyargs and kw_defaults attributes of AST argument nodes.
hodgestar
parents: 968
diff changeset
605 suite = Suite("""
a5e09a7ba12d Add support for kwonlyargs and kw_defaults attributes of AST argument nodes.
hodgestar
parents: 968
diff changeset
606 def kwonly(*args, k="bar"):
a5e09a7ba12d Add support for kwonlyargs and kw_defaults attributes of AST argument nodes.
hodgestar
parents: 968
diff changeset
607 return k
a5e09a7ba12d Add support for kwonlyargs and kw_defaults attributes of AST argument nodes.
hodgestar
parents: 968
diff changeset
608 x = kwonly(k="foo")
a5e09a7ba12d Add support for kwonlyargs and kw_defaults attributes of AST argument nodes.
hodgestar
parents: 968
diff changeset
609 y = kwonly()
a5e09a7ba12d Add support for kwonlyargs and kw_defaults attributes of AST argument nodes.
hodgestar
parents: 968
diff changeset
610 """)
a5e09a7ba12d Add support for kwonlyargs and kw_defaults attributes of AST argument nodes.
hodgestar
parents: 968
diff changeset
611 data = {}
a5e09a7ba12d Add support for kwonlyargs and kw_defaults attributes of AST argument nodes.
hodgestar
parents: 968
diff changeset
612 suite.execute(data)
a5e09a7ba12d Add support for kwonlyargs and kw_defaults attributes of AST argument nodes.
hodgestar
parents: 968
diff changeset
613 self.assertEqual("foo", data['x'])
a5e09a7ba12d Add support for kwonlyargs and kw_defaults attributes of AST argument nodes.
hodgestar
parents: 968
diff changeset
614 self.assertEqual("bar", data['y'])
a5e09a7ba12d Add support for kwonlyargs and kw_defaults attributes of AST argument nodes.
hodgestar
parents: 968
diff changeset
615
586
3670ea49c65a Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
616 def test_def_nested(self):
3670ea49c65a Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
617 suite = Suite("""
3670ea49c65a Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
618 def doit():
3670ea49c65a Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
619 values = []
3670ea49c65a Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
620 def add(value):
3670ea49c65a Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
621 if value not in values:
3670ea49c65a Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
622 values.append(value)
3670ea49c65a Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
623 add('foo')
3670ea49c65a Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
624 add('bar')
3670ea49c65a Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
625 return values
3670ea49c65a Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
626 x = doit()
3670ea49c65a Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
627 """)
3670ea49c65a Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
628 data = {}
3670ea49c65a Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
629 suite.execute(data)
3670ea49c65a Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
630 self.assertEqual(['foo', 'bar'], data['x'])
3670ea49c65a Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
631
879
fe5b8e29fda8 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
632 def test_def_with_decorator(self):
fe5b8e29fda8 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
633 suite = Suite("""
fe5b8e29fda8 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
634 def lower(fun):
fe5b8e29fda8 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
635 return lambda: fun().lower()
fe5b8e29fda8 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
636
fe5b8e29fda8 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
637 @lower
fe5b8e29fda8 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
638 def say_hi():
fe5b8e29fda8 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
639 return 'Hi!'
fe5b8e29fda8 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
640
fe5b8e29fda8 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
641 result = say_hi()
fe5b8e29fda8 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
642 """)
fe5b8e29fda8 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
643 data = {}
fe5b8e29fda8 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
644 suite.execute(data)
fe5b8e29fda8 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
645 self.assertEqual('hi!', data['result'])
fe5b8e29fda8 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
646
405
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
647 def test_delete(self):
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
648 suite = Suite("""foo = 42
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
649 del foo
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
650 """)
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
651 data = {}
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
652 suite.execute(data)
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
653 assert 'foo' not in data
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
654
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
655 def test_class(self):
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
656 suite = Suite("class plain(object): pass")
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
657 data = {}
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
658 suite.execute(data)
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
659 assert 'plain' in data
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
660
586
3670ea49c65a Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
661 def test_class_in_def(self):
3670ea49c65a Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
662 suite = Suite("""
3670ea49c65a Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
663 def create():
3670ea49c65a Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
664 class Foobar(object):
3670ea49c65a Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
665 def __str__(self):
3670ea49c65a Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
666 return 'foobar'
3670ea49c65a Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
667 return Foobar()
3670ea49c65a Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
668 x = create()
3670ea49c65a Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
669 """)
3670ea49c65a Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
670 data = {}
3670ea49c65a Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
671 suite.execute(data)
3670ea49c65a Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
672 self.assertEqual('foobar', str(data['x']))
3670ea49c65a Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents: 582
diff changeset
673
568
461d9d0e85a4 AST transformer needs to also handle the class body.
cmlenz
parents: 473
diff changeset
674 def test_class_with_methods(self):
461d9d0e85a4 AST transformer needs to also handle the class body.
cmlenz
parents: 473
diff changeset
675 suite = Suite("""class plain(object):
461d9d0e85a4 AST transformer needs to also handle the class body.
cmlenz
parents: 473
diff changeset
676 def donothing():
461d9d0e85a4 AST transformer needs to also handle the class body.
cmlenz
parents: 473
diff changeset
677 pass
461d9d0e85a4 AST transformer needs to also handle the class body.
cmlenz
parents: 473
diff changeset
678 """)
461d9d0e85a4 AST transformer needs to also handle the class body.
cmlenz
parents: 473
diff changeset
679 data = {}
461d9d0e85a4 AST transformer needs to also handle the class body.
cmlenz
parents: 473
diff changeset
680 suite.execute(data)
461d9d0e85a4 AST transformer needs to also handle the class body.
cmlenz
parents: 473
diff changeset
681 assert 'plain' in data
461d9d0e85a4 AST transformer needs to also handle the class body.
cmlenz
parents: 473
diff changeset
682
405
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
683 def test_import(self):
934
31bbb6f9e87b Merge r1142 from py3k:
hodgestar
parents: 897
diff changeset
684 suite = Suite("from itertools import repeat")
405
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
685 data = {}
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
686 suite.execute(data)
934
31bbb6f9e87b Merge r1142 from py3k:
hodgestar
parents: 897
diff changeset
687 assert 'repeat' in data
405
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
688
731
01bdf155db95 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
689 def test_import_star(self):
01bdf155db95 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
690 suite = Suite("from itertools import *")
736
a1e8b24bc1bb Yet another followup fix for #221.
cmlenz
parents: 731
diff changeset
691 data = Context()
750
52219748e5c1 Remove some cruft for supporting Python 2.3.
cmlenz
parents: 739
diff changeset
692 suite.execute(data)
934
31bbb6f9e87b Merge r1142 from py3k:
hodgestar
parents: 897
diff changeset
693 assert 'repeat' in data
731
01bdf155db95 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
694
807
ef9cc8988260 Added test for [1008].
cmlenz
parents: 803
diff changeset
695 def test_import_in_def(self):
ef9cc8988260 Added test for [1008].
cmlenz
parents: 803
diff changeset
696 suite = Suite("""def fun():
934
31bbb6f9e87b Merge r1142 from py3k:
hodgestar
parents: 897
diff changeset
697 from itertools import repeat
31bbb6f9e87b Merge r1142 from py3k:
hodgestar
parents: 897
diff changeset
698 return repeat(1, 3)
807
ef9cc8988260 Added test for [1008].
cmlenz
parents: 803
diff changeset
699 """)
ef9cc8988260 Added test for [1008].
cmlenz
parents: 803
diff changeset
700 data = Context()
ef9cc8988260 Added test for [1008].
cmlenz
parents: 803
diff changeset
701 suite.execute(data)
934
31bbb6f9e87b Merge r1142 from py3k:
hodgestar
parents: 897
diff changeset
702 assert 'repeat' not in data
31bbb6f9e87b Merge r1142 from py3k:
hodgestar
parents: 897
diff changeset
703 self.assertEqual([1, 1, 1], list(data['fun']()))
807
ef9cc8988260 Added test for [1008].
cmlenz
parents: 803
diff changeset
704
405
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
705 def test_for(self):
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
706 suite = Suite("""x = []
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
707 for i in range(3):
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
708 x.append(i**2)
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
709 """)
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
710 data = {}
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
711 suite.execute(data)
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
712 self.assertEqual([0, 1, 4], data['x'])
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
713
803
f8ca33751678 `for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents: 802
diff changeset
714 def test_for_in_def(self):
f8ca33751678 `for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents: 802
diff changeset
715 suite = Suite("""def loop():
f8ca33751678 `for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents: 802
diff changeset
716 for i in range(10):
f8ca33751678 `for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents: 802
diff changeset
717 if i == 5:
f8ca33751678 `for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents: 802
diff changeset
718 break
f8ca33751678 `for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents: 802
diff changeset
719 return i
f8ca33751678 `for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents: 802
diff changeset
720 """)
f8ca33751678 `for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents: 802
diff changeset
721 data = {}
f8ca33751678 `for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents: 802
diff changeset
722 suite.execute(data)
f8ca33751678 `for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents: 802
diff changeset
723 assert 'loop' in data
f8ca33751678 `for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents: 802
diff changeset
724 self.assertEqual(5, data['loop']())
f8ca33751678 `for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents: 802
diff changeset
725
405
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
726 def test_if(self):
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
727 suite = Suite("""if foo == 42:
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
728 x = True
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
729 """)
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
730 data = {'foo': 42}
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
731 suite.execute(data)
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
732 self.assertEqual(True, data['x'])
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
733
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
734 def test_raise(self):
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
735 suite = Suite("""raise NotImplementedError""")
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
736 self.assertRaises(NotImplementedError, suite.execute, {})
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
737
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
738 def test_try_except(self):
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
739 suite = Suite("""try:
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
740 import somemod
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
741 except ImportError:
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
742 somemod = None
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
743 else:
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
744 somemod.dosth()""")
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
745 data = {}
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
746 suite.execute(data)
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
747 self.assertEqual(None, data['somemod'])
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
748
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
749 def test_finally(self):
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
750 suite = Suite("""try:
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
751 x = 2
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
752 finally:
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
753 x = None
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
754 """)
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
755 data = {}
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
756 suite.execute(data)
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
757 self.assertEqual(None, data['x'])
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
758
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
759 def test_while_break(self):
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
760 suite = Suite("""x = 0
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
761 while x < 5:
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
762 x += step
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
763 if x == 4:
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
764 break
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
765 """)
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
766 data = {'step': 2}
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
767 suite.execute(data)
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
768 self.assertEqual(4, data['x'])
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
769
473
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
770 def test_augmented_attribute_assignment(self):
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
771 suite = Suite("d['k'] += 42")
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
772 d = {"k": 1}
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
773 suite.execute({"d": d})
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
774 self.assertEqual(43, d["k"])
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
775
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
776 def test_local_augmented_assign(self):
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
777 Suite("x = 1; x += 42; assert x == 43").execute({})
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
778
579
2f7f6b70d5b9 Fix for augmented assignments to local variables. Thanks to Erik Bray for reporting the problem.
cmlenz
parents: 569
diff changeset
779 def test_augmented_assign_in_def(self):
2f7f6b70d5b9 Fix for augmented assignments to local variables. Thanks to Erik Bray for reporting the problem.
cmlenz
parents: 569
diff changeset
780 d = {}
2f7f6b70d5b9 Fix for augmented assignments to local variables. Thanks to Erik Bray for reporting the problem.
cmlenz
parents: 569
diff changeset
781 Suite("""def foo():
2f7f6b70d5b9 Fix for augmented assignments to local variables. Thanks to Erik Bray for reporting the problem.
cmlenz
parents: 569
diff changeset
782 i = 1
2f7f6b70d5b9 Fix for augmented assignments to local variables. Thanks to Erik Bray for reporting the problem.
cmlenz
parents: 569
diff changeset
783 i += 1
2f7f6b70d5b9 Fix for augmented assignments to local variables. Thanks to Erik Bray for reporting the problem.
cmlenz
parents: 569
diff changeset
784 return i
2f7f6b70d5b9 Fix for augmented assignments to local variables. Thanks to Erik Bray for reporting the problem.
cmlenz
parents: 569
diff changeset
785 x = foo()""").execute(d)
2f7f6b70d5b9 Fix for augmented assignments to local variables. Thanks to Erik Bray for reporting the problem.
cmlenz
parents: 569
diff changeset
786 self.assertEqual(2, d['x'])
2f7f6b70d5b9 Fix for augmented assignments to local variables. Thanks to Erik Bray for reporting the problem.
cmlenz
parents: 569
diff changeset
787
582
0dc024715f93 Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents: 579
diff changeset
788 def test_augmented_assign_in_loop_in_def(self):
0dc024715f93 Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents: 579
diff changeset
789 d = {}
0dc024715f93 Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents: 579
diff changeset
790 Suite("""def foo():
0dc024715f93 Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents: 579
diff changeset
791 i = 0
0dc024715f93 Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents: 579
diff changeset
792 for n in range(5):
0dc024715f93 Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents: 579
diff changeset
793 i += n
0dc024715f93 Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents: 579
diff changeset
794 return i
0dc024715f93 Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents: 579
diff changeset
795 x = foo()""").execute(d)
0dc024715f93 Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents: 579
diff changeset
796 self.assertEqual(10, d['x'])
0dc024715f93 Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents: 579
diff changeset
797
473
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
798 def test_assign_in_list(self):
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
799 suite = Suite("[d['k']] = 'foo',; assert d['k'] == 'foo'")
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
800 d = {"k": "bar"}
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
801 suite.execute({"d": d})
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
802 self.assertEqual("foo", d["k"])
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
803
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
804 def test_exec(self):
934
31bbb6f9e87b Merge r1142 from py3k:
hodgestar
parents: 897
diff changeset
805 suite = Suite("x = 1; exec(d['k']); assert x == 42, x")
473
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
806 suite.execute({"d": {"k": "x = 42"}})
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
807
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
808 def test_return(self):
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
809 suite = Suite("""
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
810 def f():
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
811 return v
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
812
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
813 assert f() == 42
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
814 """)
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
815 suite.execute({"v": 42})
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
816
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
817 def test_assign_to_dict_item(self):
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
818 suite = Suite("d['k'] = 'foo'")
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
819 data = {'d': {}}
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
820 suite.execute(data)
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
821 self.assertEqual('foo', data['d']['k'])
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
822
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
823 def test_assign_to_attribute(self):
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
824 class Something(object): pass
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
825 something = Something()
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
826 suite = Suite("obj.attr = 'foo'")
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
827 data = {"obj": something}
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
828 suite.execute(data)
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
829 self.assertEqual('foo', something.attr)
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
830
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
831 def test_delattr(self):
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
832 class Something(object):
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
833 def __init__(self):
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
834 self.attr = 'foo'
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
835 obj = Something()
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
836 Suite("del obj.attr").execute({'obj': obj})
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
837 self.failIf(hasattr(obj, 'attr'))
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
838
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
839 def test_delitem(self):
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
840 d = {'k': 'foo'}
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
841 Suite("del d['k']").execute({'d': d})
793
be88c77839fc Get rid of a couple more -3 warnings.
cmlenz
parents: 750
diff changeset
842 self.failIf('k' in d, repr(d))
473
4ed941aa0cbf Apply patch from #113, also closing #114.
cmlenz
parents: 442
diff changeset
843
867
82ba3198d913 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
844 if sys.version_info >= (2, 5):
82ba3198d913 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
845 def test_with_statement(self):
868
eb80cbd8e4fe 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
846 fd, path = mkstemp()
eb80cbd8e4fe 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
847 f = os.fdopen(fd, "w")
eb80cbd8e4fe 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
848 try:
eb80cbd8e4fe 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
849 f.write('foo\nbar\n')
eb80cbd8e4fe 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
850 f.seek(0)
eb80cbd8e4fe 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
851 f.close()
867
82ba3198d913 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
852
868
eb80cbd8e4fe 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
853 d = {'path': path}
eb80cbd8e4fe 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
854 suite = Suite("""from __future__ import with_statement
867
82ba3198d913 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
855 lines = []
82ba3198d913 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
856 with open(path) as file:
82ba3198d913 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
857 for line in file:
82ba3198d913 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
858 lines.append(line)
82ba3198d913 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
859 """)
868
eb80cbd8e4fe 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
860 suite.execute(d)
eb80cbd8e4fe 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
861 self.assertEqual(['foo\n', 'bar\n'], d['lines'])
eb80cbd8e4fe 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
862 finally:
eb80cbd8e4fe 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
863 os.remove(path)
867
82ba3198d913 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
864
82ba3198d913 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
865 def test_yield_expression(self):
82ba3198d913 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
866 d = {}
934
31bbb6f9e87b Merge r1142 from py3k:
hodgestar
parents: 897
diff changeset
867 suite = Suite("""from genshi.compat import next
31bbb6f9e87b Merge r1142 from py3k:
hodgestar
parents: 897
diff changeset
868 results = []
867
82ba3198d913 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
869 def counter(maximum):
82ba3198d913 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
870 i = 0
82ba3198d913 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
871 while i < maximum:
82ba3198d913 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
872 val = (yield i)
82ba3198d913 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
873 if val is not None:
82ba3198d913 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
874 i = val
82ba3198d913 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
875 else:
82ba3198d913 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
876 i += 1
82ba3198d913 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
877 it = counter(5)
934
31bbb6f9e87b Merge r1142 from py3k:
hodgestar
parents: 897
diff changeset
878 results.append(next(it))
867
82ba3198d913 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
879 results.append(it.send(3))
934
31bbb6f9e87b Merge r1142 from py3k:
hodgestar
parents: 897
diff changeset
880 results.append(next(it))
867
82ba3198d913 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
881 """)
82ba3198d913 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
882 suite.execute(d)
82ba3198d913 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
883 self.assertEqual([0, 3, 4], d['results'])
82ba3198d913 Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents: 854
diff changeset
884
968
2ed9827596a4 Update genshi.template AST handling to support Python 3.4 (see #537).
hodgestar
parents: 967
diff changeset
885 if sys.version_info >= (3, 3):
2ed9827596a4 Update genshi.template AST handling to support Python 3.4 (see #537).
hodgestar
parents: 967
diff changeset
886 def test_with_statement_with_multiple_items(self):
2ed9827596a4 Update genshi.template AST handling to support Python 3.4 (see #537).
hodgestar
parents: 967
diff changeset
887 fd, path = mkstemp()
2ed9827596a4 Update genshi.template AST handling to support Python 3.4 (see #537).
hodgestar
parents: 967
diff changeset
888 f = os.fdopen(fd, "w")
2ed9827596a4 Update genshi.template AST handling to support Python 3.4 (see #537).
hodgestar
parents: 967
diff changeset
889 try:
2ed9827596a4 Update genshi.template AST handling to support Python 3.4 (see #537).
hodgestar
parents: 967
diff changeset
890 f.write('foo\n')
2ed9827596a4 Update genshi.template AST handling to support Python 3.4 (see #537).
hodgestar
parents: 967
diff changeset
891 f.seek(0)
2ed9827596a4 Update genshi.template AST handling to support Python 3.4 (see #537).
hodgestar
parents: 967
diff changeset
892 f.close()
2ed9827596a4 Update genshi.template AST handling to support Python 3.4 (see #537).
hodgestar
parents: 967
diff changeset
893
2ed9827596a4 Update genshi.template AST handling to support Python 3.4 (see #537).
hodgestar
parents: 967
diff changeset
894 d = {'path': path}
2ed9827596a4 Update genshi.template AST handling to support Python 3.4 (see #537).
hodgestar
parents: 967
diff changeset
895 suite = Suite("""from __future__ import with_statement
2ed9827596a4 Update genshi.template AST handling to support Python 3.4 (see #537).
hodgestar
parents: 967
diff changeset
896 lines = []
2ed9827596a4 Update genshi.template AST handling to support Python 3.4 (see #537).
hodgestar
parents: 967
diff changeset
897 with open(path) as file1, open(path) as file2:
2ed9827596a4 Update genshi.template AST handling to support Python 3.4 (see #537).
hodgestar
parents: 967
diff changeset
898 for line in file1:
2ed9827596a4 Update genshi.template AST handling to support Python 3.4 (see #537).
hodgestar
parents: 967
diff changeset
899 lines.append(line)
2ed9827596a4 Update genshi.template AST handling to support Python 3.4 (see #537).
hodgestar
parents: 967
diff changeset
900 for line in file2:
2ed9827596a4 Update genshi.template AST handling to support Python 3.4 (see #537).
hodgestar
parents: 967
diff changeset
901 lines.append(line)
2ed9827596a4 Update genshi.template AST handling to support Python 3.4 (see #537).
hodgestar
parents: 967
diff changeset
902 """)
2ed9827596a4 Update genshi.template AST handling to support Python 3.4 (see #537).
hodgestar
parents: 967
diff changeset
903 suite.execute(d)
2ed9827596a4 Update genshi.template AST handling to support Python 3.4 (see #537).
hodgestar
parents: 967
diff changeset
904 self.assertEqual(['foo\n', 'foo\n'], d['lines'])
2ed9827596a4 Update genshi.template AST handling to support Python 3.4 (see #537).
hodgestar
parents: 967
diff changeset
905 finally:
2ed9827596a4 Update genshi.template AST handling to support Python 3.4 (see #537).
hodgestar
parents: 967
diff changeset
906 os.remove(path)
2ed9827596a4 Update genshi.template AST handling to support Python 3.4 (see #537).
hodgestar
parents: 967
diff changeset
907
405
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
908
336
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
909 def suite():
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
910 suite = unittest.TestSuite()
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
911 suite.addTest(doctest.DocTestSuite(Expression.__module__))
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
912 suite.addTest(unittest.makeSuite(ExpressionTestCase, 'test'))
405
5340931530e2 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
913 suite.addTest(unittest.makeSuite(SuiteTestCase, 'test'))
336
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
914 return suite
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
915
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
916 if __name__ == '__main__':
7763f7aec949 Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
917 unittest.main(defaultTest='suite')
Copyright (C) 2012-2017 Edgewall Software