Debian Codegraph @

pypy/module/__builtin__/test/test_dir.py

raw symbols SCIP
1 class AppTestDir:
2
3 def test_dir_obj__dir__tuple(self):
4 """If __dir__ method returns a tuple, cpython3 converts it to list."""
5 class Foo(object):
6 def __dir__(self):
7 return ("b", "c", "a")
8 res = dir(Foo())
9 assert isinstance(res, list)
10 assert res == ["a", "b", "c"]
11
12 def test_dir_obj__dir__genexp(self):
13 """Generator expression is also converted to list by cpython3."""
14 class Foo(object):
15 def __dir__(self):
16 return (i for i in ["b", "c", "a"])
17 res = dir(Foo())
18 assert isinstance(res, list)
19 assert res == ["a", "b", "c"]
20
21 def test_dir_obj__dir__noniter(self):
22 """If result of __dir__ is not iterable, it's an error."""
23 class Foo(object):
24 def __dir__(self):
25 return 42
26 raises(TypeError, dir, Foo())
27
28 def test_dir_traceback(self):
29 """Test dir() of traceback."""
30 try:
31 raise IndexError
32 except Exception as e:
33 tb_dir = dir(e.__traceback__)
34 assert tb_dir == ['tb_frame', 'tb_lasti', 'tb_lineno', 'tb_next']
35
36 def test_dir_object_inheritance(self):
37 """Dir should behave the same regardless of inheriting from object."""
38 class A:
39 pass
40
41 class B(object):
42 pass
43 assert dir(A) == dir(B)
44
45 def test_dir_sanity(self):
46 """Test that dir returns reasonable items."""
47 class A(object):
48 a = 1
49
50 class B(A):
51 y = 2
52
53 b = B()
54 b.z = 1
55
56 names = dir(b)
57 for name in 'ayz':
58 assert name in names
59
60 assert '__doc__' in names
61 assert '__module__' in names
62 assert '__dict__' in names
63 assert '__dir__' in names
64 assert '__weakref__' in names
65 assert '__class__' in names
66 assert '__format__' in names
67 # Not an exhaustive list, but will be enough if dir is very broken.
68
69 def test_dir_module(self):
70 import sys
71 assert dir(sys) == list(sorted(sys.__dict__))
72
73 def test_dir_list(self):
74 """Check that dir([]) has methods from list and from object."""
75 names = dir([])
76
77 dct = {}
78 dct.update(list.__dict__)
79 dct.update(object.__dict__)
80
81 assert names == sorted(dct)
82
83 def test_dir_builtins(self):
84 """Test that builtin objects have sane __dir__()."""
85 import sys
86
87 for builtin in [sys, object(), [], {}, {1}, "", 1, (), sys,
88 map(ord, "abc"), filter(None, "abc"), zip([1, 2], [3, 4]),
89 compile('1', '', 'exec')]:
90 assert sorted(builtin.__dir__()) == dir(builtin)
91
92 def test_dir_type(self):
93 """Test .__dir__() and dir(...) behavior on types.
94
95 * t.__dir__() throws a TypeError,
96 * dir(t) == sorted(t().__dir__())
97
98 This is the behavior that I observe with cpython3.3.2.
99 """
100 for t in [int, list, tuple, set, str]:
101 raises(TypeError, t.__dir__)
102 assert dir(t) == sorted(t().__dir__())
103
104 def test_dir_none(self):
105 assert dir(None) == sorted(None.__dir__())