summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsmiddlek <smiddlek@b1010a0a-674b-0410-b734-77272b80c875>2008-07-01 20:47:46 +0000
committersmiddlek <smiddlek@b1010a0a-674b-0410-b734-77272b80c875>2008-07-01 20:47:46 +0000
commit807e7567d447997b0887164f36571b104af03572 (patch)
tree8953500bb91571ccce00698c7a1ae3ab1dad4ec1
parent5a84531d0e4f81fa4af078140a97b82aeca7c4c5 (diff)
downloadpymox-807e7567d447997b0887164f36571b104af03572.tar.gz
Add a Not comparator to compose with other comparators.
Patch by: Benoit Sigoure <benoits@google.com>
-rwxr-xr-xmox.py34
-rwxr-xr-xmox_test.py32
2 files changed, 66 insertions, 0 deletions
diff --git a/mox.py b/mox.py
index 70fba4f..0dd4cc2 100755
--- a/mox.py
+++ b/mox.py
@@ -965,6 +965,40 @@ class In(Comparator):
return '<sequence or map containing \'%s\'>' % self._key
+class Not(Comparator):
+ """Checks whether a predicates is False.
+
+ Example:
+ mock_dao.UpdateUsers(Not(ContainsKeyValue('stevepm', stevepm_user_info)))
+ """
+
+ def __init__(self, predicate):
+ """Initialize.
+
+ Args:
+ # predicate: a Comparator instance.
+ """
+
+ assert isinstance(predicate, Comparator), ("predicate %r must be a"
+ " Comparator." % predicate)
+ self._predicate = predicate
+
+ def equals(self, rhs):
+ """Check to see whether the predicate is False.
+
+ Args:
+ rhs: A value that will be given in argument of the predicate.
+
+ Returns:
+ bool
+ """
+
+ return not self._predicate.equals(rhs)
+
+ def __repr__(self):
+ return '<not \'%s\'>' % self._predicate
+
+
class ContainsKeyValue(Comparator):
"""Checks whether a key/value pair is in a dict parameter.
diff --git a/mox_test.py b/mox_test.py
index be35bae..5f00f0c 100755
--- a/mox_test.py
+++ b/mox_test.py
@@ -159,6 +159,38 @@ class InTest(unittest.TestCase):
self.assert_(mox.In("test") == {"test" : "module"})
+class NotTest(unittest.TestCase):
+ """Test Not correctly identifies False predicates."""
+
+ def testItemInList(self):
+ """Should return True if the item is NOT in the list."""
+ self.assert_(mox.Not(mox.In(42)) == [1, 2, 3])
+
+ def testKeyInDict(self):
+ """Should return True if the item is NOT a key in a dict."""
+ self.assert_(mox.Not(mox.In("foo")) == {"key" : 42})
+
+ def testInvalidKeyWithNot(self):
+ """Should return False if they key is NOT in the dict."""
+ self.assert_(mox.Not(mox.ContainsKeyValue("qux", 1)) == {"key": 2})
+
+
+class NotTest(unittest.TestCase):
+ """Test Not correctly identifies False predicates."""
+
+ def testItemInList(self):
+ """Should return True if the item is NOT in the list."""
+ self.assert_(mox.Not(mox.In(42)) == [1, 2, 3])
+
+ def testKeyInDict(self):
+ """Should return True if the item is NOT a key in a dict."""
+ self.assert_(mox.Not(mox.In("foo")) == {"key" : 42})
+
+ def testInvalidKeyWithNot(self):
+ """Should return False if they key is NOT in the dict."""
+ self.assert_(mox.Not(mox.ContainsKeyValue("qux", 1)) == {"key": 2})
+
+
class StrContainsTest(unittest.TestCase):
"""Test StrContains correctly checks for substring occurrence of a parameter.
"""