##// END OF EJS Templates
[utils][tests][dir2] Improve test_misbehaving_object_without_trait_names
Samuel Gaist -
Show More
@@ -1,5 +1,7 b''
1 1 from IPython.utils.dir2 import dir2
2 2
3 import pytest
4
3 5
4 6 class Base(object):
5 7 x = 1
@@ -35,24 +37,31 b' def test_SubClass_with_trait_names_attr():'
35 37 trait_names = 44
36 38
37 39 res = dir2(SubClass())
38 assert('trait_names' in res)
40 assert "trait_names" in res
39 41
40 42
41 43 def test_misbehaving_object_without_trait_names():
42 44 # dir2 shouldn't raise even when objects are dumb and raise
43 45 # something other than AttribteErrors on bad getattr.
44 46
45 class MisbehavingGetattr(object):
46 def __getattr__(self):
47 class MisbehavingGetattr:
48 def __getattr__(self, attr):
47 49 raise KeyError("I should be caught")
48 50
49 51 def some_method(self):
50 pass
52 return True
51 53
52 54 class SillierWithDir(MisbehavingGetattr):
53 55 def __dir__(self):
54 56 return ['some_method']
55 57
56 58 for bad_klass in (MisbehavingGetattr, SillierWithDir):
57 res = dir2(bad_klass())
58 assert('some_method' in res)
59 obj = bad_klass()
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