From 981e2eb43edcec55b62ac38fc58b01f9e9dfdc80 Mon Sep 17 00:00:00 2001 From: smiddlek Date: Tue, 1 Jul 2008 20:47:46 +0000 Subject: Add a Not comparator to compose with other comparators. Patch by: Benoit Sigoure git-svn-id: http://pymox.googlecode.com/svn/trunk@18 b1010a0a-674b-0410-b734-77272b80c875 --- mox.py | 34 ++++++++++++++++++++++++++++++++++ mox_test.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) 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 '' % 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 '' % 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. """ -- cgit v1.2.1