diff options
| author | Serhiy Storchaka <storchaka@gmail.com> | 2017-04-28 19:17:26 +0300 |
|---|---|---|
| committer | Victor Stinner <victor.stinner@gmail.com> | 2017-04-28 18:17:26 +0200 |
| commit | d1a1def7bf221b04dcf3fc3a67aa19aa2f622f83 (patch) | |
| tree | 0d13f2482de3cad5c65d9507d1bb4a27234578a6 /Lib/test/test_support.py | |
| parent | 80a3da4d4aad0b51893e1e2f696b6252eca80e07 (diff) | |
| download | cpython-git-d1a1def7bf221b04dcf3fc3a67aa19aa2f622f83.tar.gz | |
bpo-30197: Enhance functions swap_attr() and swap_item() in test.support. (#1341)
* bpo-30197: Enhance functions swap_attr() and swap_item() in test.support.
They now work when delete replaced attribute or item inside the with
statement. The old value of the attribute or item (or None if it doesn't
exist) now will be assigned to the target of the "as" clause, if there is
one.
* Update docstrings.
Diffstat (limited to 'Lib/test/test_support.py')
| -rw-r--r-- | Lib/test/test_support.py | 29 |
1 files changed, 23 insertions, 6 deletions
diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py index 0dbe02eeb3..1e6b2b54e2 100644 --- a/Lib/test/test_support.py +++ b/Lib/test/test_support.py @@ -295,17 +295,34 @@ class TestSupport(unittest.TestCase): def test_swap_attr(self): class Obj: - x = 1 + pass obj = Obj() - with support.swap_attr(obj, "x", 5): + obj.x = 1 + with support.swap_attr(obj, "x", 5) as x: self.assertEqual(obj.x, 5) + self.assertEqual(x, 1) self.assertEqual(obj.x, 1) + with support.swap_attr(obj, "y", 5) as y: + self.assertEqual(obj.y, 5) + self.assertIsNone(y) + self.assertFalse(hasattr(obj, 'y')) + with support.swap_attr(obj, "y", 5): + del obj.y + self.assertFalse(hasattr(obj, 'y')) def test_swap_item(self): - D = {"item":1} - with support.swap_item(D, "item", 5): - self.assertEqual(D["item"], 5) - self.assertEqual(D["item"], 1) + D = {"x":1} + with support.swap_item(D, "x", 5) as x: + self.assertEqual(D["x"], 5) + self.assertEqual(x, 1) + self.assertEqual(D["x"], 1) + with support.swap_item(D, "y", 5) as y: + self.assertEqual(D["y"], 5) + self.assertIsNone(y) + self.assertNotIn("y", D) + with support.swap_item(D, "y", 5): + del D["y"] + self.assertNotIn("y", D) class RefClass: attribute1 = None |
