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