Show More
@@ -1,975 +1,1033 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(list(a._trait_dyn_inits.keys()), ['x']) |
|
151 | self.assertEqual(list(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(list(a._trait_dyn_inits.keys()), ['x']) |
|
156 | self.assertEqual(list(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(list(a._trait_dyn_inits.keys()), ['x']) |
|
160 | self.assertEqual(list(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(list(a._trait_dyn_inits.keys()), ['x']) |
|
168 | self.assertEqual(list(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 | def test_notify_only_once(self): |
|
366 | def test_notify_only_once(self): | |
367 |
|
367 | |||
368 | class A(HasTraits): |
|
368 | class A(HasTraits): | |
369 | listen_to = ['a'] |
|
369 | listen_to = ['a'] | |
370 |
|
370 | |||
371 | a = Int(0) |
|
371 | a = Int(0) | |
372 | b = 0 |
|
372 | b = 0 | |
373 |
|
373 | |||
374 | def __init__(self, **kwargs): |
|
374 | def __init__(self, **kwargs): | |
375 | super(A, self).__init__(**kwargs) |
|
375 | super(A, self).__init__(**kwargs) | |
376 | self.on_trait_change(self.listener1, ['a']) |
|
376 | self.on_trait_change(self.listener1, ['a']) | |
377 |
|
377 | |||
378 | def listener1(self, name, old, new): |
|
378 | def listener1(self, name, old, new): | |
379 | self.b += 1 |
|
379 | self.b += 1 | |
380 |
|
380 | |||
381 | class B(A): |
|
381 | class B(A): | |
382 |
|
382 | |||
383 | c = 0 |
|
383 | c = 0 | |
384 | d = 0 |
|
384 | d = 0 | |
385 |
|
385 | |||
386 | def __init__(self, **kwargs): |
|
386 | def __init__(self, **kwargs): | |
387 | super(B, self).__init__(**kwargs) |
|
387 | super(B, self).__init__(**kwargs) | |
388 | self.on_trait_change(self.listener2) |
|
388 | self.on_trait_change(self.listener2) | |
389 |
|
389 | |||
390 | def listener2(self, name, old, new): |
|
390 | def listener2(self, name, old, new): | |
391 | self.c += 1 |
|
391 | self.c += 1 | |
392 |
|
392 | |||
393 | def _a_changed(self, name, old, new): |
|
393 | def _a_changed(self, name, old, new): | |
394 | self.d += 1 |
|
394 | self.d += 1 | |
395 |
|
395 | |||
396 | b = B() |
|
396 | b = B() | |
397 | b.a += 1 |
|
397 | b.a += 1 | |
398 | self.assertEqual(b.b, b.c) |
|
398 | self.assertEqual(b.b, b.c) | |
399 | self.assertEqual(b.b, b.d) |
|
399 | self.assertEqual(b.b, b.d) | |
400 | b.a += 1 |
|
400 | b.a += 1 | |
401 | self.assertEqual(b.b, b.c) |
|
401 | self.assertEqual(b.b, b.c) | |
402 | self.assertEqual(b.b, b.d) |
|
402 | self.assertEqual(b.b, b.d) | |
403 |
|
403 | |||
404 |
|
404 | |||
405 | class TestHasTraits(TestCase): |
|
405 | class TestHasTraits(TestCase): | |
406 |
|
406 | |||
407 | def test_trait_names(self): |
|
407 | def test_trait_names(self): | |
408 | class A(HasTraits): |
|
408 | class A(HasTraits): | |
409 | i = Int |
|
409 | i = Int | |
410 | f = Float |
|
410 | f = Float | |
411 | a = A() |
|
411 | a = A() | |
412 | self.assertEqual(sorted(a.trait_names()),['f','i']) |
|
412 | self.assertEqual(sorted(a.trait_names()),['f','i']) | |
413 | self.assertEqual(sorted(A.class_trait_names()),['f','i']) |
|
413 | self.assertEqual(sorted(A.class_trait_names()),['f','i']) | |
414 |
|
414 | |||
415 | def test_trait_metadata(self): |
|
415 | def test_trait_metadata(self): | |
416 | class A(HasTraits): |
|
416 | class A(HasTraits): | |
417 | i = Int(config_key='MY_VALUE') |
|
417 | i = Int(config_key='MY_VALUE') | |
418 | a = A() |
|
418 | a = A() | |
419 | self.assertEqual(a.trait_metadata('i','config_key'), 'MY_VALUE') |
|
419 | self.assertEqual(a.trait_metadata('i','config_key'), 'MY_VALUE') | |
420 |
|
420 | |||
421 | def test_traits(self): |
|
421 | def test_traits(self): | |
422 | class A(HasTraits): |
|
422 | class A(HasTraits): | |
423 | i = Int |
|
423 | i = Int | |
424 | f = Float |
|
424 | f = Float | |
425 | a = A() |
|
425 | a = A() | |
426 | self.assertEqual(a.traits(), dict(i=A.i, f=A.f)) |
|
426 | self.assertEqual(a.traits(), dict(i=A.i, f=A.f)) | |
427 | self.assertEqual(A.class_traits(), dict(i=A.i, f=A.f)) |
|
427 | self.assertEqual(A.class_traits(), dict(i=A.i, f=A.f)) | |
428 |
|
428 | |||
429 | def test_traits_metadata(self): |
|
429 | def test_traits_metadata(self): | |
430 | class A(HasTraits): |
|
430 | class A(HasTraits): | |
431 | i = Int(config_key='VALUE1', other_thing='VALUE2') |
|
431 | i = Int(config_key='VALUE1', other_thing='VALUE2') | |
432 | f = Float(config_key='VALUE3', other_thing='VALUE2') |
|
432 | f = Float(config_key='VALUE3', other_thing='VALUE2') | |
433 | j = Int(0) |
|
433 | j = Int(0) | |
434 | a = A() |
|
434 | a = A() | |
435 | self.assertEqual(a.traits(), dict(i=A.i, f=A.f, j=A.j)) |
|
435 | self.assertEqual(a.traits(), dict(i=A.i, f=A.f, j=A.j)) | |
436 | traits = a.traits(config_key='VALUE1', other_thing='VALUE2') |
|
436 | traits = a.traits(config_key='VALUE1', other_thing='VALUE2') | |
437 | self.assertEqual(traits, dict(i=A.i)) |
|
437 | self.assertEqual(traits, dict(i=A.i)) | |
438 |
|
438 | |||
439 | # This passes, but it shouldn't because I am replicating a bug in |
|
439 | # This passes, but it shouldn't because I am replicating a bug in | |
440 | # traits. |
|
440 | # traits. | |
441 | traits = a.traits(config_key=lambda v: True) |
|
441 | traits = a.traits(config_key=lambda v: True) | |
442 | self.assertEqual(traits, dict(i=A.i, f=A.f, j=A.j)) |
|
442 | self.assertEqual(traits, dict(i=A.i, f=A.f, j=A.j)) | |
443 |
|
443 | |||
444 | def test_init(self): |
|
444 | def test_init(self): | |
445 | class A(HasTraits): |
|
445 | class A(HasTraits): | |
446 | i = Int() |
|
446 | i = Int() | |
447 | x = Float() |
|
447 | x = Float() | |
448 | a = A(i=1, x=10.0) |
|
448 | a = A(i=1, x=10.0) | |
449 | self.assertEqual(a.i, 1) |
|
449 | self.assertEqual(a.i, 1) | |
450 | self.assertEqual(a.x, 10.0) |
|
450 | self.assertEqual(a.x, 10.0) | |
451 |
|
451 | |||
452 | def test_positional_args(self): |
|
452 | def test_positional_args(self): | |
453 | class A(HasTraits): |
|
453 | class A(HasTraits): | |
454 | i = Int(0) |
|
454 | i = Int(0) | |
455 | def __init__(self, i): |
|
455 | def __init__(self, i): | |
456 | super(A, self).__init__() |
|
456 | super(A, self).__init__() | |
457 | self.i = i |
|
457 | self.i = i | |
458 |
|
458 | |||
459 | a = A(5) |
|
459 | a = A(5) | |
460 | self.assertEqual(a.i, 5) |
|
460 | self.assertEqual(a.i, 5) | |
461 | # should raise TypeError if no positional arg given |
|
461 | # should raise TypeError if no positional arg given | |
462 | self.assertRaises(TypeError, A) |
|
462 | self.assertRaises(TypeError, A) | |
463 |
|
463 | |||
464 | #----------------------------------------------------------------------------- |
|
464 | #----------------------------------------------------------------------------- | |
465 | # Tests for specific trait types |
|
465 | # Tests for specific trait types | |
466 | #----------------------------------------------------------------------------- |
|
466 | #----------------------------------------------------------------------------- | |
467 |
|
467 | |||
468 |
|
468 | |||
469 | class TestType(TestCase): |
|
469 | class TestType(TestCase): | |
470 |
|
470 | |||
471 | def test_default(self): |
|
471 | def test_default(self): | |
472 |
|
472 | |||
473 | class B(object): pass |
|
473 | class B(object): pass | |
474 | class A(HasTraits): |
|
474 | class A(HasTraits): | |
475 | klass = Type |
|
475 | klass = Type | |
476 |
|
476 | |||
477 | a = A() |
|
477 | a = A() | |
478 | self.assertEqual(a.klass, None) |
|
478 | self.assertEqual(a.klass, None) | |
479 |
|
479 | |||
480 | a.klass = B |
|
480 | a.klass = B | |
481 | self.assertEqual(a.klass, B) |
|
481 | self.assertEqual(a.klass, B) | |
482 | self.assertRaises(TraitError, setattr, a, 'klass', 10) |
|
482 | self.assertRaises(TraitError, setattr, a, 'klass', 10) | |
483 |
|
483 | |||
484 | def test_value(self): |
|
484 | def test_value(self): | |
485 |
|
485 | |||
486 | class B(object): pass |
|
486 | class B(object): pass | |
487 | class C(object): pass |
|
487 | class C(object): pass | |
488 | class A(HasTraits): |
|
488 | class A(HasTraits): | |
489 | klass = Type(B) |
|
489 | klass = Type(B) | |
490 |
|
490 | |||
491 | a = A() |
|
491 | a = A() | |
492 | self.assertEqual(a.klass, B) |
|
492 | self.assertEqual(a.klass, B) | |
493 | self.assertRaises(TraitError, setattr, a, 'klass', C) |
|
493 | self.assertRaises(TraitError, setattr, a, 'klass', C) | |
494 | self.assertRaises(TraitError, setattr, a, 'klass', object) |
|
494 | self.assertRaises(TraitError, setattr, a, 'klass', object) | |
495 | a.klass = B |
|
495 | a.klass = B | |
496 |
|
496 | |||
497 | def test_allow_none(self): |
|
497 | def test_allow_none(self): | |
498 |
|
498 | |||
499 | class B(object): pass |
|
499 | class B(object): pass | |
500 | class C(B): pass |
|
500 | class C(B): pass | |
501 | class A(HasTraits): |
|
501 | class A(HasTraits): | |
502 | klass = Type(B, allow_none=False) |
|
502 | klass = Type(B, allow_none=False) | |
503 |
|
503 | |||
504 | a = A() |
|
504 | a = A() | |
505 | self.assertEqual(a.klass, B) |
|
505 | self.assertEqual(a.klass, B) | |
506 | self.assertRaises(TraitError, setattr, a, 'klass', None) |
|
506 | self.assertRaises(TraitError, setattr, a, 'klass', None) | |
507 | a.klass = C |
|
507 | a.klass = C | |
508 | self.assertEqual(a.klass, C) |
|
508 | self.assertEqual(a.klass, C) | |
509 |
|
509 | |||
510 | def test_validate_klass(self): |
|
510 | def test_validate_klass(self): | |
511 |
|
511 | |||
512 | class A(HasTraits): |
|
512 | class A(HasTraits): | |
513 | klass = Type('no strings allowed') |
|
513 | klass = Type('no strings allowed') | |
514 |
|
514 | |||
515 | self.assertRaises(ImportError, A) |
|
515 | self.assertRaises(ImportError, A) | |
516 |
|
516 | |||
517 | class A(HasTraits): |
|
517 | class A(HasTraits): | |
518 | klass = Type('rub.adub.Duck') |
|
518 | klass = Type('rub.adub.Duck') | |
519 |
|
519 | |||
520 | self.assertRaises(ImportError, A) |
|
520 | self.assertRaises(ImportError, A) | |
521 |
|
521 | |||
522 | def test_validate_default(self): |
|
522 | def test_validate_default(self): | |
523 |
|
523 | |||
524 | class B(object): pass |
|
524 | class B(object): pass | |
525 | class A(HasTraits): |
|
525 | class A(HasTraits): | |
526 | klass = Type('bad default', B) |
|
526 | klass = Type('bad default', B) | |
527 |
|
527 | |||
528 | self.assertRaises(ImportError, A) |
|
528 | self.assertRaises(ImportError, A) | |
529 |
|
529 | |||
530 | class C(HasTraits): |
|
530 | class C(HasTraits): | |
531 | klass = Type(None, B, allow_none=False) |
|
531 | klass = Type(None, B, allow_none=False) | |
532 |
|
532 | |||
533 | self.assertRaises(TraitError, C) |
|
533 | self.assertRaises(TraitError, C) | |
534 |
|
534 | |||
535 | def test_str_klass(self): |
|
535 | def test_str_klass(self): | |
536 |
|
536 | |||
537 | class A(HasTraits): |
|
537 | class A(HasTraits): | |
538 | klass = Type('IPython.utils.ipstruct.Struct') |
|
538 | klass = Type('IPython.utils.ipstruct.Struct') | |
539 |
|
539 | |||
540 | from IPython.utils.ipstruct import Struct |
|
540 | from IPython.utils.ipstruct import Struct | |
541 | a = A() |
|
541 | a = A() | |
542 | a.klass = Struct |
|
542 | a.klass = Struct | |
543 | self.assertEqual(a.klass, Struct) |
|
543 | self.assertEqual(a.klass, Struct) | |
544 |
|
544 | |||
545 | self.assertRaises(TraitError, setattr, a, 'klass', 10) |
|
545 | self.assertRaises(TraitError, setattr, a, 'klass', 10) | |
546 |
|
546 | |||
547 | class TestInstance(TestCase): |
|
547 | class TestInstance(TestCase): | |
548 |
|
548 | |||
549 | def test_basic(self): |
|
549 | def test_basic(self): | |
550 | class Foo(object): pass |
|
550 | class Foo(object): pass | |
551 | class Bar(Foo): pass |
|
551 | class Bar(Foo): pass | |
552 | class Bah(object): pass |
|
552 | class Bah(object): pass | |
553 |
|
553 | |||
554 | class A(HasTraits): |
|
554 | class A(HasTraits): | |
555 | inst = Instance(Foo) |
|
555 | inst = Instance(Foo) | |
556 |
|
556 | |||
557 | a = A() |
|
557 | a = A() | |
558 | self.assertTrue(a.inst is None) |
|
558 | self.assertTrue(a.inst is None) | |
559 | a.inst = Foo() |
|
559 | a.inst = Foo() | |
560 | self.assertTrue(isinstance(a.inst, Foo)) |
|
560 | self.assertTrue(isinstance(a.inst, Foo)) | |
561 | a.inst = Bar() |
|
561 | a.inst = Bar() | |
562 | self.assertTrue(isinstance(a.inst, Foo)) |
|
562 | self.assertTrue(isinstance(a.inst, Foo)) | |
563 | self.assertRaises(TraitError, setattr, a, 'inst', Foo) |
|
563 | self.assertRaises(TraitError, setattr, a, 'inst', Foo) | |
564 | self.assertRaises(TraitError, setattr, a, 'inst', Bar) |
|
564 | self.assertRaises(TraitError, setattr, a, 'inst', Bar) | |
565 | self.assertRaises(TraitError, setattr, a, 'inst', Bah()) |
|
565 | self.assertRaises(TraitError, setattr, a, 'inst', Bah()) | |
566 |
|
566 | |||
567 | def test_unique_default_value(self): |
|
567 | def test_unique_default_value(self): | |
568 | class Foo(object): pass |
|
568 | class Foo(object): pass | |
569 | class A(HasTraits): |
|
569 | class A(HasTraits): | |
570 | inst = Instance(Foo,(),{}) |
|
570 | inst = Instance(Foo,(),{}) | |
571 |
|
571 | |||
572 | a = A() |
|
572 | a = A() | |
573 | b = A() |
|
573 | b = A() | |
574 | self.assertTrue(a.inst is not b.inst) |
|
574 | self.assertTrue(a.inst is not b.inst) | |
575 |
|
575 | |||
576 | def test_args_kw(self): |
|
576 | def test_args_kw(self): | |
577 | class Foo(object): |
|
577 | class Foo(object): | |
578 | def __init__(self, c): self.c = c |
|
578 | def __init__(self, c): self.c = c | |
579 | class Bar(object): pass |
|
579 | class Bar(object): pass | |
580 | class Bah(object): |
|
580 | class Bah(object): | |
581 | def __init__(self, c, d): |
|
581 | def __init__(self, c, d): | |
582 | self.c = c; self.d = d |
|
582 | self.c = c; self.d = d | |
583 |
|
583 | |||
584 | class A(HasTraits): |
|
584 | class A(HasTraits): | |
585 | inst = Instance(Foo, (10,)) |
|
585 | inst = Instance(Foo, (10,)) | |
586 | a = A() |
|
586 | a = A() | |
587 | self.assertEqual(a.inst.c, 10) |
|
587 | self.assertEqual(a.inst.c, 10) | |
588 |
|
588 | |||
589 | class B(HasTraits): |
|
589 | class B(HasTraits): | |
590 | inst = Instance(Bah, args=(10,), kw=dict(d=20)) |
|
590 | inst = Instance(Bah, args=(10,), kw=dict(d=20)) | |
591 | b = B() |
|
591 | b = B() | |
592 | self.assertEqual(b.inst.c, 10) |
|
592 | self.assertEqual(b.inst.c, 10) | |
593 | self.assertEqual(b.inst.d, 20) |
|
593 | self.assertEqual(b.inst.d, 20) | |
594 |
|
594 | |||
595 | class C(HasTraits): |
|
595 | class C(HasTraits): | |
596 | inst = Instance(Foo) |
|
596 | inst = Instance(Foo) | |
597 | c = C() |
|
597 | c = C() | |
598 | self.assertTrue(c.inst is None) |
|
598 | self.assertTrue(c.inst is None) | |
599 |
|
599 | |||
600 | def test_bad_default(self): |
|
600 | def test_bad_default(self): | |
601 | class Foo(object): pass |
|
601 | class Foo(object): pass | |
602 |
|
602 | |||
603 | class A(HasTraits): |
|
603 | class A(HasTraits): | |
604 | inst = Instance(Foo, allow_none=False) |
|
604 | inst = Instance(Foo, allow_none=False) | |
605 |
|
605 | |||
606 | self.assertRaises(TraitError, A) |
|
606 | self.assertRaises(TraitError, A) | |
607 |
|
607 | |||
608 | def test_instance(self): |
|
608 | def test_instance(self): | |
609 | class Foo(object): pass |
|
609 | class Foo(object): pass | |
610 |
|
610 | |||
611 | def inner(): |
|
611 | def inner(): | |
612 | class A(HasTraits): |
|
612 | class A(HasTraits): | |
613 | inst = Instance(Foo()) |
|
613 | inst = Instance(Foo()) | |
614 |
|
614 | |||
615 | self.assertRaises(TraitError, inner) |
|
615 | self.assertRaises(TraitError, inner) | |
616 |
|
616 | |||
617 |
|
617 | |||
618 | class TestThis(TestCase): |
|
618 | class TestThis(TestCase): | |
619 |
|
619 | |||
620 | def test_this_class(self): |
|
620 | def test_this_class(self): | |
621 | class Foo(HasTraits): |
|
621 | class Foo(HasTraits): | |
622 | this = This |
|
622 | this = This | |
623 |
|
623 | |||
624 | f = Foo() |
|
624 | f = Foo() | |
625 | self.assertEqual(f.this, None) |
|
625 | self.assertEqual(f.this, None) | |
626 | g = Foo() |
|
626 | g = Foo() | |
627 | f.this = g |
|
627 | f.this = g | |
628 | self.assertEqual(f.this, g) |
|
628 | self.assertEqual(f.this, g) | |
629 | self.assertRaises(TraitError, setattr, f, 'this', 10) |
|
629 | self.assertRaises(TraitError, setattr, f, 'this', 10) | |
630 |
|
630 | |||
631 | def test_this_inst(self): |
|
631 | def test_this_inst(self): | |
632 | class Foo(HasTraits): |
|
632 | class Foo(HasTraits): | |
633 | this = This() |
|
633 | this = This() | |
634 |
|
634 | |||
635 | f = Foo() |
|
635 | f = Foo() | |
636 | f.this = Foo() |
|
636 | f.this = Foo() | |
637 | self.assertTrue(isinstance(f.this, Foo)) |
|
637 | self.assertTrue(isinstance(f.this, Foo)) | |
638 |
|
638 | |||
639 | def test_subclass(self): |
|
639 | def test_subclass(self): | |
640 | class Foo(HasTraits): |
|
640 | class Foo(HasTraits): | |
641 | t = This() |
|
641 | t = This() | |
642 | class Bar(Foo): |
|
642 | class Bar(Foo): | |
643 | pass |
|
643 | pass | |
644 | f = Foo() |
|
644 | f = Foo() | |
645 | b = Bar() |
|
645 | b = Bar() | |
646 | f.t = b |
|
646 | f.t = b | |
647 | b.t = f |
|
647 | b.t = f | |
648 | self.assertEqual(f.t, b) |
|
648 | self.assertEqual(f.t, b) | |
649 | self.assertEqual(b.t, f) |
|
649 | self.assertEqual(b.t, f) | |
650 |
|
650 | |||
651 | def test_subclass_override(self): |
|
651 | def test_subclass_override(self): | |
652 | class Foo(HasTraits): |
|
652 | class Foo(HasTraits): | |
653 | t = This() |
|
653 | t = This() | |
654 | class Bar(Foo): |
|
654 | class Bar(Foo): | |
655 | t = This() |
|
655 | t = This() | |
656 | f = Foo() |
|
656 | f = Foo() | |
657 | b = Bar() |
|
657 | b = Bar() | |
658 | f.t = b |
|
658 | f.t = b | |
659 | self.assertEqual(f.t, b) |
|
659 | self.assertEqual(f.t, b) | |
660 | self.assertRaises(TraitError, setattr, b, 't', f) |
|
660 | self.assertRaises(TraitError, setattr, b, 't', f) | |
661 |
|
661 | |||
662 | class TraitTestBase(TestCase): |
|
662 | class TraitTestBase(TestCase): | |
663 | """A best testing class for basic trait types.""" |
|
663 | """A best testing class for basic trait types.""" | |
664 |
|
664 | |||
665 | def assign(self, value): |
|
665 | def assign(self, value): | |
666 | self.obj.value = value |
|
666 | self.obj.value = value | |
667 |
|
667 | |||
668 | def coerce(self, value): |
|
668 | def coerce(self, value): | |
669 | return value |
|
669 | return value | |
670 |
|
670 | |||
671 | def test_good_values(self): |
|
671 | def test_good_values(self): | |
672 | if hasattr(self, '_good_values'): |
|
672 | if hasattr(self, '_good_values'): | |
673 | for value in self._good_values: |
|
673 | for value in self._good_values: | |
674 | self.assign(value) |
|
674 | self.assign(value) | |
675 | self.assertEqual(self.obj.value, self.coerce(value)) |
|
675 | self.assertEqual(self.obj.value, self.coerce(value)) | |
676 |
|
676 | |||
677 | def test_bad_values(self): |
|
677 | def test_bad_values(self): | |
678 | if hasattr(self, '_bad_values'): |
|
678 | if hasattr(self, '_bad_values'): | |
679 | for value in self._bad_values: |
|
679 | for value in self._bad_values: | |
680 | try: |
|
680 | try: | |
681 | self.assertRaises(TraitError, self.assign, value) |
|
681 | self.assertRaises(TraitError, self.assign, value) | |
682 | except AssertionError: |
|
682 | except AssertionError: | |
683 | assert False, value |
|
683 | assert False, value | |
684 |
|
684 | |||
685 | def test_default_value(self): |
|
685 | def test_default_value(self): | |
686 | if hasattr(self, '_default_value'): |
|
686 | if hasattr(self, '_default_value'): | |
687 | self.assertEqual(self._default_value, self.obj.value) |
|
687 | self.assertEqual(self._default_value, self.obj.value) | |
688 |
|
688 | |||
689 | def tearDown(self): |
|
689 | def tearDown(self): | |
690 | # restore default value after tests, if set |
|
690 | # restore default value after tests, if set | |
691 | if hasattr(self, '_default_value'): |
|
691 | if hasattr(self, '_default_value'): | |
692 | self.obj.value = self._default_value |
|
692 | self.obj.value = self._default_value | |
693 |
|
693 | |||
694 |
|
694 | |||
695 | class AnyTrait(HasTraits): |
|
695 | class AnyTrait(HasTraits): | |
696 |
|
696 | |||
697 | value = Any |
|
697 | value = Any | |
698 |
|
698 | |||
699 | class AnyTraitTest(TraitTestBase): |
|
699 | class AnyTraitTest(TraitTestBase): | |
700 |
|
700 | |||
701 | obj = AnyTrait() |
|
701 | obj = AnyTrait() | |
702 |
|
702 | |||
703 | _default_value = None |
|
703 | _default_value = None | |
704 | _good_values = [10.0, 'ten', u'ten', [10], {'ten': 10},(10,), None, 1j] |
|
704 | _good_values = [10.0, 'ten', u'ten', [10], {'ten': 10},(10,), None, 1j] | |
705 | _bad_values = [] |
|
705 | _bad_values = [] | |
706 |
|
706 | |||
707 |
|
707 | |||
708 | class IntTrait(HasTraits): |
|
708 | class IntTrait(HasTraits): | |
709 |
|
709 | |||
710 | value = Int(99) |
|
710 | value = Int(99) | |
711 |
|
711 | |||
712 | class TestInt(TraitTestBase): |
|
712 | class TestInt(TraitTestBase): | |
713 |
|
713 | |||
714 | obj = IntTrait() |
|
714 | obj = IntTrait() | |
715 | _default_value = 99 |
|
715 | _default_value = 99 | |
716 | _good_values = [10, -10] |
|
716 | _good_values = [10, -10] | |
717 | _bad_values = ['ten', u'ten', [10], {'ten': 10},(10,), None, 1j, |
|
717 | _bad_values = ['ten', u'ten', [10], {'ten': 10},(10,), None, 1j, | |
718 | 10.1, -10.1, '10L', '-10L', '10.1', '-10.1', u'10L', |
|
718 | 10.1, -10.1, '10L', '-10L', '10.1', '-10.1', u'10L', | |
719 | u'-10L', u'10.1', u'-10.1', '10', '-10', u'10', u'-10'] |
|
719 | u'-10L', u'10.1', u'-10.1', '10', '-10', u'10', u'-10'] | |
720 | if not py3compat.PY3: |
|
720 | if not py3compat.PY3: | |
721 | _bad_values.extend([long(10), long(-10), 10*sys.maxint, -10*sys.maxint]) |
|
721 | _bad_values.extend([long(10), long(-10), 10*sys.maxint, -10*sys.maxint]) | |
722 |
|
722 | |||
723 |
|
723 | |||
724 | class LongTrait(HasTraits): |
|
724 | class LongTrait(HasTraits): | |
725 |
|
725 | |||
726 | value = Long(99 if py3compat.PY3 else long(99)) |
|
726 | value = Long(99 if py3compat.PY3 else long(99)) | |
727 |
|
727 | |||
728 | class TestLong(TraitTestBase): |
|
728 | class TestLong(TraitTestBase): | |
729 |
|
729 | |||
730 | obj = LongTrait() |
|
730 | obj = LongTrait() | |
731 |
|
731 | |||
732 | _default_value = 99 if py3compat.PY3 else long(99) |
|
732 | _default_value = 99 if py3compat.PY3 else long(99) | |
733 | _good_values = [10, -10] |
|
733 | _good_values = [10, -10] | |
734 | _bad_values = ['ten', u'ten', [10], {'ten': 10},(10,), |
|
734 | _bad_values = ['ten', u'ten', [10], {'ten': 10},(10,), | |
735 | None, 1j, 10.1, -10.1, '10', '-10', '10L', '-10L', '10.1', |
|
735 | None, 1j, 10.1, -10.1, '10', '-10', '10L', '-10L', '10.1', | |
736 | '-10.1', u'10', u'-10', u'10L', u'-10L', u'10.1', |
|
736 | '-10.1', u'10', u'-10', u'10L', u'-10L', u'10.1', | |
737 | u'-10.1'] |
|
737 | u'-10.1'] | |
738 | if not py3compat.PY3: |
|
738 | if not py3compat.PY3: | |
739 | # maxint undefined on py3, because int == long |
|
739 | # maxint undefined on py3, because int == long | |
740 | _good_values.extend([long(10), long(-10), 10*sys.maxint, -10*sys.maxint]) |
|
740 | _good_values.extend([long(10), long(-10), 10*sys.maxint, -10*sys.maxint]) | |
741 | _bad_values.extend([[long(10)], (long(10),)]) |
|
741 | _bad_values.extend([[long(10)], (long(10),)]) | |
742 |
|
742 | |||
743 | @skipif(py3compat.PY3, "not relevant on py3") |
|
743 | @skipif(py3compat.PY3, "not relevant on py3") | |
744 | def test_cast_small(self): |
|
744 | def test_cast_small(self): | |
745 | """Long casts ints to long""" |
|
745 | """Long casts ints to long""" | |
746 | self.obj.value = 10 |
|
746 | self.obj.value = 10 | |
747 | self.assertEqual(type(self.obj.value), long) |
|
747 | self.assertEqual(type(self.obj.value), long) | |
748 |
|
748 | |||
749 |
|
749 | |||
750 | class IntegerTrait(HasTraits): |
|
750 | class IntegerTrait(HasTraits): | |
751 | value = Integer(1) |
|
751 | value = Integer(1) | |
752 |
|
752 | |||
753 | class TestInteger(TestLong): |
|
753 | class TestInteger(TestLong): | |
754 | obj = IntegerTrait() |
|
754 | obj = IntegerTrait() | |
755 | _default_value = 1 |
|
755 | _default_value = 1 | |
756 |
|
756 | |||
757 | def coerce(self, n): |
|
757 | def coerce(self, n): | |
758 | return int(n) |
|
758 | return int(n) | |
759 |
|
759 | |||
760 | @skipif(py3compat.PY3, "not relevant on py3") |
|
760 | @skipif(py3compat.PY3, "not relevant on py3") | |
761 | def test_cast_small(self): |
|
761 | def test_cast_small(self): | |
762 | """Integer casts small longs to int""" |
|
762 | """Integer casts small longs to int""" | |
763 | if py3compat.PY3: |
|
763 | if py3compat.PY3: | |
764 | raise SkipTest("not relevant on py3") |
|
764 | raise SkipTest("not relevant on py3") | |
765 |
|
765 | |||
766 | self.obj.value = long(100) |
|
766 | self.obj.value = long(100) | |
767 | self.assertEqual(type(self.obj.value), int) |
|
767 | self.assertEqual(type(self.obj.value), int) | |
768 |
|
768 | |||
769 |
|
769 | |||
770 | class FloatTrait(HasTraits): |
|
770 | class FloatTrait(HasTraits): | |
771 |
|
771 | |||
772 | value = Float(99.0) |
|
772 | value = Float(99.0) | |
773 |
|
773 | |||
774 | class TestFloat(TraitTestBase): |
|
774 | class TestFloat(TraitTestBase): | |
775 |
|
775 | |||
776 | obj = FloatTrait() |
|
776 | obj = FloatTrait() | |
777 |
|
777 | |||
778 | _default_value = 99.0 |
|
778 | _default_value = 99.0 | |
779 | _good_values = [10, -10, 10.1, -10.1] |
|
779 | _good_values = [10, -10, 10.1, -10.1] | |
780 | _bad_values = ['ten', u'ten', [10], {'ten': 10},(10,), None, |
|
780 | _bad_values = ['ten', u'ten', [10], {'ten': 10},(10,), None, | |
781 | 1j, '10', '-10', '10L', '-10L', '10.1', '-10.1', u'10', |
|
781 | 1j, '10', '-10', '10L', '-10L', '10.1', '-10.1', u'10', | |
782 | u'-10', u'10L', u'-10L', u'10.1', u'-10.1'] |
|
782 | u'-10', u'10L', u'-10L', u'10.1', u'-10.1'] | |
783 | if not py3compat.PY3: |
|
783 | if not py3compat.PY3: | |
784 | _bad_values.extend([long(10), long(-10)]) |
|
784 | _bad_values.extend([long(10), long(-10)]) | |
785 |
|
785 | |||
786 |
|
786 | |||
787 | class ComplexTrait(HasTraits): |
|
787 | class ComplexTrait(HasTraits): | |
788 |
|
788 | |||
789 | value = Complex(99.0-99.0j) |
|
789 | value = Complex(99.0-99.0j) | |
790 |
|
790 | |||
791 | class TestComplex(TraitTestBase): |
|
791 | class TestComplex(TraitTestBase): | |
792 |
|
792 | |||
793 | obj = ComplexTrait() |
|
793 | obj = ComplexTrait() | |
794 |
|
794 | |||
795 | _default_value = 99.0-99.0j |
|
795 | _default_value = 99.0-99.0j | |
796 | _good_values = [10, -10, 10.1, -10.1, 10j, 10+10j, 10-10j, |
|
796 | _good_values = [10, -10, 10.1, -10.1, 10j, 10+10j, 10-10j, | |
797 | 10.1j, 10.1+10.1j, 10.1-10.1j] |
|
797 | 10.1j, 10.1+10.1j, 10.1-10.1j] | |
798 | _bad_values = [u'10L', u'-10L', 'ten', [10], {'ten': 10},(10,), None] |
|
798 | _bad_values = [u'10L', u'-10L', 'ten', [10], {'ten': 10},(10,), None] | |
799 | if not py3compat.PY3: |
|
799 | if not py3compat.PY3: | |
800 | _bad_values.extend([long(10), long(-10)]) |
|
800 | _bad_values.extend([long(10), long(-10)]) | |
801 |
|
801 | |||
802 |
|
802 | |||
803 | class BytesTrait(HasTraits): |
|
803 | class BytesTrait(HasTraits): | |
804 |
|
804 | |||
805 | value = Bytes(b'string') |
|
805 | value = Bytes(b'string') | |
806 |
|
806 | |||
807 | class TestBytes(TraitTestBase): |
|
807 | class TestBytes(TraitTestBase): | |
808 |
|
808 | |||
809 | obj = BytesTrait() |
|
809 | obj = BytesTrait() | |
810 |
|
810 | |||
811 | _default_value = b'string' |
|
811 | _default_value = b'string' | |
812 | _good_values = [b'10', b'-10', b'10L', |
|
812 | _good_values = [b'10', b'-10', b'10L', | |
813 | b'-10L', b'10.1', b'-10.1', b'string'] |
|
813 | b'-10L', b'10.1', b'-10.1', b'string'] | |
814 | _bad_values = [10, -10, 10.1, -10.1, 1j, [10], |
|
814 | _bad_values = [10, -10, 10.1, -10.1, 1j, [10], | |
815 | ['ten'],{'ten': 10},(10,), None, u'string'] |
|
815 | ['ten'],{'ten': 10},(10,), None, u'string'] | |
816 | if not py3compat.PY3: |
|
816 | if not py3compat.PY3: | |
817 | _bad_values.extend([long(10), long(-10)]) |
|
817 | _bad_values.extend([long(10), long(-10)]) | |
818 |
|
818 | |||
819 |
|
819 | |||
820 | class UnicodeTrait(HasTraits): |
|
820 | class UnicodeTrait(HasTraits): | |
821 |
|
821 | |||
822 | value = Unicode(u'unicode') |
|
822 | value = Unicode(u'unicode') | |
823 |
|
823 | |||
824 | class TestUnicode(TraitTestBase): |
|
824 | class TestUnicode(TraitTestBase): | |
825 |
|
825 | |||
826 | obj = UnicodeTrait() |
|
826 | obj = UnicodeTrait() | |
827 |
|
827 | |||
828 | _default_value = u'unicode' |
|
828 | _default_value = u'unicode' | |
829 | _good_values = ['10', '-10', '10L', '-10L', '10.1', |
|
829 | _good_values = ['10', '-10', '10L', '-10L', '10.1', | |
830 | '-10.1', '', u'', 'string', u'string', u"β¬"] |
|
830 | '-10.1', '', u'', 'string', u'string', u"β¬"] | |
831 | _bad_values = [10, -10, 10.1, -10.1, 1j, |
|
831 | _bad_values = [10, -10, 10.1, -10.1, 1j, | |
832 | [10], ['ten'], [u'ten'], {'ten': 10},(10,), None] |
|
832 | [10], ['ten'], [u'ten'], {'ten': 10},(10,), None] | |
833 | if not py3compat.PY3: |
|
833 | if not py3compat.PY3: | |
834 | _bad_values.extend([long(10), long(-10)]) |
|
834 | _bad_values.extend([long(10), long(-10)]) | |
835 |
|
835 | |||
836 |
|
836 | |||
837 | class ObjectNameTrait(HasTraits): |
|
837 | class ObjectNameTrait(HasTraits): | |
838 | value = ObjectName("abc") |
|
838 | value = ObjectName("abc") | |
839 |
|
839 | |||
840 | class TestObjectName(TraitTestBase): |
|
840 | class TestObjectName(TraitTestBase): | |
841 | obj = ObjectNameTrait() |
|
841 | obj = ObjectNameTrait() | |
842 |
|
842 | |||
843 | _default_value = "abc" |
|
843 | _default_value = "abc" | |
844 | _good_values = ["a", "gh", "g9", "g_", "_G", u"a345_"] |
|
844 | _good_values = ["a", "gh", "g9", "g_", "_G", u"a345_"] | |
845 | _bad_values = [1, "", u"β¬", "9g", "!", "#abc", "aj@", "a.b", "a()", "a[0]", |
|
845 | _bad_values = [1, "", u"β¬", "9g", "!", "#abc", "aj@", "a.b", "a()", "a[0]", | |
846 | object(), object] |
|
846 | object(), object] | |
847 | if sys.version_info[0] < 3: |
|
847 | if sys.version_info[0] < 3: | |
848 | _bad_values.append(u"ΓΎ") |
|
848 | _bad_values.append(u"ΓΎ") | |
849 | else: |
|
849 | else: | |
850 | _good_values.append(u"ΓΎ") # ΓΎ=1 is valid in Python 3 (PEP 3131). |
|
850 | _good_values.append(u"ΓΎ") # ΓΎ=1 is valid in Python 3 (PEP 3131). | |
851 |
|
851 | |||
852 |
|
852 | |||
853 | class DottedObjectNameTrait(HasTraits): |
|
853 | class DottedObjectNameTrait(HasTraits): | |
854 | value = DottedObjectName("a.b") |
|
854 | value = DottedObjectName("a.b") | |
855 |
|
855 | |||
856 | class TestDottedObjectName(TraitTestBase): |
|
856 | class TestDottedObjectName(TraitTestBase): | |
857 | obj = DottedObjectNameTrait() |
|
857 | obj = DottedObjectNameTrait() | |
858 |
|
858 | |||
859 | _default_value = "a.b" |
|
859 | _default_value = "a.b" | |
860 | _good_values = ["A", "y.t", "y765.__repr__", "os.path.join", u"os.path.join"] |
|
860 | _good_values = ["A", "y.t", "y765.__repr__", "os.path.join", u"os.path.join"] | |
861 | _bad_values = [1, u"abc.β¬", "_.@", ".", ".abc", "abc.", ".abc."] |
|
861 | _bad_values = [1, u"abc.β¬", "_.@", ".", ".abc", "abc.", ".abc."] | |
862 | if sys.version_info[0] < 3: |
|
862 | if sys.version_info[0] < 3: | |
863 | _bad_values.append(u"t.ΓΎ") |
|
863 | _bad_values.append(u"t.ΓΎ") | |
864 | else: |
|
864 | else: | |
865 | _good_values.append(u"t.ΓΎ") |
|
865 | _good_values.append(u"t.ΓΎ") | |
866 |
|
866 | |||
867 |
|
867 | |||
868 | class TCPAddressTrait(HasTraits): |
|
868 | class TCPAddressTrait(HasTraits): | |
869 |
|
869 | |||
870 | value = TCPAddress() |
|
870 | value = TCPAddress() | |
871 |
|
871 | |||
872 | class TestTCPAddress(TraitTestBase): |
|
872 | class TestTCPAddress(TraitTestBase): | |
873 |
|
873 | |||
874 | obj = TCPAddressTrait() |
|
874 | obj = TCPAddressTrait() | |
875 |
|
875 | |||
876 | _default_value = ('127.0.0.1',0) |
|
876 | _default_value = ('127.0.0.1',0) | |
877 | _good_values = [('localhost',0),('192.168.0.1',1000),('www.google.com',80)] |
|
877 | _good_values = [('localhost',0),('192.168.0.1',1000),('www.google.com',80)] | |
878 | _bad_values = [(0,0),('localhost',10.0),('localhost',-1)] |
|
878 | _bad_values = [(0,0),('localhost',10.0),('localhost',-1)] | |
879 |
|
879 | |||
880 | class ListTrait(HasTraits): |
|
880 | class ListTrait(HasTraits): | |
881 |
|
881 | |||
882 | value = List(Int) |
|
882 | value = List(Int) | |
883 |
|
883 | |||
884 | class TestList(TraitTestBase): |
|
884 | class TestList(TraitTestBase): | |
885 |
|
885 | |||
886 | obj = ListTrait() |
|
886 | obj = ListTrait() | |
887 |
|
887 | |||
888 | _default_value = [] |
|
888 | _default_value = [] | |
889 | _good_values = [[], [1], list(range(10))] |
|
889 | _good_values = [[], [1], list(range(10))] | |
890 | _bad_values = [10, [1,'a'], 'a', (1,2)] |
|
890 | _bad_values = [10, [1,'a'], 'a', (1,2)] | |
891 |
|
891 | |||
892 | class LenListTrait(HasTraits): |
|
892 | class LenListTrait(HasTraits): | |
893 |
|
893 | |||
894 | value = List(Int, [0], minlen=1, maxlen=2) |
|
894 | value = List(Int, [0], minlen=1, maxlen=2) | |
895 |
|
895 | |||
896 | class TestLenList(TraitTestBase): |
|
896 | class TestLenList(TraitTestBase): | |
897 |
|
897 | |||
898 | obj = LenListTrait() |
|
898 | obj = LenListTrait() | |
899 |
|
899 | |||
900 | _default_value = [0] |
|
900 | _default_value = [0] | |
901 | _good_values = [[1], list(range(2))] |
|
901 | _good_values = [[1], list(range(2))] | |
902 | _bad_values = [10, [1,'a'], 'a', (1,2), [], list(range(3))] |
|
902 | _bad_values = [10, [1,'a'], 'a', (1,2), [], list(range(3))] | |
903 |
|
903 | |||
904 | class TupleTrait(HasTraits): |
|
904 | class TupleTrait(HasTraits): | |
905 |
|
905 | |||
906 | value = Tuple(Int) |
|
906 | value = Tuple(Int) | |
907 |
|
907 | |||
908 | class TestTupleTrait(TraitTestBase): |
|
908 | class TestTupleTrait(TraitTestBase): | |
909 |
|
909 | |||
910 | obj = TupleTrait() |
|
910 | obj = TupleTrait() | |
911 |
|
911 | |||
912 | _default_value = None |
|
912 | _default_value = None | |
913 | _good_values = [(1,), None,(0,)] |
|
913 | _good_values = [(1,), None,(0,)] | |
914 | _bad_values = [10, (1,2), [1],('a'), ()] |
|
914 | _bad_values = [10, (1,2), [1],('a'), ()] | |
915 |
|
915 | |||
916 | def test_invalid_args(self): |
|
916 | def test_invalid_args(self): | |
917 | self.assertRaises(TypeError, Tuple, 5) |
|
917 | self.assertRaises(TypeError, Tuple, 5) | |
918 | self.assertRaises(TypeError, Tuple, default_value='hello') |
|
918 | self.assertRaises(TypeError, Tuple, default_value='hello') | |
919 | t = Tuple(Int, CBytes, default_value=(1,5)) |
|
919 | t = Tuple(Int, CBytes, default_value=(1,5)) | |
920 |
|
920 | |||
921 | class LooseTupleTrait(HasTraits): |
|
921 | class LooseTupleTrait(HasTraits): | |
922 |
|
922 | |||
923 | value = Tuple((1,2,3)) |
|
923 | value = Tuple((1,2,3)) | |
924 |
|
924 | |||
925 | class TestLooseTupleTrait(TraitTestBase): |
|
925 | class TestLooseTupleTrait(TraitTestBase): | |
926 |
|
926 | |||
927 | obj = LooseTupleTrait() |
|
927 | obj = LooseTupleTrait() | |
928 |
|
928 | |||
929 | _default_value = (1,2,3) |
|
929 | _default_value = (1,2,3) | |
930 | _good_values = [(1,), None, (0,), tuple(range(5)), tuple('hello'), ('a',5), ()] |
|
930 | _good_values = [(1,), None, (0,), tuple(range(5)), tuple('hello'), ('a',5), ()] | |
931 | _bad_values = [10, 'hello', [1], []] |
|
931 | _bad_values = [10, 'hello', [1], []] | |
932 |
|
932 | |||
933 | def test_invalid_args(self): |
|
933 | def test_invalid_args(self): | |
934 | self.assertRaises(TypeError, Tuple, 5) |
|
934 | self.assertRaises(TypeError, Tuple, 5) | |
935 | self.assertRaises(TypeError, Tuple, default_value='hello') |
|
935 | self.assertRaises(TypeError, Tuple, default_value='hello') | |
936 | t = Tuple(Int, CBytes, default_value=(1,5)) |
|
936 | t = Tuple(Int, CBytes, default_value=(1,5)) | |
937 |
|
937 | |||
938 |
|
938 | |||
939 | class MultiTupleTrait(HasTraits): |
|
939 | class MultiTupleTrait(HasTraits): | |
940 |
|
940 | |||
941 | value = Tuple(Int, Bytes, default_value=[99,b'bottles']) |
|
941 | value = Tuple(Int, Bytes, default_value=[99,b'bottles']) | |
942 |
|
942 | |||
943 | class TestMultiTuple(TraitTestBase): |
|
943 | class TestMultiTuple(TraitTestBase): | |
944 |
|
944 | |||
945 | obj = MultiTupleTrait() |
|
945 | obj = MultiTupleTrait() | |
946 |
|
946 | |||
947 | _default_value = (99,b'bottles') |
|
947 | _default_value = (99,b'bottles') | |
948 | _good_values = [(1,b'a'), (2,b'b')] |
|
948 | _good_values = [(1,b'a'), (2,b'b')] | |
949 | _bad_values = ((),10, b'a', (1,b'a',3), (b'a',1), (1, u'a')) |
|
949 | _bad_values = ((),10, b'a', (1,b'a',3), (b'a',1), (1, u'a')) | |
950 |
|
950 | |||
951 | class CRegExpTrait(HasTraits): |
|
951 | class CRegExpTrait(HasTraits): | |
952 |
|
952 | |||
953 | value = CRegExp(r'') |
|
953 | value = CRegExp(r'') | |
954 |
|
954 | |||
955 | class TestCRegExp(TraitTestBase): |
|
955 | class TestCRegExp(TraitTestBase): | |
956 |
|
956 | |||
957 | def coerce(self, value): |
|
957 | def coerce(self, value): | |
958 | return re.compile(value) |
|
958 | return re.compile(value) | |
959 |
|
959 | |||
960 | obj = CRegExpTrait() |
|
960 | obj = CRegExpTrait() | |
961 |
|
961 | |||
962 | _default_value = re.compile(r'') |
|
962 | _default_value = re.compile(r'') | |
963 | _good_values = [r'\d+', re.compile(r'\d+')] |
|
963 | _good_values = [r'\d+', re.compile(r'\d+')] | |
964 | _bad_values = [r'(', None, ()] |
|
964 | _bad_values = [r'(', None, ()] | |
965 |
|
965 | |||
966 | class DictTrait(HasTraits): |
|
966 | class DictTrait(HasTraits): | |
967 | value = Dict() |
|
967 | value = Dict() | |
968 |
|
968 | |||
969 | def test_dict_assignment(): |
|
969 | def test_dict_assignment(): | |
970 | d = dict() |
|
970 | d = dict() | |
971 | c = DictTrait() |
|
971 | c = DictTrait() | |
972 | c.value = d |
|
972 | c.value = d | |
973 | d['a'] = 5 |
|
973 | d['a'] = 5 | |
974 | nt.assert_equal(d, c.value) |
|
974 | nt.assert_equal(d, c.value) | |
975 | nt.assert_true(c.value is d) |
|
975 | nt.assert_true(c.value is d) | |
|
976 | ||||
|
977 | def test_connect_same: | |||
|
978 | """Verify two traitlets of the same type can be bound together using Connect""" | |||
|
979 | ||||
|
980 | # Create two simple classes with Int traitlets. | |||
|
981 | class A(): | |||
|
982 | value = Int() | |||
|
983 | a = A(value=9) | |||
|
984 | b = A(value=8) | |||
|
985 | ||||
|
986 | # Conenct the two classes. | |||
|
987 | c = Connect((a, 'value'), (b, 'value')) | |||
|
988 | ||||
|
989 | # Make sure the values are the same at the point of connection. | |||
|
990 | assertEqual(a.value, b.value) | |||
|
991 | ||||
|
992 | # Change one of the values to make sure they stay in sync. | |||
|
993 | a.value = 5 | |||
|
994 | assertEqual(a.value, b.value) | |||
|
995 | ||||
|
996 | def test_connect_different: | |||
|
997 | """Verify two traitlets of different types can be bound together using Connect""" | |||
|
998 | ||||
|
999 | # Create two simple classes with Int traitlets. | |||
|
1000 | class A(): | |||
|
1001 | value = Int() | |||
|
1002 | class B(): | |||
|
1003 | count = Int() | |||
|
1004 | a = A(value=9) | |||
|
1005 | b = B(count=8) | |||
|
1006 | ||||
|
1007 | # Conenct the two classes. | |||
|
1008 | c = Connect((a, 'value'), (b, 'count')) | |||
|
1009 | ||||
|
1010 | # Make sure the values are the same at the point of connection. | |||
|
1011 | assertEqual(a.value, b.count) | |||
|
1012 | ||||
|
1013 | # Change one of the values to make sure they stay in sync. | |||
|
1014 | a.value = 5 | |||
|
1015 | assertEqual(a.value, b.count) | |||
|
1016 | ||||
|
1017 | def test_disconnect: | |||
|
1018 | """Verify two connected traitlets can be disconnected""" | |||
|
1019 | ||||
|
1020 | # Create two simple classes with Int traitlets. | |||
|
1021 | class A(): | |||
|
1022 | value = Int() | |||
|
1023 | a = A(value=9) | |||
|
1024 | b = A(value=8) | |||
|
1025 | ||||
|
1026 | # Conenct the two classes. | |||
|
1027 | c = Connect((a, 'value'), (b, 'value')) | |||
|
1028 | a.value = 4 | |||
|
1029 | c.disconnect() | |||
|
1030 | ||||
|
1031 | # Change one of the values to make sure they stay in sync. | |||
|
1032 | a.value = 5 | |||
|
1033 | assertNotEqual(a.value, b.value) No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now