Mercurial > genshi > genshi-test
annotate genshi/template/tests/eval.py @ 896:248d6671a7e1
Fix for infinite recursion when parsing argument names from tuples. Closes #383.
author | cmlenz |
---|---|
date | Wed, 21 Apr 2010 22:02:28 +0000 |
parents | 27d210790d27 |
children | 85e4678337cf |
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 |
896
248d6671a7e1
Fix for infinite recursion when parsing argument names from tuples. Closes #383.
cmlenz
parents:
879
diff
changeset
|
251 def test_lambda_tuple_arg(self): |
248d6671a7e1
Fix for infinite recursion when parsing argument names from tuples. Closes #383.
cmlenz
parents:
879
diff
changeset
|
252 data = {'items': [(1, 2), (2, 1)]} |
248d6671a7e1
Fix for infinite recursion when parsing argument names from tuples. Closes #383.
cmlenz
parents:
879
diff
changeset
|
253 expr = Expression("filter(lambda (x, y): x > y, items)") |
248d6671a7e1
Fix for infinite recursion when parsing argument names from tuples. Closes #383.
cmlenz
parents:
879
diff
changeset
|
254 self.assertEqual([(2, 1)], expr.evaluate(data)) |
248d6671a7e1
Fix for infinite recursion when parsing argument names from tuples. Closes #383.
cmlenz
parents:
879
diff
changeset
|
255 |
336
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
256 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
|
257 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
|
258 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
|
259 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
260 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
|
261 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
|
262 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
|
263 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
264 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
|
265 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
|
266 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
|
267 |
845
9cabfbc4ac5c
Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents:
807
diff
changeset
|
268 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
|
269 self.assertEqual([0, 1, 0, 1, 2], |
9cabfbc4ac5c
Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents:
807
diff
changeset
|
270 expr.evaluate({'groups': [range(2), range(3)]})) |
9cabfbc4ac5c
Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents:
807
diff
changeset
|
271 |
9cabfbc4ac5c
Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents:
807
diff
changeset
|
272 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
|
273 self.assertEqual([('x0', 'y0'), ('x0', 'y1'), ('x1', 'y0'), ('x1', 'y1')], |
9cabfbc4ac5c
Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents:
807
diff
changeset
|
274 expr.evaluate({'x': ['x0', 'x1'], 'y': ['y0', 'y1']})) |
9cabfbc4ac5c
Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents:
807
diff
changeset
|
275 |
336
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_getattr(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 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
281 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
|
282 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
|
283 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
|
284 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
|
285 |
750 | 286 def test_generator_expression(self): |
287 expr = Expression("list(n for n in numbers if n < 2)") | |
288 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
|
289 |
750 | 290 expr = Expression("list((i, n + 1) for i, n in enumerate(numbers))") |
291 self.assertEqual([(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)], | |
292 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
|
293 |
750 | 294 expr = Expression("list(offset + n for n in numbers)") |
295 self.assertEqual([2, 3, 4, 5, 6], | |
296 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
|
297 |
845
9cabfbc4ac5c
Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents:
807
diff
changeset
|
298 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
|
299 self.assertEqual([0, 1, 0, 1, 2], |
9cabfbc4ac5c
Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents:
807
diff
changeset
|
300 expr.evaluate({'groups': [range(2), range(3)]})) |
9cabfbc4ac5c
Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents:
807
diff
changeset
|
301 |
9cabfbc4ac5c
Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents:
807
diff
changeset
|
302 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
|
303 self.assertEqual([('x0', 'y0'), ('x0', 'y1'), ('x1', 'y0'), ('x1', 'y1')], |
9cabfbc4ac5c
Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents:
807
diff
changeset
|
304 expr.evaluate({'x': ['x0', 'x1'], 'y': ['y0', 'y1']})) |
9cabfbc4ac5c
Fix nested list comprehensions & generator expressions (fixes #327)
mgood
parents:
807
diff
changeset
|
305 |
750 | 306 def test_generator_expression_with_getattr(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})) | |
310 | |
311 def test_generator_expression_with_getitem(self): | |
312 items = [{'name': 'a', 'value': 1}, {'name': 'b', 'value': 2}] | |
313 expr = Expression("list(i['name'] for i in items if i['value'] > 1)") | |
314 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
|
315 |
393
a056613621ab
add support for Python 2.5 conditional expressions (fixes #74)
mgood
parents:
343
diff
changeset
|
316 if sys.version_info >= (2, 5): |
a056613621ab
add support for Python 2.5 conditional expressions (fixes #74)
mgood
parents:
343
diff
changeset
|
317 def test_conditional_expression(self): |
a056613621ab
add support for Python 2.5 conditional expressions (fixes #74)
mgood
parents:
343
diff
changeset
|
318 expr = Expression("'T' if foo else 'F'") |
a056613621ab
add support for Python 2.5 conditional expressions (fixes #74)
mgood
parents:
343
diff
changeset
|
319 self.assertEqual('T', expr.evaluate({'foo': True})) |
a056613621ab
add support for Python 2.5 conditional expressions (fixes #74)
mgood
parents:
343
diff
changeset
|
320 self.assertEqual('F', expr.evaluate({'foo': False})) |
a056613621ab
add support for Python 2.5 conditional expressions (fixes #74)
mgood
parents:
343
diff
changeset
|
321 |
336
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
322 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
|
323 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
|
324 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
|
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_with_vars(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[start:end]") |
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], 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
|
329 'end': 2})) |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
330 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
331 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
|
332 expr = Expression("numbers[:]") |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
333 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
|
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_stride(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[::stride]") |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
337 self.assertEqual([0, 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
|
338 'stride': 2})) |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
339 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
340 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
|
341 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
|
342 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
|
343 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
344 def test_slice_negative_end(self): |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
345 expr = Expression("numbers[:-1]") |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
346 self.assertEqual([0, 1, 2, 3], expr.evaluate({'numbers': range(5)})) |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
347 |
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
|
348 def test_access_undefined(self): |
606
9ada030ad986
Changed the default error handling mode to "strict".
cmlenz
parents:
586
diff
changeset
|
349 expr = Expression("nothing", filename='index.html', lineno=50, |
9ada030ad986
Changed the default error handling mode to "strict".
cmlenz
parents:
586
diff
changeset
|
350 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
|
351 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
|
352 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
|
353 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
|
354 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
|
355 |
ff7c72b52fb2
Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents:
428
diff
changeset
|
356 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
|
357 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
|
358 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
|
359 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
|
360 something = Something() |
606
9ada030ad986
Changed the default error handling mode to "strict".
cmlenz
parents:
586
diff
changeset
|
361 expr = Expression('something.nil', filename='index.html', lineno=50, |
9ada030ad986
Changed the default error handling mode to "strict".
cmlenz
parents:
586
diff
changeset
|
362 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
|
363 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
|
364 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
|
365 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
|
366 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
|
367 |
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
|
368 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
|
369 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
|
370 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
|
371 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
|
372 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
|
373 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
|
374 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
|
375 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
|
376 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
|
377 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
|
378 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
|
379 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
|
380 |
442
ff7c72b52fb2
Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents:
428
diff
changeset
|
381 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
|
382 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
|
383 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
|
384 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
|
385 something = Something() |
606
9ada030ad986
Changed the default error handling mode to "strict".
cmlenz
parents:
586
diff
changeset
|
386 expr = Expression('something["nil"]', filename='index.html', lineno=50, |
9ada030ad986
Changed the default error handling mode to "strict".
cmlenz
parents:
586
diff
changeset
|
387 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
|
388 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
|
389 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
|
390 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
|
391 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
|
392 |
569
4cbd8031ed76
Attribute access in template expressions no longer silently ignores exceptions other than `AttributeError` raised in the attribute accessor.
cmlenz
parents:
568
diff
changeset
|
393 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
|
394 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
|
395 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
|
396 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
|
397 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
|
398 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
|
399 |
336
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
400 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
|
401 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
|
402 lookup='strict') |
336
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
403 try: |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
404 expr.evaluate({}) |
418
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
405 self.fail('Expected UndefinedError') |
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
406 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
|
407 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
|
408 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
|
409 frames = [] |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
410 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
|
411 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
|
412 frames.append(frame) |
418
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
413 self.assertEqual('"nothing" not defined', str(e)) |
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
414 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
|
415 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
|
416 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
|
417 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
|
418 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
|
419 |
418
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
420 def test_error_getattr_undefined(self): |
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
421 class Something(object): |
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
422 def __repr__(self): |
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
423 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
|
424 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
|
425 lookup='strict') |
336
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
426 try: |
418
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
427 expr.evaluate({'something': Something()}) |
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
428 self.fail('Expected UndefinedError') |
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
429 except UndefinedError, e: |
473 | 430 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
|
431 exc_type, exc_value, exc_traceback = sys.exc_info() |
473 | 432 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
|
433 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
|
434 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
|
435 frame = frame.tb_next |
473 | 436 code = frame.tb_frame.f_code |
437 if code.co_name == search_string: | |
438 break | |
439 else: | |
440 self.fail("never found the frame I was looking for") | |
441 self.assertEqual('index.html', code.co_filename) | |
442 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
|
443 |
418
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
444 def test_error_getitem_undefined_string(self): |
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
445 class Something(object): |
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
446 def __repr__(self): |
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
447 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
|
448 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
|
449 lookup='strict') |
418
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
450 try: |
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
451 expr.evaluate({'something': Something()}) |
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
452 self.fail('Expected UndefinedError') |
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
453 except UndefinedError, e: |
473 | 454 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
|
455 exc_type, exc_value, exc_traceback = sys.exc_info() |
473 | 456 search_string = '''<Expression 'something["nil"]'>''' |
418
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
457 frame = exc_traceback.tb_next |
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
458 while frame.tb_next: |
878ffab274a6
Make expression error handling more strict. Closes #88.
cmlenz
parents:
408
diff
changeset
|
459 frame = frame.tb_next |
473 | 460 code = frame.tb_frame.f_code |
461 if code.co_name == search_string: | |
462 break | |
463 else: | |
464 self.fail("never found the frame I was looking for") | |
465 self.assertEqual('index.html', code.co_filename) | |
466 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
|
467 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
468 |
405
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
469 class SuiteTestCase(unittest.TestCase): |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
470 |
715 | 471 def test_pickle(self): |
472 suite = Suite('foo = 42') | |
473 buf = StringIO() | |
474 pickle.dump(suite, buf, 2) | |
475 buf.seek(0) | |
476 unpickled = pickle.load(buf) | |
477 data = {} | |
478 unpickled.execute(data) | |
479 self.assertEqual(42, data['foo']) | |
480 | |
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
|
481 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
|
482 # 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
|
483 # 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
|
484 # 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
|
485 # 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
|
486 # 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
|
487 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
|
488 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
|
489 """) |
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
|
490 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
|
491 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
|
492 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
|
493 |
405
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
494 def test_assign(self): |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
495 suite = Suite("foo = 42") |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
496 data = {} |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
497 suite.execute(data) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
498 self.assertEqual(42, data['foo']) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
499 |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
500 def test_def(self): |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
501 suite = Suite("def donothing(): pass") |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
502 data = {} |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
503 suite.execute(data) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
504 assert 'donothing' in data |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
505 self.assertEqual(None, data['donothing']()) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
506 |
473 | 507 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
|
508 suite = Suite(""" |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
509 def donothing(): |
473 | 510 if True: |
511 return foo | |
512 """) | |
513 data = {'foo': 'bar'} | |
514 suite.execute(data) | |
515 assert 'donothing' in data | |
516 self.assertEqual('bar', data['donothing']()) | |
517 | |
586
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
518 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
|
519 suite = Suite(""" |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
520 values = [] |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
521 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
|
522 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
|
523 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
|
524 add('foo') |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
525 add('bar') |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
526 """) |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
527 data = {} |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
528 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
|
529 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
|
530 |
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
|
531 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
|
532 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
|
533 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
|
534 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
|
535 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
|
536 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
|
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 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
|
539 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
|
540 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
|
541 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
|
542 |
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 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
|
544 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
|
545 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
|
546 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
|
547 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
|
548 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
|
549 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
|
550 """) |
aa274188b77a
Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents:
793
diff
changeset
|
551 data = {} |
aa274188b77a
Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents:
793
diff
changeset
|
552 suite.execute(data) |
aa274188b77a
Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents:
793
diff
changeset
|
553 self.assertEqual(1, data['x']) |
aa274188b77a
Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents:
793
diff
changeset
|
554 self.assertEqual(10, data['y']) |
aa274188b77a
Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents:
793
diff
changeset
|
555 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
|
556 |
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 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
|
558 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
|
559 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
|
560 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
|
561 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
|
562 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
|
563 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
|
564 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
|
565 """) |
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 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
|
567 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
|
568 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
|
569 |
aa274188b77a
Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents:
793
diff
changeset
|
570 def test_def_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
|
571 suite = Suite(""" |
aa274188b77a
Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents:
793
diff
changeset
|
572 def 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
|
573 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
|
574 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
|
575 """) |
aa274188b77a
Fix handling of function arguments with default values in template code, applying patch by Scott Wilson plus an additional fix. Closes #292.
cmlenz
parents:
793
diff
changeset
|
576 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
|
577 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
|
578 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
|
579 |
586
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
580 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
|
581 suite = Suite(""" |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
582 def doit(): |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
583 values = [] |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
584 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
|
585 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
|
586 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
|
587 add('foo') |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
588 add('bar') |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
589 return values |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
590 x = doit() |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
591 """) |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
592 data = {} |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
593 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
|
594 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
|
595 |
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
|
596 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
|
597 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
|
598 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
|
599 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
|
600 |
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 @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
|
602 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
|
603 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
|
604 |
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 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
|
606 """) |
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
|
607 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
|
608 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
|
609 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
|
610 |
405
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
611 def test_delete(self): |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
612 suite = Suite("""foo = 42 |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
613 del foo |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
614 """) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
615 data = {} |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
616 suite.execute(data) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
617 assert 'foo' not in data |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
618 |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
619 def test_class(self): |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
620 suite = Suite("class plain(object): pass") |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
621 data = {} |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
622 suite.execute(data) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
623 assert 'plain' in data |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
624 |
586
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
625 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
|
626 suite = Suite(""" |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
627 def create(): |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
628 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
|
629 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
|
630 return 'foobar' |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
631 return Foobar() |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
632 x = create() |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
633 """) |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
634 data = {} |
5413c9d95db1
Fixes for nonlocal variable access in code blocks, as well as nested function and class definitions.
cmlenz
parents:
582
diff
changeset
|
635 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
|
636 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
|
637 |
568
8f0d601afc0c
AST transformer needs to also handle the class body.
cmlenz
parents:
473
diff
changeset
|
638 def test_class_with_methods(self): |
8f0d601afc0c
AST transformer needs to also handle the class body.
cmlenz
parents:
473
diff
changeset
|
639 suite = Suite("""class plain(object): |
8f0d601afc0c
AST transformer needs to also handle the class body.
cmlenz
parents:
473
diff
changeset
|
640 def donothing(): |
8f0d601afc0c
AST transformer needs to also handle the class body.
cmlenz
parents:
473
diff
changeset
|
641 pass |
8f0d601afc0c
AST transformer needs to also handle the class body.
cmlenz
parents:
473
diff
changeset
|
642 """) |
8f0d601afc0c
AST transformer needs to also handle the class body.
cmlenz
parents:
473
diff
changeset
|
643 data = {} |
8f0d601afc0c
AST transformer needs to also handle the class body.
cmlenz
parents:
473
diff
changeset
|
644 suite.execute(data) |
8f0d601afc0c
AST transformer needs to also handle the class body.
cmlenz
parents:
473
diff
changeset
|
645 assert 'plain' in data |
8f0d601afc0c
AST transformer needs to also handle the class body.
cmlenz
parents:
473
diff
changeset
|
646 |
405
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
647 def test_import(self): |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
648 suite = Suite("from itertools import ifilter") |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
649 data = {} |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
650 suite.execute(data) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
651 assert 'ifilter' in data |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
652 |
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
|
653 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
|
654 suite = Suite("from itertools import *") |
736 | 655 data = Context() |
750 | 656 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
|
657 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
|
658 |
807 | 659 def test_import_in_def(self): |
660 suite = Suite("""def fun(): | |
661 from itertools import ifilter | |
854
0d9e87c6cf6e
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
845
diff
changeset
|
662 return ifilter(None, range(3)) |
807 | 663 """) |
664 data = Context() | |
665 suite.execute(data) | |
666 assert 'ifilter' not in data | |
667 self.assertEqual([1, 2], list(data['fun']())) | |
668 | |
405
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
669 def test_for(self): |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
670 suite = Suite("""x = [] |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
671 for i in range(3): |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
672 x.append(i**2) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
673 """) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
674 data = {} |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
675 suite.execute(data) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
676 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
|
677 |
803
92b9b647e1fe
`for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents:
802
diff
changeset
|
678 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
|
679 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
|
680 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
|
681 if i == 5: |
92b9b647e1fe
`for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents:
802
diff
changeset
|
682 break |
92b9b647e1fe
`for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents:
802
diff
changeset
|
683 return i |
92b9b647e1fe
`for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents:
802
diff
changeset
|
684 """) |
92b9b647e1fe
`for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents:
802
diff
changeset
|
685 data = {} |
92b9b647e1fe
`for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents:
802
diff
changeset
|
686 suite.execute(data) |
92b9b647e1fe
`for` loops in template code blocks should not establish their own locals block. Closes #259.
cmlenz
parents:
802
diff
changeset
|
687 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
|
688 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
|
689 |
405
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
690 def test_if(self): |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
691 suite = Suite("""if foo == 42: |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
692 x = True |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
693 """) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
694 data = {'foo': 42} |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
695 suite.execute(data) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
696 self.assertEqual(True, data['x']) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
697 |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
698 def test_raise(self): |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
699 suite = Suite("""raise NotImplementedError""") |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
700 self.assertRaises(NotImplementedError, suite.execute, {}) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
701 |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
702 def test_try_except(self): |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
703 suite = Suite("""try: |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
704 import somemod |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
705 except ImportError: |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
706 somemod = None |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
707 else: |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
708 somemod.dosth()""") |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
709 data = {} |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
710 suite.execute(data) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
711 self.assertEqual(None, data['somemod']) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
712 |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
713 def test_finally(self): |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
714 suite = Suite("""try: |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
715 x = 2 |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
716 finally: |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
717 x = None |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
718 """) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
719 data = {} |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
720 suite.execute(data) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
721 self.assertEqual(None, data['x']) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
722 |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
723 def test_while_break(self): |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
724 suite = Suite("""x = 0 |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
725 while x < 5: |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
726 x += step |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
727 if x == 4: |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
728 break |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
729 """) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
730 data = {'step': 2} |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
731 suite.execute(data) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
732 self.assertEqual(4, data['x']) |
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
733 |
473 | 734 def test_augmented_attribute_assignment(self): |
735 suite = Suite("d['k'] += 42") | |
736 d = {"k": 1} | |
737 suite.execute({"d": d}) | |
738 self.assertEqual(43, d["k"]) | |
739 | |
740 def test_local_augmented_assign(self): | |
741 Suite("x = 1; x += 42; assert x == 43").execute({}) | |
742 | |
579
71e3205925e6
Fix for augmented assignments to local variables. Thanks to Erik Bray for reporting the problem.
cmlenz
parents:
569
diff
changeset
|
743 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
|
744 d = {} |
71e3205925e6
Fix for augmented assignments to local variables. Thanks to Erik Bray for reporting the problem.
cmlenz
parents:
569
diff
changeset
|
745 Suite("""def foo(): |
71e3205925e6
Fix for augmented assignments to local variables. Thanks to Erik Bray for reporting the problem.
cmlenz
parents:
569
diff
changeset
|
746 i = 1 |
71e3205925e6
Fix for augmented assignments to local variables. Thanks to Erik Bray for reporting the problem.
cmlenz
parents:
569
diff
changeset
|
747 i += 1 |
71e3205925e6
Fix for augmented assignments to local variables. Thanks to Erik Bray for reporting the problem.
cmlenz
parents:
569
diff
changeset
|
748 return i |
71e3205925e6
Fix for augmented assignments to local variables. Thanks to Erik Bray for reporting the problem.
cmlenz
parents:
569
diff
changeset
|
749 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
|
750 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
|
751 |
582
b21da79c9bde
Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents:
579
diff
changeset
|
752 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
|
753 d = {} |
b21da79c9bde
Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents:
579
diff
changeset
|
754 Suite("""def foo(): |
b21da79c9bde
Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents:
579
diff
changeset
|
755 i = 0 |
b21da79c9bde
Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents:
579
diff
changeset
|
756 for n in range(5): |
b21da79c9bde
Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents:
579
diff
changeset
|
757 i += n |
b21da79c9bde
Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents:
579
diff
changeset
|
758 return i |
b21da79c9bde
Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents:
579
diff
changeset
|
759 x = foo()""").execute(d) |
b21da79c9bde
Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents:
579
diff
changeset
|
760 self.assertEqual(10, d['x']) |
b21da79c9bde
Follow-up fix to [693:694]. Again, thanks to Erik Bray for reporting.
cmlenz
parents:
579
diff
changeset
|
761 |
473 | 762 def test_assign_in_list(self): |
763 suite = Suite("[d['k']] = 'foo',; assert d['k'] == 'foo'") | |
764 d = {"k": "bar"} | |
765 suite.execute({"d": d}) | |
766 self.assertEqual("foo", d["k"]) | |
767 | |
768 def test_exec(self): | |
769 suite = Suite("x = 1; exec d['k']; assert x == 42, x") | |
770 suite.execute({"d": {"k": "x = 42"}}) | |
771 | |
772 def test_return(self): | |
773 suite = Suite(""" | |
774 def f(): | |
775 return v | |
776 | |
777 assert f() == 42 | |
778 """) | |
779 suite.execute({"v": 42}) | |
780 | |
781 def test_assign_to_dict_item(self): | |
782 suite = Suite("d['k'] = 'foo'") | |
783 data = {'d': {}} | |
784 suite.execute(data) | |
785 self.assertEqual('foo', data['d']['k']) | |
786 | |
787 def test_assign_to_attribute(self): | |
788 class Something(object): pass | |
789 something = Something() | |
790 suite = Suite("obj.attr = 'foo'") | |
791 data = {"obj": something} | |
792 suite.execute(data) | |
793 self.assertEqual('foo', something.attr) | |
794 | |
795 def test_delattr(self): | |
796 class Something(object): | |
797 def __init__(self): | |
798 self.attr = 'foo' | |
799 obj = Something() | |
800 Suite("del obj.attr").execute({'obj': obj}) | |
801 self.failIf(hasattr(obj, 'attr')) | |
802 | |
803 def test_delitem(self): | |
804 d = {'k': 'foo'} | |
805 Suite("del d['k']").execute({'d': d}) | |
793 | 806 self.failIf('k' in d, repr(d)) |
473 | 807 |
867
efc03d0719f3
Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents:
854
diff
changeset
|
808 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
|
809 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
|
810 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
|
811 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
|
812 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
|
813 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
|
814 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
|
815 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
|
816 |
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
|
817 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
|
818 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
|
819 lines = [] |
efc03d0719f3
Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents:
854
diff
changeset
|
820 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
|
821 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
|
822 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
|
823 """) |
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
|
824 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
|
825 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
|
826 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
|
827 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
|
828 |
efc03d0719f3
Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents:
854
diff
changeset
|
829 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
|
830 d = {} |
efc03d0719f3
Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents:
854
diff
changeset
|
831 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
|
832 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
|
833 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
|
834 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
|
835 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
|
836 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
|
837 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
|
838 else: |
efc03d0719f3
Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents:
854
diff
changeset
|
839 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
|
840 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
|
841 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
|
842 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
|
843 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
|
844 """) |
efc03d0719f3
Add unit tests for correct handling of with statements and yield expressions in the AST code generator.
cmlenz
parents:
854
diff
changeset
|
845 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
|
846 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
|
847 |
405
bd5da099c113
Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents:
401
diff
changeset
|
848 |
336
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
849 def suite(): |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
850 suite = unittest.TestSuite() |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
851 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
|
852 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
|
853 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
|
854 return suite |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
855 |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
856 if __name__ == '__main__': |
5f2c7782cd8a
Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff
changeset
|
857 unittest.main(defaultTest='suite') |