From 0351c33996da073395f5af405f3cb36d800839ac Mon Sep 17 00:00:00 2001 From: smiddlek Date: Wed, 7 Jul 2010 17:48:13 +0000 Subject: 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 --- mox.py | 7 +++++++ mox_test.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) 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 "" % (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): "") +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.""" -- cgit v1.2.1