MarkBerlin78 / pymox

Automatically exported from code.google.com/p/pymox
Apache License 2.0
0 stars 0 forks source link

Add Is() comparator to mox #23

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Would be nice when testing objects that have __eq__/__cmp__ logic that might 
create false positives or true negatives.

Suggested patch included:
-----------8< ------- 8< ---------

diff --git a/mox.py b/mox.py
index ba73bd3..dd66f3b 100755
--- a/mox.py
+++ b/mox.py
@@ -1220,6 +1220,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 2e429e6..f1c4b56 100755
--- a/mox_test.py
+++ b/mox_test.py
@@ -259,6 +259,51 @@ 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."""

Original issue reported on code.google.com by vmal...@gmail.com on 23 Jun 2010 at 11:50

GoogleCodeExporter commented 9 years ago
Oops, I categorized this as a Defect by mistake... I meant for it to be an 
enhancement request. Sorry.

Original comment by vmal...@gmail.com on 23 Jun 2010 at 11:51

GoogleCodeExporter commented 9 years ago
I actually just ran into this problem the other day when writing a test.  It 
was annoying.  I'll try and look at this patch soon.

Original comment by steve.mi...@gmail.com on 23 Jun 2010 at 7:07

GoogleCodeExporter commented 9 years ago

Original comment by steve.mi...@gmail.com on 7 Jul 2010 at 5:53