annotate genshi/template/tests/eval.py @ 583:60a906b93acd stable-0.4.x

Ported [696] to 0.4.x branch.
author cmlenz
date Wed, 01 Aug 2007 16:23:41 +0000
parents e9e1239960f5
children 587910938507
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 #
408
49a3bae5a8bb Update copyright year for files modified this year.
cmlenz
parents: 407
diff changeset
3 # Copyright (C) 2006-2007 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
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
15 import sys
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
16 import unittest
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
17
401
9582328f82e5 Make the `Markup` class available by default in template expressions. Closes #67.
cmlenz
parents: 396
diff changeset
18 from genshi.core import Markup
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
19 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
20 UNDEFINED
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
21
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
22
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
23 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
24
340
6c8b7a1fb50d Make expressions hashable.
cmlenz
parents: 336
diff changeset
25 def test_eq(self):
6c8b7a1fb50d Make expressions hashable.
cmlenz
parents: 336
diff changeset
26 expr = Expression('x,y')
6c8b7a1fb50d Make expressions hashable.
cmlenz
parents: 336
diff changeset
27 self.assertEqual(expr, Expression('x,y'))
6c8b7a1fb50d Make expressions hashable.
cmlenz
parents: 336
diff changeset
28 self.assertNotEqual(expr, Expression('y, x'))
6c8b7a1fb50d Make expressions hashable.
cmlenz
parents: 336
diff changeset
29
6c8b7a1fb50d Make expressions hashable.
cmlenz
parents: 336
diff changeset
30 def test_hash(self):
6c8b7a1fb50d Make expressions hashable.
cmlenz
parents: 336
diff changeset
31 expr = Expression('x,y')
6c8b7a1fb50d Make expressions hashable.
cmlenz
parents: 336
diff changeset
32 self.assertEqual(hash(expr), hash(Expression('x,y')))
6c8b7a1fb50d Make expressions hashable.
cmlenz
parents: 336
diff changeset
33 self.assertNotEqual(hash(expr), hash(Expression('y, x')))
6c8b7a1fb50d Make expressions hashable.
cmlenz
parents: 336
diff changeset
34
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
35 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
36 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
37 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
38 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
39 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
40
401
9582328f82e5 Make the `Markup` class available by default in template expressions. Closes #67.
cmlenz
parents: 396
diff changeset
41 def test_builtins(self):
9582328f82e5 Make the `Markup` class available by default in template expressions. Closes #67.
cmlenz
parents: 396
diff changeset
42 expr = Expression('Markup')
9582328f82e5 Make the `Markup` class available by default in template expressions. Closes #67.
cmlenz
parents: 396
diff changeset
43 self.assertEqual(expr.evaluate({}), Markup)
9582328f82e5 Make the `Markup` class available by default in template expressions. Closes #67.
cmlenz
parents: 396
diff changeset
44
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
45 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
46 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
47 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
48 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
49 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
50 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
51 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
52
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
53 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
54 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
55 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
56 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
57 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
58 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
59 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
60
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
61 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
62 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
63 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
64 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
65 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
66 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
67 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
68
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
69 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
70 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
71 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
72 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
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_list_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([], Expression("[]").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([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
77 self.assertEqual([True],
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
78 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
79
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
80 def test_tuple_literal(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
81 self.assertEqual((), Expression("()").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
82 self.assertEqual((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
83 self.assertEqual((True,),
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
84 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
85
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
86 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
87 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
88 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
89
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
90 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
91 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
92 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
93
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
94 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
95 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
96 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
97
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
98 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
99 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
100 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
101
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
102 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
103 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
104 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
105
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
106 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
107 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
108 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
109
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
110 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
111 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
112 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
113
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
114 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
115 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
116 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
117
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
118 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
119 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
120 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
121
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
122 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
123 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
124 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
125
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
126 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
127 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
128 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
129
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
130 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
131 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
132 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
133
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
134 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
135 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
136 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
137
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
138 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
139 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
140 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
141
396
22a581cfa537 add visitor for xor operator
mgood
parents: 393
diff changeset
142 def test_binop_xor(self):
22a581cfa537 add visitor for xor operator
mgood
parents: 393
diff changeset
143 self.assertEqual(1, Expression("1 ^ 0").evaluate({}))
22a581cfa537 add visitor for xor operator
mgood
parents: 393
diff changeset
144 self.assertEqual(1, Expression("x ^ y").evaluate({'x': 1, 'y': 0}))
22a581cfa537 add visitor for xor operator
mgood
parents: 393
diff changeset
145
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
146 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
147 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
148 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
149 '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
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_not_contains(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(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
153 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
154 '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
155
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
156 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
157 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
158 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
159 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
160 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
161
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
162 def test_binop_is_not(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
163 self.assertEqual(True, Expression("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
164 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
165 'y': 2}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
166 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
167 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
168 'y': 1}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
169
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
170 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
171 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
172 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
173 'y': False}))
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_boolop_or(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("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
177 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
178 'y': False}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
179
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
180 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
181 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
182 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
183
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
184 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
185 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
186 self.assertEqual(False, 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
187 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
188 self.assertEqual(False, 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
189
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
190 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
191 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
192 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
193
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
194 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
195 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
196 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
197
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
198 def test_compare_gt(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
199 self.assertEqual(True, Expression("2 > 1").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
200 self.assertEqual(True, Expression("x > 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
201
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
202 def test_compare_ge(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
203 self.assertEqual(True, Expression("1 >= 1").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
204 self.assertEqual(True, Expression("x >= y").evaluate({'x': 1, 'y': 1}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
205
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
206 def test_compare_multi(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
207 self.assertEqual(True, Expression("1 != 3 == 3").evaluate({}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
208 self.assertEqual(True, Expression("x != y == 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
209 'y': 3}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
210
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
211 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
212 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
213 data = {'foo': 'bar'}
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
214 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
215 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
216 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
217
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
218 def test_call_keywords(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
219 self.assertEqual(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
220 'bar': 42}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
221
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
222 def test_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
223 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
224 'bar': [42]}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
225
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
226 def test_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
227 def foo(x):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
228 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
229 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
230 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
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_lambda(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
233 # Define a custom `sorted` function cause the builtin isn't available
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
234 # on Python 2.3
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
235 def sorted(items, compfunc):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
236 items.sort(compfunc)
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
237 return items
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
238 data = {'items': [{'name': 'b', 'value': 0}, {'name': 'a', 'value': 1}],
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
239 'sorted': sorted}
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
240 expr = Expression("sorted(items, lambda a, b: cmp(a.name, b.name))")
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
241 self.assertEqual([{'name': 'a', 'value': 1}, {'name': 'b', 'value': 0}],
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
242 expr.evaluate(data))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
243
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
244 def test_list_comprehension(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
245 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
246 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
247
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
248 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
249 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
250 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
251
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
252 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
253 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
254 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
255
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
256 def test_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
257 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
258 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
259 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
260
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
261 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
262 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
263 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
264 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
265
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
266 if sys.version_info >= (2, 4):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
267 # Generator expressions only supported in Python 2.4 and up
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
268
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
269 def test_generator_expression(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
270 expr = Expression("list(n for n in numbers if n < 2)")
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
271 self.assertEqual([0, 1], expr.evaluate({'numbers': range(5)}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
272
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
273 expr = Expression("list((i, n + 1) for i, n in enumerate(numbers))")
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
274 self.assertEqual([(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)],
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
275 expr.evaluate({'numbers': range(5)}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
276
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
277 expr = Expression("list(offset + n for n in numbers)")
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
278 self.assertEqual([2, 3, 4, 5, 6],
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
279 expr.evaluate({'numbers': range(5), 'offset': 2}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
280
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
281 def test_generator_expression_with_getattr(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("list(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
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
286 def test_generator_expression_with_getitem(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
287 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
288 expr = Expression("list(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
289 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
290
393
a056613621ab add support for Python 2.5 conditional expressions (fixes #74)
mgood
parents: 343
diff changeset
291 if sys.version_info >= (2, 5):
a056613621ab add support for Python 2.5 conditional expressions (fixes #74)
mgood
parents: 343
diff changeset
292 def test_conditional_expression(self):
a056613621ab add support for Python 2.5 conditional expressions (fixes #74)
mgood
parents: 343
diff changeset
293 expr = Expression("'T' if foo else 'F'")
a056613621ab add support for Python 2.5 conditional expressions (fixes #74)
mgood
parents: 343
diff changeset
294 self.assertEqual('T', expr.evaluate({'foo': True}))
a056613621ab add support for Python 2.5 conditional expressions (fixes #74)
mgood
parents: 343
diff changeset
295 self.assertEqual('F', expr.evaluate({'foo': False}))
a056613621ab add support for Python 2.5 conditional expressions (fixes #74)
mgood
parents: 343
diff changeset
296
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
297 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
298 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
299 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
300
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
301 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
302 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
303 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
304 'end': 2}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
305
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
306 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
307 expr = Expression("numbers[:]")
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
308 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
309
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
310 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
311 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
312 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
313 'stride': 2}))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
314
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
315 def test_slice_negative_start(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
316 expr = Expression("numbers[-1:]")
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
317 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
318
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
319 def test_slice_negative_end(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
320 expr = Expression("numbers[:-1]")
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
321 self.assertEqual([0, 1, 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
322
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
323 def test_access_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
324 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
325 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
326 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
327 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
328 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
329
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
330 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
331 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
332 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
333 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
334 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
335 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
336 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
337 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
338 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
339 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
340
570
e2f9fe115441 Ported [680] to 0.4.x branch.
cmlenz
parents: 474
diff changeset
341 def test_getattr_exception(self):
e2f9fe115441 Ported [680] to 0.4.x branch.
cmlenz
parents: 474
diff changeset
342 class Something(object):
e2f9fe115441 Ported [680] to 0.4.x branch.
cmlenz
parents: 474
diff changeset
343 def prop(self):
e2f9fe115441 Ported [680] to 0.4.x branch.
cmlenz
parents: 474
diff changeset
344 raise NotImplementedError
e2f9fe115441 Ported [680] to 0.4.x branch.
cmlenz
parents: 474
diff changeset
345 prop = property(prop)
e2f9fe115441 Ported [680] to 0.4.x branch.
cmlenz
parents: 474
diff changeset
346 self.assertRaises(NotImplementedError,
e2f9fe115441 Ported [680] to 0.4.x branch.
cmlenz
parents: 474
diff changeset
347 Expression('s.prop').evaluate, {'s': Something()})
e2f9fe115441 Ported [680] to 0.4.x branch.
cmlenz
parents: 474
diff changeset
348
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
349 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
350 class Something(object):
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
351 def __repr__(self):
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
352 return '<Something>'
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 428
diff changeset
353 something = Something()
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 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
355 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
356 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
357 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
358 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
359
570
e2f9fe115441 Ported [680] to 0.4.x branch.
cmlenz
parents: 474
diff changeset
360 def test_getitem_exception(self):
e2f9fe115441 Ported [680] to 0.4.x branch.
cmlenz
parents: 474
diff changeset
361 class Something(object):
e2f9fe115441 Ported [680] to 0.4.x branch.
cmlenz
parents: 474
diff changeset
362 def __getitem__(self, key):
e2f9fe115441 Ported [680] to 0.4.x branch.
cmlenz
parents: 474
diff changeset
363 raise NotImplementedError
e2f9fe115441 Ported [680] to 0.4.x branch.
cmlenz
parents: 474
diff changeset
364 self.assertRaises(NotImplementedError,
e2f9fe115441 Ported [680] to 0.4.x branch.
cmlenz
parents: 474
diff changeset
365 Expression('s["foo"]').evaluate, {'s': Something()})
e2f9fe115441 Ported [680] to 0.4.x branch.
cmlenz
parents: 474
diff changeset
366
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
367 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
368 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
369 lookup='strict')
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
370 try:
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
371 expr.evaluate({})
418
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
372 self.fail('Expected UndefinedError')
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
373 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
374 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
375 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
376 frames = []
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
377 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
378 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
379 frames.append(frame)
418
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
380 self.assertEqual('"nothing" not defined', str(e))
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
381 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
382 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
383 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
384 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
385 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
386
418
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
387 def test_error_getattr_undefined(self):
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
388 class Something(object):
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
389 def __repr__(self):
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
390 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
391 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
392 lookup='strict')
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
393 try:
418
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
394 expr.evaluate({'something': Something()})
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
395 self.fail('Expected UndefinedError')
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
396 except UndefinedError, e:
474
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
397 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
398 exc_type, exc_value, exc_traceback = sys.exc_info()
474
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
399 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
400 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
401 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
402 frame = frame.tb_next
474
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
403 code = frame.tb_frame.f_code
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
404 if code.co_name == search_string:
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
405 break
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
406 else:
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
407 self.fail("never found the frame I was looking for")
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
408 self.assertEqual('index.html', code.co_filename)
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
409 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
410
418
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
411 def test_error_getitem_undefined_string(self):
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
412 class Something(object):
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
413 def __repr__(self):
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
414 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
415 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
416 lookup='strict')
418
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
417 try:
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
418 expr.evaluate({'something': Something()})
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
419 self.fail('Expected UndefinedError')
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
420 except UndefinedError, e:
474
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
421 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
422 exc_type, exc_value, exc_traceback = sys.exc_info()
474
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
423 search_string = '''<Expression 'something["nil"]'>'''
418
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
424 frame = exc_traceback.tb_next
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
425 while frame.tb_next:
878ffab274a6 Make expression error handling more strict. Closes #88.
cmlenz
parents: 408
diff changeset
426 frame = frame.tb_next
474
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
427 code = frame.tb_frame.f_code
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
428 if code.co_name == search_string:
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
429 break
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
430 else:
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
431 self.fail("never found the frame I was looking for")
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
432 self.assertEqual('index.html', code.co_filename)
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
433 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
434
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
435
405
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
436 class SuiteTestCase(unittest.TestCase):
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
437
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
438 def test_assign(self):
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
439 suite = Suite("foo = 42")
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
440 data = {}
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
441 suite.execute(data)
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
442 self.assertEqual(42, data['foo'])
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
443
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
444 def test_def(self):
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
445 suite = Suite("def donothing(): pass")
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
446 data = {}
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
447 suite.execute(data)
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
448 assert 'donothing' in data
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
449 self.assertEqual(None, data['donothing']())
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
450
474
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
451 def test_def_with_multiple_statements(self):
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
452 suite = Suite("""def donothing():
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
453 if True:
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
454 return foo
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
455 """)
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
456 data = {'foo': 'bar'}
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
457 suite.execute(data)
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
458 assert 'donothing' in data
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
459 self.assertEqual('bar', data['donothing']())
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
460
405
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
461 def test_delete(self):
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
462 suite = Suite("""foo = 42
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
463 del foo
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
464 """)
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
465 data = {}
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
466 suite.execute(data)
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
467 assert 'foo' not in data
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
468
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
469 def test_class(self):
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
470 suite = Suite("class plain(object): pass")
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
471 data = {}
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
472 suite.execute(data)
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
473 assert 'plain' in data
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
474
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
475 def test_import(self):
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
476 suite = Suite("from itertools import ifilter")
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
477 data = {}
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
478 suite.execute(data)
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
479 assert 'ifilter' in data
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
480
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
481 def test_for(self):
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
482 suite = Suite("""x = []
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
483 for i in range(3):
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
484 x.append(i**2)
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
485 """)
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
486 data = {}
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
487 suite.execute(data)
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
488 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
489
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
490 def test_if(self):
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
491 suite = Suite("""if foo == 42:
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
492 x = True
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
493 """)
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
494 data = {'foo': 42}
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
495 suite.execute(data)
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
496 self.assertEqual(True, data['x'])
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
497
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
498 def test_raise(self):
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
499 suite = Suite("""raise NotImplementedError""")
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
500 self.assertRaises(NotImplementedError, suite.execute, {})
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
501
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
502 def test_try_except(self):
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
503 suite = Suite("""try:
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
504 import somemod
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
505 except ImportError:
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
506 somemod = None
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
507 else:
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
508 somemod.dosth()""")
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
509 data = {}
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
510 suite.execute(data)
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
511 self.assertEqual(None, data['somemod'])
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
512
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
513 def test_finally(self):
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
514 suite = Suite("""try:
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
515 x = 2
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
516 finally:
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
517 x = None
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
518 """)
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
519 data = {}
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
520 suite.execute(data)
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
521 self.assertEqual(None, data['x'])
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
522
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
523 def test_while_break(self):
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
524 suite = Suite("""x = 0
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
525 while x < 5:
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
526 x += step
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
527 if x == 4:
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
528 break
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
529 """)
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
530 data = {'step': 2}
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
531 suite.execute(data)
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
532 self.assertEqual(4, data['x'])
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
533
474
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
534 def test_augmented_attribute_assignment(self):
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
535 suite = Suite("d['k'] += 42")
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
536 d = {"k": 1}
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
537 suite.execute({"d": d})
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
538 self.assertEqual(43, d["k"])
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
539
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
540 def test_local_augmented_assign(self):
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
541 Suite("x = 1; x += 42; assert x == 43").execute({})
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
542
581
e9e1239960f5 Ported [693:694] to 0.4.x branch.
cmlenz
parents: 570
diff changeset
543 def test_augmented_assign_in_def(self):
e9e1239960f5 Ported [693:694] to 0.4.x branch.
cmlenz
parents: 570
diff changeset
544 d = {}
e9e1239960f5 Ported [693:694] to 0.4.x branch.
cmlenz
parents: 570
diff changeset
545 Suite("""def foo():
e9e1239960f5 Ported [693:694] to 0.4.x branch.
cmlenz
parents: 570
diff changeset
546 i = 1
e9e1239960f5 Ported [693:694] to 0.4.x branch.
cmlenz
parents: 570
diff changeset
547 i += 1
e9e1239960f5 Ported [693:694] to 0.4.x branch.
cmlenz
parents: 570
diff changeset
548 return i
e9e1239960f5 Ported [693:694] to 0.4.x branch.
cmlenz
parents: 570
diff changeset
549 x = foo()""").execute(d)
e9e1239960f5 Ported [693:694] to 0.4.x branch.
cmlenz
parents: 570
diff changeset
550 self.assertEqual(2, d['x'])
e9e1239960f5 Ported [693:694] to 0.4.x branch.
cmlenz
parents: 570
diff changeset
551
583
60a906b93acd Ported [696] to 0.4.x branch.
cmlenz
parents: 581
diff changeset
552 def test_augmented_assign_in_loop_in_def(self):
60a906b93acd Ported [696] to 0.4.x branch.
cmlenz
parents: 581
diff changeset
553 d = {}
60a906b93acd Ported [696] to 0.4.x branch.
cmlenz
parents: 581
diff changeset
554 Suite("""def foo():
60a906b93acd Ported [696] to 0.4.x branch.
cmlenz
parents: 581
diff changeset
555 i = 0
60a906b93acd Ported [696] to 0.4.x branch.
cmlenz
parents: 581
diff changeset
556 for n in range(5):
60a906b93acd Ported [696] to 0.4.x branch.
cmlenz
parents: 581
diff changeset
557 i += n
60a906b93acd Ported [696] to 0.4.x branch.
cmlenz
parents: 581
diff changeset
558 return i
60a906b93acd Ported [696] to 0.4.x branch.
cmlenz
parents: 581
diff changeset
559 x = foo()""").execute(d)
60a906b93acd Ported [696] to 0.4.x branch.
cmlenz
parents: 581
diff changeset
560 self.assertEqual(10, d['x'])
60a906b93acd Ported [696] to 0.4.x branch.
cmlenz
parents: 581
diff changeset
561
474
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
562 def test_assign_in_list(self):
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
563 suite = Suite("[d['k']] = 'foo',; assert d['k'] == 'foo'")
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
564 d = {"k": "bar"}
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
565 suite.execute({"d": d})
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
566 self.assertEqual("foo", d["k"])
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
567
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
568 def test_exec(self):
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
569 suite = Suite("x = 1; exec d['k']; assert x == 42, x")
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
570 suite.execute({"d": {"k": "x = 42"}})
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
571
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
572 def test_return(self):
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
573 suite = Suite("""
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
574 def f():
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
575 return v
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
576
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
577 assert f() == 42
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
578 """)
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
579 suite.execute({"v": 42})
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
580
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
581 def test_assign_to_dict_item(self):
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
582 suite = Suite("d['k'] = 'foo'")
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
583 data = {'d': {}}
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
584 suite.execute(data)
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
585 self.assertEqual('foo', data['d']['k'])
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
586
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
587 def test_assign_to_attribute(self):
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
588 class Something(object): pass
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
589 something = Something()
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
590 suite = Suite("obj.attr = 'foo'")
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
591 data = {"obj": something}
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
592 suite.execute(data)
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
593 self.assertEqual('foo', something.attr)
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
594
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
595 def test_delattr(self):
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
596 class Something(object):
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
597 def __init__(self):
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
598 self.attr = 'foo'
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
599 obj = Something()
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
600 Suite("del obj.attr").execute({'obj': obj})
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
601 self.failIf(hasattr(obj, 'attr'))
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
602
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
603 def test_delitem(self):
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
604 d = {'k': 'foo'}
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
605 Suite("del d['k']").execute({'d': d})
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
606 self.failIf('k' in d, `d`)
2f54482aa019 Ported [573] to 0.4.x.
cmlenz
parents: 442
diff changeset
607
405
bd5da099c113 Support for Python code blocks using the `<?python ?>` processing instruction. Closes #84.
cmlenz
parents: 401
diff changeset
608
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
609 def suite():
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
610 suite = unittest.TestSuite()
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
611 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
612 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
613 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
614 return suite
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
615
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
616 if __name__ == '__main__':
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
617 unittest.main(defaultTest='suite')
Copyright (C) 2012-2017 Edgewall Software