test equality

This commit is contained in:
Mike Dirolf 2009-02-02 10:04:49 -05:00
parent 5c65676543
commit beb8e7def4
2 changed files with 18 additions and 3 deletions

View File

@ -45,12 +45,19 @@ class Binary(str):
raise TypeError("subtype must be an instance of int")
if subtype >= 256 or subtype < 0:
raise ValueError("subtype must be contained in [0, 256)")
binary = str.__new__(cls, data)
binary.__subtype = subtype
return binary
self = str.__new__(cls, data)
self.__subtype = subtype
return self
def subtype(self):
"""Get the subtype of this binary data.
"""
return self.__subtype
def __eq__(self, other):
if isinstance(other, Binary):
return (self.__subtype, str(self)) == (other.__subtype, str(other))
return NotImplemented
def __repr__(self):
return "Binary(%s, %s)" % (str.__repr__(self), self.__subtype)

View File

@ -50,6 +50,14 @@ class TestBinary(unittest.TestCase):
c = Binary("hello", 100)
self.assertEqual(c.subtype(), 100)
def test_equality(self):
b = Binary("hello")
c = Binary("hello", 100)
self.assertNotEqual(b, c)
self.assertEqual(c, Binary("hello", 100))
self.assertEqual(b, Binary("hello"))
self.assertNotEqual(b, Binary("hello "))
def test_repr(self):
b = Binary("hello world")
self.assertEqual(repr(b), "Binary('hello world', 2)")