Show More
@@ -1,58 +1,67 b'' | |||||
1 | from IPython.utils.dir2 import dir2 |
|
1 | from IPython.utils.dir2 import dir2 | |
2 |
|
2 | |||
|
3 | import pytest | |||
|
4 | ||||
3 |
|
5 | |||
4 | class Base(object): |
|
6 | class Base(object): | |
5 | x = 1 |
|
7 | x = 1 | |
6 | z = 23 |
|
8 | z = 23 | |
7 |
|
9 | |||
8 |
|
10 | |||
9 | def test_base(): |
|
11 | def test_base(): | |
10 | res = dir2(Base()) |
|
12 | res = dir2(Base()) | |
11 | assert "x" in res |
|
13 | assert "x" in res | |
12 | assert "z" in res |
|
14 | assert "z" in res | |
13 | assert "y" not in res |
|
15 | assert "y" not in res | |
14 | assert "__class__" in res |
|
16 | assert "__class__" in res | |
15 | assert res.count("x") == 1 |
|
17 | assert res.count("x") == 1 | |
16 | assert res.count("__class__") == 1 |
|
18 | assert res.count("__class__") == 1 | |
17 |
|
19 | |||
18 |
|
20 | |||
19 | def test_SubClass(): |
|
21 | def test_SubClass(): | |
20 |
|
22 | |||
21 | class SubClass(Base): |
|
23 | class SubClass(Base): | |
22 | y = 2 |
|
24 | y = 2 | |
23 |
|
25 | |||
24 | res = dir2(SubClass()) |
|
26 | res = dir2(SubClass()) | |
25 | assert "y" in res |
|
27 | assert "y" in res | |
26 | assert res.count("y") == 1 |
|
28 | assert res.count("y") == 1 | |
27 | assert res.count("x") == 1 |
|
29 | assert res.count("x") == 1 | |
28 |
|
30 | |||
29 |
|
31 | |||
30 | def test_SubClass_with_trait_names_attr(): |
|
32 | def test_SubClass_with_trait_names_attr(): | |
31 | # usecase: trait_names is used in a class describing psychological classification |
|
33 | # usecase: trait_names is used in a class describing psychological classification | |
32 |
|
34 | |||
33 | class SubClass(Base): |
|
35 | class SubClass(Base): | |
34 | y = 2 |
|
36 | y = 2 | |
35 | trait_names = 44 |
|
37 | trait_names = 44 | |
36 |
|
38 | |||
37 | res = dir2(SubClass()) |
|
39 | res = dir2(SubClass()) | |
38 |
assert |
|
40 | assert "trait_names" in res | |
39 |
|
41 | |||
40 |
|
42 | |||
41 | def test_misbehaving_object_without_trait_names(): |
|
43 | def test_misbehaving_object_without_trait_names(): | |
42 | # dir2 shouldn't raise even when objects are dumb and raise |
|
44 | # dir2 shouldn't raise even when objects are dumb and raise | |
43 | # something other than AttribteErrors on bad getattr. |
|
45 | # something other than AttribteErrors on bad getattr. | |
44 |
|
46 | |||
45 |
class MisbehavingGetattr |
|
47 | class MisbehavingGetattr: | |
46 | def __getattr__(self): |
|
48 | def __getattr__(self, attr): | |
47 | raise KeyError("I should be caught") |
|
49 | raise KeyError("I should be caught") | |
48 |
|
50 | |||
49 | def some_method(self): |
|
51 | def some_method(self): | |
50 |
|
|
52 | return True | |
51 |
|
53 | |||
52 | class SillierWithDir(MisbehavingGetattr): |
|
54 | class SillierWithDir(MisbehavingGetattr): | |
53 | def __dir__(self): |
|
55 | def __dir__(self): | |
54 | return ['some_method'] |
|
56 | return ['some_method'] | |
55 |
|
57 | |||
56 | for bad_klass in (MisbehavingGetattr, SillierWithDir): |
|
58 | for bad_klass in (MisbehavingGetattr, SillierWithDir): | |
57 |
|
|
59 | obj = bad_klass() | |
58 | assert('some_method' in res) |
|
60 | ||
|
61 | assert obj.some_method() | |||
|
62 | ||||
|
63 | with pytest.raises(KeyError): | |||
|
64 | obj.other_method() | |||
|
65 | ||||
|
66 | res = dir2(obj) | |||
|
67 | assert "some_method" in res |
General Comments 0
You need to be logged in to leave comments.
Login now