Mercurial > genshi > mirror
annotate markup/tests/eval.py @ 148:dcc9dc25bc59 trunk
Added changelog file, plus some README and setup tweaks.
author | cmlenz |
---|---|
date | Tue, 15 Aug 2006 14:41:08 +0000 |
parents | d681d2c3cd8d |
children | 1999291f7a30 |
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 | |
131
2ad83f1d337c
* Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents:
118
diff
changeset
|
15 import sys |
1 | 16 import unittest |
17 | |
27 | 18 from markup.eval import Expression |
1 | 19 |
30
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
20 |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
21 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
|
22 |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
23 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
|
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("'''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("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
|
29 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
|
30 |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
31 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
|
32 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
|
33 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
|
34 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
|
35 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
|
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 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
|
38 |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
39 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
|
40 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
|
41 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
|
42 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
|
43 |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
44 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
|
45 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
|
46 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
|
47 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
|
48 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
|
49 |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
50 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
|
51 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
|
52 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
|
53 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
|
54 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
|
55 |
31 | 56 def test_unaryop_pos(self): |
57 self.assertEqual(1, Expression("+1").evaluate({})) | |
58 self.assertEqual(1, Expression("+x").evaluate({'x': 1})) | |
59 | |
60 def test_unaryop_neg(self): | |
61 self.assertEqual(-1, Expression("-1").evaluate({})) | |
62 self.assertEqual(-1, Expression("-x").evaluate({'x': 1})) | |
63 | |
64 def test_unaryop_not(self): | |
65 self.assertEqual(False, Expression("not True").evaluate({})) | |
66 self.assertEqual(False, Expression("not x").evaluate({'x': True})) | |
67 | |
32 | 68 def test_unaryop_inv(self): |
69 self.assertEqual(-2, Expression("~1").evaluate({})) | |
70 self.assertEqual(-2, Expression("~x").evaluate({'x': 1})) | |
71 | |
31 | 72 def test_binop_add(self): |
73 self.assertEqual(3, Expression("2 + 1").evaluate({})) | |
74 self.assertEqual(3, Expression("x + y").evaluate({'x': 2, 'y': 1})) | |
75 | |
76 def test_binop_sub(self): | |
77 self.assertEqual(1, Expression("2 - 1").evaluate({})) | |
78 self.assertEqual(1, Expression("x - y").evaluate({'x': 1, 'y': 1})) | |
79 | |
80 def test_binop_sub(self): | |
81 self.assertEqual(1, Expression("2 - 1").evaluate({})) | |
82 self.assertEqual(1, Expression("x - y").evaluate({'x': 2, 'y': 1})) | |
83 | |
84 def test_binop_mul(self): | |
85 self.assertEqual(4, Expression("2 * 2").evaluate({})) | |
86 self.assertEqual(4, Expression("x * y").evaluate({'x': 2, 'y': 2})) | |
87 | |
88 def test_binop_pow(self): | |
89 self.assertEqual(4, Expression("2 ** 2").evaluate({})) | |
90 self.assertEqual(4, Expression("x ** y").evaluate({'x': 2, 'y': 2})) | |
91 | |
92 def test_binop_div(self): | |
93 self.assertEqual(2, Expression("4 / 2").evaluate({})) | |
94 self.assertEqual(2, Expression("x / y").evaluate({'x': 4, 'y': 2})) | |
95 | |
96 def test_binop_floordiv(self): | |
97 self.assertEqual(1, Expression("3 // 2").evaluate({})) | |
98 self.assertEqual(1, Expression("x // y").evaluate({'x': 3, 'y': 2})) | |
99 | |
100 def test_binop_mod(self): | |
101 self.assertEqual(1, Expression("3 % 2").evaluate({})) | |
102 self.assertEqual(1, Expression("x % y").evaluate({'x': 3, 'y': 2})) | |
103 | |
32 | 104 def test_binop_and(self): |
105 self.assertEqual(0, Expression("1 & 0").evaluate({})) | |
106 self.assertEqual(0, Expression("x & y").evaluate({'x': 1, 'y': 0})) | |
107 | |
108 def test_binop_or(self): | |
109 self.assertEqual(1, Expression("1 | 0").evaluate({})) | |
110 self.assertEqual(1, Expression("x | y").evaluate({'x': 1, 'y': 0})) | |
111 | |
31 | 112 def test_binop_contains(self): |
113 self.assertEqual(True, Expression("1 in (1, 2, 3)").evaluate({})) | |
114 self.assertEqual(True, Expression("x in y").evaluate({'x': 1, | |
115 'y': (1, 2, 3)})) | |
116 | |
117 def test_binop_not_contains(self): | |
118 self.assertEqual(True, Expression("4 not in (1, 2, 3)").evaluate({})) | |
119 self.assertEqual(True, Expression("x not in y").evaluate({'x': 4, | |
120 'y': (1, 2, 3)})) | |
121 | |
32 | 122 def test_binop_is(self): |
123 self.assertEqual(True, Expression("1 is 1").evaluate({})) | |
124 self.assertEqual(True, Expression("x is y").evaluate({'x': 1, 'y': 1})) | |
125 self.assertEqual(False, Expression("1 is 2").evaluate({})) | |
126 self.assertEqual(False, Expression("x is y").evaluate({'x': 1, 'y': 2})) | |
127 | |
128 def test_binop_is_not(self): | |
129 self.assertEqual(True, Expression("1 is not 2").evaluate({})) | |
130 self.assertEqual(True, Expression("x is not y").evaluate({'x': 1, | |
131 'y': 2})) | |
132 self.assertEqual(False, Expression("1 is not 1").evaluate({})) | |
133 self.assertEqual(False, Expression("x is not y").evaluate({'x': 1, | |
134 'y': 1})) | |
135 | |
31 | 136 def test_boolop_and(self): |
137 self.assertEqual(False, Expression("True and False").evaluate({})) | |
138 self.assertEqual(False, Expression("x and y").evaluate({'x': True, | |
139 'y': False})) | |
140 | |
141 def test_boolop_or(self): | |
142 self.assertEqual(True, Expression("True or False").evaluate({})) | |
143 self.assertEqual(True, Expression("x or y").evaluate({'x': True, | |
144 'y': False})) | |
145 | |
146 def test_compare_eq(self): | |
147 self.assertEqual(True, Expression("1 == 1").evaluate({})) | |
148 self.assertEqual(True, Expression("x == y").evaluate({'x': 1, 'y': 1})) | |
149 | |
150 def test_compare_ne(self): | |
151 self.assertEqual(False, Expression("1 != 1").evaluate({})) | |
152 self.assertEqual(False, Expression("x != y").evaluate({'x': 1, 'y': 1})) | |
32 | 153 self.assertEqual(False, Expression("1 <> 1").evaluate({})) |
154 self.assertEqual(False, Expression("x <> y").evaluate({'x': 1, 'y': 1})) | |
31 | 155 |
156 def test_compare_lt(self): | |
157 self.assertEqual(True, Expression("1 < 2").evaluate({})) | |
158 self.assertEqual(True, Expression("x < y").evaluate({'x': 1, 'y': 2})) | |
159 | |
160 def test_compare_le(self): | |
161 self.assertEqual(True, Expression("1 <= 1").evaluate({})) | |
162 self.assertEqual(True, Expression("x <= y").evaluate({'x': 1, 'y': 1})) | |
163 | |
164 def test_compare_gt(self): | |
165 self.assertEqual(True, Expression("2 > 1").evaluate({})) | |
166 self.assertEqual(True, Expression("x > y").evaluate({'x': 2, 'y': 1})) | |
167 | |
168 def test_compare_ge(self): | |
169 self.assertEqual(True, Expression("1 >= 1").evaluate({})) | |
170 self.assertEqual(True, Expression("x >= y").evaluate({'x': 1, 'y': 1})) | |
171 | |
172 def test_compare_multi(self): | |
173 self.assertEqual(True, Expression("1 != 3 == 3").evaluate({})) | |
81
d60486018004
Template expressions are now compiled to Python bytecode.
cmlenz
parents:
66
diff
changeset
|
174 self.assertEqual(True, Expression("x != y == y").evaluate({'x': 1, |
31 | 175 'y': 3})) |
176 | |
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
|
177 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
|
178 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
|
179 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
|
180 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
|
181 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
|
182 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
|
183 |
102 | 184 def test_call_keywords(self): |
185 self.assertEqual(42, Expression("foo(x=bar)").evaluate({'foo': lambda x: x, | |
186 'bar': 42})) | |
187 | |
90
c835e81c50af
When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents:
88
diff
changeset
|
188 def test_call_function_without_params(self): |
c835e81c50af
When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents:
88
diff
changeset
|
189 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
|
190 data = {'foo': 'bar'} |
c835e81c50af
When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents:
88
diff
changeset
|
191 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
|
192 data = {'foo': {'bar': range(42)}} |
c835e81c50af
When an expression evaluates to a callable, it is called implicitly.
cmlenz
parents:
88
diff
changeset
|
193 |
118
c392d38694d9
Add basic support for using `lambda`s in expressions. Closes #21. (Not sure about default arguments, need a test case).
cmlenz
parents:
102
diff
changeset
|
194 def test_lambda(self): |
c392d38694d9
Add basic support for using `lambda`s in expressions. Closes #21. (Not sure about default arguments, need a test case).
cmlenz
parents:
102
diff
changeset
|
195 # Define a custom `sorted` function cause the builtin isn't available |
c392d38694d9
Add basic support for using `lambda`s in expressions. Closes #21. (Not sure about default arguments, need a test case).
cmlenz
parents:
102
diff
changeset
|
196 # on Python 2.3 |
c392d38694d9
Add basic support for using `lambda`s in expressions. Closes #21. (Not sure about default arguments, need a test case).
cmlenz
parents:
102
diff
changeset
|
197 def sorted(items, compfunc): |
c392d38694d9
Add basic support for using `lambda`s in expressions. Closes #21. (Not sure about default arguments, need a test case).
cmlenz
parents:
102
diff
changeset
|
198 items.sort(compfunc) |
c392d38694d9
Add basic support for using `lambda`s in expressions. Closes #21. (Not sure about default arguments, need a test case).
cmlenz
parents:
102
diff
changeset
|
199 return items |
c392d38694d9
Add basic support for using `lambda`s in expressions. Closes #21. (Not sure about default arguments, need a test case).
cmlenz
parents:
102
diff
changeset
|
200 data = {'items': [{'name': 'b', 'value': 0}, {'name': 'a', 'value': 1}], |
c392d38694d9
Add basic support for using `lambda`s in expressions. Closes #21. (Not sure about default arguments, need a test case).
cmlenz
parents:
102
diff
changeset
|
201 'sorted': sorted} |
c392d38694d9
Add basic support for using `lambda`s in expressions. Closes #21. (Not sure about default arguments, need a test case).
cmlenz
parents:
102
diff
changeset
|
202 expr = Expression("sorted(items, lambda a, b: cmp(a.name, b.name))") |
c392d38694d9
Add basic support for using `lambda`s in expressions. Closes #21. (Not sure about default arguments, need a test case).
cmlenz
parents:
102
diff
changeset
|
203 self.assertEqual([{'name': 'a', 'value': 1}, {'name': 'b', 'value': 0}], |
c392d38694d9
Add basic support for using `lambda`s in expressions. Closes #21. (Not sure about default arguments, need a test case).
cmlenz
parents:
102
diff
changeset
|
204 expr.evaluate(data)) |
c392d38694d9
Add basic support for using `lambda`s in expressions. Closes #21. (Not sure about default arguments, need a test case).
cmlenz
parents:
102
diff
changeset
|
205 |
88
628ba9ed39ef
Add support for list comprehension in expressions (see #12).
cmlenz
parents:
87
diff
changeset
|
206 def test_list_comprehension(self): |
628ba9ed39ef
Add support for list comprehension in expressions (see #12).
cmlenz
parents:
87
diff
changeset
|
207 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
|
208 self.assertEqual([0, 1], expr.evaluate({'numbers': range(5)})) |
628ba9ed39ef
Add support for list comprehension in expressions (see #12).
cmlenz
parents:
87
diff
changeset
|
209 |
628ba9ed39ef
Add support for list comprehension in expressions (see #12).
cmlenz
parents:
87
diff
changeset
|
210 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
|
211 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
|
212 expr.evaluate({'numbers': range(5)})) |
628ba9ed39ef
Add support for list comprehension in expressions (see #12).
cmlenz
parents:
87
diff
changeset
|
213 |
628ba9ed39ef
Add support for list comprehension in expressions (see #12).
cmlenz
parents:
87
diff
changeset
|
214 expr = Expression("[offset + n for n in numbers]") |
628ba9ed39ef
Add support for list comprehension in expressions (see #12).
cmlenz
parents:
87
diff
changeset
|
215 self.assertEqual([2, 3, 4, 5, 6], |
628ba9ed39ef
Add support for list comprehension in expressions (see #12).
cmlenz
parents:
87
diff
changeset
|
216 expr.evaluate({'numbers': range(5), 'offset': 2})) |
81
d60486018004
Template expressions are now compiled to Python bytecode.
cmlenz
parents:
66
diff
changeset
|
217 |
131
2ad83f1d337c
* Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents:
118
diff
changeset
|
218 def test_list_comprehension_with_getattr(self): |
2ad83f1d337c
* Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents:
118
diff
changeset
|
219 items = [{'name': 'a', 'value': 1}, {'name': 'b', 'value': 2}] |
2ad83f1d337c
* Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents:
118
diff
changeset
|
220 expr = Expression("[i.name for i in items if i.value > 1]") |
2ad83f1d337c
* Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents:
118
diff
changeset
|
221 self.assertEqual(['b'], expr.evaluate({'items': items})) |
2ad83f1d337c
* Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents:
118
diff
changeset
|
222 |
2ad83f1d337c
* Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents:
118
diff
changeset
|
223 def test_list_comprehension_with_getitem(self): |
2ad83f1d337c
* Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents:
118
diff
changeset
|
224 items = [{'name': 'a', 'value': 1}, {'name': 'b', 'value': 2}] |
2ad83f1d337c
* Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents:
118
diff
changeset
|
225 expr = Expression("[i['name'] for i in items if i['value'] > 1]") |
2ad83f1d337c
* Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents:
118
diff
changeset
|
226 self.assertEqual(['b'], expr.evaluate({'items': items})) |
2ad83f1d337c
* Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents:
118
diff
changeset
|
227 |
2ad83f1d337c
* Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents:
118
diff
changeset
|
228 def test_error_position(self): |
2ad83f1d337c
* Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents:
118
diff
changeset
|
229 expr = Expression("nothing()", filename='index.html', lineno=50) |
2ad83f1d337c
* Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents:
118
diff
changeset
|
230 try: |
2ad83f1d337c
* Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents:
118
diff
changeset
|
231 expr.evaluate({}) |
2ad83f1d337c
* Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents:
118
diff
changeset
|
232 self.fail('Expected TypeError') |
2ad83f1d337c
* Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents:
118
diff
changeset
|
233 except TypeError, e: |
2ad83f1d337c
* Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents:
118
diff
changeset
|
234 exc_type, exc_value, exc_traceback = sys.exc_info() |
2ad83f1d337c
* Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents:
118
diff
changeset
|
235 frame = exc_traceback.tb_next |
2ad83f1d337c
* Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents:
118
diff
changeset
|
236 while frame.tb_next: |
2ad83f1d337c
* Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents:
118
diff
changeset
|
237 frame = frame.tb_next |
134
d681d2c3cd8d
* Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents:
131
diff
changeset
|
238 self.assertEqual('<Expression "nothing()">', |
d681d2c3cd8d
* Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents:
131
diff
changeset
|
239 frame.tb_frame.f_code.co_name) |
131
2ad83f1d337c
* Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents:
118
diff
changeset
|
240 self.assertEqual('index.html', frame.tb_frame.f_code.co_filename) |
2ad83f1d337c
* Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents:
118
diff
changeset
|
241 self.assertEqual(50, frame.tb_lineno) |
2ad83f1d337c
* Support for line numbers in exceptions in expression evaluation (#22).
cmlenz
parents:
118
diff
changeset
|
242 |
30
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
243 |
1 | 244 def suite(): |
245 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
|
246 suite.addTest(unittest.makeSuite(ExpressionTestCase, 'test')) |
27 | 247 suite.addTest(doctest.DocTestSuite(Expression.__module__)) |
1 | 248 return suite |
249 | |
250 if __name__ == '__main__': | |
251 unittest.main(defaultTest='suite') |