Show More
@@ -1,920 +1,932 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """ |
|
2 | """ | |
3 | Tests for IPython.utils.traitlets. |
|
3 | Tests for IPython.utils.traitlets. | |
4 |
|
4 | |||
5 | Authors: |
|
5 | Authors: | |
6 |
|
6 | |||
7 | * Brian Granger |
|
7 | * Brian Granger | |
8 | * Enthought, Inc. Some of the code in this file comes from enthought.traits |
|
8 | * Enthought, Inc. Some of the code in this file comes from enthought.traits | |
9 | and is licensed under the BSD license. Also, many of the ideas also come |
|
9 | and is licensed under the BSD license. Also, many of the ideas also come | |
10 | from enthought.traits even though our implementation is very different. |
|
10 | from enthought.traits even though our implementation is very different. | |
11 | """ |
|
11 | """ | |
12 |
|
12 | |||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Copyright (C) 2008-2011 The IPython Development Team |
|
14 | # Copyright (C) 2008-2011 The IPython Development Team | |
15 | # |
|
15 | # | |
16 | # Distributed under the terms of the BSD License. The full license is in |
|
16 | # Distributed under the terms of the BSD License. The full license is in | |
17 | # the file COPYING, distributed as part of this software. |
|
17 | # the file COPYING, distributed as part of this software. | |
18 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
19 |
|
19 | |||
20 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
21 | # Imports |
|
21 | # Imports | |
22 | #----------------------------------------------------------------------------- |
|
22 | #----------------------------------------------------------------------------- | |
23 |
|
23 | |||
24 | import re |
|
24 | import re | |
25 | import sys |
|
25 | import sys | |
26 | from unittest import TestCase |
|
26 | from unittest import TestCase | |
27 |
|
27 | |||
28 | import nose.tools as nt |
|
28 | import nose.tools as nt | |
29 | from nose import SkipTest |
|
29 | from nose import SkipTest | |
30 |
|
30 | |||
31 | from IPython.utils.traitlets import ( |
|
31 | from IPython.utils.traitlets import ( | |
32 | HasTraits, MetaHasTraits, TraitType, Any, CBytes, Dict, |
|
32 | HasTraits, MetaHasTraits, TraitType, Any, CBytes, Dict, | |
33 | Int, Long, Integer, Float, Complex, Bytes, Unicode, TraitError, |
|
33 | Int, Long, Integer, Float, Complex, Bytes, Unicode, TraitError, | |
34 | Undefined, Type, This, Instance, TCPAddress, List, Tuple, |
|
34 | Undefined, Type, This, Instance, TCPAddress, List, Tuple, | |
35 | ObjectName, DottedObjectName, CRegExp |
|
35 | ObjectName, DottedObjectName, CRegExp | |
36 | ) |
|
36 | ) | |
37 | from IPython.utils import py3compat |
|
37 | from IPython.utils import py3compat | |
38 | from IPython.testing.decorators import skipif |
|
38 | from IPython.testing.decorators import skipif | |
39 |
|
39 | |||
40 | #----------------------------------------------------------------------------- |
|
40 | #----------------------------------------------------------------------------- | |
41 | # Helper classes for testing |
|
41 | # Helper classes for testing | |
42 | #----------------------------------------------------------------------------- |
|
42 | #----------------------------------------------------------------------------- | |
43 |
|
43 | |||
44 |
|
44 | |||
45 | class HasTraitsStub(HasTraits): |
|
45 | class HasTraitsStub(HasTraits): | |
46 |
|
46 | |||
47 | def _notify_trait(self, name, old, new): |
|
47 | def _notify_trait(self, name, old, new): | |
48 | self._notify_name = name |
|
48 | self._notify_name = name | |
49 | self._notify_old = old |
|
49 | self._notify_old = old | |
50 | self._notify_new = new |
|
50 | self._notify_new = new | |
51 |
|
51 | |||
52 |
|
52 | |||
53 | #----------------------------------------------------------------------------- |
|
53 | #----------------------------------------------------------------------------- | |
54 | # Test classes |
|
54 | # Test classes | |
55 | #----------------------------------------------------------------------------- |
|
55 | #----------------------------------------------------------------------------- | |
56 |
|
56 | |||
57 |
|
57 | |||
58 | class TestTraitType(TestCase): |
|
58 | class TestTraitType(TestCase): | |
59 |
|
59 | |||
60 | def test_get_undefined(self): |
|
60 | def test_get_undefined(self): | |
61 | class A(HasTraits): |
|
61 | class A(HasTraits): | |
62 | a = TraitType |
|
62 | a = TraitType | |
63 | a = A() |
|
63 | a = A() | |
64 | self.assertEqual(a.a, Undefined) |
|
64 | self.assertEqual(a.a, Undefined) | |
65 |
|
65 | |||
66 | def test_set(self): |
|
66 | def test_set(self): | |
67 | class A(HasTraitsStub): |
|
67 | class A(HasTraitsStub): | |
68 | a = TraitType |
|
68 | a = TraitType | |
69 |
|
69 | |||
70 | a = A() |
|
70 | a = A() | |
71 | a.a = 10 |
|
71 | a.a = 10 | |
72 | self.assertEqual(a.a, 10) |
|
72 | self.assertEqual(a.a, 10) | |
73 | self.assertEqual(a._notify_name, 'a') |
|
73 | self.assertEqual(a._notify_name, 'a') | |
74 | self.assertEqual(a._notify_old, Undefined) |
|
74 | self.assertEqual(a._notify_old, Undefined) | |
75 | self.assertEqual(a._notify_new, 10) |
|
75 | self.assertEqual(a._notify_new, 10) | |
76 |
|
76 | |||
77 | def test_validate(self): |
|
77 | def test_validate(self): | |
78 | class MyTT(TraitType): |
|
78 | class MyTT(TraitType): | |
79 | def validate(self, inst, value): |
|
79 | def validate(self, inst, value): | |
80 | return -1 |
|
80 | return -1 | |
81 | class A(HasTraitsStub): |
|
81 | class A(HasTraitsStub): | |
82 | tt = MyTT |
|
82 | tt = MyTT | |
83 |
|
83 | |||
84 | a = A() |
|
84 | a = A() | |
85 | a.tt = 10 |
|
85 | a.tt = 10 | |
86 | self.assertEqual(a.tt, -1) |
|
86 | self.assertEqual(a.tt, -1) | |
87 |
|
87 | |||
88 | def test_default_validate(self): |
|
88 | def test_default_validate(self): | |
89 | class MyIntTT(TraitType): |
|
89 | class MyIntTT(TraitType): | |
90 | def validate(self, obj, value): |
|
90 | def validate(self, obj, value): | |
91 | if isinstance(value, int): |
|
91 | if isinstance(value, int): | |
92 | return value |
|
92 | return value | |
93 | self.error(obj, value) |
|
93 | self.error(obj, value) | |
94 | class A(HasTraits): |
|
94 | class A(HasTraits): | |
95 | tt = MyIntTT(10) |
|
95 | tt = MyIntTT(10) | |
96 | a = A() |
|
96 | a = A() | |
97 | self.assertEqual(a.tt, 10) |
|
97 | self.assertEqual(a.tt, 10) | |
98 |
|
98 | |||
99 | # Defaults are validated when the HasTraits is instantiated |
|
99 | # Defaults are validated when the HasTraits is instantiated | |
100 | class B(HasTraits): |
|
100 | class B(HasTraits): | |
101 | tt = MyIntTT('bad default') |
|
101 | tt = MyIntTT('bad default') | |
102 | self.assertRaises(TraitError, B) |
|
102 | self.assertRaises(TraitError, B) | |
103 |
|
103 | |||
104 | def test_is_valid_for(self): |
|
104 | def test_is_valid_for(self): | |
105 | class MyTT(TraitType): |
|
105 | class MyTT(TraitType): | |
106 | def is_valid_for(self, value): |
|
106 | def is_valid_for(self, value): | |
107 | return True |
|
107 | return True | |
108 | class A(HasTraits): |
|
108 | class A(HasTraits): | |
109 | tt = MyTT |
|
109 | tt = MyTT | |
110 |
|
110 | |||
111 | a = A() |
|
111 | a = A() | |
112 | a.tt = 10 |
|
112 | a.tt = 10 | |
113 | self.assertEqual(a.tt, 10) |
|
113 | self.assertEqual(a.tt, 10) | |
114 |
|
114 | |||
115 | def test_value_for(self): |
|
115 | def test_value_for(self): | |
116 | class MyTT(TraitType): |
|
116 | class MyTT(TraitType): | |
117 | def value_for(self, value): |
|
117 | def value_for(self, value): | |
118 | return 20 |
|
118 | return 20 | |
119 | class A(HasTraits): |
|
119 | class A(HasTraits): | |
120 | tt = MyTT |
|
120 | tt = MyTT | |
121 |
|
121 | |||
122 | a = A() |
|
122 | a = A() | |
123 | a.tt = 10 |
|
123 | a.tt = 10 | |
124 | self.assertEqual(a.tt, 20) |
|
124 | self.assertEqual(a.tt, 20) | |
125 |
|
125 | |||
126 | def test_info(self): |
|
126 | def test_info(self): | |
127 | class A(HasTraits): |
|
127 | class A(HasTraits): | |
128 | tt = TraitType |
|
128 | tt = TraitType | |
129 | a = A() |
|
129 | a = A() | |
130 | self.assertEqual(A.tt.info(), 'any value') |
|
130 | self.assertEqual(A.tt.info(), 'any value') | |
131 |
|
131 | |||
132 | def test_error(self): |
|
132 | def test_error(self): | |
133 | class A(HasTraits): |
|
133 | class A(HasTraits): | |
134 | tt = TraitType |
|
134 | tt = TraitType | |
135 | a = A() |
|
135 | a = A() | |
136 | self.assertRaises(TraitError, A.tt.error, a, 10) |
|
136 | self.assertRaises(TraitError, A.tt.error, a, 10) | |
137 |
|
137 | |||
138 | def test_dynamic_initializer(self): |
|
138 | def test_dynamic_initializer(self): | |
139 | class A(HasTraits): |
|
139 | class A(HasTraits): | |
140 | x = Int(10) |
|
140 | x = Int(10) | |
141 | def _x_default(self): |
|
141 | def _x_default(self): | |
142 | return 11 |
|
142 | return 11 | |
143 | class B(A): |
|
143 | class B(A): | |
144 | x = Int(20) |
|
144 | x = Int(20) | |
145 | class C(A): |
|
145 | class C(A): | |
146 | def _x_default(self): |
|
146 | def _x_default(self): | |
147 | return 21 |
|
147 | return 21 | |
148 |
|
148 | |||
149 | a = A() |
|
149 | a = A() | |
150 | self.assertEqual(a._trait_values, {}) |
|
150 | self.assertEqual(a._trait_values, {}) | |
151 | self.assertEqual(a._trait_dyn_inits.keys(), ['x']) |
|
151 | self.assertEqual(a._trait_dyn_inits.keys(), ['x']) | |
152 | self.assertEqual(a.x, 11) |
|
152 | self.assertEqual(a.x, 11) | |
153 | self.assertEqual(a._trait_values, {'x': 11}) |
|
153 | self.assertEqual(a._trait_values, {'x': 11}) | |
154 | b = B() |
|
154 | b = B() | |
155 | self.assertEqual(b._trait_values, {'x': 20}) |
|
155 | self.assertEqual(b._trait_values, {'x': 20}) | |
156 | self.assertEqual(a._trait_dyn_inits.keys(), ['x']) |
|
156 | self.assertEqual(a._trait_dyn_inits.keys(), ['x']) | |
157 | self.assertEqual(b.x, 20) |
|
157 | self.assertEqual(b.x, 20) | |
158 | c = C() |
|
158 | c = C() | |
159 | self.assertEqual(c._trait_values, {}) |
|
159 | self.assertEqual(c._trait_values, {}) | |
160 | self.assertEqual(a._trait_dyn_inits.keys(), ['x']) |
|
160 | self.assertEqual(a._trait_dyn_inits.keys(), ['x']) | |
161 | self.assertEqual(c.x, 21) |
|
161 | self.assertEqual(c.x, 21) | |
162 | self.assertEqual(c._trait_values, {'x': 21}) |
|
162 | self.assertEqual(c._trait_values, {'x': 21}) | |
163 | # Ensure that the base class remains unmolested when the _default |
|
163 | # Ensure that the base class remains unmolested when the _default | |
164 | # initializer gets overridden in a subclass. |
|
164 | # initializer gets overridden in a subclass. | |
165 | a = A() |
|
165 | a = A() | |
166 | c = C() |
|
166 | c = C() | |
167 | self.assertEqual(a._trait_values, {}) |
|
167 | self.assertEqual(a._trait_values, {}) | |
168 | self.assertEqual(a._trait_dyn_inits.keys(), ['x']) |
|
168 | self.assertEqual(a._trait_dyn_inits.keys(), ['x']) | |
169 | self.assertEqual(a.x, 11) |
|
169 | self.assertEqual(a.x, 11) | |
170 | self.assertEqual(a._trait_values, {'x': 11}) |
|
170 | self.assertEqual(a._trait_values, {'x': 11}) | |
171 |
|
171 | |||
172 |
|
172 | |||
173 |
|
173 | |||
174 | class TestHasTraitsMeta(TestCase): |
|
174 | class TestHasTraitsMeta(TestCase): | |
175 |
|
175 | |||
176 | def test_metaclass(self): |
|
176 | def test_metaclass(self): | |
177 | self.assertEqual(type(HasTraits), MetaHasTraits) |
|
177 | self.assertEqual(type(HasTraits), MetaHasTraits) | |
178 |
|
178 | |||
179 | class A(HasTraits): |
|
179 | class A(HasTraits): | |
180 | a = Int |
|
180 | a = Int | |
181 |
|
181 | |||
182 | a = A() |
|
182 | a = A() | |
183 | self.assertEqual(type(a.__class__), MetaHasTraits) |
|
183 | self.assertEqual(type(a.__class__), MetaHasTraits) | |
184 | self.assertEqual(a.a,0) |
|
184 | self.assertEqual(a.a,0) | |
185 | a.a = 10 |
|
185 | a.a = 10 | |
186 | self.assertEqual(a.a,10) |
|
186 | self.assertEqual(a.a,10) | |
187 |
|
187 | |||
188 | class B(HasTraits): |
|
188 | class B(HasTraits): | |
189 | b = Int() |
|
189 | b = Int() | |
190 |
|
190 | |||
191 | b = B() |
|
191 | b = B() | |
192 | self.assertEqual(b.b,0) |
|
192 | self.assertEqual(b.b,0) | |
193 | b.b = 10 |
|
193 | b.b = 10 | |
194 | self.assertEqual(b.b,10) |
|
194 | self.assertEqual(b.b,10) | |
195 |
|
195 | |||
196 | class C(HasTraits): |
|
196 | class C(HasTraits): | |
197 | c = Int(30) |
|
197 | c = Int(30) | |
198 |
|
198 | |||
199 | c = C() |
|
199 | c = C() | |
200 | self.assertEqual(c.c,30) |
|
200 | self.assertEqual(c.c,30) | |
201 | c.c = 10 |
|
201 | c.c = 10 | |
202 | self.assertEqual(c.c,10) |
|
202 | self.assertEqual(c.c,10) | |
203 |
|
203 | |||
204 | def test_this_class(self): |
|
204 | def test_this_class(self): | |
205 | class A(HasTraits): |
|
205 | class A(HasTraits): | |
206 | t = This() |
|
206 | t = This() | |
207 | tt = This() |
|
207 | tt = This() | |
208 | class B(A): |
|
208 | class B(A): | |
209 | tt = This() |
|
209 | tt = This() | |
210 | ttt = This() |
|
210 | ttt = This() | |
211 | self.assertEqual(A.t.this_class, A) |
|
211 | self.assertEqual(A.t.this_class, A) | |
212 | self.assertEqual(B.t.this_class, A) |
|
212 | self.assertEqual(B.t.this_class, A) | |
213 | self.assertEqual(B.tt.this_class, B) |
|
213 | self.assertEqual(B.tt.this_class, B) | |
214 | self.assertEqual(B.ttt.this_class, B) |
|
214 | self.assertEqual(B.ttt.this_class, B) | |
215 |
|
215 | |||
216 | class TestHasTraitsNotify(TestCase): |
|
216 | class TestHasTraitsNotify(TestCase): | |
217 |
|
217 | |||
218 | def setUp(self): |
|
218 | def setUp(self): | |
219 | self._notify1 = [] |
|
219 | self._notify1 = [] | |
220 | self._notify2 = [] |
|
220 | self._notify2 = [] | |
221 |
|
221 | |||
222 | def notify1(self, name, old, new): |
|
222 | def notify1(self, name, old, new): | |
223 | self._notify1.append((name, old, new)) |
|
223 | self._notify1.append((name, old, new)) | |
224 |
|
224 | |||
225 | def notify2(self, name, old, new): |
|
225 | def notify2(self, name, old, new): | |
226 | self._notify2.append((name, old, new)) |
|
226 | self._notify2.append((name, old, new)) | |
227 |
|
227 | |||
228 | def test_notify_all(self): |
|
228 | def test_notify_all(self): | |
229 |
|
229 | |||
230 | class A(HasTraits): |
|
230 | class A(HasTraits): | |
231 | a = Int |
|
231 | a = Int | |
232 | b = Float |
|
232 | b = Float | |
233 |
|
233 | |||
234 | a = A() |
|
234 | a = A() | |
235 | a.on_trait_change(self.notify1) |
|
235 | a.on_trait_change(self.notify1) | |
236 | a.a = 0 |
|
236 | a.a = 0 | |
237 | self.assertEqual(len(self._notify1),0) |
|
237 | self.assertEqual(len(self._notify1),0) | |
238 | a.b = 0.0 |
|
238 | a.b = 0.0 | |
239 | self.assertEqual(len(self._notify1),0) |
|
239 | self.assertEqual(len(self._notify1),0) | |
240 | a.a = 10 |
|
240 | a.a = 10 | |
241 | self.assertTrue(('a',0,10) in self._notify1) |
|
241 | self.assertTrue(('a',0,10) in self._notify1) | |
242 | a.b = 10.0 |
|
242 | a.b = 10.0 | |
243 | self.assertTrue(('b',0.0,10.0) in self._notify1) |
|
243 | self.assertTrue(('b',0.0,10.0) in self._notify1) | |
244 | self.assertRaises(TraitError,setattr,a,'a','bad string') |
|
244 | self.assertRaises(TraitError,setattr,a,'a','bad string') | |
245 | self.assertRaises(TraitError,setattr,a,'b','bad string') |
|
245 | self.assertRaises(TraitError,setattr,a,'b','bad string') | |
246 | self._notify1 = [] |
|
246 | self._notify1 = [] | |
247 | a.on_trait_change(self.notify1,remove=True) |
|
247 | a.on_trait_change(self.notify1,remove=True) | |
248 | a.a = 20 |
|
248 | a.a = 20 | |
249 | a.b = 20.0 |
|
249 | a.b = 20.0 | |
250 | self.assertEqual(len(self._notify1),0) |
|
250 | self.assertEqual(len(self._notify1),0) | |
251 |
|
251 | |||
252 | def test_notify_one(self): |
|
252 | def test_notify_one(self): | |
253 |
|
253 | |||
254 | class A(HasTraits): |
|
254 | class A(HasTraits): | |
255 | a = Int |
|
255 | a = Int | |
256 | b = Float |
|
256 | b = Float | |
257 |
|
257 | |||
258 | a = A() |
|
258 | a = A() | |
259 | a.on_trait_change(self.notify1, 'a') |
|
259 | a.on_trait_change(self.notify1, 'a') | |
260 | a.a = 0 |
|
260 | a.a = 0 | |
261 | self.assertEqual(len(self._notify1),0) |
|
261 | self.assertEqual(len(self._notify1),0) | |
262 | a.a = 10 |
|
262 | a.a = 10 | |
263 | self.assertTrue(('a',0,10) in self._notify1) |
|
263 | self.assertTrue(('a',0,10) in self._notify1) | |
264 | self.assertRaises(TraitError,setattr,a,'a','bad string') |
|
264 | self.assertRaises(TraitError,setattr,a,'a','bad string') | |
265 |
|
265 | |||
266 | def test_subclass(self): |
|
266 | def test_subclass(self): | |
267 |
|
267 | |||
268 | class A(HasTraits): |
|
268 | class A(HasTraits): | |
269 | a = Int |
|
269 | a = Int | |
270 |
|
270 | |||
271 | class B(A): |
|
271 | class B(A): | |
272 | b = Float |
|
272 | b = Float | |
273 |
|
273 | |||
274 | b = B() |
|
274 | b = B() | |
275 | self.assertEqual(b.a,0) |
|
275 | self.assertEqual(b.a,0) | |
276 | self.assertEqual(b.b,0.0) |
|
276 | self.assertEqual(b.b,0.0) | |
277 | b.a = 100 |
|
277 | b.a = 100 | |
278 | b.b = 100.0 |
|
278 | b.b = 100.0 | |
279 | self.assertEqual(b.a,100) |
|
279 | self.assertEqual(b.a,100) | |
280 | self.assertEqual(b.b,100.0) |
|
280 | self.assertEqual(b.b,100.0) | |
281 |
|
281 | |||
282 | def test_notify_subclass(self): |
|
282 | def test_notify_subclass(self): | |
283 |
|
283 | |||
284 | class A(HasTraits): |
|
284 | class A(HasTraits): | |
285 | a = Int |
|
285 | a = Int | |
286 |
|
286 | |||
287 | class B(A): |
|
287 | class B(A): | |
288 | b = Float |
|
288 | b = Float | |
289 |
|
289 | |||
290 | b = B() |
|
290 | b = B() | |
291 | b.on_trait_change(self.notify1, 'a') |
|
291 | b.on_trait_change(self.notify1, 'a') | |
292 | b.on_trait_change(self.notify2, 'b') |
|
292 | b.on_trait_change(self.notify2, 'b') | |
293 | b.a = 0 |
|
293 | b.a = 0 | |
294 | b.b = 0.0 |
|
294 | b.b = 0.0 | |
295 | self.assertEqual(len(self._notify1),0) |
|
295 | self.assertEqual(len(self._notify1),0) | |
296 | self.assertEqual(len(self._notify2),0) |
|
296 | self.assertEqual(len(self._notify2),0) | |
297 | b.a = 10 |
|
297 | b.a = 10 | |
298 | b.b = 10.0 |
|
298 | b.b = 10.0 | |
299 | self.assertTrue(('a',0,10) in self._notify1) |
|
299 | self.assertTrue(('a',0,10) in self._notify1) | |
300 | self.assertTrue(('b',0.0,10.0) in self._notify2) |
|
300 | self.assertTrue(('b',0.0,10.0) in self._notify2) | |
301 |
|
301 | |||
302 | def test_static_notify(self): |
|
302 | def test_static_notify(self): | |
303 |
|
303 | |||
304 | class A(HasTraits): |
|
304 | class A(HasTraits): | |
305 | a = Int |
|
305 | a = Int | |
306 | _notify1 = [] |
|
306 | _notify1 = [] | |
307 | def _a_changed(self, name, old, new): |
|
307 | def _a_changed(self, name, old, new): | |
308 | self._notify1.append((name, old, new)) |
|
308 | self._notify1.append((name, old, new)) | |
309 |
|
309 | |||
310 | a = A() |
|
310 | a = A() | |
311 | a.a = 0 |
|
311 | a.a = 0 | |
312 | # This is broken!!! |
|
312 | # This is broken!!! | |
313 | self.assertEqual(len(a._notify1),0) |
|
313 | self.assertEqual(len(a._notify1),0) | |
314 | a.a = 10 |
|
314 | a.a = 10 | |
315 | self.assertTrue(('a',0,10) in a._notify1) |
|
315 | self.assertTrue(('a',0,10) in a._notify1) | |
316 |
|
316 | |||
317 | class B(A): |
|
317 | class B(A): | |
318 | b = Float |
|
318 | b = Float | |
319 | _notify2 = [] |
|
319 | _notify2 = [] | |
320 | def _b_changed(self, name, old, new): |
|
320 | def _b_changed(self, name, old, new): | |
321 | self._notify2.append((name, old, new)) |
|
321 | self._notify2.append((name, old, new)) | |
322 |
|
322 | |||
323 | b = B() |
|
323 | b = B() | |
324 | b.a = 10 |
|
324 | b.a = 10 | |
325 | b.b = 10.0 |
|
325 | b.b = 10.0 | |
326 | self.assertTrue(('a',0,10) in b._notify1) |
|
326 | self.assertTrue(('a',0,10) in b._notify1) | |
327 | self.assertTrue(('b',0.0,10.0) in b._notify2) |
|
327 | self.assertTrue(('b',0.0,10.0) in b._notify2) | |
328 |
|
328 | |||
329 | def test_notify_args(self): |
|
329 | def test_notify_args(self): | |
330 |
|
330 | |||
331 | def callback0(): |
|
331 | def callback0(): | |
332 | self.cb = () |
|
332 | self.cb = () | |
333 | def callback1(name): |
|
333 | def callback1(name): | |
334 | self.cb = (name,) |
|
334 | self.cb = (name,) | |
335 | def callback2(name, new): |
|
335 | def callback2(name, new): | |
336 | self.cb = (name, new) |
|
336 | self.cb = (name, new) | |
337 | def callback3(name, old, new): |
|
337 | def callback3(name, old, new): | |
338 | self.cb = (name, old, new) |
|
338 | self.cb = (name, old, new) | |
339 |
|
339 | |||
340 | class A(HasTraits): |
|
340 | class A(HasTraits): | |
341 | a = Int |
|
341 | a = Int | |
342 |
|
342 | |||
343 | a = A() |
|
343 | a = A() | |
344 | a.on_trait_change(callback0, 'a') |
|
344 | a.on_trait_change(callback0, 'a') | |
345 | a.a = 10 |
|
345 | a.a = 10 | |
346 | self.assertEqual(self.cb,()) |
|
346 | self.assertEqual(self.cb,()) | |
347 | a.on_trait_change(callback0, 'a', remove=True) |
|
347 | a.on_trait_change(callback0, 'a', remove=True) | |
348 |
|
348 | |||
349 | a.on_trait_change(callback1, 'a') |
|
349 | a.on_trait_change(callback1, 'a') | |
350 | a.a = 100 |
|
350 | a.a = 100 | |
351 | self.assertEqual(self.cb,('a',)) |
|
351 | self.assertEqual(self.cb,('a',)) | |
352 | a.on_trait_change(callback1, 'a', remove=True) |
|
352 | a.on_trait_change(callback1, 'a', remove=True) | |
353 |
|
353 | |||
354 | a.on_trait_change(callback2, 'a') |
|
354 | a.on_trait_change(callback2, 'a') | |
355 | a.a = 1000 |
|
355 | a.a = 1000 | |
356 | self.assertEqual(self.cb,('a',1000)) |
|
356 | self.assertEqual(self.cb,('a',1000)) | |
357 | a.on_trait_change(callback2, 'a', remove=True) |
|
357 | a.on_trait_change(callback2, 'a', remove=True) | |
358 |
|
358 | |||
359 | a.on_trait_change(callback3, 'a') |
|
359 | a.on_trait_change(callback3, 'a') | |
360 | a.a = 10000 |
|
360 | a.a = 10000 | |
361 | self.assertEqual(self.cb,('a',1000,10000)) |
|
361 | self.assertEqual(self.cb,('a',1000,10000)) | |
362 | a.on_trait_change(callback3, 'a', remove=True) |
|
362 | a.on_trait_change(callback3, 'a', remove=True) | |
363 |
|
363 | |||
364 | self.assertEqual(len(a._trait_notifiers['a']),0) |
|
364 | self.assertEqual(len(a._trait_notifiers['a']),0) | |
365 |
|
365 | |||
366 |
|
366 | |||
367 | class TestHasTraits(TestCase): |
|
367 | class TestHasTraits(TestCase): | |
368 |
|
368 | |||
369 | def test_trait_names(self): |
|
369 | def test_trait_names(self): | |
370 | class A(HasTraits): |
|
370 | class A(HasTraits): | |
371 | i = Int |
|
371 | i = Int | |
372 | f = Float |
|
372 | f = Float | |
373 | a = A() |
|
373 | a = A() | |
374 | self.assertEqual(sorted(a.trait_names()),['f','i']) |
|
374 | self.assertEqual(sorted(a.trait_names()),['f','i']) | |
375 | self.assertEqual(sorted(A.class_trait_names()),['f','i']) |
|
375 | self.assertEqual(sorted(A.class_trait_names()),['f','i']) | |
376 |
|
376 | |||
377 | def test_trait_metadata(self): |
|
377 | def test_trait_metadata(self): | |
378 | class A(HasTraits): |
|
378 | class A(HasTraits): | |
379 | i = Int(config_key='MY_VALUE') |
|
379 | i = Int(config_key='MY_VALUE') | |
380 | a = A() |
|
380 | a = A() | |
381 | self.assertEqual(a.trait_metadata('i','config_key'), 'MY_VALUE') |
|
381 | self.assertEqual(a.trait_metadata('i','config_key'), 'MY_VALUE') | |
382 |
|
382 | |||
383 | def test_traits(self): |
|
383 | def test_traits(self): | |
384 | class A(HasTraits): |
|
384 | class A(HasTraits): | |
385 | i = Int |
|
385 | i = Int | |
386 | f = Float |
|
386 | f = Float | |
387 | a = A() |
|
387 | a = A() | |
388 | self.assertEqual(a.traits(), dict(i=A.i, f=A.f)) |
|
388 | self.assertEqual(a.traits(), dict(i=A.i, f=A.f)) | |
389 | self.assertEqual(A.class_traits(), dict(i=A.i, f=A.f)) |
|
389 | self.assertEqual(A.class_traits(), dict(i=A.i, f=A.f)) | |
390 |
|
390 | |||
391 | def test_traits_metadata(self): |
|
391 | def test_traits_metadata(self): | |
392 | class A(HasTraits): |
|
392 | class A(HasTraits): | |
393 | i = Int(config_key='VALUE1', other_thing='VALUE2') |
|
393 | i = Int(config_key='VALUE1', other_thing='VALUE2') | |
394 | f = Float(config_key='VALUE3', other_thing='VALUE2') |
|
394 | f = Float(config_key='VALUE3', other_thing='VALUE2') | |
395 | j = Int(0) |
|
395 | j = Int(0) | |
396 | a = A() |
|
396 | a = A() | |
397 | self.assertEqual(a.traits(), dict(i=A.i, f=A.f, j=A.j)) |
|
397 | self.assertEqual(a.traits(), dict(i=A.i, f=A.f, j=A.j)) | |
398 | traits = a.traits(config_key='VALUE1', other_thing='VALUE2') |
|
398 | traits = a.traits(config_key='VALUE1', other_thing='VALUE2') | |
399 | self.assertEqual(traits, dict(i=A.i)) |
|
399 | self.assertEqual(traits, dict(i=A.i)) | |
400 |
|
400 | |||
401 | # This passes, but it shouldn't because I am replicating a bug in |
|
401 | # This passes, but it shouldn't because I am replicating a bug in | |
402 | # traits. |
|
402 | # traits. | |
403 | traits = a.traits(config_key=lambda v: True) |
|
403 | traits = a.traits(config_key=lambda v: True) | |
404 | self.assertEqual(traits, dict(i=A.i, f=A.f, j=A.j)) |
|
404 | self.assertEqual(traits, dict(i=A.i, f=A.f, j=A.j)) | |
405 |
|
405 | |||
406 | def test_init(self): |
|
406 | def test_init(self): | |
407 | class A(HasTraits): |
|
407 | class A(HasTraits): | |
408 | i = Int() |
|
408 | i = Int() | |
409 | x = Float() |
|
409 | x = Float() | |
410 | a = A(i=1, x=10.0) |
|
410 | a = A(i=1, x=10.0) | |
411 | self.assertEqual(a.i, 1) |
|
411 | self.assertEqual(a.i, 1) | |
412 | self.assertEqual(a.x, 10.0) |
|
412 | self.assertEqual(a.x, 10.0) | |
413 |
|
413 | |||
|
414 | def test_positional_args(self): | |||
|
415 | class A(HasTraits): | |||
|
416 | i = Int(0) | |||
|
417 | def __init__(self, i): | |||
|
418 | super(A, self).__init__() | |||
|
419 | self.i = i | |||
|
420 | ||||
|
421 | a = A(5) | |||
|
422 | self.assertEqual(a.i, 5) | |||
|
423 | # should raise TypeError if no positional arg given | |||
|
424 | self.assertRaises(TypeError, A) | |||
|
425 | ||||
414 | #----------------------------------------------------------------------------- |
|
426 | #----------------------------------------------------------------------------- | |
415 | # Tests for specific trait types |
|
427 | # Tests for specific trait types | |
416 | #----------------------------------------------------------------------------- |
|
428 | #----------------------------------------------------------------------------- | |
417 |
|
429 | |||
418 |
|
430 | |||
419 | class TestType(TestCase): |
|
431 | class TestType(TestCase): | |
420 |
|
432 | |||
421 | def test_default(self): |
|
433 | def test_default(self): | |
422 |
|
434 | |||
423 | class B(object): pass |
|
435 | class B(object): pass | |
424 | class A(HasTraits): |
|
436 | class A(HasTraits): | |
425 | klass = Type |
|
437 | klass = Type | |
426 |
|
438 | |||
427 | a = A() |
|
439 | a = A() | |
428 | self.assertEqual(a.klass, None) |
|
440 | self.assertEqual(a.klass, None) | |
429 |
|
441 | |||
430 | a.klass = B |
|
442 | a.klass = B | |
431 | self.assertEqual(a.klass, B) |
|
443 | self.assertEqual(a.klass, B) | |
432 | self.assertRaises(TraitError, setattr, a, 'klass', 10) |
|
444 | self.assertRaises(TraitError, setattr, a, 'klass', 10) | |
433 |
|
445 | |||
434 | def test_value(self): |
|
446 | def test_value(self): | |
435 |
|
447 | |||
436 | class B(object): pass |
|
448 | class B(object): pass | |
437 | class C(object): pass |
|
449 | class C(object): pass | |
438 | class A(HasTraits): |
|
450 | class A(HasTraits): | |
439 | klass = Type(B) |
|
451 | klass = Type(B) | |
440 |
|
452 | |||
441 | a = A() |
|
453 | a = A() | |
442 | self.assertEqual(a.klass, B) |
|
454 | self.assertEqual(a.klass, B) | |
443 | self.assertRaises(TraitError, setattr, a, 'klass', C) |
|
455 | self.assertRaises(TraitError, setattr, a, 'klass', C) | |
444 | self.assertRaises(TraitError, setattr, a, 'klass', object) |
|
456 | self.assertRaises(TraitError, setattr, a, 'klass', object) | |
445 | a.klass = B |
|
457 | a.klass = B | |
446 |
|
458 | |||
447 | def test_allow_none(self): |
|
459 | def test_allow_none(self): | |
448 |
|
460 | |||
449 | class B(object): pass |
|
461 | class B(object): pass | |
450 | class C(B): pass |
|
462 | class C(B): pass | |
451 | class A(HasTraits): |
|
463 | class A(HasTraits): | |
452 | klass = Type(B, allow_none=False) |
|
464 | klass = Type(B, allow_none=False) | |
453 |
|
465 | |||
454 | a = A() |
|
466 | a = A() | |
455 | self.assertEqual(a.klass, B) |
|
467 | self.assertEqual(a.klass, B) | |
456 | self.assertRaises(TraitError, setattr, a, 'klass', None) |
|
468 | self.assertRaises(TraitError, setattr, a, 'klass', None) | |
457 | a.klass = C |
|
469 | a.klass = C | |
458 | self.assertEqual(a.klass, C) |
|
470 | self.assertEqual(a.klass, C) | |
459 |
|
471 | |||
460 | def test_validate_klass(self): |
|
472 | def test_validate_klass(self): | |
461 |
|
473 | |||
462 | class A(HasTraits): |
|
474 | class A(HasTraits): | |
463 | klass = Type('no strings allowed') |
|
475 | klass = Type('no strings allowed') | |
464 |
|
476 | |||
465 | self.assertRaises(ImportError, A) |
|
477 | self.assertRaises(ImportError, A) | |
466 |
|
478 | |||
467 | class A(HasTraits): |
|
479 | class A(HasTraits): | |
468 | klass = Type('rub.adub.Duck') |
|
480 | klass = Type('rub.adub.Duck') | |
469 |
|
481 | |||
470 | self.assertRaises(ImportError, A) |
|
482 | self.assertRaises(ImportError, A) | |
471 |
|
483 | |||
472 | def test_validate_default(self): |
|
484 | def test_validate_default(self): | |
473 |
|
485 | |||
474 | class B(object): pass |
|
486 | class B(object): pass | |
475 | class A(HasTraits): |
|
487 | class A(HasTraits): | |
476 | klass = Type('bad default', B) |
|
488 | klass = Type('bad default', B) | |
477 |
|
489 | |||
478 | self.assertRaises(ImportError, A) |
|
490 | self.assertRaises(ImportError, A) | |
479 |
|
491 | |||
480 | class C(HasTraits): |
|
492 | class C(HasTraits): | |
481 | klass = Type(None, B, allow_none=False) |
|
493 | klass = Type(None, B, allow_none=False) | |
482 |
|
494 | |||
483 | self.assertRaises(TraitError, C) |
|
495 | self.assertRaises(TraitError, C) | |
484 |
|
496 | |||
485 | def test_str_klass(self): |
|
497 | def test_str_klass(self): | |
486 |
|
498 | |||
487 | class A(HasTraits): |
|
499 | class A(HasTraits): | |
488 | klass = Type('IPython.utils.ipstruct.Struct') |
|
500 | klass = Type('IPython.utils.ipstruct.Struct') | |
489 |
|
501 | |||
490 | from IPython.utils.ipstruct import Struct |
|
502 | from IPython.utils.ipstruct import Struct | |
491 | a = A() |
|
503 | a = A() | |
492 | a.klass = Struct |
|
504 | a.klass = Struct | |
493 | self.assertEqual(a.klass, Struct) |
|
505 | self.assertEqual(a.klass, Struct) | |
494 |
|
506 | |||
495 | self.assertRaises(TraitError, setattr, a, 'klass', 10) |
|
507 | self.assertRaises(TraitError, setattr, a, 'klass', 10) | |
496 |
|
508 | |||
497 | class TestInstance(TestCase): |
|
509 | class TestInstance(TestCase): | |
498 |
|
510 | |||
499 | def test_basic(self): |
|
511 | def test_basic(self): | |
500 | class Foo(object): pass |
|
512 | class Foo(object): pass | |
501 | class Bar(Foo): pass |
|
513 | class Bar(Foo): pass | |
502 | class Bah(object): pass |
|
514 | class Bah(object): pass | |
503 |
|
515 | |||
504 | class A(HasTraits): |
|
516 | class A(HasTraits): | |
505 | inst = Instance(Foo) |
|
517 | inst = Instance(Foo) | |
506 |
|
518 | |||
507 | a = A() |
|
519 | a = A() | |
508 | self.assertTrue(a.inst is None) |
|
520 | self.assertTrue(a.inst is None) | |
509 | a.inst = Foo() |
|
521 | a.inst = Foo() | |
510 | self.assertTrue(isinstance(a.inst, Foo)) |
|
522 | self.assertTrue(isinstance(a.inst, Foo)) | |
511 | a.inst = Bar() |
|
523 | a.inst = Bar() | |
512 | self.assertTrue(isinstance(a.inst, Foo)) |
|
524 | self.assertTrue(isinstance(a.inst, Foo)) | |
513 | self.assertRaises(TraitError, setattr, a, 'inst', Foo) |
|
525 | self.assertRaises(TraitError, setattr, a, 'inst', Foo) | |
514 | self.assertRaises(TraitError, setattr, a, 'inst', Bar) |
|
526 | self.assertRaises(TraitError, setattr, a, 'inst', Bar) | |
515 | self.assertRaises(TraitError, setattr, a, 'inst', Bah()) |
|
527 | self.assertRaises(TraitError, setattr, a, 'inst', Bah()) | |
516 |
|
528 | |||
517 | def test_unique_default_value(self): |
|
529 | def test_unique_default_value(self): | |
518 | class Foo(object): pass |
|
530 | class Foo(object): pass | |
519 | class A(HasTraits): |
|
531 | class A(HasTraits): | |
520 | inst = Instance(Foo,(),{}) |
|
532 | inst = Instance(Foo,(),{}) | |
521 |
|
533 | |||
522 | a = A() |
|
534 | a = A() | |
523 | b = A() |
|
535 | b = A() | |
524 | self.assertTrue(a.inst is not b.inst) |
|
536 | self.assertTrue(a.inst is not b.inst) | |
525 |
|
537 | |||
526 | def test_args_kw(self): |
|
538 | def test_args_kw(self): | |
527 | class Foo(object): |
|
539 | class Foo(object): | |
528 | def __init__(self, c): self.c = c |
|
540 | def __init__(self, c): self.c = c | |
529 | class Bar(object): pass |
|
541 | class Bar(object): pass | |
530 | class Bah(object): |
|
542 | class Bah(object): | |
531 | def __init__(self, c, d): |
|
543 | def __init__(self, c, d): | |
532 | self.c = c; self.d = d |
|
544 | self.c = c; self.d = d | |
533 |
|
545 | |||
534 | class A(HasTraits): |
|
546 | class A(HasTraits): | |
535 | inst = Instance(Foo, (10,)) |
|
547 | inst = Instance(Foo, (10,)) | |
536 | a = A() |
|
548 | a = A() | |
537 | self.assertEqual(a.inst.c, 10) |
|
549 | self.assertEqual(a.inst.c, 10) | |
538 |
|
550 | |||
539 | class B(HasTraits): |
|
551 | class B(HasTraits): | |
540 | inst = Instance(Bah, args=(10,), kw=dict(d=20)) |
|
552 | inst = Instance(Bah, args=(10,), kw=dict(d=20)) | |
541 | b = B() |
|
553 | b = B() | |
542 | self.assertEqual(b.inst.c, 10) |
|
554 | self.assertEqual(b.inst.c, 10) | |
543 | self.assertEqual(b.inst.d, 20) |
|
555 | self.assertEqual(b.inst.d, 20) | |
544 |
|
556 | |||
545 | class C(HasTraits): |
|
557 | class C(HasTraits): | |
546 | inst = Instance(Foo) |
|
558 | inst = Instance(Foo) | |
547 | c = C() |
|
559 | c = C() | |
548 | self.assertTrue(c.inst is None) |
|
560 | self.assertTrue(c.inst is None) | |
549 |
|
561 | |||
550 | def test_bad_default(self): |
|
562 | def test_bad_default(self): | |
551 | class Foo(object): pass |
|
563 | class Foo(object): pass | |
552 |
|
564 | |||
553 | class A(HasTraits): |
|
565 | class A(HasTraits): | |
554 | inst = Instance(Foo, allow_none=False) |
|
566 | inst = Instance(Foo, allow_none=False) | |
555 |
|
567 | |||
556 | self.assertRaises(TraitError, A) |
|
568 | self.assertRaises(TraitError, A) | |
557 |
|
569 | |||
558 | def test_instance(self): |
|
570 | def test_instance(self): | |
559 | class Foo(object): pass |
|
571 | class Foo(object): pass | |
560 |
|
572 | |||
561 | def inner(): |
|
573 | def inner(): | |
562 | class A(HasTraits): |
|
574 | class A(HasTraits): | |
563 | inst = Instance(Foo()) |
|
575 | inst = Instance(Foo()) | |
564 |
|
576 | |||
565 | self.assertRaises(TraitError, inner) |
|
577 | self.assertRaises(TraitError, inner) | |
566 |
|
578 | |||
567 |
|
579 | |||
568 | class TestThis(TestCase): |
|
580 | class TestThis(TestCase): | |
569 |
|
581 | |||
570 | def test_this_class(self): |
|
582 | def test_this_class(self): | |
571 | class Foo(HasTraits): |
|
583 | class Foo(HasTraits): | |
572 | this = This |
|
584 | this = This | |
573 |
|
585 | |||
574 | f = Foo() |
|
586 | f = Foo() | |
575 | self.assertEqual(f.this, None) |
|
587 | self.assertEqual(f.this, None) | |
576 | g = Foo() |
|
588 | g = Foo() | |
577 | f.this = g |
|
589 | f.this = g | |
578 | self.assertEqual(f.this, g) |
|
590 | self.assertEqual(f.this, g) | |
579 | self.assertRaises(TraitError, setattr, f, 'this', 10) |
|
591 | self.assertRaises(TraitError, setattr, f, 'this', 10) | |
580 |
|
592 | |||
581 | def test_this_inst(self): |
|
593 | def test_this_inst(self): | |
582 | class Foo(HasTraits): |
|
594 | class Foo(HasTraits): | |
583 | this = This() |
|
595 | this = This() | |
584 |
|
596 | |||
585 | f = Foo() |
|
597 | f = Foo() | |
586 | f.this = Foo() |
|
598 | f.this = Foo() | |
587 | self.assertTrue(isinstance(f.this, Foo)) |
|
599 | self.assertTrue(isinstance(f.this, Foo)) | |
588 |
|
600 | |||
589 | def test_subclass(self): |
|
601 | def test_subclass(self): | |
590 | class Foo(HasTraits): |
|
602 | class Foo(HasTraits): | |
591 | t = This() |
|
603 | t = This() | |
592 | class Bar(Foo): |
|
604 | class Bar(Foo): | |
593 | pass |
|
605 | pass | |
594 | f = Foo() |
|
606 | f = Foo() | |
595 | b = Bar() |
|
607 | b = Bar() | |
596 | f.t = b |
|
608 | f.t = b | |
597 | b.t = f |
|
609 | b.t = f | |
598 | self.assertEqual(f.t, b) |
|
610 | self.assertEqual(f.t, b) | |
599 | self.assertEqual(b.t, f) |
|
611 | self.assertEqual(b.t, f) | |
600 |
|
612 | |||
601 | def test_subclass_override(self): |
|
613 | def test_subclass_override(self): | |
602 | class Foo(HasTraits): |
|
614 | class Foo(HasTraits): | |
603 | t = This() |
|
615 | t = This() | |
604 | class Bar(Foo): |
|
616 | class Bar(Foo): | |
605 | t = This() |
|
617 | t = This() | |
606 | f = Foo() |
|
618 | f = Foo() | |
607 | b = Bar() |
|
619 | b = Bar() | |
608 | f.t = b |
|
620 | f.t = b | |
609 | self.assertEqual(f.t, b) |
|
621 | self.assertEqual(f.t, b) | |
610 | self.assertRaises(TraitError, setattr, b, 't', f) |
|
622 | self.assertRaises(TraitError, setattr, b, 't', f) | |
611 |
|
623 | |||
612 | class TraitTestBase(TestCase): |
|
624 | class TraitTestBase(TestCase): | |
613 | """A best testing class for basic trait types.""" |
|
625 | """A best testing class for basic trait types.""" | |
614 |
|
626 | |||
615 | def assign(self, value): |
|
627 | def assign(self, value): | |
616 | self.obj.value = value |
|
628 | self.obj.value = value | |
617 |
|
629 | |||
618 | def coerce(self, value): |
|
630 | def coerce(self, value): | |
619 | return value |
|
631 | return value | |
620 |
|
632 | |||
621 | def test_good_values(self): |
|
633 | def test_good_values(self): | |
622 | if hasattr(self, '_good_values'): |
|
634 | if hasattr(self, '_good_values'): | |
623 | for value in self._good_values: |
|
635 | for value in self._good_values: | |
624 | self.assign(value) |
|
636 | self.assign(value) | |
625 | self.assertEqual(self.obj.value, self.coerce(value)) |
|
637 | self.assertEqual(self.obj.value, self.coerce(value)) | |
626 |
|
638 | |||
627 | def test_bad_values(self): |
|
639 | def test_bad_values(self): | |
628 | if hasattr(self, '_bad_values'): |
|
640 | if hasattr(self, '_bad_values'): | |
629 | for value in self._bad_values: |
|
641 | for value in self._bad_values: | |
630 | try: |
|
642 | try: | |
631 | self.assertRaises(TraitError, self.assign, value) |
|
643 | self.assertRaises(TraitError, self.assign, value) | |
632 | except AssertionError: |
|
644 | except AssertionError: | |
633 | assert False, value |
|
645 | assert False, value | |
634 |
|
646 | |||
635 | def test_default_value(self): |
|
647 | def test_default_value(self): | |
636 | if hasattr(self, '_default_value'): |
|
648 | if hasattr(self, '_default_value'): | |
637 | self.assertEqual(self._default_value, self.obj.value) |
|
649 | self.assertEqual(self._default_value, self.obj.value) | |
638 |
|
650 | |||
639 | def tearDown(self): |
|
651 | def tearDown(self): | |
640 | # restore default value after tests, if set |
|
652 | # restore default value after tests, if set | |
641 | if hasattr(self, '_default_value'): |
|
653 | if hasattr(self, '_default_value'): | |
642 | self.obj.value = self._default_value |
|
654 | self.obj.value = self._default_value | |
643 |
|
655 | |||
644 |
|
656 | |||
645 | class AnyTrait(HasTraits): |
|
657 | class AnyTrait(HasTraits): | |
646 |
|
658 | |||
647 | value = Any |
|
659 | value = Any | |
648 |
|
660 | |||
649 | class AnyTraitTest(TraitTestBase): |
|
661 | class AnyTraitTest(TraitTestBase): | |
650 |
|
662 | |||
651 | obj = AnyTrait() |
|
663 | obj = AnyTrait() | |
652 |
|
664 | |||
653 | _default_value = None |
|
665 | _default_value = None | |
654 | _good_values = [10.0, 'ten', u'ten', [10], {'ten': 10},(10,), None, 1j] |
|
666 | _good_values = [10.0, 'ten', u'ten', [10], {'ten': 10},(10,), None, 1j] | |
655 | _bad_values = [] |
|
667 | _bad_values = [] | |
656 |
|
668 | |||
657 |
|
669 | |||
658 | class IntTrait(HasTraits): |
|
670 | class IntTrait(HasTraits): | |
659 |
|
671 | |||
660 | value = Int(99) |
|
672 | value = Int(99) | |
661 |
|
673 | |||
662 | class TestInt(TraitTestBase): |
|
674 | class TestInt(TraitTestBase): | |
663 |
|
675 | |||
664 | obj = IntTrait() |
|
676 | obj = IntTrait() | |
665 | _default_value = 99 |
|
677 | _default_value = 99 | |
666 | _good_values = [10, -10] |
|
678 | _good_values = [10, -10] | |
667 | _bad_values = ['ten', u'ten', [10], {'ten': 10},(10,), None, 1j, |
|
679 | _bad_values = ['ten', u'ten', [10], {'ten': 10},(10,), None, 1j, | |
668 | 10.1, -10.1, '10L', '-10L', '10.1', '-10.1', u'10L', |
|
680 | 10.1, -10.1, '10L', '-10L', '10.1', '-10.1', u'10L', | |
669 | u'-10L', u'10.1', u'-10.1', '10', '-10', u'10', u'-10'] |
|
681 | u'-10L', u'10.1', u'-10.1', '10', '-10', u'10', u'-10'] | |
670 | if not py3compat.PY3: |
|
682 | if not py3compat.PY3: | |
671 | _bad_values.extend([10L, -10L, 10*sys.maxint, -10*sys.maxint]) |
|
683 | _bad_values.extend([10L, -10L, 10*sys.maxint, -10*sys.maxint]) | |
672 |
|
684 | |||
673 |
|
685 | |||
674 | class LongTrait(HasTraits): |
|
686 | class LongTrait(HasTraits): | |
675 |
|
687 | |||
676 | value = Long(99L) |
|
688 | value = Long(99L) | |
677 |
|
689 | |||
678 | class TestLong(TraitTestBase): |
|
690 | class TestLong(TraitTestBase): | |
679 |
|
691 | |||
680 | obj = LongTrait() |
|
692 | obj = LongTrait() | |
681 |
|
693 | |||
682 | _default_value = 99L |
|
694 | _default_value = 99L | |
683 | _good_values = [10, -10, 10L, -10L] |
|
695 | _good_values = [10, -10, 10L, -10L] | |
684 | _bad_values = ['ten', u'ten', [10], [10l], {'ten': 10},(10,),(10L,), |
|
696 | _bad_values = ['ten', u'ten', [10], [10l], {'ten': 10},(10,),(10L,), | |
685 | None, 1j, 10.1, -10.1, '10', '-10', '10L', '-10L', '10.1', |
|
697 | None, 1j, 10.1, -10.1, '10', '-10', '10L', '-10L', '10.1', | |
686 | '-10.1', u'10', u'-10', u'10L', u'-10L', u'10.1', |
|
698 | '-10.1', u'10', u'-10', u'10L', u'-10L', u'10.1', | |
687 | u'-10.1'] |
|
699 | u'-10.1'] | |
688 | if not py3compat.PY3: |
|
700 | if not py3compat.PY3: | |
689 | # maxint undefined on py3, because int == long |
|
701 | # maxint undefined on py3, because int == long | |
690 | _good_values.extend([10*sys.maxint, -10*sys.maxint]) |
|
702 | _good_values.extend([10*sys.maxint, -10*sys.maxint]) | |
691 |
|
703 | |||
692 | @skipif(py3compat.PY3, "not relevant on py3") |
|
704 | @skipif(py3compat.PY3, "not relevant on py3") | |
693 | def test_cast_small(self): |
|
705 | def test_cast_small(self): | |
694 | """Long casts ints to long""" |
|
706 | """Long casts ints to long""" | |
695 | self.obj.value = 10 |
|
707 | self.obj.value = 10 | |
696 | self.assertEqual(type(self.obj.value), long) |
|
708 | self.assertEqual(type(self.obj.value), long) | |
697 |
|
709 | |||
698 |
|
710 | |||
699 | class IntegerTrait(HasTraits): |
|
711 | class IntegerTrait(HasTraits): | |
700 | value = Integer(1) |
|
712 | value = Integer(1) | |
701 |
|
713 | |||
702 | class TestInteger(TestLong): |
|
714 | class TestInteger(TestLong): | |
703 | obj = IntegerTrait() |
|
715 | obj = IntegerTrait() | |
704 | _default_value = 1 |
|
716 | _default_value = 1 | |
705 |
|
717 | |||
706 | def coerce(self, n): |
|
718 | def coerce(self, n): | |
707 | return int(n) |
|
719 | return int(n) | |
708 |
|
720 | |||
709 | @skipif(py3compat.PY3, "not relevant on py3") |
|
721 | @skipif(py3compat.PY3, "not relevant on py3") | |
710 | def test_cast_small(self): |
|
722 | def test_cast_small(self): | |
711 | """Integer casts small longs to int""" |
|
723 | """Integer casts small longs to int""" | |
712 | if py3compat.PY3: |
|
724 | if py3compat.PY3: | |
713 | raise SkipTest("not relevant on py3") |
|
725 | raise SkipTest("not relevant on py3") | |
714 |
|
726 | |||
715 | self.obj.value = 100L |
|
727 | self.obj.value = 100L | |
716 | self.assertEqual(type(self.obj.value), int) |
|
728 | self.assertEqual(type(self.obj.value), int) | |
717 |
|
729 | |||
718 |
|
730 | |||
719 | class FloatTrait(HasTraits): |
|
731 | class FloatTrait(HasTraits): | |
720 |
|
732 | |||
721 | value = Float(99.0) |
|
733 | value = Float(99.0) | |
722 |
|
734 | |||
723 | class TestFloat(TraitTestBase): |
|
735 | class TestFloat(TraitTestBase): | |
724 |
|
736 | |||
725 | obj = FloatTrait() |
|
737 | obj = FloatTrait() | |
726 |
|
738 | |||
727 | _default_value = 99.0 |
|
739 | _default_value = 99.0 | |
728 | _good_values = [10, -10, 10.1, -10.1] |
|
740 | _good_values = [10, -10, 10.1, -10.1] | |
729 | _bad_values = ['ten', u'ten', [10], {'ten': 10},(10,), None, |
|
741 | _bad_values = ['ten', u'ten', [10], {'ten': 10},(10,), None, | |
730 | 1j, '10', '-10', '10L', '-10L', '10.1', '-10.1', u'10', |
|
742 | 1j, '10', '-10', '10L', '-10L', '10.1', '-10.1', u'10', | |
731 | u'-10', u'10L', u'-10L', u'10.1', u'-10.1'] |
|
743 | u'-10', u'10L', u'-10L', u'10.1', u'-10.1'] | |
732 | if not py3compat.PY3: |
|
744 | if not py3compat.PY3: | |
733 | _bad_values.extend([10L, -10L]) |
|
745 | _bad_values.extend([10L, -10L]) | |
734 |
|
746 | |||
735 |
|
747 | |||
736 | class ComplexTrait(HasTraits): |
|
748 | class ComplexTrait(HasTraits): | |
737 |
|
749 | |||
738 | value = Complex(99.0-99.0j) |
|
750 | value = Complex(99.0-99.0j) | |
739 |
|
751 | |||
740 | class TestComplex(TraitTestBase): |
|
752 | class TestComplex(TraitTestBase): | |
741 |
|
753 | |||
742 | obj = ComplexTrait() |
|
754 | obj = ComplexTrait() | |
743 |
|
755 | |||
744 | _default_value = 99.0-99.0j |
|
756 | _default_value = 99.0-99.0j | |
745 | _good_values = [10, -10, 10.1, -10.1, 10j, 10+10j, 10-10j, |
|
757 | _good_values = [10, -10, 10.1, -10.1, 10j, 10+10j, 10-10j, | |
746 | 10.1j, 10.1+10.1j, 10.1-10.1j] |
|
758 | 10.1j, 10.1+10.1j, 10.1-10.1j] | |
747 | _bad_values = [u'10L', u'-10L', 'ten', [10], {'ten': 10},(10,), None] |
|
759 | _bad_values = [u'10L', u'-10L', 'ten', [10], {'ten': 10},(10,), None] | |
748 | if not py3compat.PY3: |
|
760 | if not py3compat.PY3: | |
749 | _bad_values.extend([10L, -10L]) |
|
761 | _bad_values.extend([10L, -10L]) | |
750 |
|
762 | |||
751 |
|
763 | |||
752 | class BytesTrait(HasTraits): |
|
764 | class BytesTrait(HasTraits): | |
753 |
|
765 | |||
754 | value = Bytes(b'string') |
|
766 | value = Bytes(b'string') | |
755 |
|
767 | |||
756 | class TestBytes(TraitTestBase): |
|
768 | class TestBytes(TraitTestBase): | |
757 |
|
769 | |||
758 | obj = BytesTrait() |
|
770 | obj = BytesTrait() | |
759 |
|
771 | |||
760 | _default_value = b'string' |
|
772 | _default_value = b'string' | |
761 | _good_values = [b'10', b'-10', b'10L', |
|
773 | _good_values = [b'10', b'-10', b'10L', | |
762 | b'-10L', b'10.1', b'-10.1', b'string'] |
|
774 | b'-10L', b'10.1', b'-10.1', b'string'] | |
763 | _bad_values = [10, -10, 10L, -10L, 10.1, -10.1, 1j, [10], |
|
775 | _bad_values = [10, -10, 10L, -10L, 10.1, -10.1, 1j, [10], | |
764 | ['ten'],{'ten': 10},(10,), None, u'string'] |
|
776 | ['ten'],{'ten': 10},(10,), None, u'string'] | |
765 |
|
777 | |||
766 |
|
778 | |||
767 | class UnicodeTrait(HasTraits): |
|
779 | class UnicodeTrait(HasTraits): | |
768 |
|
780 | |||
769 | value = Unicode(u'unicode') |
|
781 | value = Unicode(u'unicode') | |
770 |
|
782 | |||
771 | class TestUnicode(TraitTestBase): |
|
783 | class TestUnicode(TraitTestBase): | |
772 |
|
784 | |||
773 | obj = UnicodeTrait() |
|
785 | obj = UnicodeTrait() | |
774 |
|
786 | |||
775 | _default_value = u'unicode' |
|
787 | _default_value = u'unicode' | |
776 | _good_values = ['10', '-10', '10L', '-10L', '10.1', |
|
788 | _good_values = ['10', '-10', '10L', '-10L', '10.1', | |
777 | '-10.1', '', u'', 'string', u'string', u"β¬"] |
|
789 | '-10.1', '', u'', 'string', u'string', u"β¬"] | |
778 | _bad_values = [10, -10, 10L, -10L, 10.1, -10.1, 1j, |
|
790 | _bad_values = [10, -10, 10L, -10L, 10.1, -10.1, 1j, | |
779 | [10], ['ten'], [u'ten'], {'ten': 10},(10,), None] |
|
791 | [10], ['ten'], [u'ten'], {'ten': 10},(10,), None] | |
780 |
|
792 | |||
781 |
|
793 | |||
782 | class ObjectNameTrait(HasTraits): |
|
794 | class ObjectNameTrait(HasTraits): | |
783 | value = ObjectName("abc") |
|
795 | value = ObjectName("abc") | |
784 |
|
796 | |||
785 | class TestObjectName(TraitTestBase): |
|
797 | class TestObjectName(TraitTestBase): | |
786 | obj = ObjectNameTrait() |
|
798 | obj = ObjectNameTrait() | |
787 |
|
799 | |||
788 | _default_value = "abc" |
|
800 | _default_value = "abc" | |
789 | _good_values = ["a", "gh", "g9", "g_", "_G", u"a345_"] |
|
801 | _good_values = ["a", "gh", "g9", "g_", "_G", u"a345_"] | |
790 | _bad_values = [1, "", u"β¬", "9g", "!", "#abc", "aj@", "a.b", "a()", "a[0]", |
|
802 | _bad_values = [1, "", u"β¬", "9g", "!", "#abc", "aj@", "a.b", "a()", "a[0]", | |
791 | object(), object] |
|
803 | object(), object] | |
792 | if sys.version_info[0] < 3: |
|
804 | if sys.version_info[0] < 3: | |
793 | _bad_values.append(u"ΓΎ") |
|
805 | _bad_values.append(u"ΓΎ") | |
794 | else: |
|
806 | else: | |
795 | _good_values.append(u"ΓΎ") # ΓΎ=1 is valid in Python 3 (PEP 3131). |
|
807 | _good_values.append(u"ΓΎ") # ΓΎ=1 is valid in Python 3 (PEP 3131). | |
796 |
|
808 | |||
797 |
|
809 | |||
798 | class DottedObjectNameTrait(HasTraits): |
|
810 | class DottedObjectNameTrait(HasTraits): | |
799 | value = DottedObjectName("a.b") |
|
811 | value = DottedObjectName("a.b") | |
800 |
|
812 | |||
801 | class TestDottedObjectName(TraitTestBase): |
|
813 | class TestDottedObjectName(TraitTestBase): | |
802 | obj = DottedObjectNameTrait() |
|
814 | obj = DottedObjectNameTrait() | |
803 |
|
815 | |||
804 | _default_value = "a.b" |
|
816 | _default_value = "a.b" | |
805 | _good_values = ["A", "y.t", "y765.__repr__", "os.path.join", u"os.path.join"] |
|
817 | _good_values = ["A", "y.t", "y765.__repr__", "os.path.join", u"os.path.join"] | |
806 | _bad_values = [1, u"abc.β¬", "_.@", ".", ".abc", "abc.", ".abc."] |
|
818 | _bad_values = [1, u"abc.β¬", "_.@", ".", ".abc", "abc.", ".abc."] | |
807 | if sys.version_info[0] < 3: |
|
819 | if sys.version_info[0] < 3: | |
808 | _bad_values.append(u"t.ΓΎ") |
|
820 | _bad_values.append(u"t.ΓΎ") | |
809 | else: |
|
821 | else: | |
810 | _good_values.append(u"t.ΓΎ") |
|
822 | _good_values.append(u"t.ΓΎ") | |
811 |
|
823 | |||
812 |
|
824 | |||
813 | class TCPAddressTrait(HasTraits): |
|
825 | class TCPAddressTrait(HasTraits): | |
814 |
|
826 | |||
815 | value = TCPAddress() |
|
827 | value = TCPAddress() | |
816 |
|
828 | |||
817 | class TestTCPAddress(TraitTestBase): |
|
829 | class TestTCPAddress(TraitTestBase): | |
818 |
|
830 | |||
819 | obj = TCPAddressTrait() |
|
831 | obj = TCPAddressTrait() | |
820 |
|
832 | |||
821 | _default_value = ('127.0.0.1',0) |
|
833 | _default_value = ('127.0.0.1',0) | |
822 | _good_values = [('localhost',0),('192.168.0.1',1000),('www.google.com',80)] |
|
834 | _good_values = [('localhost',0),('192.168.0.1',1000),('www.google.com',80)] | |
823 | _bad_values = [(0,0),('localhost',10.0),('localhost',-1)] |
|
835 | _bad_values = [(0,0),('localhost',10.0),('localhost',-1)] | |
824 |
|
836 | |||
825 | class ListTrait(HasTraits): |
|
837 | class ListTrait(HasTraits): | |
826 |
|
838 | |||
827 | value = List(Int) |
|
839 | value = List(Int) | |
828 |
|
840 | |||
829 | class TestList(TraitTestBase): |
|
841 | class TestList(TraitTestBase): | |
830 |
|
842 | |||
831 | obj = ListTrait() |
|
843 | obj = ListTrait() | |
832 |
|
844 | |||
833 | _default_value = [] |
|
845 | _default_value = [] | |
834 | _good_values = [[], [1], range(10)] |
|
846 | _good_values = [[], [1], range(10)] | |
835 | _bad_values = [10, [1,'a'], 'a', (1,2)] |
|
847 | _bad_values = [10, [1,'a'], 'a', (1,2)] | |
836 |
|
848 | |||
837 | class LenListTrait(HasTraits): |
|
849 | class LenListTrait(HasTraits): | |
838 |
|
850 | |||
839 | value = List(Int, [0], minlen=1, maxlen=2) |
|
851 | value = List(Int, [0], minlen=1, maxlen=2) | |
840 |
|
852 | |||
841 | class TestLenList(TraitTestBase): |
|
853 | class TestLenList(TraitTestBase): | |
842 |
|
854 | |||
843 | obj = LenListTrait() |
|
855 | obj = LenListTrait() | |
844 |
|
856 | |||
845 | _default_value = [0] |
|
857 | _default_value = [0] | |
846 | _good_values = [[1], range(2)] |
|
858 | _good_values = [[1], range(2)] | |
847 | _bad_values = [10, [1,'a'], 'a', (1,2), [], range(3)] |
|
859 | _bad_values = [10, [1,'a'], 'a', (1,2), [], range(3)] | |
848 |
|
860 | |||
849 | class TupleTrait(HasTraits): |
|
861 | class TupleTrait(HasTraits): | |
850 |
|
862 | |||
851 | value = Tuple(Int) |
|
863 | value = Tuple(Int) | |
852 |
|
864 | |||
853 | class TestTupleTrait(TraitTestBase): |
|
865 | class TestTupleTrait(TraitTestBase): | |
854 |
|
866 | |||
855 | obj = TupleTrait() |
|
867 | obj = TupleTrait() | |
856 |
|
868 | |||
857 | _default_value = None |
|
869 | _default_value = None | |
858 | _good_values = [(1,), None,(0,)] |
|
870 | _good_values = [(1,), None,(0,)] | |
859 | _bad_values = [10, (1,2), [1],('a'), ()] |
|
871 | _bad_values = [10, (1,2), [1],('a'), ()] | |
860 |
|
872 | |||
861 | def test_invalid_args(self): |
|
873 | def test_invalid_args(self): | |
862 | self.assertRaises(TypeError, Tuple, 5) |
|
874 | self.assertRaises(TypeError, Tuple, 5) | |
863 | self.assertRaises(TypeError, Tuple, default_value='hello') |
|
875 | self.assertRaises(TypeError, Tuple, default_value='hello') | |
864 | t = Tuple(Int, CBytes, default_value=(1,5)) |
|
876 | t = Tuple(Int, CBytes, default_value=(1,5)) | |
865 |
|
877 | |||
866 | class LooseTupleTrait(HasTraits): |
|
878 | class LooseTupleTrait(HasTraits): | |
867 |
|
879 | |||
868 | value = Tuple((1,2,3)) |
|
880 | value = Tuple((1,2,3)) | |
869 |
|
881 | |||
870 | class TestLooseTupleTrait(TraitTestBase): |
|
882 | class TestLooseTupleTrait(TraitTestBase): | |
871 |
|
883 | |||
872 | obj = LooseTupleTrait() |
|
884 | obj = LooseTupleTrait() | |
873 |
|
885 | |||
874 | _default_value = (1,2,3) |
|
886 | _default_value = (1,2,3) | |
875 | _good_values = [(1,), None, (0,), tuple(range(5)), tuple('hello'), ('a',5), ()] |
|
887 | _good_values = [(1,), None, (0,), tuple(range(5)), tuple('hello'), ('a',5), ()] | |
876 | _bad_values = [10, 'hello', [1], []] |
|
888 | _bad_values = [10, 'hello', [1], []] | |
877 |
|
889 | |||
878 | def test_invalid_args(self): |
|
890 | def test_invalid_args(self): | |
879 | self.assertRaises(TypeError, Tuple, 5) |
|
891 | self.assertRaises(TypeError, Tuple, 5) | |
880 | self.assertRaises(TypeError, Tuple, default_value='hello') |
|
892 | self.assertRaises(TypeError, Tuple, default_value='hello') | |
881 | t = Tuple(Int, CBytes, default_value=(1,5)) |
|
893 | t = Tuple(Int, CBytes, default_value=(1,5)) | |
882 |
|
894 | |||
883 |
|
895 | |||
884 | class MultiTupleTrait(HasTraits): |
|
896 | class MultiTupleTrait(HasTraits): | |
885 |
|
897 | |||
886 | value = Tuple(Int, Bytes, default_value=[99,b'bottles']) |
|
898 | value = Tuple(Int, Bytes, default_value=[99,b'bottles']) | |
887 |
|
899 | |||
888 | class TestMultiTuple(TraitTestBase): |
|
900 | class TestMultiTuple(TraitTestBase): | |
889 |
|
901 | |||
890 | obj = MultiTupleTrait() |
|
902 | obj = MultiTupleTrait() | |
891 |
|
903 | |||
892 | _default_value = (99,b'bottles') |
|
904 | _default_value = (99,b'bottles') | |
893 | _good_values = [(1,b'a'), (2,b'b')] |
|
905 | _good_values = [(1,b'a'), (2,b'b')] | |
894 | _bad_values = ((),10, b'a', (1,b'a',3), (b'a',1), (1, u'a')) |
|
906 | _bad_values = ((),10, b'a', (1,b'a',3), (b'a',1), (1, u'a')) | |
895 |
|
907 | |||
896 | class CRegExpTrait(HasTraits): |
|
908 | class CRegExpTrait(HasTraits): | |
897 |
|
909 | |||
898 | value = CRegExp(r'') |
|
910 | value = CRegExp(r'') | |
899 |
|
911 | |||
900 | class TestCRegExp(TraitTestBase): |
|
912 | class TestCRegExp(TraitTestBase): | |
901 |
|
913 | |||
902 | def coerce(self, value): |
|
914 | def coerce(self, value): | |
903 | return re.compile(value) |
|
915 | return re.compile(value) | |
904 |
|
916 | |||
905 | obj = CRegExpTrait() |
|
917 | obj = CRegExpTrait() | |
906 |
|
918 | |||
907 | _default_value = re.compile(r'') |
|
919 | _default_value = re.compile(r'') | |
908 | _good_values = [r'\d+', re.compile(r'\d+')] |
|
920 | _good_values = [r'\d+', re.compile(r'\d+')] | |
909 | _bad_values = [r'(', None, ()] |
|
921 | _bad_values = [r'(', None, ()] | |
910 |
|
922 | |||
911 | class DictTrait(HasTraits): |
|
923 | class DictTrait(HasTraits): | |
912 | value = Dict() |
|
924 | value = Dict() | |
913 |
|
925 | |||
914 | def test_dict_assignment(): |
|
926 | def test_dict_assignment(): | |
915 | d = dict() |
|
927 | d = dict() | |
916 | c = DictTrait() |
|
928 | c = DictTrait() | |
917 | c.value = d |
|
929 | c.value = d | |
918 | d['a'] = 5 |
|
930 | d['a'] = 5 | |
919 | nt.assert_equal(d, c.value) |
|
931 | nt.assert_equal(d, c.value) | |
920 | nt.assert_true(c.value is d) |
|
932 | nt.assert_true(c.value is d) |
General Comments 0
You need to be logged in to leave comments.
Login now