##// END OF EJS Templates
Add test for dict_dir method in utils.wildcard
Thomas Kluyver -
Show More
@@ -1,133 +1,148 b''
1 1 """Some tests for the wildcard utilities."""
2 2
3 3 #-----------------------------------------------------------------------------
4 4 # Library imports
5 5 #-----------------------------------------------------------------------------
6 6 # Stdlib
7 7 import sys
8 8 import unittest
9 9
10 10 # Our own
11 11 from IPython.utils import wildcard
12 12
13 13 #-----------------------------------------------------------------------------
14 14 # Globals for test
15 15 #-----------------------------------------------------------------------------
16 16
17 17 class obj_t(object):
18 18 pass
19 19
20 20 root = obj_t()
21 21 l = ["arna","abel","ABEL","active","bob","bark","abbot"]
22 22 q = ["kate","loop","arne","vito","lucifer","koppel"]
23 23 for x in l:
24 24 o = obj_t()
25 25 setattr(root,x,o)
26 26 for y in q:
27 27 p = obj_t()
28 28 setattr(o,y,p)
29 29 root._apan = obj_t()
30 30 root._apan.a = 10
31 31 root._apan._a = 20
32 32 root._apan.__a = 20
33 33 root.__anka = obj_t()
34 34 root.__anka.a = 10
35 35 root.__anka._a = 20
36 36 root.__anka.__a = 20
37 37
38 38 root._APAN = obj_t()
39 39 root._APAN.a = 10
40 40 root._APAN._a = 20
41 41 root._APAN.__a = 20
42 42 root.__ANKA = obj_t()
43 43 root.__ANKA.a = 10
44 44 root.__ANKA._a = 20
45 45 root.__ANKA.__a = 20
46 46
47 47 #-----------------------------------------------------------------------------
48 48 # Test cases
49 49 #-----------------------------------------------------------------------------
50 50
51 51 class Tests (unittest.TestCase):
52 52 def test_case(self):
53 53 ns=root.__dict__
54 54 tests=[
55 55 ("a*", ["abbot","abel","active","arna",]),
56 56 ("?b*.?o*",["abbot.koppel","abbot.loop","abel.koppel","abel.loop",]),
57 57 ("_a*", []),
58 58 ("_*anka", ["__anka",]),
59 59 ("_*a*", ["__anka",]),
60 60 ]
61 61 for pat,res in tests:
62 62 res.sort()
63 63 a=wildcard.list_namespace(ns,"all",pat,ignore_case=False,
64 64 show_all=False).keys()
65 65 a.sort()
66 66 self.assertEqual(a,res)
67 67
68 68 def test_case_showall(self):
69 69 ns=root.__dict__
70 70 tests=[
71 71 ("a*", ["abbot","abel","active","arna",]),
72 72 ("?b*.?o*",["abbot.koppel","abbot.loop","abel.koppel","abel.loop",]),
73 73 ("_a*", ["_apan"]),
74 74 ("_*anka", ["__anka",]),
75 75 ("_*a*", ["__anka","_apan",]),
76 76 ]
77 77 for pat,res in tests:
78 78 res.sort()
79 79 a=wildcard.list_namespace(ns,"all",pat,ignore_case=False,
80 80 show_all=True).keys()
81 81 a.sort()
82 82 self.assertEqual(a,res)
83 83
84 84
85 85 def test_nocase(self):
86 86 ns=root.__dict__
87 87 tests=[
88 88 ("a*", ["abbot","abel","ABEL","active","arna",]),
89 89 ("?b*.?o*",["abbot.koppel","abbot.loop","abel.koppel","abel.loop",
90 90 "ABEL.koppel","ABEL.loop",]),
91 91 ("_a*", []),
92 92 ("_*anka", ["__anka","__ANKA",]),
93 93 ("_*a*", ["__anka","__ANKA",]),
94 94 ]
95 95 for pat,res in tests:
96 96 res.sort()
97 97 a=wildcard.list_namespace(ns,"all",pat,ignore_case=True,
98 98 show_all=False).keys()
99 99 a.sort()
100 100 self.assertEqual(a,res)
101 101
102 102 def test_nocase_showall(self):
103 103 ns=root.__dict__
104 104 tests=[
105 105 ("a*", ["abbot","abel","ABEL","active","arna",]),
106 106 ("?b*.?o*",["abbot.koppel","abbot.loop","abel.koppel","abel.loop",
107 107 "ABEL.koppel","ABEL.loop",]),
108 108 ("_a*", ["_apan","_APAN"]),
109 109 ("_*anka", ["__anka","__ANKA",]),
110 110 ("_*a*", ["__anka","__ANKA","_apan","_APAN"]),
111 111 ]
112 112 for pat,res in tests:
113 113 res.sort()
114 114 a=wildcard.list_namespace(ns,"all",pat,ignore_case=True,
115 115 show_all=True).keys()
116 116 a.sort()
117 117 self.assertEqual(a,res)
118 118
119 119 def test_dict_attributes(self):
120 120 """Dictionaries should be indexed by attributes, not by keys. This was
121 121 causing Github issue 129."""
122 122 ns = {"az":{"king":55}, "pq":{1:0}}
123 123 tests = [
124 124 ("a*", ["az"]),
125 125 ("az.k*", ["az.keys"]),
126 126 ("pq.k*", ["pq.keys"])
127 127 ]
128 128 for pat, res in tests:
129 129 res.sort()
130 130 a = wildcard.list_namespace(ns, "all", pat, ignore_case=False,
131 131 show_all=True).keys()
132 132 a.sort()
133 133 self.assertEqual(a, res)
134
135 def test_dict_dir(self):
136 class A(object):
137 def __init__(self):
138 self.a = 1
139 self.b = 2
140 def __getattribute__(self, name):
141 if name=="a":
142 raise AttributeError
143 return object.__getattribute__(self, name)
144
145 a = A()
146 adict = wildcard.dict_dir(a)
147 assert "a" not in adict # change to assertNotIn method in >= 2.7
148 self.assertEqual(adict["b"], 2)
General Comments 0
You need to be logged in to leave comments. Login now