summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsmiddlek <smiddlek@b1010a0a-674b-0410-b734-77272b80c875>2010-07-07 17:48:13 +0000
committersmiddlek <smiddlek@b1010a0a-674b-0410-b734-77272b80c875>2010-07-07 17:48:13 +0000
commit0351c33996da073395f5af405f3cb36d800839ac (patch)
treee4077f9a36719544baa61db054299eaf8328b59f
parentf8770adb29be14845b407494cc0e3e1b98ce75e3 (diff)
downloadmox-0351c33996da073395f5af405f3cb36d800839ac.tar.gz
Add an Is() comparator for checking identity, instead of equality.
By: vmalloc git-svn-id: http://pymox.googlecode.com/svn/trunk@53 b1010a0a-674b-0410-b734-77272b80c875
-rwxr-xr-xmox.py7
-rwxr-xr-xmox_test.py46
2 files changed, 53 insertions, 0 deletions
diff --git a/mox.py b/mox.py
index 948973a..c8ef6a6 100755
--- a/mox.py
+++ b/mox.py
@@ -1237,6 +1237,13 @@ class Comparator:
def __ne__(self, rhs):
return not self.equals(rhs)
+class Is(Comparator):
+ def __init__(self, obj):
+ self._obj = obj
+ def equals(self, rhs):
+ return rhs is self._obj
+ def __repr__(self):
+ return "<is %r (%s)>" % (self._obj, id(self._obj))
class IsA(Comparator):
"""This class wraps a basic Python type or class. It is used to verify
diff --git a/mox_test.py b/mox_test.py
index 005455a..2166107 100755
--- a/mox_test.py
+++ b/mox_test.py
@@ -259,6 +259,52 @@ class RegexTest(unittest.TestCase):
"<regular expression 'a\s+b', flags=4>")
+class IsTest(unittest.TestCase):
+ """Verify Is correctly checks equality based upon identity, not value"""
+
+ class AlwaysComparesTrue(object):
+ def __eq__(self, other):
+ return True
+ def __cmp__(self, other):
+ return 0
+ def __ne__(self, other):
+ return False
+
+ def testEqualityValid(self):
+ o1 = self.AlwaysComparesTrue()
+ self.assertTrue(mox.Is(o1), o1)
+
+ def testEqualityInvalid(self):
+ o1 = self.AlwaysComparesTrue()
+ o2 = self.AlwaysComparesTrue()
+ self.assertTrue(o1 == o2)
+ # but...
+ self.assertFalse(mox.Is(o1) == o2)
+
+ def testInequalityValid(self):
+ o1 = self.AlwaysComparesTrue()
+ o2 = self.AlwaysComparesTrue()
+ self.assertTrue(mox.Is(o1) != o2)
+
+ def testInequalityInvalid(self):
+ o1 = self.AlwaysComparesTrue()
+ self.assertFalse(mox.Is(o1) != o1)
+
+ def testEqualityInListValid(self):
+ o1 = self.AlwaysComparesTrue()
+ o2 = self.AlwaysComparesTrue()
+ isa_list = [mox.Is(o1), mox.Is(o2)]
+ str_list = [o1, o2]
+ self.assertTrue(isa_list == str_list)
+
+ def testEquailtyInListInvalid(self):
+ o1 = self.AlwaysComparesTrue()
+ o2 = self.AlwaysComparesTrue()
+ isa_list = [mox.Is(o1), mox.Is(o2)]
+ mixed_list = [o2, o1]
+ self.assertFalse(isa_list == mixed_list)
+
+
class IsATest(unittest.TestCase):
"""Verify IsA correctly checks equality based upon class type, not value."""