1 6
from __future__ import absolute_import, division, unicode_literals
2

3 6
import param
4 6
import pytest
5

6 6
from panel.io import block_comm
7 6
from panel.widgets import (
8
    CompositeWidget, Dial, FileDownload, FloatSlider, TextInput, ToggleGroup, Widget
9
)
10 6
from panel.widgets.tables import BaseTable
11 6
from panel.tests.util import check_layoutable_properties, py3_only
12

13 6
all_widgets = [
14
    w for w in param.concrete_descendents(Widget).values()
15
    if not w.__name__.startswith('_') and
16
    not issubclass(w, (CompositeWidget, BaseTable, FileDownload, ToggleGroup, Dial))
17
]
18

19

20 6
@py3_only
21 6
@pytest.mark.parametrize('widget', all_widgets)
22 1
def test_widget_signature(widget):
23 6
    from inspect import signature
24 6
    parameters = signature(widget).parameters
25 6
    assert len(parameters) == 1
26

27

28 6
@pytest.mark.parametrize('widget', all_widgets)
29 1
def test_widget_layout_properties(widget, document, comm):
30 6
    w = widget()
31 6
    model = w.get_root(document, comm)
32 6
    check_layoutable_properties(w, model)
33

34

35 6
@pytest.mark.parametrize('widget', all_widgets)
36 1
def test_widget_disabled_properties(widget, document, comm):
37 6
    w = widget(disabled=True)
38

39 6
    model = w.get_root(document, comm)
40

41 6
    assert model.disabled == True
42 6
    model.disabled = False
43 6
    assert model.disabled == False
44

45

46 6
@pytest.mark.parametrize('widget', all_widgets)
47 1
def test_widget_clone(widget):
48 6
    w = widget()
49 6
    clone = w.clone()
50

51 6
    assert ([(k, v) for k, v in sorted(w.param.get_param_values()) if k != 'name'] ==
52
            [(k, v) for k, v in sorted(clone.param.get_param_values()) if k != 'name'])
53

54

55 6
@pytest.mark.parametrize('widget', all_widgets)
56 1
def test_widget_clone_override(widget):
57 6
    w = widget()
58 6
    clone = w.clone(width=50)
59

60 6
    assert ([(k, v) for k, v in sorted(w.param.get_param_values()) if k not in ['name', 'width']] ==
61
            [(k, v) for k, v in sorted(clone.param.get_param_values()) if k not in ['name', 'width']])
62 6
    assert clone.width == 50
63 6
    assert w.width is widget.width
64

65

66 6
@pytest.mark.parametrize('widget', all_widgets)
67 1
def test_widget_model_cache_cleanup(widget, document, comm):
68 6
    w = widget()
69

70 6
    model = w.get_root(document, comm)
71

72 6
    assert model.ref['id'] in w._models
73 6
    assert w._models[model.ref['id']] == (model, None)
74

75 6
    w._cleanup(model)
76 6
    assert w._models == {}
77

78

79 6
def test_widget_triggers_events(document, comm):
80
    """
81
    Ensure widget events don't get swallowed in comm mode
82
    """
83 6
    text = TextInput(value='ABC', name='Text:')
84

85 6
    widget = text.get_root(document, comm=comm)
86 6
    document.add_root(widget)
87 6
    document.hold()
88

89
    # Simulate client side change
90 6
    document._held_events = document._held_events[:-1]
91

92
    # Set new value
93 6
    with block_comm():
94 6
        text.value = '123'
95

96 6
    assert len(document._held_events) == 1
97 6
    event = document._held_events[0]
98 6
    assert event.attr == 'value'
99 6
    assert event.model is widget
100 6
    assert event.new == '123'
101

102

103 6
def test_widget_from_param_cls():
104 6
    class Test(param.Parameterized):
105

106 6
        a = param.Parameter()
107

108 6
    widget = TextInput.from_param(Test.param.a)
109 6
    assert isinstance(widget, TextInput)
110 6
    assert widget.name == 'A'
111

112 6
    Test.a = 'abc'
113 6
    assert widget.value == 'abc'
114

115 6
    widget.value = 'def'
116 6
    assert Test.a == 'def'
117

118

119 6
def test_widget_from_param_instance():
120 6
    class Test(param.Parameterized):
121

122 6
        a = param.Parameter()
123

124 6
    test = Test()
125 6
    widget = TextInput.from_param(test.param.a)
126 6
    assert isinstance(widget, TextInput)
127 6
    assert widget.name == 'A'
128

129 6
    test.a = 'abc'
130 6
    assert widget.value == 'abc'
131

132 6
    widget.value = 'def'
133 6
    assert test.a == 'def'
134

135

136 6
def test_widget_from_param_instance_with_kwargs():
137 6
    class Test(param.Parameterized):
138

139 6
        a = param.Number(default=3.14)
140

141 6
    test = Test()
142 6
    widget = FloatSlider.from_param(test.param.a, start=0.3, end=5.2)
143 6
    assert isinstance(widget, FloatSlider)
144 6
    assert widget.name == 'A'
145 6
    assert widget.start == 0.3
146 6
    assert widget.end == 5.2
147 6
    assert widget.value == 3.14
148

149 6
    test.a = 1.57
150 6
    assert widget.value == 1.57
151

152 6
    widget.value = 4.3
153 6
    assert test.a == 4.3

Read our documentation on viewing source code .

Loading