comparison markup/tests/eval.py @ 131:203f459e7e26

* Support for line numbers in exceptions in expression evaluation (#22). * Fix bug in expression evaluation when item access was used inside a lambda or list comprehension. Thanks to Kevin Dangoor for reporting the problem.
author cmlenz
date Fri, 04 Aug 2006 13:07:52 +0000
parents 226613431921
children df44110ca91d
comparison
equal deleted inserted replaced
130:aa69c1f34a26 131:203f459e7e26
10 # This software consists of voluntary contributions made by many 10 # This software consists of voluntary contributions made by many
11 # individuals. For the exact contribution history, see the revision 11 # individuals. For the exact contribution history, see the revision
12 # history and logs, available at hhttp://markup.edgewall.org/log/. 12 # history and logs, available at hhttp://markup.edgewall.org/log/.
13 13
14 import doctest 14 import doctest
15 import sys
15 import unittest 16 import unittest
16 17
17 from markup.eval import Expression 18 from markup.eval import Expression
18 19
19 20
212 213
213 expr = Expression("[offset + n for n in numbers]") 214 expr = Expression("[offset + n for n in numbers]")
214 self.assertEqual([2, 3, 4, 5, 6], 215 self.assertEqual([2, 3, 4, 5, 6],
215 expr.evaluate({'numbers': range(5), 'offset': 2})) 216 expr.evaluate({'numbers': range(5), 'offset': 2}))
216 217
218 def test_list_comprehension_with_getattr(self):
219 items = [{'name': 'a', 'value': 1}, {'name': 'b', 'value': 2}]
220 expr = Expression("[i.name for i in items if i.value > 1]")
221 self.assertEqual(['b'], expr.evaluate({'items': items}))
222
223 def test_list_comprehension_with_getitem(self):
224 items = [{'name': 'a', 'value': 1}, {'name': 'b', 'value': 2}]
225 expr = Expression("[i['name'] for i in items if i['value'] > 1]")
226 self.assertEqual(['b'], expr.evaluate({'items': items}))
227
228 def test_error_position(self):
229 expr = Expression("nothing()", filename='index.html', lineno=50)
230 try:
231 expr.evaluate({})
232 self.fail('Expected TypeError')
233 except TypeError, e:
234 exc_type, exc_value, exc_traceback = sys.exc_info()
235 frame = exc_traceback.tb_next
236 while frame.tb_next:
237 frame = frame.tb_next
238 self.assertEqual('index.html', frame.tb_frame.f_code.co_filename)
239 self.assertEqual(50, frame.tb_lineno)
240
217 241
218 def suite(): 242 def suite():
219 suite = unittest.TestSuite() 243 suite = unittest.TestSuite()
220 suite.addTest(unittest.makeSuite(ExpressionTestCase, 'test')) 244 suite.addTest(unittest.makeSuite(ExpressionTestCase, 'test'))
221 suite.addTest(doctest.DocTestSuite(Expression.__module__)) 245 suite.addTest(doctest.DocTestSuite(Expression.__module__))
Copyright (C) 2012-2017 Edgewall Software