def test_widget_layout(web_fixture): """A Layout is used to add children to the Widget in customised ways, and to customise the Widget itself upon construction.""" class MyLayout(Layout): def customise_widget(self): self.widget.append_class('class-added-by-custom-layout') def add_wrapped(self, child): wrapper = self.widget.add_child(Div(self.view)) wrapper.add_child(child) return child fixture = web_fixture widget_with_layout = Div(fixture.view) assert not widget_with_layout.has_attribute('class') assert not widget_with_layout.children widget_with_layout.use_layout(MyLayout()) assert widget_with_layout.get_attribute( 'class') == 'class-added-by-custom-layout' assert not widget_with_layout.children widget_to_add = P(fixture.view) widget_with_layout.layout.add_wrapped(widget_to_add) [wrapper] = widget_with_layout.children assert wrapper.children == [widget_to_add]
def widget_layout_errors(fixture): """A Layout can only be used with a single Widget, and a Widget can only have a single Layout.""" widget_with_layout = Div(fixture.view).use_layout(Layout()) with expected(ProgrammerError): widget_with_layout.use_layout(Layout()) re_used_layout = Layout() widget_with_reused_layout = Div(fixture.view).use_layout(re_used_layout) with expected(ProgrammerError): Div(fixture.view).use_layout(re_used_layout)
def column_layout_basics(fixture): """A ColumnLayout turns its Widget into a sequence of columns, each of which is a Div.""" layout = ColumnLayout('column_a', 'column_b') widget = Div(fixture.view) vassert(not widget.children) widget.use_layout(layout) column_a, column_b = widget.children vassert(isinstance(column_a, Div)) vassert(isinstance(column_b, Div))