[Zope-CVS] CVS: Packages/pypes/pypes/tests - test_expression.py:1.8

Casey Duncan casey at zope.com
Wed Apr 21 01:25:54 EDT 2004


Update of /cvs-repository/Packages/pypes/pypes/tests
In directory cvs.zope.org:/tmp/cvs-serv11141/tests

Modified Files:
	test_expression.py 
Log Message:
Add __eq__, __and__ and __or__ support for comparing and combining expressions


=== Packages/pypes/pypes/tests/test_expression.py 1.7 => 1.8 ===
--- Packages/pypes/pypes/tests/test_expression.py:1.7	Tue Apr 13 00:15:28 2004
+++ Packages/pypes/pypes/tests/test_expression.py	Wed Apr 21 01:25:53 2004
@@ -16,11 +16,13 @@
 $Id$"""
 
 import unittest
+import operator
 from sets import Set
 from compiler import ast, parse
-from pypes.tests.common import sort
 from zope.interface.verify import verifyObject
+from pypes.tests.common import sort
 from pypes.expression import Expression, nodesEqual
+from pypes.exceptions import PypesError
 
 class TestExpression(unittest.TestCase):
     """Blackbox expression tests"""
@@ -34,6 +36,7 @@
         ast = parse('sin(1 / x)', mode='eval')
         e = Expression.fromAstNode(ast, {})
         self.assertEqual(e.names(), Set(['sin', 'x']))
+        self.failUnless(e.ast() is ast)
     
     def testExprParses(self):
         Expression('x')
@@ -117,6 +120,44 @@
         e.optimize(free_names=['x', 'z'])
         opt_tree = parse('x < 3 and x > z', mode='eval')
         self.failUnless(e._tree, opt_tree)
+    
+    def testEquals(self):
+        self.assertEqual(Expression('x < y'), Expression('x < y'))
+        self.assertEqual(Expression('x < y', {'x':1}),
+                         Expression('x < y', {'x':1}))
+        e = Expression('a**2 + b**2 == c**2')
+        self.assertEqual(e, e)
+        self.assertNotEqual(Expression('x < y', {'x':1}),
+                            Expression('x < y', {'x':2}))
+        self.assertNotEqual(Expression('x < y'), Expression('x > y'))
+        self.assertNotEqual(Expression('x < y', {'x':1}),
+                            Expression('x > y', {'x':1}))
+        self.assertNotEqual(Expression('x < y', {'x':1}),
+                            Expression('x > y', {'y':1}))
+    
+    def testOr(self):
+        e = Expression('x > 1') | Expression('y < 2')
+        self.assertEqual(e, Expression('x > 1 or y < 2'))
+    
+    def testOrWithNamespace(self):
+        e = Expression('x > z', {'z':3}) | Expression('y < w',{'y':-1})
+        self.assertEqual(e, Expression('x > z or y < w', {'z':3, 'y':-1}))
+        self.assertRaises(PypesError, operator.or_,
+            Expression('x > z', {'x':100}), Expression('y < x'))
+        self.assertRaises(PypesError, operator.or_,
+            Expression('x > z', {'x':100}), Expression('y < x', {'x': 1}))
+    
+    def testAnd(self):
+        e = Expression('x > 1') & Expression('y < 2')
+        self.assertEqual(e, Expression('x > 1 and y < 2'))
+    
+    def testAndWithNamespace(self):
+        e = Expression('x > z', {'z':3}) & Expression('y < w',{'y':-1})
+        self.assertEqual(e, Expression('x > z and y < w', {'z':3, 'y':-1}))
+        self.assertRaises(PypesError, operator.and_,
+            Expression('x > z', {'x':100}), Expression('y < x'))
+        self.assertRaises(PypesError, operator.and_, 
+            Expression('x > z', {'x':100}), Expression('y < x', {'x': 1}))
 
 
 class WhiteTestExpression(unittest.TestCase):




More information about the Zope-CVS mailing list