[Zope-CVS] CVS: Packages/pypes/pypes/tests - test_expression.py:1.4
Casey Duncan
casey at zope.com
Tue Feb 3 01:46:28 EST 2004
Update of /cvs-repository/Packages/pypes/pypes/tests
In directory cvs.zope.org:/tmp/cvs-serv27791/tests
Modified Files:
test_expression.py
Log Message:
Add expression optimize method with tests
=== Packages/pypes/pypes/tests/test_expression.py 1.3 => 1.4 ===
--- Packages/pypes/pypes/tests/test_expression.py:1.3 Sat Jan 31 01:09:26 2004
+++ Packages/pypes/pypes/tests/test_expression.py Tue Feb 3 01:46:27 2004
@@ -17,9 +17,9 @@
import unittest
from sets import Set
-from compiler import ast
+from compiler import ast, parse
from pypes.tests.common import sort
-from pypes.expression import Expression
+from pypes.expression import Expression, nodesEqual
class TestExpression(unittest.TestCase):
"""Blackbox expression tests"""
@@ -65,13 +65,40 @@
self.assertRaises(NameError, e)
def testNodesEqual(self):
- from compiler import parse
- from pypes.expression import nodesEqual
e1 = parse('x == 1', mode='eval')
e2 = parse('x >= 1', mode='eval')
self.failUnless(nodesEqual(e1, e1))
self.failIf(nodesEqual(e1, e2))
-
+
+ def testOptimizeSimple(self):
+ e = Expression('x*z - y < z', {'x':5, 'z':7})
+ e.optimize()
+ opt_tree = parse('35 - y < 7', mode='eval')
+ self.failUnless(nodesEqual(e._tree, opt_tree))
+
+ def testOptimizeOr(self):
+ e = Expression('len(y) > 1 or x > y', {'y':'somethin'})
+ e.optimize()
+ self.failUnless(nodesEqual(e._tree, ast.Expression(ast.Const(True))))
+ e = Expression('x > y or len(y) > 1', {'y':'somethin'})
+ e.optimize()
+ self.failUnless(nodesEqual(e._tree, ast.Expression(ast.Const(True))))
+
+ def testOptimizeAnd(self):
+ e = Expression('len(y) < 5 and x > y', {'y':'nothin'})
+ e.optimize()
+ self.failUnless(nodesEqual(e._tree, ast.Expression(ast.Const(False))))
+ e = Expression('x > y and len(y) < 5', {'y':'nothin'})
+ e.optimize()
+ self.failUnless(nodesEqual(e._tree, ast.Expression(ast.Const(False))))
+
+ def testOptimizeFreeNames(self):
+ e = Expression('x < y and x > z', {'x':2, 'y':3, 'z':0})
+ e.optimize(free_names=['x', 'z'])
+ opt_tree = parse('x < 3 and x > z', mode='eval')
+ self.failUnless(e._tree, opt_tree)
+
+
class WhiteTestExpression(unittest.TestCase):
"""Whitebox expression tests"""
More information about the Zope-CVS
mailing list