Mercurial > genshi > mirror
annotate markup/tests/eval.py @ 92:01d36818bb3d trunk
More performance improvements... this time for whitespace normalization and template loops.
author | cmlenz |
---|---|
date | Thu, 20 Jul 2006 23:06:36 +0000 |
parents | c835e81c50af |
children | e20a153cc20c |
rev | line source |
---|---|
1 | 1 # -*- coding: utf-8 -*- |
2 # | |
66
59eb24184e9c
Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents:
32
diff
changeset
|
3 # Copyright (C) 2006 Edgewall Software |
1 | 4 # All rights reserved. |
5 # | |
6 # This software is licensed as described in the file COPYING, which | |
7 # you should have received as part of this distribution. The terms | |
66
59eb24184e9c
Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents:
32
diff
changeset
|
8 # are also available at http://markup.edgewall.org/wiki/License. |
1 | 9 # |
10 # This software consists of voluntary contributions made by many | |
11 # individuals. For the exact contribution history, see the revision | |
66
59eb24184e9c
Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents:
32
diff
changeset
|
12 # history and logs, available at hhttp://markup.edgewall.org/log/. |
1 | 13 |
14 import doctest | |
15 import unittest | |
16 | |
27 | 17 from markup.eval import Expression |
1 | 18 |
30
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
19 |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
20 class ExpressionTestCase(unittest.TestCase): |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
21 |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
22 def test_str_literal(self): |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
23 self.assertEqual('foo', Expression('"foo"').evaluate({})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
24 self.assertEqual('foo', Expression('"""foo"""').evaluate({})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
25 self.assertEqual('foo', Expression("'foo'").evaluate({})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
26 self.assertEqual('foo', Expression("'''foo'''").evaluate({})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
27 self.assertEqual('foo', Expression("u'foo'").evaluate({})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
28 self.assertEqual('foo', Expression("r'foo'").evaluate({})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
29 |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
30 def test_num_literal(self): |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
31 self.assertEqual(42, Expression("42").evaluate({})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
32 self.assertEqual(42L, Expression("42L").evaluate({})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
33 self.assertEqual(.42, Expression(".42").evaluate({})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
34 self.assertEqual(07, Expression("07").evaluate({})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
35 self.assertEqual(0xF2, Expression("0xF2").evaluate({})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
36 self.assertEqual(0XF2, Expression("0XF2").evaluate({})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
37 |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
38 def test_dict_literal(self): |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
39 self.assertEqual({}, Expression("{}").evaluate({})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
40 self.assertEqual({'key': True}, |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
41 Expression("{'key': value}").evaluate({'value': True})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
42 |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
43 def test_list_literal(self): |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
44 self.assertEqual([], Expression("[]").evaluate({})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
45 self.assertEqual([1, 2, 3], Expression("[1, 2, 3]").evaluate({})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
46 self.assertEqual([True], |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
47 Expression("[value]").evaluate({'value': True})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
48 |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
49 def test_tuple_literal(self): |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
50 self.assertEqual((), Expression("()").evaluate({})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
51 self.assertEqual((1, 2, 3), Expression("(1, 2, 3)").evaluate({})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
52 self.assertEqual((True,), |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
53 Expression("(value,)").evaluate({'value': True})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
54 |
31 | 55 def test_unaryop_pos(self): |
56 self.assertEqual(1, Expression("+1").evaluate({})) | |
57 self.assertEqual(1, Expression("+x").evaluate({'x': 1})) | |
58 | |
59 def test_unaryop_neg(self): | |
60 self.assertEqual(-1, Expression("-1").evaluate({})) | |
61 self.assertEqual(-1, Expression("-x").evaluate({'x': 1})) | |
62 | |
63 def test_unaryop_not(self): | |
64 self.assertEqual(False, Expression("not True").evaluate({})) | |
65 self.assertEqual(False, Expression("not x").evaluate({'x': True})) | |
66 | |
32 | 67 def test_unaryop_inv(self): |
68 self.assertEqual(-2, Expression("~1").evaluate({})) | |
69 self.assertEqual(-2, Expression("~x").evaluate({'x': 1})) | |
70 | |
31 | 71 def test_binop_add(self): |
72 self.assertEqual(3, Expression("2 + 1").evaluate({})) | |
73 self.assertEqual(3, Expression("x + y").evaluate({'x': 2, 'y': 1})) | |
74 | |
75 def test_binop_sub(self): | |
76 self.assertEqual(1, Expression("2 - 1").evaluate({})) | |
77 self.assertEqual(1, Expression("x - y").evaluate({'x': 1, 'y': 1})) | |
78 | |
79 def test_binop_sub(self): | |
80 self.assertEqual(1, Expression("2 - 1").evaluate({})) | |
81 self.assertEqual(1, Expression("x - y").evaluate({'x': 2, 'y': 1})) | |
82 | |
83 def test_binop_mul(self): | |
84 self.assertEqual(4, Expression("2 * 2").evaluate({})) | |
85 self.assertEqual(4, Expression("x * y").evaluate({'x': 2, 'y': 2})) | |
86 | |
87 def test_binop_pow(self): | |
88 self.assertEqual(4, Expression("2 ** 2").evaluate({})) | |
89 self.assertEqual(4, Expression("x ** y").evaluate({'x': 2, 'y': 2})) | |
90 | |
91 def test_binop_div(self): | |
92 self.assertEqual(2, Expression("4 / 2").evaluate({})) | |
93 self.assertEqual(2, Expression("x / y").evaluate({'x': 4, 'y': 2})) | |
94 | |
95 def test_binop_floordiv(self): | |
96 self.assertEqual(1, Expression("3 // 2").evaluate({})) | |
97 self.assertEqual(1, Expression("x // y").evaluate({'x': 3, 'y': 2})) | |
98 | |
99 def test_binop_mod(self): | |
100 self.assertEqual(1, Expression("3 % 2").evaluate({})) | |
101 self.assertEqual(1, Expression("x % y").evaluate({'x': 3, 'y': 2})) | |
102 | |
32 | 103 def test_binop_and(self): |
104 self.assertEqual(0, Expression("1 & 0").evaluate({})) | |
105 self.assertEqual(0, Expression("x & y").evaluate({'x': 1, 'y': 0})) | |
106 | |
107 def test_binop_or(self): | |
108 self.assertEqual(1, Expression("1 | 0").evaluate({})) | |
109 self.assertEqual(1, Expression("x | y").evaluate({'x': 1, 'y': 0})) | |
110 | |
31 | 111 def test_binop_contains(self): |
112 self.assertEqual(True, Expression("1 in (1, 2, 3)").evaluate({})) | |
113 self.assertEqual(True, Expression("x in y").evaluate({'x': 1, | |
114 'y': (1, 2, 3)})) | |
115 | |
116 def test_binop_not_contains(self): | |
117 self.assertEqual(True, Expression("4 not in (1, 2, 3)").evaluate({})) | |
118 self.assertEqual(True, Expression("x not in y").evaluate({'x': 4, | |
119 'y': (1, 2, 3)})) | |
120 | |
32 | 121 def test_binop_is(self): |
122 self.assertEqual(True, Expression("1 is 1").evaluate({})) | |
123 self.assertEqual(True, Expression("x is y").evaluate({'x': 1, 'y': 1})) | |
124 self.assertEqual(False, Expression("1 is 2").evaluate({})) | |
125 self.assertEqual(False, Expression("x is y").evaluate({'x': 1, 'y': 2})) | |
126 | |
127 def test_binop_is_not(self): | |
128 self.assertEqual(True, Expression("1 is not 2").evaluate({})) | |
129 self.assertEqual(True, Expression("x is not y").evaluate({'x': 1, | |
130 'y': 2})) | |
131 self.assertEqual(False, Expression("1 is not 1").evaluate({})) | |
132 self.assertEqual(False, Expression("x is not y").evaluate({'x': 1, | |
133 'y': 1})) | |
134 | |
31 | 135 def test_boolop_and(self): |
136 self.assertEqual(False, Expression("True and False").evaluate({})) | |
137 self.assertEqual(False, Expression("x and y").evaluate({'x': True, | |
138 'y': False})) | |
139 | |
140 def test_boolop_or(self): | |
141 self.assertEqual(True, Expression("True or False").evaluate({})) | |
142 self.assertEqual(True, Expression("x or y").evaluate({'x': True, | |
143 'y': False})) | |
144 | |
145 def test_compare_eq(self): | |
146 self.assertEqual(True, Expression("1 == 1").evaluate({})) | |
147 self.assertEqual(True, Expression("x == y").evaluate({'x': 1, 'y': 1})) | |
148 | |
149 def test_compare_ne(self): | |
150 self.assertEqual(False, Expression("1 != 1").evaluate({})) | |
151 self.assertEqual(False, Expression("x != y").evaluate({'x': 1, 'y': 1})) | |
32 | 152 self.assertEqual(False, Expression("1 <> 1").evaluate({})) |
153 self.assertEqual(False, Expression("x <> y").evaluate({'x': 1, 'y': 1})) | |
31 | 154 |
155 def test_compare_lt(self): | |
156 self.assertEqual(True, Expression("1 < 2").evaluate({})) | |
157 self.assertEqual(True, Expression("x < y").evaluate({'x': 1, 'y': 2})) | |
158 | |
159 def test_compare_le(self): | |
160 self.assertEqual(True, Expression("1 <= 1").evaluate({})) | |
161 self.assertEqual(True, Expression("x <= y").evaluate({'x': 1, 'y': 1})) | |
162 | |
163 def test_compare_gt(self): | |
164 self.assertEqual(True, Expression("2 > 1").evaluate({})) | |
165 self.assertEqual(True, Expression("x > y").evaluate({'x': 2, 'y': 1})) | |
166 | |
167 def test_compare_ge(self): | |
168 self.assertEqual(True, Expression("1 >= 1").evaluate({})) | |
169 self.assertEqual(True, Expression("x >= y").evaluate({'x': 1, 'y': 1})) | |
170 | |
171 def test_compare_multi(self): | |
172 self.assertEqual(True, Expression("1 != 3 == 3").evaluate({})) | |
81
d60486018004
Template expressions are now compiled to Python bytecode.
cmlenz
parents:
66
diff
changeset
|
173 self.assertEqual(True, Expression("x != y == y").evaluate({'x': 1, |
31 | 174 'y': 3})) |
175 | |
87
1b874f032bde
Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents:
81
diff
changeset
|
176 def test_call_function(self): |
1b874f032bde
Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents:
81
diff
changeset
|
177 self.assertEqual(42, Expression("foo()").evaluate({'foo': lambda: 42})) |
1b874f032bde
Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents:
81
diff
changeset
|
178 data = {'foo': 'bar'} |
1b874f032bde
Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents:
81
diff
changeset
|
179 self.assertEqual('BAR', Expression("foo.upper()").evaluate(data)) |
1b874f032bde
Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents:
81
diff
changeset
|
180 data = {'foo': {'bar': range(42)}} |
1b874f032bde
Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents:
81
diff
changeset
|
181 self.assertEqual(42, Expression("len(foo.bar)").evaluate(data)) |
1b874f032bde
Fix some problems in expression evaluation by transforming the AST and compiling that to bytecode, instead of generating bytecode directly. Invalidates #13.
cmlenz
parents:
81
diff
changeset
|
182 |
90
c835e81c50af
When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents:
88
diff
changeset
|
183 def test_call_function_without_params(self): |
c835e81c50af
When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents:
88
diff
changeset
|
184 self.assertEqual(42, Expression("foo").evaluate({'foo': lambda: 42})) |
c835e81c50af
When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents:
88
diff
changeset
|
185 data = {'foo': 'bar'} |
c835e81c50af
When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents:
88
diff
changeset
|
186 self.assertEqual('BAR', Expression("foo.upper").evaluate(data)) |
c835e81c50af
When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents:
88
diff
changeset
|
187 data = {'foo': {'bar': range(42)}} |
c835e81c50af
When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents:
88
diff
changeset
|
188 |
88
628ba9ed39ef
Add support for list comprehension in expressions (see #12).
cmlenz
parents:
87
diff
changeset
|
189 def test_list_comprehension(self): |
628ba9ed39ef
Add support for list comprehension in expressions (see #12).
cmlenz
parents:
87
diff
changeset
|
190 expr = Expression("[n for n in numbers if n < 2]") |
628ba9ed39ef
Add support for list comprehension in expressions (see #12).
cmlenz
parents:
87
diff
changeset
|
191 self.assertEqual([0, 1], expr.evaluate({'numbers': range(5)})) |
628ba9ed39ef
Add support for list comprehension in expressions (see #12).
cmlenz
parents:
87
diff
changeset
|
192 |
628ba9ed39ef
Add support for list comprehension in expressions (see #12).
cmlenz
parents:
87
diff
changeset
|
193 expr = Expression("[(i, n + 1) for i, n in enumerate(numbers)]") |
628ba9ed39ef
Add support for list comprehension in expressions (see #12).
cmlenz
parents:
87
diff
changeset
|
194 self.assertEqual([(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)], |
628ba9ed39ef
Add support for list comprehension in expressions (see #12).
cmlenz
parents:
87
diff
changeset
|
195 expr.evaluate({'numbers': range(5)})) |
628ba9ed39ef
Add support for list comprehension in expressions (see #12).
cmlenz
parents:
87
diff
changeset
|
196 |
628ba9ed39ef
Add support for list comprehension in expressions (see #12).
cmlenz
parents:
87
diff
changeset
|
197 expr = Expression("[offset + n for n in numbers]") |
628ba9ed39ef
Add support for list comprehension in expressions (see #12).
cmlenz
parents:
87
diff
changeset
|
198 self.assertEqual([2, 3, 4, 5, 6], |
628ba9ed39ef
Add support for list comprehension in expressions (see #12).
cmlenz
parents:
87
diff
changeset
|
199 expr.evaluate({'numbers': range(5), 'offset': 2})) |
81
d60486018004
Template expressions are now compiled to Python bytecode.
cmlenz
parents:
66
diff
changeset
|
200 |
30
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
201 |
1 | 202 def suite(): |
203 suite = unittest.TestSuite() | |
30
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
204 suite.addTest(unittest.makeSuite(ExpressionTestCase, 'test')) |
27 | 205 suite.addTest(doctest.DocTestSuite(Expression.__module__)) |
1 | 206 return suite |
207 | |
208 if __name__ == '__main__': | |
209 unittest.main(defaultTest='suite') |