Rev 4145: New assertLength method based on one Martin has squirreled away somewhere. in http://people.ubuntu.com/~robertc/baz2.0/pending/assertLength

Robert Collins robertc at robertcollins.net
Mon Mar 16 01:56:24 GMT 2009


At http://people.ubuntu.com/~robertc/baz2.0/pending/assertLength

------------------------------------------------------------
revno: 4145
revision-id: robertc at robertcollins.net-20090316015618-rnbq60s0w5rr8ta8
parent: pqm at pqm.ubuntu.com-20090313062142-ndr3o27uwgysx9dv
committer: Robert Collins <robertc at robertcollins.net>
branch nick: assertLength
timestamp: Mon 2009-03-16 12:56:18 +1100
message:
  New assertLength method based on one Martin has squirreled away somewhere.
=== modified file 'NEWS'
--- a/NEWS	2009-03-13 05:05:50 +0000
+++ b/NEWS	2009-03-16 01:56:18 +0000
@@ -69,6 +69,9 @@
 
   INTERNALS:
 
+    * New ``assertLength`` method based on one Martin has squirreled away
+      somewhere. (Robert Collins, Martin Pool)
+
 
 bzr 1.13rc1 "paraskavedekatriaphobia" 2009-03-10
 ------------------------------------------------

=== modified file 'bzrlib/tests/__init__.py'
--- a/bzrlib/tests/__init__.py	2009-03-12 12:00:49 +0000
+++ b/bzrlib/tests/__init__.py	2009-03-16 01:56:18 +0000
@@ -902,6 +902,12 @@
         self.assertEqual(expected.st_ino, actual.st_ino)
         self.assertEqual(expected.st_mode, actual.st_mode)
 
+    def assertLength(self, length, obj_with_len):
+        """Assert that obj_with_len is of length length."""
+        if len(obj_with_len) != length:
+            self.fail("Incorrect length: wanted %d, got %d for %r" % (
+                length, len(obj_with_len), obj_with_len))
+
     def assertPositive(self, val):
         """Assert that val is greater than 0."""
         self.assertTrue(val > 0, 'expected a positive value, but got %s' % val)

=== modified file 'bzrlib/tests/test_selftest.py'
--- a/bzrlib/tests/test_selftest.py	2009-03-07 06:58:17 +0000
+++ b/bzrlib/tests/test_selftest.py	2009-03-16 01:56:18 +0000
@@ -1352,6 +1352,25 @@
 class TestTestCase(TestCase):
     """Tests that test the core bzrlib TestCase."""
 
+    def test_assertLength_matches_empty(self):
+        a_list = []
+        self.assertLength(0, a_list)
+
+    def test_assertLength_matches_nonempty(self):
+        a_list = [1, 2, 3]
+        self.assertLength(3, a_list)
+
+    def test_assertLength_fails_different(self):
+        a_list = []
+        self.assertRaises(AssertionError, self.assertLength, 1, a_list)
+
+    def test_assertLength_shows_sequence_in_failure(self):
+        a_list = [1, 2, 3]
+        exception = self.assertRaises(AssertionError, self.assertLength, 2,
+            a_list)
+        self.assertEqual('Incorrect length: wanted 2, got 3 for [1, 2, 3]',
+            exception.args[0])
+
     def test_debug_flags_sanitised(self):
         """The bzrlib debug flags should be sanitised by setUp."""
         if 'allow_debug' in tests.selftest_debug_flags:




More information about the bazaar-commits mailing list