Mercurial > genshi > genshi-test
annotate genshi/template/tests/eval.py @ 879:27d210790d27
Looks like the 'decorators' field in the AST was renamed to 'decorator_list' in Python 2.6, so look for both. See #379.
author | cmlenz |
---|---|
date | Thu, 15 Apr 2010 21:38:21 +0000 |
parents | 9309530ee279 |
children | 248d6671a7e1 |
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 # |
854
0d9e87c6cf6e
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
845
diff
changeset
|
3 # Copyright (C) 2006-2009 Edgewall Software |
336
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
4 # All rights reserved. |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
5 # |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
6 # This software is licensed as described in the file COPYING, which |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
7 # you should have received as part of this distribution. The terms |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
8 # are also available at http://genshi.edgewall.org/wiki/License. |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
9 # |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
10 # This software consists of voluntary contributions made by many |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
11 # individuals. For the exact contribution history, see the revision |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
12 # history and logs, available at http://genshi.edgewall.org/log/. |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
13 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
14 import doctest |
867
efc03d0719f3
Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents:
854
diff
changeset
|
15 import os |
715 | 16 import pickle |
17 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
|
18 import sys |
868
9309530ee279
Fix the unit test for execution of statements containing with statements so that it also works on Windows. Thanks to cboos for the patch.
cmlenz
parents:
867
diff
changeset
|
19 from tempfile import mkstemp |
336
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
20 import unittest |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
21 |
401
9582328f82e5
Make the `Markup` class available by default in template expressions. Closes #67.
cmlenz
parents:
396
diff
changeset
|
22 from genshi.core import Markup |
750 | 23 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
|
24 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
|
25 UNDEFINED |
336
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
26 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
27 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
28 class ExpressionTestCase(unittest.TestCase): |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
29 |
340 | 30 def test_eq(self): |
31 expr = Expression('x,y') | |
32 self.assertEqual(expr, Expression('x,y')) | |
33 self.assertNotEqual(expr, Expression('y, x')) | |
34 | |
35 def test_hash(self): | |
36 expr = Expression('x,y') | |
37 self.assertEqual(hash(expr), hash(Expression('x,y'))) | |
38 self.assertNotEqual(hash(expr), hash(Expression('y, x'))) | |
39 | |
715 | 40 def test_pickle(self): |
41 expr = Expression('1 < 2') | |
42 buf = StringIO() | |
43 pickle.dump(expr, buf, 2) | |
44 buf.seek(0) | |
45 unpickled = pickle.load(buf) | |
46 assert unpickled.evaluate({}) is True | |
47 | |
336
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
48 def test_name_lookup(self): |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
49 self.assertEqual('bar', Expression('foo').evaluate({'foo': 'bar'})) |
343
4ff2338e89cd
Remove automatic calling of expression evaluation results if they are callable. See [http://groups.google.com/group/genshi/browse_thread/thread/f515986760918d41 this mailing list thread].
cmlenz
parents:
340
diff
changeset
|
50 self.assertEqual(id, Expression('id').evaluate({})) |
336
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
51 self.assertEqual('bar', Expression('id').evaluate({'id': 'bar'})) |
343
4ff2338e89cd
Remove automatic calling of expression evaluation results if they are callable. See [http://groups.google.com/group/genshi/browse_thread/thread/f515986760918d41 this mailing list thread].
cmlenz
parents:
340
diff
changeset
|
52 self.assertEqual(None, Expression('id').evaluate({'id': None})) |
336
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
53 |
401
9582328f82e5
Make the `Markup` class available by default in template expressions. Closes #67.
cmlenz
parents:
396
diff
changeset
|
54 def test_builtins(self): |
9582328f82e5
Make the `Markup` class available by default in template expressions. Closes #67.
cmlenz
parents:
396
diff
changeset
|
55 expr = Expression('Markup') |
9582328f82e5
Make the `Markup` class available by default in template expressions. Closes #67.
cmlenz
parents:
396
diff
changeset
|
56 self.assertEqual(expr.evaluate({}), Markup) |
9582328f82e5
Make the `Markup` class available by default in template expressions. Closes #67.
cmlenz
parents:
396
diff
changeset
|
57 |
336
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
58 def test_str_literal(self): |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
59 self.assertEqual('foo', Expression('"foo"').evaluate({})) |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
60 self.assertEqual('foo', Expression('"""foo"""').evaluate({})) |
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("'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("'''foo'''").evaluate({})) |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
63 self.assertEqual('foo', Expression("u'foo'").evaluate({})) |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
64 self.assertEqual('foo', Expression("r'foo'").evaluate({})) |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
65 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
66 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
|
67 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
|
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("u'\xfe'") |
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 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
|
72 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
|
73 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
74 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
|
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(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
|
77 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
|
78 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
|
79 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
|
80 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
|
81 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
82 def test_dict_literal(self): |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
83 self.assertEqual({}, Expression("{}").evaluate({})) |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
84 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
|
85 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
|
86 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
87 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
|
88 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
|
89 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
|
90 self.assertEqual([True], |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
91 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
|
92 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
93 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
|
94 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
|
95 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
|
96 self.assertEqual((True,), |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
97 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
|
98 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
99 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
|
100 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
|
101 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
|
102 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
103 def test_unaryop_neg(self): |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
104 self.assertEqual(-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
|
105 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
|
106 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
107 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
|
108 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
|
109 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
|
110 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
111 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
|
112 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
|
113 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
|
114 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
115 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
|
116 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
|
117 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
|
118 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
119 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
|
120 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
|
121 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
|
122 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
123 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
|
124 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
|
125 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
|
126 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
127 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
|
128 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
|
129 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
|
130 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
131 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
|
132 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
|
133 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
|
134 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
135 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
|
136 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
|
137 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
|
138 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
139 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
|
140 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
|
141 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
|
142 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
143 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
|
144 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
|
145 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
|
146 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
147 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
|
148 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
|
149 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
|
150 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
151 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
|
152 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
|
153 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
|
154 |
396 | 155 def test_binop_xor(self): |
156 self.assertEqual(1, Expression("1 ^ 0").evaluate({})) | |
157 self.assertEqual(1, Expression("x ^ y").evaluate({'x': 1, 'y': 0})) | |
158 | |
336
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
159 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
|
160 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
|
161 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
|
162 '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
|
163 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
164 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
|
165 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
|
166 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
|
167 '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
|
168 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
169 def test_binop_is(self): |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
170 self.assertEqual(True, Expression("1 is 1").evaluate({})) |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
171 self.assertEqual(True, Expression("x 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
|
172 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
|
173 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
|
174 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
175 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
|
176 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
|
177 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
|
178 'y': 2})) |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
179 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
|
180 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
|
181 'y': 1})) |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
182 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
183 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
|
184 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
|
185 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
|
186 'y': False})) |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
187 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
188 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
|
189 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
|
190 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
|
191 'y': False})) |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
192 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
193 def test_compare_eq(self): |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
194 self.assertEqual(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
|
195 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
|
196 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
197 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
|
198 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
|
199 self.assertEqual(False, Expression("x != y").evaluate({'x': 1, 'y': 1})) |
793 | 200 if sys.version < '3': |
201 self.assertEqual(False, Expression("1 <> 1").evaluate({})) | |
202 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
|
203 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
204 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
|
205 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
|
206 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
|
207 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
208 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
|
209 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
|
210 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
|
211 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
212 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
|
213 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
|
214 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
|
215 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
216 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
|
217 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
|
218 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
|
219 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
220 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
|
221 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
|
222 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
|
223 'y': 3})) |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
224 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
225 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
|
226 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
|
227 data = {'foo': 'bar'} |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
228 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
|
229 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
|
230 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
|
231 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
232 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
|
233 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
|
234 'bar': 42})) |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
235 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
236 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
|
237 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
|
238 'bar': [42]})) |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
239 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
240 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
|
241 def foo(x): |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
242 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
|
243 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
|
244 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
|
245 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
246 def test_lambda(self): |
793 | 247 data = {'items': range(5)} |
248 expr = Expression("filter(lambda x: x > 2, items)") | |
249 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
|
250 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
251 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
|
252 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
|
253 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
|
254 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
255 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
|
256 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
|
257 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
|
258 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
259 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
|
260 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
|
261 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
|
262 |
845
9cabfbc4ac5c
Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents:
807
diff
changeset
|
263 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
|
264 self.assertEqual([0, 1, 0, 1, 2], |
9cabfbc4ac5c
Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents:
807
diff
changeset
|
265 expr.evaluate({'groups': [range(2), range(3)]})) |
9cabfbc4ac5c
Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents:
807
diff
changeset
|
266 |
9cabfbc4ac5c
Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents:
807
diff
changeset
|
267 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
|
268 self.assertEqual([('x0', 'y0'), ('x0', 'y1'), ('x1', 'y0'), ('x1', 'y1')], |
9cabfbc4ac5c
Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents:
807
diff
changeset
|
269 expr.evaluate({'x': ['x0', 'x1'], 'y': ['y0', 'y1']})) |
9cabfbc4ac5c
Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents:
807
diff
changeset
|
270 |
336
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
271 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
|
272 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
|
273 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
|
274 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
|
275 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
276 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
|
277 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
|
278 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
|
279 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
|
280 |
750 | 281 def test_generator_expression(self): |
282 expr = Expression("list(n for n in numbers if n < 2)") | |
283 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
|
284 |
750 | 285 expr = Expression("list((i, n + 1) for i, n in enumerate(numbers))") |
286 self.assertEqual([(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)], | |
287 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
|
288 |
750 | 289 expr = Expression("list(offset + n for n in numbers)") |
290 self.assertEqual([2, 3, 4, 5, 6], | |
291 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
|
292 |
845
9cabfbc4ac5c
Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents:
807
diff
changeset
|
293 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
|
294 self.assertEqual([0, 1, 0, 1, 2], |
9cabfbc4ac5c
Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents:
807
diff
changeset
|
295 expr.evaluate({'groups': [range(2), range(3)]})) |
9cabfbc4ac5c
Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents:
807
diff
changeset
|
296 |
9cabfbc4ac5c
Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents:
807
diff
changeset
|
297 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
|
298 self.assertEqual([('x0', 'y0'), ('x0', 'y1'), ('x1', 'y0'), ('x1', 'y1')], |
9cabfbc4ac5c
Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents:
807
diff
changeset
|
299 expr.evaluate({'x': ['x0', 'x1'], 'y': ['y0', 'y1']})) |
9cabfbc4ac5c
Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents:
807
diff
changeset
|
300 |
750 | 301 def test_generator_expression_with_getattr(self): |
302 items = [{'name': 'a', 'value': 1}, {'name': 'b', 'value': 2}] | |
303 expr = Expression("list(i.name for i in items if i.value > 1)") | |
304 self.assertEqual(['b'], expr.evaluate({'items': items})) | |
305 | |
306 def test_generator_expression_with_getitem(self): | |
307 items = [{'name': 'a', 'value': 1}, {'name': 'b', 'value': 2}] | |
308 expr = Expression("list(i['name'] for i in items if i['value'] > 1)") | |
309 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
|
310 |
393
a056613621ab
add support for Python 2.5 conditional expressions (fixes #74)
mgood
parents:
343
diff
changeset
|
311 if sys.version_info >= (2, 5): |
a056613621ab
add support for Python 2.5 conditional expressions (fixes #74)
mgood
parents:
343
diff
changeset
|
312 def test_conditional_expression(self): |
a056613621ab
add support for Python 2.5 conditional expressions (fixes #74)
mgood
parents:
343
diff
changeset
|
313 expr = Expression("'T' if foo else 'F'") |
a056613621ab
add support for Python 2.5 conditional expressions (fixes #74)
mgood
parents:
343
diff
changeset
|
314 self.assertEqual('T', expr.evaluate({'foo': True})) |
a056613621ab
add support for Python 2.5 conditional expressions (fixes #74)
mgood
parents:
343
diff
changeset
|
315 self.assertEqual('F', expr.evaluate({'foo': False})) |
a056613621ab
add support for Python 2.5 conditional expressions (fixes #74)
mgood
parents:
343
diff
changeset
|
316 |
336
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
317 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
|
318 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
|
319 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
|
320 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
321 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
|
322 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
|
323 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
|
324 'end': 2})) |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
325 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
326 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
|
327 expr = Expression("numbers[:]") |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
328 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
|
329 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
330 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
|
331 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
|
332 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
|
333 'stride': 2})) |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
334 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
335 def test_slice_negative_start(self): |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
336 expr = Expression("numbers[-1:]") |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
337 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
|
338 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
339 def test_slice_negative_end(self): |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
340 expr = Expression("numbers[:-1]") |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
341 self.assertEqual([0, 1, 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
|
342 |
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
|
343 def test_access_undefined(self): |
606
9ada030ad986
Changed the default error handling mode to "strict".
cmlenz
parents:
586
diff
changeset
|
344 expr = Expression("nothing", filename='index.html', lineno=50, |
9ada030ad986
Changed the default error handling mode to "strict".
cmlenz
parents:
586
diff
changeset
|
345 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
|
346 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
|
347 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
|
348 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
|
349 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
|
350 |
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 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
|
352 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
|
353 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
|
354 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
|
355 something = Something() |
606
9ada030ad986
Changed the default error handling mode to "strict".
cmlenz
parents:
586
diff
changeset
|
356 expr = Expression('something.nil', filename='index.html', lineno=50, |
9ada030ad986
Changed the default error handling mode to "strict".
cmlenz
parents:
586
diff
changeset
|
357 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
|
358 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
|
359 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
|
360 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
|
361 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
|
362 |
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
|
363 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
|
364 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
|
365 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
|
366 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
|
367 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
|
368 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
|
369 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
|
370 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
|
371 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
|
372 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
|
373 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
|
374 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
|
375 |
442
ff7c72b52fb2
Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents:
428
diff
changeset
|
376 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
|
377 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
|
378 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
|
379 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
|
380 something = Something() |
606
9ada030ad986
Changed the default error handling mode to "strict".
cmlenz
parents:
586
diff
changeset
|
381 expr = Expression('something["nil"]', filename='index.html', lineno=50, |
9ada030ad986
Changed the default error handling mode to "strict".
cmlenz
parents:
586
diff
changeset
|
382 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
|
383 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
|
384 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
|
385 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
|
386 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
|
387 |
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
|
388 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
|
389 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
|
390 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
|
391 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
|
392 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
|
393 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
|
394 |
336
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
395 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
|
396 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
|
397 lookup='strict') |
336
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
398 try: |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
399 expr.evaluate({}) |
418
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
400 self.fail('Expected UndefinedError') |
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
401 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
|
402 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
|
403 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
|
404 frames = [] |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
405 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
|
406 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
|
407 frames.append(frame) |
418
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
408 self.assertEqual('"nothing" not defined', str(e)) |
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
409 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
|
410 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
|
411 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
|
412 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
|
413 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
|
414 |
418
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
415 def test_error_getattr_undefined(self): |
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
416 class Something(object): |
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
417 def __repr__(self): |
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
418 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
|
419 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
|
420 lookup='strict') |
336
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
421 try: |
418
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
422 expr.evaluate({'something': Something()}) |
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
423 self.fail('Expected UndefinedError') |
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
424 except UndefinedError, e: |
473 | 425 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
|
426 exc_type, exc_value, exc_traceback = sys.exc_info() |
473 | 427 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
|
428 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
|
429 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
|
430 frame = frame.tb_next |
473 | 431 code = frame.tb_frame.f_code |
432 if code.co_name == search_string: | |
433 break | |
434 else: | |
435 self.fail("never found the frame I was looking for") | |
436 self.assertEqual('index.html', code.co_filename) | |
437 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
|
438 |
418
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
439 def test_error_getitem_undefined_string(self): |
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
440 class Something(object): |
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
441 def __repr__(self): |
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
442 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
|
443 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
|
444 lookup='strict') |
418
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
445 try: |
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
446 expr.evaluate({'something': Something()}) |
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
447 self.fail('Expected UndefinedError') |
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
448 except UndefinedError, e: |
473 | 449 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
|
450 exc_type, exc_value, exc_traceback = sys.exc_info() |
473 | 451 search_string = '''<Expression 'something["nil"]'>''' |
418
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
452 frame = exc_traceback.tb_next |
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
453 while frame.tb_next: |
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
454 frame = frame.tb_next |
473 | 455 code = frame.tb_frame.f_code |
456 if code.co_name == search_string: | |
457 break | |
458 else: | |
459 self.fail("never found the frame I was looking for") | |
460 self.assertEqual('index.html', code.co_filename) | |
461 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
|
462 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
463 |
405
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
464 class SuiteTestCase(unittest.TestCase): |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
465 |
715 | 466 def test_pickle(self): |
467 suite = Suite('foo = 42') | |
468 buf = StringIO() | |
469 pickle.dump(suite, buf, 2) | |
470 buf.seek(0) | |
471 unpickled = pickle.load(buf) | |
472 data = {} | |
473 unpickled.execute(data) | |
474 self.assertEqual(42, data['foo']) | |
475 | |
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
|
476 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
|
477 # 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
|
478 # 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
|
479 # 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
|
480 # 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
|
481 # 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
|
482 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
|
483 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
|
484 """) |
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 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
|
486 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
|
487 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
|
488 |
405
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
489 def test_assign(self): |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
490 suite = Suite("foo = 42") |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
491 data = {} |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
492 suite.execute(data) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
493 self.assertEqual(42, data['foo']) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
494 |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
495 def test_def(self): |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
496 suite = Suite("def donothing(): pass") |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
497 data = {} |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
498 suite.execute(data) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
499 assert 'donothing' in data |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
500 self.assertEqual(None, data['donothing']()) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
501 |
473 | 502 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
|
503 suite = Suite(""" |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
504 def donothing(): |
473 | 505 if True: |
506 return foo | |
507 """) | |
508 data = {'foo': 'bar'} | |
509 suite.execute(data) | |
510 assert 'donothing' in data | |
511 self.assertEqual('bar', data['donothing']()) | |
512 | |
586
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
513 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
|
514 suite = Suite(""" |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
515 values = [] |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
516 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
|
517 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
|
518 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
|
519 add('foo') |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
520 add('bar') |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
521 """) |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
522 data = {} |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
523 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
|
524 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
|
525 |
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
|
526 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
|
527 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
|
528 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
|
529 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
|
530 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
|
531 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
|
532 """) |
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 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
|
534 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
|
535 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
|
536 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
|
537 |
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 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
|
539 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
|
540 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
|
541 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
|
542 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
|
543 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
|
544 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
|
545 """) |
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 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
|
547 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
|
548 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
|
549 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
|
550 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
|
551 |
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 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
|
553 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
|
554 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
|
555 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
|
556 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
|
557 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
|
558 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
|
559 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
|
560 """) |
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 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
|
562 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
|
563 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
|
564 |
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 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
|
566 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
|
567 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
|
568 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
|
569 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
|
570 """) |
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 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
|
572 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
|
573 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
|
574 |
586
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
575 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
|
576 suite = Suite(""" |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
577 def doit(): |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
578 values = [] |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
579 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
|
580 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
|
581 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
|
582 add('foo') |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
583 add('bar') |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
584 return values |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
585 x = doit() |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
586 """) |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
587 data = {} |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
588 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
|
589 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
|
590 |
879
27d210790d27
Looks like the 'decorators' field in the AST was renamed to 'decorator_list' in Python 2.6, so look for both. See #379.
cmlenz
parents:
868
diff
changeset
|
591 def test_def_with_decorator(self): |
27d210790d27
Looks like the 'decorators' field in the AST was renamed to 'decorator_list' in Python 2.6, so look for both. See #379.
cmlenz
parents:
868
diff
changeset
|
592 suite = Suite(""" |
27d210790d27
Looks like the 'decorators' field in the AST was renamed to 'decorator_list' in Python 2.6, so look for both. See #379.
cmlenz
parents:
868
diff
changeset
|
593 def lower(fun): |
27d210790d27
Looks like the 'decorators' field in the AST was renamed to 'decorator_list' in Python 2.6, so look for both. See #379.
cmlenz
parents:
868
diff
changeset
|
594 return lambda: fun().lower() |
27d210790d27
Looks like the 'decorators' field in the AST was renamed to 'decorator_list' in Python 2.6, so look for both. See #379.
cmlenz
parents:
868
diff
changeset
|
595 |
27d210790d27
Looks like the 'decorators' field in the AST was renamed to 'decorator_list' in Python 2.6, so look for both. See #379.
cmlenz
parents:
868
diff
changeset
|
596 @lower |
27d210790d27
Looks like the 'decorators' field in the AST was renamed to 'decorator_list' in Python 2.6, so look for both. See #379.
cmlenz
parents:
868
diff
changeset
|
597 def say_hi(): |
27d210790d27
Looks like the 'decorators' field in the AST was renamed to 'decorator_list' in Python 2.6, so look for both. See #379.
cmlenz
parents:
868
diff
changeset
|
598 return 'Hi!' |
27d210790d27
Looks like the 'decorators' field in the AST was renamed to 'decorator_list' in Python 2.6, so look for both. See #379.
cmlenz
parents:
868
diff
changeset
|
599 |
27d210790d27
Looks like the 'decorators' field in the AST was renamed to 'decorator_list' in Python 2.6, so look for both. See #379.
cmlenz
parents:
868
diff
changeset
|
600 result = say_hi() |
27d210790d27
Looks like the 'decorators' field in the AST was renamed to 'decorator_list' in Python 2.6, so look for both. See #379.
cmlenz
parents:
868
diff
changeset
|
601 """) |
27d210790d27
Looks like the 'decorators' field in the AST was renamed to 'decorator_list' in Python 2.6, so look for both. See #379.
cmlenz
parents:
868
diff
changeset
|
602 data = {} |
27d210790d27
Looks like the 'decorators' field in the AST was renamed to 'decorator_list' in Python 2.6, so look for both. See #379.
cmlenz
parents:
868
diff
changeset
|
603 suite.execute(data) |
27d210790d27
Looks like the 'decorators' field in the AST was renamed to 'decorator_list' in Python 2.6, so look for both. See #379.
cmlenz
parents:
868
diff
changeset
|
604 self.assertEqual('hi!', data['result']) |
27d210790d27
Looks like the 'decorators' field in the AST was renamed to 'decorator_list' in Python 2.6, so look for both. See #379.
cmlenz
parents:
868
diff
changeset
|
605 |
405
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
606 def test_delete(self): |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
607 suite = Suite("""foo = 42 |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
608 del foo |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
609 """) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
610 data = {} |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
611 suite.execute(data) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
612 assert 'foo' not in data |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
613 |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
614 def test_class(self): |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
615 suite = Suite("class plain(object): pass") |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
616 data = {} |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
617 suite.execute(data) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
618 assert 'plain' in data |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
619 |
586
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
620 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
|
621 suite = Suite(""" |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
622 def create(): |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
623 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
|
624 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
|
625 return 'foobar' |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
626 return Foobar() |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
627 x = create() |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
628 """) |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
629 data = {} |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
630 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
|
631 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
|
632 |
568
8f0d601afc0c
AST transformer needs to also handle the class body.
cmlenz
parents:
473
diff
changeset
|
633 def test_class_with_methods(self): |
8f0d601afc0c
AST transformer needs to also handle the class body.
cmlenz
parents:
473
diff
changeset
|
634 suite = Suite("""class plain(object): |
8f0d601afc0c
AST transformer needs to also handle the class body.
cmlenz
parents:
473
diff
changeset
|
635 def donothing(): |
8f0d601afc0c
AST transformer needs to also handle the class body.
cmlenz
parents:
473
diff
changeset
|
636 pass |
8f0d601afc0c
AST transformer needs to also handle the class body.
cmlenz
parents:
473
diff
changeset
|
637 """) |
8f0d601afc0c
AST transformer needs to also handle the class body.
cmlenz
parents:
473
diff
changeset
|
638 data = {} |
8f0d601afc0c
AST transformer needs to also handle the class body.
cmlenz
parents:
473
diff
changeset
|
639 suite.execute(data) |
8f0d601afc0c
AST transformer needs to also handle the class body.
cmlenz
parents:
473
diff
changeset
|
640 assert 'plain' in data |
8f0d601afc0c
AST transformer needs to also handle the class body.
cmlenz
parents:
473
diff
changeset
|
641 |
405
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
642 def test_import(self): |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
643 suite = Suite("from itertools import ifilter") |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
644 data = {} |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
645 suite.execute(data) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
646 assert 'ifilter' in data |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
647 |
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
|
648 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
|
649 suite = Suite("from itertools import *") |
736 | 650 data = Context() |
750 | 651 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
|
652 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
|
653 |
807 | 654 def test_import_in_def(self): |
655 suite = Suite("""def fun(): | |
656 from itertools import ifilter | |
854
0d9e87c6cf6e
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
845
diff
changeset
|
657 return ifilter(None, range(3)) |
807 | 658 """) |
659 data = Context() | |
660 suite.execute(data) | |
661 assert 'ifilter' not in data | |
662 self.assertEqual([1, 2], list(data['fun']())) | |
663 | |
405
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
664 def test_for(self): |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
665 suite = Suite("""x = [] |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
666 for i in range(3): |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
667 x.append(i**2) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
668 """) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
669 data = {} |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
670 suite.execute(data) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
671 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
|
672 |
803
92b9b647e1fe
`for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents:
802
diff
changeset
|
673 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
|
674 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
|
675 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
|
676 if i == 5: |
92b9b647e1fe
`for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents:
802
diff
changeset
|
677 break |
92b9b647e1fe
`for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents:
802
diff
changeset
|
678 return i |
92b9b647e1fe
`for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents:
802
diff
changeset
|
679 """) |
92b9b647e1fe
`for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents:
802
diff
changeset
|
680 data = {} |
92b9b647e1fe
`for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents:
802
diff
changeset
|
681 suite.execute(data) |
92b9b647e1fe
`for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents:
802
diff
changeset
|
682 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
|
683 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
|
684 |
405
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
685 def test_if(self): |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
686 suite = Suite("""if foo == 42: |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
687 x = True |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
688 """) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
689 data = {'foo': 42} |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
690 suite.execute(data) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
691 self.assertEqual(True, data['x']) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
692 |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
693 def test_raise(self): |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
694 suite = Suite("""raise NotImplementedError""") |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
695 self.assertRaises(NotImplementedError, suite.execute, {}) |
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 def test_try_except(self): |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
698 suite = Suite("""try: |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
699 import somemod |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
700 except ImportError: |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
701 somemod = None |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
702 else: |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
703 somemod.dosth()""") |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
704 data = {} |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
705 suite.execute(data) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
706 self.assertEqual(None, data['somemod']) |
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 def test_finally(self): |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
709 suite = Suite("""try: |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
710 x = 2 |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
711 finally: |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
712 x = None |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
713 """) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
714 data = {} |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
715 suite.execute(data) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
716 self.assertEqual(None, data['x']) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
717 |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
718 def test_while_break(self): |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
719 suite = Suite("""x = 0 |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
720 while x < 5: |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
721 x += step |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
722 if x == 4: |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
723 break |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
724 """) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
725 data = {'step': 2} |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
726 suite.execute(data) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
727 self.assertEqual(4, data['x']) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
728 |
473 | 729 def test_augmented_attribute_assignment(self): |
730 suite = Suite("d['k'] += 42") | |
731 d = {"k": 1} | |
732 suite.execute({"d": d}) | |
733 self.assertEqual(43, d["k"]) | |
734 | |
735 def test_local_augmented_assign(self): | |
736 Suite("x = 1; x += 42; assert x == 43").execute({}) | |
737 | |
579
71e3205925e6
Fix for augmented assignments to local variables. Thanks to Erik Bray for reporting the problem.
cmlenz
parents:
569
diff
changeset
|
738 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
|
739 d = {} |
71e3205925e6
Fix for augmented assignments to local variables. Thanks to Erik Bray for reporting the problem.
cmlenz
parents:
569
diff
changeset
|
740 Suite("""def foo(): |
71e3205925e6
Fix for augmented assignments to local variables. Thanks to Erik Bray for reporting the problem.
cmlenz
parents:
569
diff
changeset
|
741 i = 1 |
71e3205925e6
Fix for augmented assignments to local variables. Thanks to Erik Bray for reporting the problem.
cmlenz
parents:
569
diff
changeset
|
742 i += 1 |
71e3205925e6
Fix for augmented assignments to local variables. Thanks to Erik Bray for reporting the problem.
cmlenz
parents:
569
diff
changeset
|
743 return i |
71e3205925e6
Fix for augmented assignments to local variables. Thanks to Erik Bray for reporting the problem.
cmlenz
parents:
569
diff
changeset
|
744 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
|
745 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
|
746 |
582
b21da79c9bde
Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents:
579
diff
changeset
|
747 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
|
748 d = {} |
b21da79c9bde
Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents:
579
diff
changeset
|
749 Suite("""def foo(): |
b21da79c9bde
Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents:
579
diff
changeset
|
750 i = 0 |
b21da79c9bde
Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents:
579
diff
changeset
|
751 for n in range(5): |
b21da79c9bde
Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents:
579
diff
changeset
|
752 i += n |
b21da79c9bde
Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents:
579
diff
changeset
|
753 return i |
b21da79c9bde
Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents:
579
diff
changeset
|
754 x = foo()""").execute(d) |
b21da79c9bde
Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents:
579
diff
changeset
|
755 self.assertEqual(10, d['x']) |
b21da79c9bde
Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents:
579
diff
changeset
|
756 |
473 | 757 def test_assign_in_list(self): |
758 suite = Suite("[d['k']] = 'foo',; assert d['k'] == 'foo'") | |
759 d = {"k": "bar"} | |
760 suite.execute({"d": d}) | |
761 self.assertEqual("foo", d["k"]) | |
762 | |
763 def test_exec(self): | |
764 suite = Suite("x = 1; exec d['k']; assert x == 42, x") | |
765 suite.execute({"d": {"k": "x = 42"}}) | |
766 | |
767 def test_return(self): | |
768 suite = Suite(""" | |
769 def f(): | |
770 return v | |
771 | |
772 assert f() == 42 | |
773 """) | |
774 suite.execute({"v": 42}) | |
775 | |
776 def test_assign_to_dict_item(self): | |
777 suite = Suite("d['k'] = 'foo'") | |
778 data = {'d': {}} | |
779 suite.execute(data) | |
780 self.assertEqual('foo', data['d']['k']) | |
781 | |
782 def test_assign_to_attribute(self): | |
783 class Something(object): pass | |
784 something = Something() | |
785 suite = Suite("obj.attr = 'foo'") | |
786 data = {"obj": something} | |
787 suite.execute(data) | |
788 self.assertEqual('foo', something.attr) | |
789 | |
790 def test_delattr(self): | |
791 class Something(object): | |
792 def __init__(self): | |
793 self.attr = 'foo' | |
794 obj = Something() | |
795 Suite("del obj.attr").execute({'obj': obj}) | |
796 self.failIf(hasattr(obj, 'attr')) | |
797 | |
798 def test_delitem(self): | |
799 d = {'k': 'foo'} | |
800 Suite("del d['k']").execute({'d': d}) | |
793 | 801 self.failIf('k' in d, repr(d)) |
473 | 802 |
867
efc03d0719f3
Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents:
854
diff
changeset
|
803 if sys.version_info >= (2, 5): |
efc03d0719f3
Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents:
854
diff
changeset
|
804 def test_with_statement(self): |
868
9309530ee279
Fix the unit test for execution of statements containing with statements so that it also works on Windows. Thanks to cboos for the patch.
cmlenz
parents:
867
diff
changeset
|
805 fd, path = mkstemp() |
9309530ee279
Fix the unit test for execution of statements containing with statements so that it also works on Windows. Thanks to cboos for the patch.
cmlenz
parents:
867
diff
changeset
|
806 f = os.fdopen(fd, "w") |
9309530ee279
Fix the unit test for execution of statements containing with statements so that it also works on Windows. Thanks to cboos for the patch.
cmlenz
parents:
867
diff
changeset
|
807 try: |
9309530ee279
Fix the unit test for execution of statements containing with statements so that it also works on Windows. Thanks to cboos for the patch.
cmlenz
parents:
867
diff
changeset
|
808 f.write('foo\nbar\n') |
9309530ee279
Fix the unit test for execution of statements containing with statements so that it also works on Windows. Thanks to cboos for the patch.
cmlenz
parents:
867
diff
changeset
|
809 f.seek(0) |
9309530ee279
Fix the unit test for execution of statements containing with statements so that it also works on Windows. Thanks to cboos for the patch.
cmlenz
parents:
867
diff
changeset
|
810 f.close() |
867
efc03d0719f3
Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents:
854
diff
changeset
|
811 |
868
9309530ee279
Fix the unit test for execution of statements containing with statements so that it also works on Windows. Thanks to cboos for the patch.
cmlenz
parents:
867
diff
changeset
|
812 d = {'path': path} |
9309530ee279
Fix the unit test for execution of statements containing with statements so that it also works on Windows. Thanks to cboos for the patch.
cmlenz
parents:
867
diff
changeset
|
813 suite = Suite("""from __future__ import with_statement |
867
efc03d0719f3
Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents:
854
diff
changeset
|
814 lines = [] |
efc03d0719f3
Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents:
854
diff
changeset
|
815 with open(path) as file: |
efc03d0719f3
Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents:
854
diff
changeset
|
816 for line in file: |
efc03d0719f3
Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents:
854
diff
changeset
|
817 lines.append(line) |
efc03d0719f3
Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents:
854
diff
changeset
|
818 """) |
868
9309530ee279
Fix the unit test for execution of statements containing with statements so that it also works on Windows. Thanks to cboos for the patch.
cmlenz
parents:
867
diff
changeset
|
819 suite.execute(d) |
9309530ee279
Fix the unit test for execution of statements containing with statements so that it also works on Windows. Thanks to cboos for the patch.
cmlenz
parents:
867
diff
changeset
|
820 self.assertEqual(['foo\n', 'bar\n'], d['lines']) |
9309530ee279
Fix the unit test for execution of statements containing with statements so that it also works on Windows. Thanks to cboos for the patch.
cmlenz
parents:
867
diff
changeset
|
821 finally: |
9309530ee279
Fix the unit test for execution of statements containing with statements so that it also works on Windows. Thanks to cboos for the patch.
cmlenz
parents:
867
diff
changeset
|
822 os.remove(path) |
867
efc03d0719f3
Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents:
854
diff
changeset
|
823 |
efc03d0719f3
Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents:
854
diff
changeset
|
824 def test_yield_expression(self): |
efc03d0719f3
Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents:
854
diff
changeset
|
825 d = {} |
efc03d0719f3
Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents:
854
diff
changeset
|
826 suite = Suite("""results = [] |
efc03d0719f3
Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents:
854
diff
changeset
|
827 def counter(maximum): |
efc03d0719f3
Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents:
854
diff
changeset
|
828 i = 0 |
efc03d0719f3
Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents:
854
diff
changeset
|
829 while i < maximum: |
efc03d0719f3
Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents:
854
diff
changeset
|
830 val = (yield i) |
efc03d0719f3
Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents:
854
diff
changeset
|
831 if val is not None: |
efc03d0719f3
Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents:
854
diff
changeset
|
832 i = val |
efc03d0719f3
Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents:
854
diff
changeset
|
833 else: |
efc03d0719f3
Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents:
854
diff
changeset
|
834 i += 1 |
efc03d0719f3
Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents:
854
diff
changeset
|
835 it = counter(5) |
efc03d0719f3
Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents:
854
diff
changeset
|
836 results.append(it.next()) |
efc03d0719f3
Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents:
854
diff
changeset
|
837 results.append(it.send(3)) |
efc03d0719f3
Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents:
854
diff
changeset
|
838 results.append(it.next()) |
efc03d0719f3
Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents:
854
diff
changeset
|
839 """) |
efc03d0719f3
Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents:
854
diff
changeset
|
840 suite.execute(d) |
efc03d0719f3
Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents:
854
diff
changeset
|
841 self.assertEqual([0, 3, 4], d['results']) |
efc03d0719f3
Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents:
854
diff
changeset
|
842 |
405
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
843 |
336
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
844 def suite(): |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
845 suite = unittest.TestSuite() |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
846 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
|
847 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
|
848 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
|
849 return suite |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
850 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
851 if __name__ == '__main__': |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
852 unittest.main(defaultTest='suite') |