MCPcopy Index your code
hub / github.com/python/cpython / test_specials

Method test_specials

Lib/test/test_descr.py:2294–2390  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

2292 self.assertRaises(MyException, runner, X())
2293
2294 def test_specials(self):
2295 # Testing special operators...
2296 # Test operators like __hash__ for which a built-in default exists
2297
2298 # Test the default behavior for static classes
2299 class C(object):
2300 def __getitem__(self, i):
2301 if 0 <= i < 10: return i
2302 raise IndexError
2303 c1 = C()
2304 c2 = C()
2305 self.assertFalse(not c1)
2306 self.assertNotEqual(id(c1), id(c2))
2307 hash(c1)
2308 hash(c2)
2309 self.assertEqual(c1, c1)
2310 self.assertTrue(c1 != c2)
2311 self.assertFalse(c1 != c1)
2312 self.assertFalse(c1 == c2)
2313 # Note that the module name appears in str/repr, and that varies
2314 # depending on whether this test is run standalone or from a framework.
2315 self.assertGreaterEqual(str(c1).find('C object at '), 0)
2316 self.assertEqual(str(c1), repr(c1))
2317 self.assertNotIn(-1, c1)
2318 for i in range(10):
2319 self.assertIn(i, c1)
2320 self.assertNotIn(10, c1)
2321 # Test the default behavior for dynamic classes
2322 class D(object):
2323 def __getitem__(self, i):
2324 if 0 <= i < 10: return i
2325 raise IndexError
2326 d1 = D()
2327 d2 = D()
2328 self.assertFalse(not d1)
2329 self.assertNotEqual(id(d1), id(d2))
2330 hash(d1)
2331 hash(d2)
2332 self.assertEqual(d1, d1)
2333 self.assertNotEqual(d1, d2)
2334 self.assertFalse(d1 != d1)
2335 self.assertFalse(d1 == d2)
2336 # Note that the module name appears in str/repr, and that varies
2337 # depending on whether this test is run standalone or from a framework.
2338 self.assertGreaterEqual(str(d1).find('D object at '), 0)
2339 self.assertEqual(str(d1), repr(d1))
2340 self.assertNotIn(-1, d1)
2341 for i in range(10):
2342 self.assertIn(i, d1)
2343 self.assertNotIn(10, d1)
2344 # Test overridden behavior
2345 class Proxy(object):
2346 def __init__(self, x):
2347 self.x = x
2348 def __bool__(self):
2349 return not not self.x
2350 def __hash__(self):
2351 return hash(self.x)

Callers

nothing calls this directly

Calls 13

idFunction · 0.85
strFunction · 0.85
assertFalseMethod · 0.80
assertNotEqualMethod · 0.80
assertTrueMethod · 0.80
assertGreaterEqualMethod · 0.80
assertNotInMethod · 0.80
assertInMethod · 0.80
CClass · 0.70
DClass · 0.70
ProxyClass · 0.70
assertEqualMethod · 0.45

Tested by

no test coverage detected