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