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