def test_container_widget(): """Test basic container functionality.""" container = widgets.Container(labels=False) labela = widgets.Label(value="hi", name="labela") labelb = widgets.Label(value="hi", name="labelb") container.append(labela) container.extend([labelb]) # different ways to index assert container[0] == labela assert container["labelb"] == labelb assert container[:1] == [labela] assert container[-1] == labelb with pytest.raises(NotImplementedError): container[0] = "something" assert container.layout == "vertical" with pytest.raises(NotImplementedError): container.layout = "horizontal" assert all(x in dir(container) for x in ["labela", "labelb"]) assert container.margins container.margins = (8, 8, 8, 8) assert container.margins == (8, 8, 8, 8) del container[1:] del container[-1] assert not container
def test_visible_in_container(): """Test that visibility depends on containers.""" w1 = widgets.Label(value="hi", name="w1") w2 = widgets.Label(value="hi", name="w2") w3 = widgets.Label(value="hi", name="w2", visible=False) container = widgets.Container(widgets=[w2, w3]) assert not w1.visible assert not w2.visible assert not w3.visible assert not container.visible container.show() assert container.visible assert w2.visible assert not w3.visible w1.show() assert w1.visible
def test_container_label_widths(): """Test basic container functionality.""" container = widgets.Container(layout="vertical") labela = widgets.Label(value="hi", name="labela") labelb = widgets.Label(value="hi", name="I have a very long label") def _label_width(): measure = use_app().get_obj("get_text_width") return max( measure(w.label) for w in container if not isinstance(w, widgets._bases.ButtonWidget)) container.append(labela) before = _label_width() container.append(labelb) assert _label_width() > before
def test_labeled_widget_container(): """Test that _LabeledWidgets follow their children.""" from magicgui.widgets._concrete import _LabeledWidget w1 = widgets.Label(value="hi", name="w1") w2 = widgets.Label(value="hi", name="w2") container = widgets.Container(widgets=[w1, w2], layout="vertical") assert w1._labeled_widget lw = w1._labeled_widget() assert isinstance(lw, _LabeledWidget) assert not lw.visible container.show() assert w1.visible assert lw.visible w1.hide() assert not w1.visible assert not lw.visible w1.label = "another label" assert lw._label_widget.value == "another label"
def test_container_indexing_with_native_mucking(): """Mostly make sure that the inner model isn't messed up. keeping indexes with a manipulated native model *may* be something to do in future. """ l1 = widgets.Label(name="l1") l2 = widgets.Label(name="l2") l3 = widgets.Label(name="l3") c = widgets.Container(widgets=[l1, l2, l3]) assert c[-1] == l3 # so far they should be in sync native = c.native.layout() assert native.count() == len(c) # much with native layout native.addStretch() # haven't changed the magicgui container assert len(c) == 3 assert c[-1] == l3 # though it has changed the native model assert native.count() == 4
def test_delete_widget(): """We can delete widgets from containers.""" a = widgets.Label(name="a") container = widgets.Container(widgets=[a]) # we can delete widgets del container.a with pytest.raises(AttributeError): getattr(container, "a") # they disappear from the layout with pytest.raises(ValueError): container.index(a)
def test_tooltip(): label = widgets.Label() assert not label.tooltip label.tooltip = "My Tooltip" assert label.tooltip == "My Tooltip"
def test_extra_kwargs_error(): """Test that unrecognized kwargs gives a FutureWarning.""" with pytest.raises(TypeError) as wrn: widgets.Label(unknown_kwarg="hi") assert "unexpected keyword argument" in str(wrn)
def test_extra_kwargs_warn(): """Test that unrecognized kwargs gives a FutureWarning.""" with pytest.warns(FutureWarning) as wrn: widgets.Label(unknown_kwarg="hi") assert "unexpected keyword arguments" in str(wrn[0].message)