##// END OF EJS Templates
avoid copious warnings in interact tests...
MinRK -
Show More
@@ -1,599 +1,596 b''
1 """Test interact and interactive."""
1 """Test interact and interactive."""
2
2
3 #-----------------------------------------------------------------------------
3 # Copyright (c) IPython Development Team.
4 # Copyright (C) 2014 The IPython Development Team
4 # Distributed under the terms of the Modified BSD License.
5 #
6 # Distributed under the terms of the BSD License. The full license is in
7 # the file COPYING, distributed as part of this software.
8 #-----------------------------------------------------------------------------
9
10 #-----------------------------------------------------------------------------
11 # Imports
12 #-----------------------------------------------------------------------------
13
5
14 from __future__ import print_function
6 from __future__ import print_function
15
7
16 from collections import OrderedDict
8 from collections import OrderedDict
17
9
18 import nose.tools as nt
10 import nose.tools as nt
19 import IPython.testing.tools as tt
11 import IPython.testing.tools as tt
20
12
21 # from IPython.core.getipython import get_ipython
13 from IPython.kernel.comm import Comm
22 from IPython.html import widgets
14 from IPython.html import widgets
23 from IPython.html.widgets import interact, interactive, Widget, interaction
15 from IPython.html.widgets import interact, interactive, Widget, interaction
24 from IPython.utils.py3compat import annotate
16 from IPython.utils.py3compat import annotate
25
17
26 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
27 # Utility stuff
19 # Utility stuff
28 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
29
21
30 class DummyComm(object):
22 class DummyComm(Comm):
31 comm_id = 'a-b-c-d'
23 comm_id = 'a-b-c-d'
24
32 def send(self, *args, **kwargs):
25 def send(self, *args, **kwargs):
33 pass
26 pass
34
27
35 def close(self, *args, **kwargs):
28 def close(self, *args, **kwargs):
36 pass
29 pass
37
30
38 _widget_attrs = {}
31 _widget_attrs = {}
39 displayed = []
32 displayed = []
33 undefined = object()
40
34
41 def setup():
35 def setup():
42 _widget_attrs['comm'] = Widget.comm
36 _widget_attrs['_comm_default'] = getattr(Widget, '_comm_default', undefined)
43 Widget.comm = DummyComm()
37 Widget._comm_default = lambda self: DummyComm()
44 _widget_attrs['_ipython_display_'] = Widget._ipython_display_
38 _widget_attrs['_ipython_display_'] = Widget._ipython_display_
45 def raise_not_implemented(*args, **kwargs):
39 def raise_not_implemented(*args, **kwargs):
46 raise NotImplementedError()
40 raise NotImplementedError()
47 Widget._ipython_display_ = raise_not_implemented
41 Widget._ipython_display_ = raise_not_implemented
48
42
49 def teardown():
43 def teardown():
50 for attr, value in _widget_attrs.items():
44 for attr, value in _widget_attrs.items():
51 setattr(Widget, attr, value)
45 if value is undefined:
46 delattr(Widget, attr)
47 else:
48 setattr(Widget, attr, value)
52
49
53 def f(**kwargs):
50 def f(**kwargs):
54 pass
51 pass
55
52
56 def clear_display():
53 def clear_display():
57 global displayed
54 global displayed
58 displayed = []
55 displayed = []
59
56
60 def record_display(*args):
57 def record_display(*args):
61 displayed.extend(args)
58 displayed.extend(args)
62
59
63 #-----------------------------------------------------------------------------
60 #-----------------------------------------------------------------------------
64 # Actual tests
61 # Actual tests
65 #-----------------------------------------------------------------------------
62 #-----------------------------------------------------------------------------
66
63
67 def check_widget(w, **d):
64 def check_widget(w, **d):
68 """Check a single widget against a dict"""
65 """Check a single widget against a dict"""
69 for attr, expected in d.items():
66 for attr, expected in d.items():
70 if attr == 'cls':
67 if attr == 'cls':
71 nt.assert_is(w.__class__, expected)
68 nt.assert_is(w.__class__, expected)
72 else:
69 else:
73 value = getattr(w, attr)
70 value = getattr(w, attr)
74 nt.assert_equal(value, expected,
71 nt.assert_equal(value, expected,
75 "%s.%s = %r != %r" % (w.__class__.__name__, attr, value, expected)
72 "%s.%s = %r != %r" % (w.__class__.__name__, attr, value, expected)
76 )
73 )
77
74
78 def check_widgets(container, **to_check):
75 def check_widgets(container, **to_check):
79 """Check that widgets are created as expected"""
76 """Check that widgets are created as expected"""
80 # build a widget dictionary, so it matches
77 # build a widget dictionary, so it matches
81 widgets = {}
78 widgets = {}
82 for w in container.children:
79 for w in container.children:
83 widgets[w.description] = w
80 widgets[w.description] = w
84
81
85 for key, d in to_check.items():
82 for key, d in to_check.items():
86 nt.assert_in(key, widgets)
83 nt.assert_in(key, widgets)
87 check_widget(widgets[key], **d)
84 check_widget(widgets[key], **d)
88
85
89
86
90 def test_single_value_string():
87 def test_single_value_string():
91 a = u'hello'
88 a = u'hello'
92 c = interactive(f, a=a)
89 c = interactive(f, a=a)
93 w = c.children[0]
90 w = c.children[0]
94 check_widget(w,
91 check_widget(w,
95 cls=widgets.Text,
92 cls=widgets.Text,
96 description='a',
93 description='a',
97 value=a,
94 value=a,
98 )
95 )
99
96
100 def test_single_value_bool():
97 def test_single_value_bool():
101 for a in (True, False):
98 for a in (True, False):
102 c = interactive(f, a=a)
99 c = interactive(f, a=a)
103 w = c.children[0]
100 w = c.children[0]
104 check_widget(w,
101 check_widget(w,
105 cls=widgets.Checkbox,
102 cls=widgets.Checkbox,
106 description='a',
103 description='a',
107 value=a,
104 value=a,
108 )
105 )
109
106
110 def test_single_value_dict():
107 def test_single_value_dict():
111 for d in [
108 for d in [
112 dict(a=5),
109 dict(a=5),
113 dict(a=5, b='b', c=dict),
110 dict(a=5, b='b', c=dict),
114 ]:
111 ]:
115 c = interactive(f, d=d)
112 c = interactive(f, d=d)
116 w = c.children[0]
113 w = c.children[0]
117 check_widget(w,
114 check_widget(w,
118 cls=widgets.Dropdown,
115 cls=widgets.Dropdown,
119 description='d',
116 description='d',
120 values=d,
117 values=d,
121 value=next(iter(d.values())),
118 value=next(iter(d.values())),
122 )
119 )
123
120
124 def test_single_value_float():
121 def test_single_value_float():
125 for a in (2.25, 1.0, -3.5):
122 for a in (2.25, 1.0, -3.5):
126 c = interactive(f, a=a)
123 c = interactive(f, a=a)
127 w = c.children[0]
124 w = c.children[0]
128 check_widget(w,
125 check_widget(w,
129 cls=widgets.FloatSlider,
126 cls=widgets.FloatSlider,
130 description='a',
127 description='a',
131 value=a,
128 value=a,
132 min= -a if a > 0 else 3*a,
129 min= -a if a > 0 else 3*a,
133 max= 3*a if a > 0 else -a,
130 max= 3*a if a > 0 else -a,
134 step=0.1,
131 step=0.1,
135 readout=True,
132 readout=True,
136 )
133 )
137
134
138 def test_single_value_int():
135 def test_single_value_int():
139 for a in (1, 5, -3):
136 for a in (1, 5, -3):
140 c = interactive(f, a=a)
137 c = interactive(f, a=a)
141 nt.assert_equal(len(c.children), 1)
138 nt.assert_equal(len(c.children), 1)
142 w = c.children[0]
139 w = c.children[0]
143 check_widget(w,
140 check_widget(w,
144 cls=widgets.IntSlider,
141 cls=widgets.IntSlider,
145 description='a',
142 description='a',
146 value=a,
143 value=a,
147 min= -a if a > 0 else 3*a,
144 min= -a if a > 0 else 3*a,
148 max= 3*a if a > 0 else -a,
145 max= 3*a if a > 0 else -a,
149 step=1,
146 step=1,
150 readout=True,
147 readout=True,
151 )
148 )
152
149
153 def test_list_tuple_2_int():
150 def test_list_tuple_2_int():
154 with nt.assert_raises(ValueError):
151 with nt.assert_raises(ValueError):
155 c = interactive(f, tup=(1,1))
152 c = interactive(f, tup=(1,1))
156 with nt.assert_raises(ValueError):
153 with nt.assert_raises(ValueError):
157 c = interactive(f, tup=(1,-1))
154 c = interactive(f, tup=(1,-1))
158 for min, max in [ (0,1), (1,10), (1,2), (-5,5), (-20,-19) ]:
155 for min, max in [ (0,1), (1,10), (1,2), (-5,5), (-20,-19) ]:
159 c = interactive(f, tup=(min, max), lis=[min, max])
156 c = interactive(f, tup=(min, max), lis=[min, max])
160 nt.assert_equal(len(c.children), 2)
157 nt.assert_equal(len(c.children), 2)
161 d = dict(
158 d = dict(
162 cls=widgets.IntSlider,
159 cls=widgets.IntSlider,
163 min=min,
160 min=min,
164 max=max,
161 max=max,
165 step=1,
162 step=1,
166 readout=True,
163 readout=True,
167 )
164 )
168 check_widgets(c, tup=d, lis=d)
165 check_widgets(c, tup=d, lis=d)
169
166
170 def test_list_tuple_3_int():
167 def test_list_tuple_3_int():
171 with nt.assert_raises(ValueError):
168 with nt.assert_raises(ValueError):
172 c = interactive(f, tup=(1,2,0))
169 c = interactive(f, tup=(1,2,0))
173 with nt.assert_raises(ValueError):
170 with nt.assert_raises(ValueError):
174 c = interactive(f, tup=(1,2,-1))
171 c = interactive(f, tup=(1,2,-1))
175 for min, max, step in [ (0,2,1), (1,10,2), (1,100,2), (-5,5,4), (-100,-20,4) ]:
172 for min, max, step in [ (0,2,1), (1,10,2), (1,100,2), (-5,5,4), (-100,-20,4) ]:
176 c = interactive(f, tup=(min, max, step), lis=[min, max, step])
173 c = interactive(f, tup=(min, max, step), lis=[min, max, step])
177 nt.assert_equal(len(c.children), 2)
174 nt.assert_equal(len(c.children), 2)
178 d = dict(
175 d = dict(
179 cls=widgets.IntSlider,
176 cls=widgets.IntSlider,
180 min=min,
177 min=min,
181 max=max,
178 max=max,
182 step=step,
179 step=step,
183 readout=True,
180 readout=True,
184 )
181 )
185 check_widgets(c, tup=d, lis=d)
182 check_widgets(c, tup=d, lis=d)
186
183
187 def test_list_tuple_2_float():
184 def test_list_tuple_2_float():
188 with nt.assert_raises(ValueError):
185 with nt.assert_raises(ValueError):
189 c = interactive(f, tup=(1.0,1.0))
186 c = interactive(f, tup=(1.0,1.0))
190 with nt.assert_raises(ValueError):
187 with nt.assert_raises(ValueError):
191 c = interactive(f, tup=(0.5,-0.5))
188 c = interactive(f, tup=(0.5,-0.5))
192 for min, max in [ (0.5, 1.5), (1.1,10.2), (1,2.2), (-5.,5), (-20,-19.) ]:
189 for min, max in [ (0.5, 1.5), (1.1,10.2), (1,2.2), (-5.,5), (-20,-19.) ]:
193 c = interactive(f, tup=(min, max), lis=[min, max])
190 c = interactive(f, tup=(min, max), lis=[min, max])
194 nt.assert_equal(len(c.children), 2)
191 nt.assert_equal(len(c.children), 2)
195 d = dict(
192 d = dict(
196 cls=widgets.FloatSlider,
193 cls=widgets.FloatSlider,
197 min=min,
194 min=min,
198 max=max,
195 max=max,
199 step=.1,
196 step=.1,
200 readout=True,
197 readout=True,
201 )
198 )
202 check_widgets(c, tup=d, lis=d)
199 check_widgets(c, tup=d, lis=d)
203
200
204 def test_list_tuple_3_float():
201 def test_list_tuple_3_float():
205 with nt.assert_raises(ValueError):
202 with nt.assert_raises(ValueError):
206 c = interactive(f, tup=(1,2,0.0))
203 c = interactive(f, tup=(1,2,0.0))
207 with nt.assert_raises(ValueError):
204 with nt.assert_raises(ValueError):
208 c = interactive(f, tup=(-1,-2,1.))
205 c = interactive(f, tup=(-1,-2,1.))
209 with nt.assert_raises(ValueError):
206 with nt.assert_raises(ValueError):
210 c = interactive(f, tup=(1,2.,-1.))
207 c = interactive(f, tup=(1,2.,-1.))
211 for min, max, step in [ (0.,2,1), (1,10.,2), (1,100,2.), (-5.,5.,4), (-100,-20.,4.) ]:
208 for min, max, step in [ (0.,2,1), (1,10.,2), (1,100,2.), (-5.,5.,4), (-100,-20.,4.) ]:
212 c = interactive(f, tup=(min, max, step), lis=[min, max, step])
209 c = interactive(f, tup=(min, max, step), lis=[min, max, step])
213 nt.assert_equal(len(c.children), 2)
210 nt.assert_equal(len(c.children), 2)
214 d = dict(
211 d = dict(
215 cls=widgets.FloatSlider,
212 cls=widgets.FloatSlider,
216 min=min,
213 min=min,
217 max=max,
214 max=max,
218 step=step,
215 step=step,
219 readout=True,
216 readout=True,
220 )
217 )
221 check_widgets(c, tup=d, lis=d)
218 check_widgets(c, tup=d, lis=d)
222
219
223 def test_list_tuple_str():
220 def test_list_tuple_str():
224 values = ['hello', 'there', 'guy']
221 values = ['hello', 'there', 'guy']
225 first = values[0]
222 first = values[0]
226 dvalues = OrderedDict((v,v) for v in values)
223 dvalues = OrderedDict((v,v) for v in values)
227 c = interactive(f, tup=tuple(values), lis=list(values))
224 c = interactive(f, tup=tuple(values), lis=list(values))
228 nt.assert_equal(len(c.children), 2)
225 nt.assert_equal(len(c.children), 2)
229 d = dict(
226 d = dict(
230 cls=widgets.Dropdown,
227 cls=widgets.Dropdown,
231 value=first,
228 value=first,
232 values=dvalues
229 values=dvalues
233 )
230 )
234 check_widgets(c, tup=d, lis=d)
231 check_widgets(c, tup=d, lis=d)
235
232
236 def test_list_tuple_invalid():
233 def test_list_tuple_invalid():
237 for bad in [
234 for bad in [
238 (),
235 (),
239 (5, 'hi'),
236 (5, 'hi'),
240 ('hi', 5),
237 ('hi', 5),
241 ({},),
238 ({},),
242 (None,),
239 (None,),
243 ]:
240 ]:
244 with nt.assert_raises(ValueError):
241 with nt.assert_raises(ValueError):
245 print(bad) # because there is no custom message in assert_raises
242 print(bad) # because there is no custom message in assert_raises
246 c = interactive(f, tup=bad)
243 c = interactive(f, tup=bad)
247
244
248 def test_defaults():
245 def test_defaults():
249 @annotate(n=10)
246 @annotate(n=10)
250 def f(n, f=4.5, g=1):
247 def f(n, f=4.5, g=1):
251 pass
248 pass
252
249
253 c = interactive(f)
250 c = interactive(f)
254 check_widgets(c,
251 check_widgets(c,
255 n=dict(
252 n=dict(
256 cls=widgets.IntSlider,
253 cls=widgets.IntSlider,
257 value=10,
254 value=10,
258 ),
255 ),
259 f=dict(
256 f=dict(
260 cls=widgets.FloatSlider,
257 cls=widgets.FloatSlider,
261 value=4.5,
258 value=4.5,
262 ),
259 ),
263 g=dict(
260 g=dict(
264 cls=widgets.IntSlider,
261 cls=widgets.IntSlider,
265 value=1,
262 value=1,
266 ),
263 ),
267 )
264 )
268
265
269 def test_default_values():
266 def test_default_values():
270 @annotate(n=10, f=(0, 10.), g=5, h={'a': 1, 'b': 2}, j=['hi', 'there'])
267 @annotate(n=10, f=(0, 10.), g=5, h={'a': 1, 'b': 2}, j=['hi', 'there'])
271 def f(n, f=4.5, g=1, h=2, j='there'):
268 def f(n, f=4.5, g=1, h=2, j='there'):
272 pass
269 pass
273
270
274 c = interactive(f)
271 c = interactive(f)
275 check_widgets(c,
272 check_widgets(c,
276 n=dict(
273 n=dict(
277 cls=widgets.IntSlider,
274 cls=widgets.IntSlider,
278 value=10,
275 value=10,
279 ),
276 ),
280 f=dict(
277 f=dict(
281 cls=widgets.FloatSlider,
278 cls=widgets.FloatSlider,
282 value=4.5,
279 value=4.5,
283 ),
280 ),
284 g=dict(
281 g=dict(
285 cls=widgets.IntSlider,
282 cls=widgets.IntSlider,
286 value=5,
283 value=5,
287 ),
284 ),
288 h=dict(
285 h=dict(
289 cls=widgets.Dropdown,
286 cls=widgets.Dropdown,
290 values={'a': 1, 'b': 2},
287 values={'a': 1, 'b': 2},
291 value=2
288 value=2
292 ),
289 ),
293 j=dict(
290 j=dict(
294 cls=widgets.Dropdown,
291 cls=widgets.Dropdown,
295 values={'hi':'hi', 'there':'there'},
292 values={'hi':'hi', 'there':'there'},
296 value='there'
293 value='there'
297 ),
294 ),
298 )
295 )
299
296
300 def test_default_out_of_bounds():
297 def test_default_out_of_bounds():
301 @annotate(f=(0, 10.), h={'a': 1}, j=['hi', 'there'])
298 @annotate(f=(0, 10.), h={'a': 1}, j=['hi', 'there'])
302 def f(f='hi', h=5, j='other'):
299 def f(f='hi', h=5, j='other'):
303 pass
300 pass
304
301
305 c = interactive(f)
302 c = interactive(f)
306 check_widgets(c,
303 check_widgets(c,
307 f=dict(
304 f=dict(
308 cls=widgets.FloatSlider,
305 cls=widgets.FloatSlider,
309 value=5.,
306 value=5.,
310 ),
307 ),
311 h=dict(
308 h=dict(
312 cls=widgets.Dropdown,
309 cls=widgets.Dropdown,
313 values={'a': 1},
310 values={'a': 1},
314 value=1,
311 value=1,
315 ),
312 ),
316 j=dict(
313 j=dict(
317 cls=widgets.Dropdown,
314 cls=widgets.Dropdown,
318 values={'hi':'hi', 'there':'there'},
315 values={'hi':'hi', 'there':'there'},
319 value='hi',
316 value='hi',
320 ),
317 ),
321 )
318 )
322
319
323 def test_annotations():
320 def test_annotations():
324 @annotate(n=10, f=widgets.FloatText())
321 @annotate(n=10, f=widgets.FloatText())
325 def f(n, f):
322 def f(n, f):
326 pass
323 pass
327
324
328 c = interactive(f)
325 c = interactive(f)
329 check_widgets(c,
326 check_widgets(c,
330 n=dict(
327 n=dict(
331 cls=widgets.IntSlider,
328 cls=widgets.IntSlider,
332 value=10,
329 value=10,
333 ),
330 ),
334 f=dict(
331 f=dict(
335 cls=widgets.FloatText,
332 cls=widgets.FloatText,
336 ),
333 ),
337 )
334 )
338
335
339 def test_priority():
336 def test_priority():
340 @annotate(annotate='annotate', kwarg='annotate')
337 @annotate(annotate='annotate', kwarg='annotate')
341 def f(kwarg='default', annotate='default', default='default'):
338 def f(kwarg='default', annotate='default', default='default'):
342 pass
339 pass
343
340
344 c = interactive(f, kwarg='kwarg')
341 c = interactive(f, kwarg='kwarg')
345 check_widgets(c,
342 check_widgets(c,
346 kwarg=dict(
343 kwarg=dict(
347 cls=widgets.Text,
344 cls=widgets.Text,
348 value='kwarg',
345 value='kwarg',
349 ),
346 ),
350 annotate=dict(
347 annotate=dict(
351 cls=widgets.Text,
348 cls=widgets.Text,
352 value='annotate',
349 value='annotate',
353 ),
350 ),
354 )
351 )
355
352
356 @nt.with_setup(clear_display)
353 @nt.with_setup(clear_display)
357 def test_decorator_kwarg():
354 def test_decorator_kwarg():
358 with tt.monkeypatch(interaction, 'display', record_display):
355 with tt.monkeypatch(interaction, 'display', record_display):
359 @interact(a=5)
356 @interact(a=5)
360 def foo(a):
357 def foo(a):
361 pass
358 pass
362 nt.assert_equal(len(displayed), 1)
359 nt.assert_equal(len(displayed), 1)
363 w = displayed[0].children[0]
360 w = displayed[0].children[0]
364 check_widget(w,
361 check_widget(w,
365 cls=widgets.IntSlider,
362 cls=widgets.IntSlider,
366 value=5,
363 value=5,
367 )
364 )
368
365
369 @nt.with_setup(clear_display)
366 @nt.with_setup(clear_display)
370 def test_decorator_no_call():
367 def test_decorator_no_call():
371 with tt.monkeypatch(interaction, 'display', record_display):
368 with tt.monkeypatch(interaction, 'display', record_display):
372 @interact
369 @interact
373 def foo(a='default'):
370 def foo(a='default'):
374 pass
371 pass
375 nt.assert_equal(len(displayed), 1)
372 nt.assert_equal(len(displayed), 1)
376 w = displayed[0].children[0]
373 w = displayed[0].children[0]
377 check_widget(w,
374 check_widget(w,
378 cls=widgets.Text,
375 cls=widgets.Text,
379 value='default',
376 value='default',
380 )
377 )
381
378
382 @nt.with_setup(clear_display)
379 @nt.with_setup(clear_display)
383 def test_call_interact():
380 def test_call_interact():
384 def foo(a='default'):
381 def foo(a='default'):
385 pass
382 pass
386 with tt.monkeypatch(interaction, 'display', record_display):
383 with tt.monkeypatch(interaction, 'display', record_display):
387 ifoo = interact(foo)
384 ifoo = interact(foo)
388 nt.assert_equal(len(displayed), 1)
385 nt.assert_equal(len(displayed), 1)
389 w = displayed[0].children[0]
386 w = displayed[0].children[0]
390 check_widget(w,
387 check_widget(w,
391 cls=widgets.Text,
388 cls=widgets.Text,
392 value='default',
389 value='default',
393 )
390 )
394
391
395 @nt.with_setup(clear_display)
392 @nt.with_setup(clear_display)
396 def test_call_interact_kwargs():
393 def test_call_interact_kwargs():
397 def foo(a='default'):
394 def foo(a='default'):
398 pass
395 pass
399 with tt.monkeypatch(interaction, 'display', record_display):
396 with tt.monkeypatch(interaction, 'display', record_display):
400 ifoo = interact(foo, a=10)
397 ifoo = interact(foo, a=10)
401 nt.assert_equal(len(displayed), 1)
398 nt.assert_equal(len(displayed), 1)
402 w = displayed[0].children[0]
399 w = displayed[0].children[0]
403 check_widget(w,
400 check_widget(w,
404 cls=widgets.IntSlider,
401 cls=widgets.IntSlider,
405 value=10,
402 value=10,
406 )
403 )
407
404
408 @nt.with_setup(clear_display)
405 @nt.with_setup(clear_display)
409 def test_call_decorated_on_trait_change():
406 def test_call_decorated_on_trait_change():
410 """test calling @interact decorated functions"""
407 """test calling @interact decorated functions"""
411 d = {}
408 d = {}
412 with tt.monkeypatch(interaction, 'display', record_display):
409 with tt.monkeypatch(interaction, 'display', record_display):
413 @interact
410 @interact
414 def foo(a='default'):
411 def foo(a='default'):
415 d['a'] = a
412 d['a'] = a
416 return a
413 return a
417 nt.assert_equal(len(displayed), 1)
414 nt.assert_equal(len(displayed), 1)
418 w = displayed[0].children[0]
415 w = displayed[0].children[0]
419 check_widget(w,
416 check_widget(w,
420 cls=widgets.Text,
417 cls=widgets.Text,
421 value='default',
418 value='default',
422 )
419 )
423 # test calling the function directly
420 # test calling the function directly
424 a = foo('hello')
421 a = foo('hello')
425 nt.assert_equal(a, 'hello')
422 nt.assert_equal(a, 'hello')
426 nt.assert_equal(d['a'], 'hello')
423 nt.assert_equal(d['a'], 'hello')
427
424
428 # test that setting trait values calls the function
425 # test that setting trait values calls the function
429 w.value = 'called'
426 w.value = 'called'
430 nt.assert_equal(d['a'], 'called')
427 nt.assert_equal(d['a'], 'called')
431
428
432 @nt.with_setup(clear_display)
429 @nt.with_setup(clear_display)
433 def test_call_decorated_kwargs_on_trait_change():
430 def test_call_decorated_kwargs_on_trait_change():
434 """test calling @interact(foo=bar) decorated functions"""
431 """test calling @interact(foo=bar) decorated functions"""
435 d = {}
432 d = {}
436 with tt.monkeypatch(interaction, 'display', record_display):
433 with tt.monkeypatch(interaction, 'display', record_display):
437 @interact(a='kwarg')
434 @interact(a='kwarg')
438 def foo(a='default'):
435 def foo(a='default'):
439 d['a'] = a
436 d['a'] = a
440 return a
437 return a
441 nt.assert_equal(len(displayed), 1)
438 nt.assert_equal(len(displayed), 1)
442 w = displayed[0].children[0]
439 w = displayed[0].children[0]
443 check_widget(w,
440 check_widget(w,
444 cls=widgets.Text,
441 cls=widgets.Text,
445 value='kwarg',
442 value='kwarg',
446 )
443 )
447 # test calling the function directly
444 # test calling the function directly
448 a = foo('hello')
445 a = foo('hello')
449 nt.assert_equal(a, 'hello')
446 nt.assert_equal(a, 'hello')
450 nt.assert_equal(d['a'], 'hello')
447 nt.assert_equal(d['a'], 'hello')
451
448
452 # test that setting trait values calls the function
449 # test that setting trait values calls the function
453 w.value = 'called'
450 w.value = 'called'
454 nt.assert_equal(d['a'], 'called')
451 nt.assert_equal(d['a'], 'called')
455
452
456 def test_fixed():
453 def test_fixed():
457 c = interactive(f, a=widgets.fixed(5), b='text')
454 c = interactive(f, a=widgets.fixed(5), b='text')
458 nt.assert_equal(len(c.children), 1)
455 nt.assert_equal(len(c.children), 1)
459 w = c.children[0]
456 w = c.children[0]
460 check_widget(w,
457 check_widget(w,
461 cls=widgets.Text,
458 cls=widgets.Text,
462 value='text',
459 value='text',
463 description='b',
460 description='b',
464 )
461 )
465
462
466 def test_default_description():
463 def test_default_description():
467 c = interactive(f, b='text')
464 c = interactive(f, b='text')
468 w = c.children[0]
465 w = c.children[0]
469 check_widget(w,
466 check_widget(w,
470 cls=widgets.Text,
467 cls=widgets.Text,
471 value='text',
468 value='text',
472 description='b',
469 description='b',
473 )
470 )
474
471
475 def test_custom_description():
472 def test_custom_description():
476 c = interactive(f, b=widgets.Text(value='text', description='foo'))
473 c = interactive(f, b=widgets.Text(value='text', description='foo'))
477 w = c.children[0]
474 w = c.children[0]
478 check_widget(w,
475 check_widget(w,
479 cls=widgets.Text,
476 cls=widgets.Text,
480 value='text',
477 value='text',
481 description='foo',
478 description='foo',
482 )
479 )
483
480
484 def test_int_range_logic():
481 def test_int_range_logic():
485 irsw = widgets.IntRangeSlider
482 irsw = widgets.IntRangeSlider
486 w = irsw(value=(2, 4), min=0, max=6)
483 w = irsw(value=(2, 4), min=0, max=6)
487 check_widget(w, cls=irsw, value=(2, 4), min=0, max=6)
484 check_widget(w, cls=irsw, value=(2, 4), min=0, max=6)
488 w.value = (4, 2)
485 w.value = (4, 2)
489 check_widget(w, cls=irsw, value=(2, 4), min=0, max=6)
486 check_widget(w, cls=irsw, value=(2, 4), min=0, max=6)
490 w.value = (-1, 7)
487 w.value = (-1, 7)
491 check_widget(w, cls=irsw, value=(0, 6), min=0, max=6)
488 check_widget(w, cls=irsw, value=(0, 6), min=0, max=6)
492 w.min = 3
489 w.min = 3
493 check_widget(w, cls=irsw, value=(3, 6), min=3, max=6)
490 check_widget(w, cls=irsw, value=(3, 6), min=3, max=6)
494 w.max = 3
491 w.max = 3
495 check_widget(w, cls=irsw, value=(3, 3), min=3, max=3)
492 check_widget(w, cls=irsw, value=(3, 3), min=3, max=3)
496
493
497 w.min = 0
494 w.min = 0
498 w.max = 6
495 w.max = 6
499 w.lower = 2
496 w.lower = 2
500 w.upper = 4
497 w.upper = 4
501 check_widget(w, cls=irsw, value=(2, 4), min=0, max=6)
498 check_widget(w, cls=irsw, value=(2, 4), min=0, max=6)
502 w.value = (0, 1) #lower non-overlapping range
499 w.value = (0, 1) #lower non-overlapping range
503 check_widget(w, cls=irsw, value=(0, 1), min=0, max=6)
500 check_widget(w, cls=irsw, value=(0, 1), min=0, max=6)
504 w.value = (5, 6) #upper non-overlapping range
501 w.value = (5, 6) #upper non-overlapping range
505 check_widget(w, cls=irsw, value=(5, 6), min=0, max=6)
502 check_widget(w, cls=irsw, value=(5, 6), min=0, max=6)
506 w.value = (-1, 4) #semi out-of-range
503 w.value = (-1, 4) #semi out-of-range
507 check_widget(w, cls=irsw, value=(0, 4), min=0, max=6)
504 check_widget(w, cls=irsw, value=(0, 4), min=0, max=6)
508 w.lower = 2
505 w.lower = 2
509 check_widget(w, cls=irsw, value=(2, 4), min=0, max=6)
506 check_widget(w, cls=irsw, value=(2, 4), min=0, max=6)
510 w.value = (-2, -1) #wholly out of range
507 w.value = (-2, -1) #wholly out of range
511 check_widget(w, cls=irsw, value=(0, 0), min=0, max=6)
508 check_widget(w, cls=irsw, value=(0, 0), min=0, max=6)
512 w.value = (7, 8)
509 w.value = (7, 8)
513 check_widget(w, cls=irsw, value=(6, 6), min=0, max=6)
510 check_widget(w, cls=irsw, value=(6, 6), min=0, max=6)
514
511
515 with nt.assert_raises(ValueError):
512 with nt.assert_raises(ValueError):
516 w.min = 7
513 w.min = 7
517 with nt.assert_raises(ValueError):
514 with nt.assert_raises(ValueError):
518 w.max = -1
515 w.max = -1
519 with nt.assert_raises(ValueError):
516 with nt.assert_raises(ValueError):
520 w.lower = 5
517 w.lower = 5
521 with nt.assert_raises(ValueError):
518 with nt.assert_raises(ValueError):
522 w.upper = 1
519 w.upper = 1
523
520
524 w = irsw(min=2, max=3)
521 w = irsw(min=2, max=3)
525 check_widget(w, min=2, max=3)
522 check_widget(w, min=2, max=3)
526 w = irsw(min=100, max=200)
523 w = irsw(min=100, max=200)
527 check_widget(w, lower=125, upper=175, value=(125, 175))
524 check_widget(w, lower=125, upper=175, value=(125, 175))
528
525
529 with nt.assert_raises(ValueError):
526 with nt.assert_raises(ValueError):
530 irsw(value=(2, 4), lower=3)
527 irsw(value=(2, 4), lower=3)
531 with nt.assert_raises(ValueError):
528 with nt.assert_raises(ValueError):
532 irsw(value=(2, 4), upper=3)
529 irsw(value=(2, 4), upper=3)
533 with nt.assert_raises(ValueError):
530 with nt.assert_raises(ValueError):
534 irsw(value=(2, 4), lower=3, upper=3)
531 irsw(value=(2, 4), lower=3, upper=3)
535 with nt.assert_raises(ValueError):
532 with nt.assert_raises(ValueError):
536 irsw(min=2, max=1)
533 irsw(min=2, max=1)
537 with nt.assert_raises(ValueError):
534 with nt.assert_raises(ValueError):
538 irsw(lower=5)
535 irsw(lower=5)
539 with nt.assert_raises(ValueError):
536 with nt.assert_raises(ValueError):
540 irsw(upper=5)
537 irsw(upper=5)
541
538
542
539
543 def test_float_range_logic():
540 def test_float_range_logic():
544 frsw = widgets.FloatRangeSlider
541 frsw = widgets.FloatRangeSlider
545 w = frsw(value=(.2, .4), min=0., max=.6)
542 w = frsw(value=(.2, .4), min=0., max=.6)
546 check_widget(w, cls=frsw, value=(.2, .4), min=0., max=.6)
543 check_widget(w, cls=frsw, value=(.2, .4), min=0., max=.6)
547 w.value = (.4, .2)
544 w.value = (.4, .2)
548 check_widget(w, cls=frsw, value=(.2, .4), min=0., max=.6)
545 check_widget(w, cls=frsw, value=(.2, .4), min=0., max=.6)
549 w.value = (-.1, .7)
546 w.value = (-.1, .7)
550 check_widget(w, cls=frsw, value=(0., .6), min=0., max=.6)
547 check_widget(w, cls=frsw, value=(0., .6), min=0., max=.6)
551 w.min = .3
548 w.min = .3
552 check_widget(w, cls=frsw, value=(.3, .6), min=.3, max=.6)
549 check_widget(w, cls=frsw, value=(.3, .6), min=.3, max=.6)
553 w.max = .3
550 w.max = .3
554 check_widget(w, cls=frsw, value=(.3, .3), min=.3, max=.3)
551 check_widget(w, cls=frsw, value=(.3, .3), min=.3, max=.3)
555
552
556 w.min = 0.
553 w.min = 0.
557 w.max = .6
554 w.max = .6
558 w.lower = .2
555 w.lower = .2
559 w.upper = .4
556 w.upper = .4
560 check_widget(w, cls=frsw, value=(.2, .4), min=0., max=.6)
557 check_widget(w, cls=frsw, value=(.2, .4), min=0., max=.6)
561 w.value = (0., .1) #lower non-overlapping range
558 w.value = (0., .1) #lower non-overlapping range
562 check_widget(w, cls=frsw, value=(0., .1), min=0., max=.6)
559 check_widget(w, cls=frsw, value=(0., .1), min=0., max=.6)
563 w.value = (.5, .6) #upper non-overlapping range
560 w.value = (.5, .6) #upper non-overlapping range
564 check_widget(w, cls=frsw, value=(.5, .6), min=0., max=.6)
561 check_widget(w, cls=frsw, value=(.5, .6), min=0., max=.6)
565 w.value = (-.1, .4) #semi out-of-range
562 w.value = (-.1, .4) #semi out-of-range
566 check_widget(w, cls=frsw, value=(0., .4), min=0., max=.6)
563 check_widget(w, cls=frsw, value=(0., .4), min=0., max=.6)
567 w.lower = .2
564 w.lower = .2
568 check_widget(w, cls=frsw, value=(.2, .4), min=0., max=.6)
565 check_widget(w, cls=frsw, value=(.2, .4), min=0., max=.6)
569 w.value = (-.2, -.1) #wholly out of range
566 w.value = (-.2, -.1) #wholly out of range
570 check_widget(w, cls=frsw, value=(0., 0.), min=0., max=.6)
567 check_widget(w, cls=frsw, value=(0., 0.), min=0., max=.6)
571 w.value = (.7, .8)
568 w.value = (.7, .8)
572 check_widget(w, cls=frsw, value=(.6, .6), min=.0, max=.6)
569 check_widget(w, cls=frsw, value=(.6, .6), min=.0, max=.6)
573
570
574 with nt.assert_raises(ValueError):
571 with nt.assert_raises(ValueError):
575 w.min = .7
572 w.min = .7
576 with nt.assert_raises(ValueError):
573 with nt.assert_raises(ValueError):
577 w.max = -.1
574 w.max = -.1
578 with nt.assert_raises(ValueError):
575 with nt.assert_raises(ValueError):
579 w.lower = .5
576 w.lower = .5
580 with nt.assert_raises(ValueError):
577 with nt.assert_raises(ValueError):
581 w.upper = .1
578 w.upper = .1
582
579
583 w = frsw(min=2, max=3)
580 w = frsw(min=2, max=3)
584 check_widget(w, min=2, max=3)
581 check_widget(w, min=2, max=3)
585 w = frsw(min=1., max=2.)
582 w = frsw(min=1., max=2.)
586 check_widget(w, lower=1.25, upper=1.75, value=(1.25, 1.75))
583 check_widget(w, lower=1.25, upper=1.75, value=(1.25, 1.75))
587
584
588 with nt.assert_raises(ValueError):
585 with nt.assert_raises(ValueError):
589 frsw(value=(2, 4), lower=3)
586 frsw(value=(2, 4), lower=3)
590 with nt.assert_raises(ValueError):
587 with nt.assert_raises(ValueError):
591 frsw(value=(2, 4), upper=3)
588 frsw(value=(2, 4), upper=3)
592 with nt.assert_raises(ValueError):
589 with nt.assert_raises(ValueError):
593 frsw(value=(2, 4), lower=3, upper=3)
590 frsw(value=(2, 4), lower=3, upper=3)
594 with nt.assert_raises(ValueError):
591 with nt.assert_raises(ValueError):
595 frsw(min=.2, max=.1)
592 frsw(min=.2, max=.1)
596 with nt.assert_raises(ValueError):
593 with nt.assert_raises(ValueError):
597 frsw(lower=5)
594 frsw(lower=5)
598 with nt.assert_raises(ValueError):
595 with nt.assert_raises(ValueError):
599 frsw(upper=5)
596 frsw(upper=5)
General Comments 0
You need to be logged in to leave comments. Login now