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