Rev 4988: Change it to a more usable form. in file:///home/vila/src/bzr/bugs/add-attr-cleanup/

Vincent Ladeuil v.ladeuil+lp at free.fr
Mon Jan 25 15:37:03 GMT 2010


At file:///home/vila/src/bzr/bugs/add-attr-cleanup/

------------------------------------------------------------
revno: 4988
revision-id: v.ladeuil+lp at free.fr-20100125153703-cbllzww8bdp3xqxo
parent: v.ladeuil+lp at free.fr-20100123174313-a8mhs9t1jxy3i7p7
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: add-attr-cleanup
timestamp: Mon 2010-01-25 16:37:03 +0100
message:
  Change it to a more usable form.
  
  * bzrlib/tests/__init__.py:
  (TestCase.overrideAttr): Renamed from addAttrCleanup.
-------------- next part --------------
=== modified file 'bzrlib/tests/__init__.py'
--- a/bzrlib/tests/__init__.py	2010-01-23 10:27:37 +0000
+++ b/bzrlib/tests/__init__.py	2010-01-25 15:37:03 +0000
@@ -126,6 +126,11 @@
 
 default_transport = LocalURLServer
 
+
+_unitialized_attr = object()
+"""A sentinel needed to act as a default value in a method signature."""
+
+
 # Subunit result codes, defined here to prevent a hard dependency on subunit.
 SUBUNIT_SEEK_SET = 0
 SUBUNIT_SEEK_CUR = 1
@@ -1479,7 +1484,7 @@
         """
         self._cleanups.append((callable, args, kwargs))
 
-    def addAttrCleanup(self, obj, attr_name):
+    def overrideAttr(test, obj, attr_name, new=_unitialized_attr):
         """Add a cleanup which restores the attribute to its original value.
 
         :returns: The actual attr value.
@@ -1487,6 +1492,8 @@
         value = getattr(obj, attr_name)
         # The actual value is captured by the call below
         self.addCleanup(setattr, obj, attr_name, value)
+        if new is not _unitialized_attr:
+            setattr(obj, attr_name, new)
         return value
 
     def _cleanEnvironment(self):

=== modified file 'bzrlib/tests/test_selftest.py'
--- a/bzrlib/tests/test_selftest.py	2010-01-23 17:43:13 +0000
+++ b/bzrlib/tests/test_selftest.py	2010-01-25 15:37:03 +0000
@@ -1634,15 +1634,33 @@
         self.assertRaises(AssertionError,
             self.assertListRaises, _TestException, success_generator)
 
-    def test_addAttrCleanup(self):
+    def test_overrideAttr_without_value(self):
         self.test_attr = 'original' # Define a test attribute
         obj = self # Make 'obj' visible to the embedded test
         class Test(tests.TestCase):
 
             def setUp(self):
                 tests.TestCase.setUp(self)
-                self.orig = self.addAttrCleanup(obj, 'test_attr')
+                self.orig = self.overrideAttr(obj, 'test_attr')
+
+            def test_value(self):
+                self.assertEqual('original', self.orig)
+                self.assertEqual('original', obj.test_attr)
                 obj.test_attr = 'modified'
+                self.assertEqual('modified', obj.test_attr)
+
+        test = Test('test_value')
+        test.run(unittest.TestResult())
+        self.assertEqual('original', obj.test_attr)
+
+    def test_overrideAttr_with_value(self):
+        self.test_attr = 'original' # Define a test attribute
+        obj = self # Make 'obj' visible to the embedded test
+        class Test(tests.TestCase):
+
+            def setUp(self):
+                tests.TestCase.setUp(self)
+                self.orig = self.overrideAttr(obj, 'test_attr', new='modified')
 
             def test_value(self):
                 self.assertEqual('original', self.orig)



More information about the bazaar-commits mailing list