diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2013-11-17 11:20:09 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2013-11-17 11:20:09 (GMT) |
commit | 2e29c9e1c274aa79d6c55104c1560c7fc66df384 (patch) | |
tree | 6b775bfea89c4134c8b99a9314222ab3b6960ccd | |
parent | 3df4dcceb9f1706e6fe1a63f0833a4c1e326d574 (diff) | |
download | cpython-2e29c9e1c274aa79d6c55104c1560c7fc66df384.zip cpython-2e29c9e1c274aa79d6c55104c1560c7fc66df384.tar.gz cpython-2e29c9e1c274aa79d6c55104c1560c7fc66df384.tar.bz2 |
Issue #19607: Use specific asserts in weakref tests.
-rw-r--r-- | Lib/test/test_weakref.py | 127 |
1 files changed, 62 insertions, 65 deletions
diff --git a/Lib/test/test_weakref.py b/Lib/test/test_weakref.py index 571e33f..85ab717 100644 --- a/Lib/test/test_weakref.py +++ b/Lib/test/test_weakref.py @@ -88,11 +88,9 @@ class ReferencesTestCase(TestBase): ref1 = weakref.ref(o, self.callback) ref2 = weakref.ref(o, self.callback) del o - self.assertTrue(ref1() is None, - "expected reference to be invalidated") - self.assertTrue(ref2() is None, - "expected reference to be invalidated") - self.assertTrue(self.cbcalled == 2, + self.assertIsNone(ref1(), "expected reference to be invalidated") + self.assertIsNone(ref2(), "expected reference to be invalidated") + self.assertEqual(self.cbcalled, 2, "callback not called the right number of times") def test_multiple_selfref_callbacks(self): @@ -131,10 +129,10 @@ class ReferencesTestCase(TestBase): def check_basic_ref(self, factory): o = factory() ref = weakref.ref(o) - self.assertTrue(ref() is not None, + self.assertIsNotNone(ref(), "weak reference to live object should be live") o2 = ref() - self.assertTrue(o is o2, + self.assertIs(o, o2, "<ref>() should return original object if live") def check_basic_callback(self, factory): @@ -142,9 +140,9 @@ class ReferencesTestCase(TestBase): o = factory() ref = weakref.ref(o, self.callback) del o - self.assertTrue(self.cbcalled == 1, + self.assertEqual(self.cbcalled, 1, "callback did not properly set 'cbcalled'") - self.assertTrue(ref() is None, + self.assertIsNone(ref(), "ref2 should be dead after deleting object reference") def test_ref_reuse(self): @@ -154,19 +152,19 @@ class ReferencesTestCase(TestBase): # between these two; it should make no difference proxy = weakref.proxy(o) ref2 = weakref.ref(o) - self.assertTrue(ref1 is ref2, + self.assertIs(ref1, ref2, "reference object w/out callback should be re-used") o = C() proxy = weakref.proxy(o) ref1 = weakref.ref(o) ref2 = weakref.ref(o) - self.assertTrue(ref1 is ref2, + self.assertIs(ref1, ref2, "reference object w/out callback should be re-used") - self.assertTrue(weakref.getweakrefcount(o) == 2, + self.assertEqual(weakref.getweakrefcount(o), 2, "wrong weak ref count for object") del proxy - self.assertTrue(weakref.getweakrefcount(o) == 1, + self.assertEqual(weakref.getweakrefcount(o), 1, "wrong weak ref count for object after deleting proxy") def test_proxy_reuse(self): @@ -174,7 +172,7 @@ class ReferencesTestCase(TestBase): proxy1 = weakref.proxy(o) ref = weakref.ref(o) proxy2 = weakref.proxy(o) - self.assertTrue(proxy1 is proxy2, + self.assertIs(proxy1, proxy2, "proxy object w/out callback should have been re-used") def test_basic_proxy(self): @@ -254,19 +252,19 @@ class ReferencesTestCase(TestBase): o = Object(1) p1 = makeref(o, None) p2 = makeref(o, None) - self.assertTrue(p1 is p2, "both callbacks were None in the C API") + self.assertIs(p1, p2, "both callbacks were None in the C API") del p1, p2 p1 = makeref(o) p2 = makeref(o, None) - self.assertTrue(p1 is p2, "callbacks were NULL, None in the C API") + self.assertIs(p1, p2, "callbacks were NULL, None in the C API") del p1, p2 p1 = makeref(o) p2 = makeref(o) - self.assertTrue(p1 is p2, "both callbacks were NULL in the C API") + self.assertIs(p1, p2, "both callbacks were NULL in the C API") del p1, p2 p1 = makeref(o, None) p2 = makeref(o) - self.assertTrue(p1 is p2, "callbacks were None, NULL in the C API") + self.assertIs(p1, p2, "callbacks were None, NULL in the C API") def test_callable_proxy(self): o = Callable() @@ -274,13 +272,13 @@ class ReferencesTestCase(TestBase): self.check_proxy(o, ref1) - self.assertTrue(type(ref1) is weakref.CallableProxyType, + self.assertIs(type(ref1), weakref.CallableProxyType, "proxy is not of callable type") ref1('twinkies!') - self.assertTrue(o.bar == 'twinkies!', + self.assertEqual(o.bar, 'twinkies!', "call through proxy not passed through to original") ref1(x='Splat.') - self.assertTrue(o.bar == 'Splat.', + self.assertEqual(o.bar, 'Splat.', "call through proxy not passed through to original") # expect due to too few args @@ -291,24 +289,23 @@ class ReferencesTestCase(TestBase): def check_proxy(self, o, proxy): o.foo = 1 - self.assertTrue(proxy.foo == 1, + self.assertEqual(proxy.foo, 1, "proxy does not reflect attribute addition") o.foo = 2 - self.assertTrue(proxy.foo == 2, + self.assertEqual(proxy.foo, 2, "proxy does not reflect attribute modification") del o.foo - self.assertTrue(not hasattr(proxy, 'foo'), + self.assertFalse(hasattr(proxy, 'foo'), "proxy does not reflect attribute removal") proxy.foo = 1 - self.assertTrue(o.foo == 1, + self.assertEqual(o.foo, 1, "object does not reflect attribute addition via proxy") proxy.foo = 2 - self.assertTrue( - o.foo == 2, + self.assertEqual(o.foo, 2, "object does not reflect attribute modification via proxy") del proxy.foo - self.assertTrue(not hasattr(o, 'foo'), + self.assertFalse(hasattr(o, 'foo'), "object does not reflect attribute removal via proxy") def test_proxy_deletion(self): @@ -332,21 +329,21 @@ class ReferencesTestCase(TestBase): o = C() ref1 = weakref.ref(o) ref2 = weakref.ref(o, self.callback) - self.assertTrue(weakref.getweakrefcount(o) == 2, + self.assertEqual(weakref.getweakrefcount(o), 2, "got wrong number of weak reference objects") proxy1 = weakref.proxy(o) proxy2 = weakref.proxy(o, self.callback) - self.assertTrue(weakref.getweakrefcount(o) == 4, + self.assertEqual(weakref.getweakrefcount(o), 4, "got wrong number of weak reference objects") del ref1, ref2, proxy1, proxy2 - self.assertTrue(weakref.getweakrefcount(o) == 0, + self.assertEqual(weakref.getweakrefcount(o), 0, "weak reference objects not unlinked from" " referent when discarded.") # assumes ints do not support weakrefs - self.assertTrue(weakref.getweakrefcount(1) == 0, + self.assertEqual(weakref.getweakrefcount(1), 0, "got wrong number of weak reference objects for int") def test_getweakrefs(self): @@ -354,22 +351,22 @@ class ReferencesTestCase(TestBase): ref1 = weakref.ref(o, self.callback) ref2 = weakref.ref(o, self.callback) del ref1 - self.assertTrue(weakref.getweakrefs(o) == [ref2], + self.assertEqual(weakref.getweakrefs(o), [ref2], "list of refs does not match") o = C() ref1 = weakref.ref(o, self.callback) ref2 = weakref.ref(o, self.callback) del ref2 - self.assertTrue(weakref.getweakrefs(o) == [ref1], + self.assertEqual(weakref.getweakrefs(o), [ref1], "list of refs does not match") del ref1 - self.assertTrue(weakref.getweakrefs(o) == [], + self.assertEqual(weakref.getweakrefs(o), [], "list of refs not cleared") # assumes ints do not support weakrefs - self.assertTrue(weakref.getweakrefs(1) == [], + self.assertEqual(weakref.getweakrefs(1), [], "list of refs does not match for int") def test_newstyle_number_ops(self): @@ -377,8 +374,8 @@ class ReferencesTestCase(TestBase): pass f = F(2.0) p = weakref.proxy(f) - self.assertTrue(p + 1.0 == 3.0) - self.assertTrue(1.0 + p == 3.0) # this used to SEGV + self.assertEqual(p + 1.0, 3.0) + self.assertEqual(1.0 + p, 3.0) # this used to SEGV def test_callbacks_protected(self): # Callbacks protected from already-set exceptions? @@ -631,7 +628,7 @@ class ReferencesTestCase(TestBase): c.wr = weakref.ref(d, callback) # this won't trigger d.wr = weakref.ref(callback, d.cb) # ditto external_wr = weakref.ref(callback, safe_callback) # but this will - self.assertTrue(external_wr() is callback) + self.assertIs(external_wr(), callback) # The weakrefs attached to c and d should get cleared, so that # C.cb is never called. But external_wr isn't part of the cyclic @@ -810,11 +807,11 @@ class SubclassableWeakrefTestCase(TestBase): return super().__call__() o = Object("foo") mr = MyRef(o, value=24) - self.assertTrue(mr() is o) + self.assertIs(mr(), o) self.assertTrue(mr.called) self.assertEqual(mr.value, 24) del o - self.assertTrue(mr() is None) + self.assertIsNone(mr()) self.assertTrue(mr.called) def test_subclass_refs_dont_replace_standard_refs(self): @@ -823,14 +820,14 @@ class SubclassableWeakrefTestCase(TestBase): o = Object(42) r1 = MyRef(o) r2 = weakref.ref(o) - self.assertTrue(r1 is not r2) + self.assertIsNot(r1, r2) self.assertEqual(weakref.getweakrefs(o), [r2, r1]) self.assertEqual(weakref.getweakrefcount(o), 2) r3 = MyRef(o) self.assertEqual(weakref.getweakrefcount(o), 3) refs = weakref.getweakrefs(o) self.assertEqual(len(refs), 3) - self.assertTrue(r2 is refs[0]) + self.assertIs(r2, refs[0]) self.assertIn(r1, refs[1:]) self.assertIn(r3, refs[1:]) @@ -840,7 +837,7 @@ class SubclassableWeakrefTestCase(TestBase): o = Object(42) r1 = MyRef(o, id) r2 = MyRef(o, str) - self.assertTrue(r1 is not r2) + self.assertIsNot(r1, r2) refs = weakref.getweakrefs(o) self.assertIn(r1, refs) self.assertIn(r2, refs) @@ -968,7 +965,7 @@ class MappingTestCase(TestBase): dict, objects = self.make_weak_valued_dict() for o in objects: self.assertEqual(weakref.getweakrefcount(o), 1) - self.assertTrue(o is dict[o.arg], + self.assertIs(o, dict[o.arg], "wrong object returned by weak dict!") items1 = list(dict.items()) items2 = list(dict.copy().items()) @@ -997,9 +994,9 @@ class MappingTestCase(TestBase): # dict, objects = self.make_weak_keyed_dict() for o in objects: - self.assertTrue(weakref.getweakrefcount(o) == 1, + self.assertEqual(weakref.getweakrefcount(o), 1, "wrong number of weak references to %r!" % o) - self.assertTrue(o.arg is dict[o], + self.assertIs(o.arg, dict[o], "wrong object returned by weak dict!") items1 = dict.items() items2 = dict.copy().items() @@ -1008,10 +1005,10 @@ class MappingTestCase(TestBase): del items1, items2 self.assertEqual(len(dict), self.COUNT) del objects[0] - self.assertTrue(len(dict) == (self.COUNT - 1), + self.assertEqual(len(dict), (self.COUNT - 1), "deleting object did not cause dictionary update") del objects, o - self.assertTrue(len(dict) == 0, + self.assertEqual(len(dict), 0, "deleting the keys did not clear the dictionary") o = Object(42) dict[o] = "What is the meaning of the universe?" @@ -1221,15 +1218,15 @@ class MappingTestCase(TestBase): k, v = weakdict.popitem() self.assertEqual(len(weakdict), 1) if k is key1: - self.assertTrue(v is value1) + self.assertIs(v, value1) else: - self.assertTrue(v is value2) + self.assertIs(v, value2) k, v = weakdict.popitem() self.assertEqual(len(weakdict), 0) if k is key1: - self.assertTrue(v is value1) + self.assertIs(v, value1) else: - self.assertTrue(v is value2) + self.assertIs(v, value2) def test_weak_valued_dict_popitem(self): self.check_popitem(weakref.WeakValueDictionary, @@ -1240,21 +1237,21 @@ class MappingTestCase(TestBase): C(), "value 1", C(), "value 2") def check_setdefault(self, klass, key, value1, value2): - self.assertTrue(value1 is not value2, + self.assertIsNot(value1, value2, "invalid test" " -- value parameters must be distinct objects") weakdict = klass() o = weakdict.setdefault(key, value1) - self.assertTrue(o is value1) + self.assertIs(o, value1) self.assertIn(key, weakdict) - self.assertTrue(weakdict.get(key) is value1) - self.assertTrue(weakdict[key] is value1) + self.assertIs(weakdict.get(key), value1) + self.assertIs(weakdict[key], value1) o = weakdict.setdefault(key, value2) - self.assertTrue(o is value1) + self.assertIs(o, value1) self.assertIn(key, weakdict) - self.assertTrue(weakdict.get(key) is value1) - self.assertTrue(weakdict[key] is value1) + self.assertIs(weakdict.get(key), value1) + self.assertIs(weakdict[key], value1) def test_weak_valued_dict_setdefault(self): self.check_setdefault(weakref.WeakValueDictionary, @@ -1275,13 +1272,13 @@ class MappingTestCase(TestBase): for k in weakdict.keys(): self.assertIn(k, dict, "mysterious new key appeared in weak dict") v = dict.get(k) - self.assertTrue(v is weakdict[k]) - self.assertTrue(v is weakdict.get(k)) + self.assertIs(v, weakdict[k]) + self.assertIs(v, weakdict.get(k)) for k in dict.keys(): self.assertIn(k, weakdict, "original key disappeared in weak dict") v = dict[k] - self.assertTrue(v is weakdict[k]) - self.assertTrue(v is weakdict.get(k)) + self.assertIs(v, weakdict[k]) + self.assertIs(v, weakdict.get(k)) def test_weak_valued_dict_update(self): self.check_update(weakref.WeakValueDictionary, @@ -1311,7 +1308,7 @@ class MappingTestCase(TestBase): self.assertEqual(len(d), 2) del d['something'] self.assertEqual(len(d), 1) - self.assertTrue(list(d.items()) == [('something else', o2)]) + self.assertEqual(list(d.items()), [('something else', o2)]) def test_weak_keyed_bad_delitem(self): d = weakref.WeakKeyDictionary() |