Rev 4767: Set up a test suite for hash() and richcompare against lots of acceptable types. in http://bazaar.launchpad.net/~jameinel/bzr/2.1-st-concat

John Arbash Meinel john at arbash-meinel.com
Wed Oct 21 05:48:07 BST 2009


At http://bazaar.launchpad.net/~jameinel/bzr/2.1-st-concat

------------------------------------------------------------
revno: 4767
revision-id: john at arbash-meinel.com-20091021044749-gaadnlcy92ybn0ke
parent: john at arbash-meinel.com-20091021043257-70q54stywsrkwh8g
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: 2.1-st-concat
timestamp: Tue 2009-10-20 23:47:49 -0500
message:
  Set up a test suite for hash() and richcompare against lots of acceptable types.
-------------- next part --------------
=== modified file 'bzrlib/_static_tuple_py.py'
--- a/bzrlib/_static_tuple_py.py	2009-10-21 03:49:03 +0000
+++ b/bzrlib/_static_tuple_py.py	2009-10-21 04:47:49 +0000
@@ -33,8 +33,11 @@
     def __init__(self, *args):
         """Create a new 'StaticTuple'"""
         for bit in args:
-            if type(bit) not in (str, StaticTuple):
-                raise TypeError('key bits must be strings or StaticTuple')
+            if type(bit) not in (str, StaticTuple, unicode, int, float,
+                                 None.__class__, bool):
+                raise TypeError('StaticTuple can only point to'
+                    ' StaticTuple, str, unicode, int, float, bool, or None'
+                    ' not %s' % (type(bit),))
         num_keys = len(args)
         if num_keys < 0 or num_keys > 255:
             raise ValueError('must have 1 => 256 key bits')

=== modified file 'bzrlib/tests/test__static_tuple.py'
--- a/bzrlib/tests/test__static_tuple.py	2009-10-21 04:14:54 +0000
+++ b/bzrlib/tests/test__static_tuple.py	2009-10-21 04:47:49 +0000
@@ -104,7 +104,7 @@
         args_300 = ['a']*300
         self.assertRaises(ValueError, self.module.StaticTuple, *args_300)
         # not a string
-        self.assertRaises(TypeError, self.module.StaticTuple, 10)
+        self.assertRaises(TypeError, self.module.StaticTuple, object())
 
     def test_concat(self):
         st1 = self.module.StaticTuple('foo')
@@ -222,11 +222,29 @@
         self.assertFalse(k1 < k2)
         self.assertFalse(k1 > k2)
 
+    def test_holds_None(self):
+        k1 = self.module.StaticTuple(None)
+
+    def test_holds_int(self):
+        k1 = self.module.StaticTuple(1)
+
+    def test_holds_float(self):
+        k1 = self.module.StaticTuple(1.2)
+
+    def test_holds_unicode(self):
+        k1 = self.module.StaticTuple(u'\xb5')
+
+    def test_hold_bool(self):
+        k1 = self.module.StaticTuple(True)
+        k2 = self.module.StaticTuple(False)
+
     def test_compare_same_obj(self):
         k1 = self.module.StaticTuple('foo', 'bar')
         self.assertCompareEqual(k1, k1)
         k2 = self.module.StaticTuple(k1, k1)
         self.assertCompareEqual(k2, k2)
+        k3 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, True, k1)
+        self.assertCompareEqual(k3, k3)
 
     def test_compare_equivalent_obj(self):
         k1 = self.module.StaticTuple('foo', 'bar')
@@ -235,6 +253,12 @@
         k3 = self.module.StaticTuple(k1, k2)
         k4 = self.module.StaticTuple(k2, k1)
         self.assertCompareEqual(k1, k2)
+        k5 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, True, k1)
+        k6 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, True, k1)
+        self.assertCompareEqual(k5, k6)
+        k7 = self.module.StaticTuple(None)
+        k8 = self.module.StaticTuple(None)
+        self.assertCompareEqual(k7, k8)
 
     def test_compare_similar_obj(self):
         k1 = self.module.StaticTuple('foo' + ' bar', 'bar' + ' baz')
@@ -285,6 +309,15 @@
         k3 = self.module.StaticTuple(k1, k2)
         k4 = self.module.StaticTuple(k2, k1)
         self.assertCompareDifferent(k3, k4)
+        k5 = self.module.StaticTuple(1)
+        k6 = self.module.StaticTuple(2)
+        self.assertCompareDifferent(k5, k6)
+        k7 = self.module.StaticTuple(1.2)
+        k8 = self.module.StaticTuple(2.4)
+        self.assertCompareDifferent(k7, k8)
+        k9 = self.module.StaticTuple(u's\xb5')
+        k10 = self.module.StaticTuple(u's\xe5')
+        self.assertCompareDifferent(k9, k10)
 
     def test_compare_some_different(self):
         k1 = self.module.StaticTuple('foo', 'bar')
@@ -293,6 +326,9 @@
         k3 = self.module.StaticTuple(k1, k1)
         k4 = self.module.StaticTuple(k1, k2)
         self.assertCompareDifferent(k3, k4)
+        k5 = self.module.StaticTuple('foo', None)
+        self.assertCompareDifferent(k5, k1)
+        self.assertCompareDifferent(k5, k2)
 
     def test_compare_diff_width(self):
         k1 = self.module.StaticTuple('foo')
@@ -302,6 +338,17 @@
         k4 = self.module.StaticTuple(k1, k2)
         self.assertCompareDifferent(k3, k4)
 
+    def test_compare_different_types(self):
+        k1 = self.module.StaticTuple('foo', 'bar')
+        k2 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, True, k1)
+        self.assertCompareNoRelation(k1, k2)
+        k3 = self.module.StaticTuple('foo')
+        self.assertCompareDifferent(k3, k1)
+        k4 = self.module.StaticTuple(None)
+        self.assertCompareDifferent(k4, k1)
+        k5 = self.module.StaticTuple(1)
+        self.assertCompareNoRelation(k1, k5)
+
     def test_compare_to_tuples(self):
         k1 = self.module.StaticTuple('foo')
         self.assertCompareEqual(k1, ('foo',))
@@ -351,6 +398,10 @@
         as_tuple2 = (('foo', 'bar', 'baz', 'bing'),)
         self.assertEqual(hash(k2), hash(as_tuple2))
 
+        k3 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, True, k)
+        as_tuple3 = ('foo', 1, None, u'\xb5', 1.2, True, k)
+        self.assertEqual(hash(as_tuple3), hash(k3))
+
     def test_slice(self):
         k = self.module.StaticTuple('foo', 'bar', 'baz', 'bing')
         self.assertEqual(('foo', 'bar'), k[:2])



More information about the bazaar-commits mailing list