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

Method test_dir

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

Source from the content-addressed store, hash-verified

2561 self.fail("no ValueError from dict(%r)" % bad)
2562
2563 def test_dir(self):
2564 # Testing dir() ...
2565 junk = 12
2566 self.assertEqual(dir(), ['junk', 'self'])
2567 del junk
2568
2569 # Just make sure these don't blow up!
2570 for arg in 2, 2, 2j, 2e0, [2], "2", b"2", (2,), {2:2}, type, self.test_dir:
2571 dir(arg)
2572
2573 # Test dir on new-style classes. Since these have object as a
2574 # base class, a lot more gets sucked in.
2575 def interesting(strings):
2576 return [s for s in strings if not s.startswith('_')]
2577
2578 class C(object):
2579 Cdata = 1
2580 def Cmethod(self): pass
2581
2582 cstuff = ['Cdata', 'Cmethod']
2583 self.assertEqual(interesting(dir(C)), cstuff)
2584
2585 c = C()
2586 self.assertEqual(interesting(dir(c)), cstuff)
2587 ## self.assertIn('__self__', dir(C.Cmethod))
2588
2589 c.cdata = 2
2590 c.cmethod = lambda self: 0
2591 self.assertEqual(interesting(dir(c)), cstuff + ['cdata', 'cmethod'])
2592 ## self.assertIn('__self__', dir(c.Cmethod))
2593
2594 class A(C):
2595 Adata = 1
2596 def Amethod(self): pass
2597
2598 astuff = ['Adata', 'Amethod'] + cstuff
2599 self.assertEqual(interesting(dir(A)), astuff)
2600 ## self.assertIn('__self__', dir(A.Amethod))
2601 a = A()
2602 self.assertEqual(interesting(dir(a)), astuff)
2603 a.adata = 42
2604 a.amethod = lambda self: 3
2605 self.assertEqual(interesting(dir(a)), astuff + ['adata', 'amethod'])
2606 ## self.assertIn('__self__', dir(a.Amethod))
2607
2608 # Try a module subclass.
2609 class M(type(sys)):
2610 pass
2611 minstance = M("m")
2612 minstance.b = 2
2613 minstance.a = 1
2614 default_attributes = ['__name__', '__doc__', '__package__',
2615 '__loader__', '__spec__']
2616 names = [x for x in dir(minstance) if x not in default_attributes]
2617 self.assertEqual(names, ['a', 'b'])
2618
2619 class M2(M):
2620 def getdict(self):

Callers

nothing calls this directly

Calls 7

MClass · 0.90
dirFunction · 0.85
CClass · 0.70
AClass · 0.70
M2Class · 0.70
assertEqualMethod · 0.45
assertRaisesMethod · 0.45

Tested by

no test coverage detected