From 848c6c616bc35d9db8b38dfc4b74f1d88501f783 Mon Sep 17 00:00:00 2001 From: Jon Lund Steffensen Date: Fri, 24 Feb 2017 18:43:04 -0500 Subject: [PATCH] boolean: Allow getting length of And/Or terms --- psamm/expression/boolean.py | 3 +++ psamm/tests/test_expression_boolean.py | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/psamm/expression/boolean.py b/psamm/expression/boolean.py index 6841e30b..4a9ba0b7 100644 --- a/psamm/expression/boolean.py +++ b/psamm/expression/boolean.py @@ -78,6 +78,9 @@ def __iter__(self): def __hash__(self): return hash(self._terms) + def __len__(self): + return len(self._terms) + def __eq__(self, other): return (isinstance(other, self.__class__) and self._terms == other._terms) diff --git a/psamm/tests/test_expression_boolean.py b/psamm/tests/test_expression_boolean.py index 3e2917ba..023093d0 100755 --- a/psamm/tests/test_expression_boolean.py +++ b/psamm/tests/test_expression_boolean.py @@ -62,6 +62,16 @@ def test_variable_hash(self): self.assertEqual(hash(Variable('xyz')), hash(Variable('xyz'))) +class TestTerm(unittest.TestCase): + def test_length_of_and(self): + term = And(Variable('b1'), Variable('b2')) + self.assertEqual(len(term), 2) + + def test_length_of_or(self): + term = Or(Variable('b1'), And(Variable('b2'), Variable('b3'))) + self.assertEqual(len(term), 2) + + class TestExpression(unittest.TestCase): def test_expression_substitute_existing(self): e = Expression('b1')