Explorar o código

Added equality chck and negation take-over to Scope object.

Taddeus Kroes %!s(int64=14) %!d(string=hai) anos
pai
achega
031e326f0a
Modificáronse 2 ficheiros con 10 adicións e 1 borrados
  1. 5 1
      src/node.py
  2. 5 0
      tests/test_node.py

+ 5 - 1
src/node.py

@@ -354,6 +354,10 @@ class Scope(object):
     def __iter__(self):
         return iter(self.nodes)
 
+    def __eq__(self, other):
+        return isinstance(other, Scope) and self.node == other.node \
+               and self.nodes == other.node
+
     def remove(self, node, replacement=None):
         if node.is_leaf:
             node_cmp = hash(node)
@@ -381,7 +385,7 @@ class Scope(object):
         self.remove(node, replacement=replacement)
 
     def as_nary_node(self):
-        return nary_node(self.node.value, self.nodes)
+        return nary_node(self.node.value, self.nodes).negate(self.node.negated)
 
 
 def nary_node(operator, scope):

+ 5 - 0
tests/test_node.py

@@ -193,3 +193,8 @@ class TestNode(RulesTestCase):
 
     def test_scope_as_nary_node(self):
         self.assertEqualNodes(self.scope.as_nary_node(), self.n)
+
+    def test_scope_as_nary_node_negated(self):
+        n = tree('-(a + b)')
+        self.assertEqualNodes(Scope(n).as_nary_node(), n)
+        self.assertEqualNodes(Scope(-n).as_nary_node(), -n)