فهرست منبع

Added negation counter to strict equivalence check and added 'replace' function to Scope class.

Taddeus Kroes 14 سال پیش
والد
کامیت
b63b5130b1
2فایلهای تغییر یافته به همراه12 افزوده شده و 22 حذف شده
  1. 8 6
      src/node.py
  2. 4 16
      tests/test_node.py

+ 8 - 6
src/node.py

@@ -170,10 +170,8 @@ class ExpressionNode(Node, ExpressionBase):
         """
         Check strict equivalence.
         """
-        if isinstance(other, ExpressionNode):
-            return self.op == other.op and self.nodes == other.nodes
-
-        return False
+        return isinstance(other, ExpressionNode) and self.op == other.op \
+               and self.negated == other.negated and self.nodes == other.nodes
 
     def substitute(self, old_child, new_child):
         self.nodes[self.nodes.index(old_child)] = new_child
@@ -267,7 +265,7 @@ class ExpressionNode(Node, ExpressionBase):
             s0 = Scope(self)
             s1 = set(Scope(other))
 
-            # Scopes sould be of equal size
+            # Scopes should be of equal size
             if len(s0) != len(s1):
                 return False
 
@@ -308,7 +306,8 @@ class ExpressionLeaf(Leaf, ExpressionBase):
         if other_type in TYPE_MAP:
             return TYPE_MAP[other_type] == self.type and self.value == other
 
-        return other.type == self.type and self.value == other.value
+        return self.negated == other.negated and self.type == other.type \
+               and self.value == other.value
 
     def equals(self, other):
         """
@@ -373,6 +372,9 @@ class Scope(object):
         raise ValueError('Node "%s" is not in the scope of "%s".'
                          % (node, self.node))
 
+    def replace(self, node, replacement):
+        self.remove(node, replacement=replacement)
+
     def as_nary_node(self):
         return nary_node(self.node.value, self.nodes)
 

+ 4 - 16
tests/test_node.py

@@ -30,22 +30,10 @@ class TestNode(RulesTestCase):
         self.assertTrue(N('+', *self.l[:2]).is_op(OP_ADD))
         self.assertFalse(N('-', *self.l[:2]).is_op(OP_ADD))
 
-    def test_is_op_or_negated(self):
-        self.assertTrue(N('+', *self.l[:2]).is_op_or_negated(OP_ADD))
-        self.assertTrue(N('-', N('+', *self.l[:2])).is_op_or_negated(OP_ADD))
-        self.assertFalse(N('-', *self.l[:2]).is_op_or_negated(OP_ADD))
-        self.assertFalse(self.l[0].is_op_or_negated(OP_ADD))
-
     def test_is_leaf(self):
         self.assertTrue(L(2).is_leaf)
         self.assertFalse(N('+', *self.l[:2]).is_leaf)
 
-    def test_is_leaf_or_negated(self):
-        self.assertTrue(L(2).is_leaf_or_negated())
-        self.assertTrue(N('-', L(2)).is_leaf_or_negated())
-        self.assertFalse(N('+', *self.l[:2]).is_leaf_or_negated())
-        self.assertFalse(N('-', N('+', *self.l[:2])).is_leaf_or_negated())
-
     def test_is_power(self):
         self.assertTrue(N('^', *self.l[:2]).is_power())
         self.assertFalse(N('+', *self.l[:2]).is_power())
@@ -185,14 +173,14 @@ class TestNode(RulesTestCase):
         self.scope.remove(self.cd)
         self.assertEqual(self.scope.nodes, [self.a, self.b])
 
-    def test_scope_remove_replace(self):
-        self.scope.remove(self.cd, self.f)
-        self.assertEqual(self.scope.nodes, [self.a, self.b, self.f])
-
     def test_scope_remove_error(self):
         with self.assertRaises(ValueError):
             self.scope.remove(self.f)
 
+    def test_scope_replace(self):
+        self.scope.replace(self.cd, self.f)
+        self.assertEqual(self.scope.nodes, [self.a, self.b, self.f])
+
     def test_nary_node(self):
         a, b, c, d = tree('a,b,c,d')