MCPcopy Index your code
hub / github.com/RustPython/RustPython / test_properties

Method test_properties

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

Source from the content-addressed store, hash-verified

2326
2327 @unittest.expectedFailure # TODO: RUSTPYTHON
2328 def test_properties(self):
2329 # Testing property...
2330 class C(object):
2331 def getx(self):
2332 return self.__x
2333 def setx(self, value):
2334 self.__x = value
2335 def delx(self):
2336 del self.__x
2337 x = property(getx, setx, delx, doc="I'm the x property.")
2338 a = C()
2339 self.assertNotHasAttr(a, "x")
2340 a.x = 42
2341 self.assertEqual(a._C__x, 42)
2342 self.assertEqual(a.x, 42)
2343 del a.x
2344 self.assertNotHasAttr(a, "x")
2345 self.assertNotHasAttr(a, "_C__x")
2346 C.x.__set__(a, 100)
2347 self.assertEqual(C.x.__get__(a), 100)
2348 C.x.__delete__(a)
2349 self.assertNotHasAttr(a, "x")
2350
2351 raw = C.__dict__['x']
2352 self.assertIsInstance(raw, property)
2353
2354 attrs = dir(raw)
2355 self.assertIn("__doc__", attrs)
2356 self.assertIn("fget", attrs)
2357 self.assertIn("fset", attrs)
2358 self.assertIn("fdel", attrs)
2359
2360 self.assertEqual(raw.__doc__, "I'm the x property.")
2361 self.assertIs(raw.fget, C.__dict__['getx'])
2362 self.assertIs(raw.fset, C.__dict__['setx'])
2363 self.assertIs(raw.fdel, C.__dict__['delx'])
2364
2365 for attr in "fget", "fset", "fdel":
2366 try:
2367 setattr(raw, attr, 42)
2368 except AttributeError as msg:
2369 if str(msg).find('readonly') < 0:
2370 self.fail("when setting readonly attr %r on a property, "
2371 "got unexpected AttributeError msg %r" % (attr, str(msg)))
2372 else:
2373 self.fail("expected AttributeError from trying to set readonly %r "
2374 "attr on a property" % attr)
2375
2376 raw.__doc__ = 42
2377 self.assertEqual(raw.__doc__, 42)
2378
2379 class D(object):
2380 __getitem__ = property(lambda s: 1/0)
2381
2382 d = D()
2383 try:
2384 for i in d:
2385 str(i)

Callers

nothing calls this directly

Calls 15

dirFunction · 0.85
setattrFunction · 0.85
strFunction · 0.85
assertIsInstanceMethod · 0.80
assertInMethod · 0.80
CClass · 0.70
DClass · 0.70
assertNotHasAttrMethod · 0.45
assertEqualMethod · 0.45
__set__Method · 0.45
__get__Method · 0.45
__delete__Method · 0.45

Tested by

no test coverage detected