def test_render_sizer_children(nodes_count): """should render all xml children for every item""" render_mock = Mock() add_singleton(render, render_mock) with patch(sizers.__name__ + '.InheritedDict') as inherited_dict_mock: inherited_dict_mock.side_effect = lambda p: {'source': p} if p else p xml_node = Mock(children=[Mock() for _ in range(nodes_count)]) parent, node = Mock(), SizerNode(Mock(), xml_node) context = WxRenderingContext({'node': node, 'parent': parent}) render_sizer_children(node, context) for actual_call, child_xml_node in zip(render_mock.call_args_list, xml_node.children): child_context = WxRenderingContext({ 'parent_node': node, 'parent': parent, 'node_globals': inherited_dict_mock(node.node_globals), 'sizer': node.instance, 'xml_node': child_xml_node }) assert actual_call == call(child_context)
def test_passes_attr_args(self, sizer_args): """should call sizer.Add with node.sizer_args""" node, sizer = self._get_mocks(sizer_args) add_to_sizer(node, WxRenderingContext({'sizer': sizer})) assert sizer.Add.call_args == call(node.instance, **sizer_args)
def test_root(): """store_root should set WidgetNode.Root to passed node""" node = Mock() store_root(node, WxRenderingContext()) assert get_root() == node
def _get_style_node(attrs): attrs = [XmlAttr('name', 'hoho') ] + [XmlAttr(attr[0], attr[1], attr[2]) for attr in attrs] xml_node = Mock(attrs=attrs) node = Style(xml_node) apply_style_items(node, WxRenderingContext()) return node
def test_requires_name_attribute(): """apply_style_items should raise StyleError if name attribute is missing""" xml_node = Mock(attrs=[]) node = Style(xml_node) with raises(StyleError): apply_style_items(node, WxRenderingContext())
def test_skips_if_sizer_missed(self): """should skip if sizer is missed""" node = self._get_mocks()[0] try: add_to_sizer(node, WxRenderingContext()) except BaseException: fail()
def test_skip_special_attributes(self, attr): """should skip attributes with "init" and "sizer" namespaces""" self.apply_attribute.reset_mock() node = Mock(xml_node=Mock(attrs=[attr])) apply_attributes(node, WxRenderingContext()) assert not self.apply_attribute.called
def test_sets_style_name(name): """apply_style_items should set style name from attributes""" xml_node = Mock(attrs=[XmlAttr('name', name)]) node = Style(xml_node) apply_style_items(node, WxRenderingContext()) assert node.name == name
def _get_menu_child_context(child_xml_node: XmlNode, node: InstanceNode, _: WxRenderingContext) -> WxRenderingContext: return WxRenderingContext({ 'parent_node': node, 'parent': node.instance, 'node_globals': InheritedDict(node.node_globals), 'xml_node': child_xml_node })
def test_sets_item_to_menu(): """should call Append menu and pass item instance""" node = Mock() menu = MenuStub() set_to_menu(node, WxRenderingContext({'parent': menu})) assert menu.Append.call_args == call(node.instance)
def test_calls_parent_set_sizer(): """should call SetSizer of parent and pass sizer as argument""" node = Mock() parent = Mock() set_sizer_to_parent(node, WxRenderingContext({'parent': parent})) assert parent.SetSizer.call_args == call(node.instance, True)
def test_passes_if_parent_none(): """should pass if parent is None""" node = Mock() try: set_sizer_to_parent(node, WxRenderingContext({'parent': None})) except BaseException: fail()
def test_sets_menu_bar_to_frame(): """should call SetMenuBar on frame and pass menu bar instance""" node = Mock() frame = FrameStub() set_to_frame(node, WxRenderingContext({'parent': frame})) assert frame.SetMenuBar.call_args == call(node.instance)
def render_app_children(node: WxNode, context: WxRenderingContext): """Renders App children""" render_children( node, context, lambda x, n, ctx: WxRenderingContext( { 'xml_node': x, 'parent_node': n, 'node_globals': InheritedDict(node.node_globals) }))
def test_creates_node_styles(): """should add node_styles to parent globals""" parent_node: Node = Mock(node_globals=InheritedDict()) setup_node_styles(Mock(), WxRenderingContext({'parent_node': parent_node})) actual = parent_node.node_globals.get(STYLES_KEY) assert isinstance(actual, InheritedDict)
def test_layout_parent_on_change(container, prop): """should call parent.Layout() on property change""" parent = Mock() context = WxRenderingContext({'parent': parent}) layout_parent_on_change(prop, container, context) setattr(container, prop, 'new value') assert parent.Layout.called
def test_apply_attributes(self, attrs): """should apply passed attributes""" self.apply_attribute.reset_mock() node = Mock(xml_node=Mock(attrs=attrs)) apply_attributes(node, WxRenderingContext()) assert self.apply_attribute.call_args_list == [ call(node, attr) for attr in attrs ]
def test_skips_not_style_parent(parent_node): """apply_parent_items do nothing if parent is not Style""" items = {'item': ('item', 'value')} node = Style(Mock()) node.items = items.copy() apply_parent_items(node, WxRenderingContext({'parent_node': parent_node})) assert items == node.items
def render_wx_children(node: WxNode, context: WxRenderingContext): """Renders WidgetNode children""" render_children( node, context, lambda xn, n, ctx: WxRenderingContext( { 'parent': n.instance, 'parent_node': n, 'node_globals': InheritedDict(n.node_globals), 'xml_node': xn }))
def render_child_styles(node: Style, context: WxRenderingContext): """Renders child styles""" render_children( node, context, lambda x, n, ctx: WxRenderingContext( { 'parent_node': n, 'node_globals': InheritedDict(node.node_globals), 'node_styles': _get_styles(context), 'xml_node': x }))
def test_add_glowable_col(properties: dict, expected_call: call): """should call AddGrowableCol with parameters""" node = GrowableCol(Mock()) sizer_ = Mock() for key, value in properties.items(): node.set_attr(key, value) add_growable_col_to_sizer(node, WxRenderingContext({'sizer': sizer_})) assert sizer_.AddGrowableCol.call_args == expected_call
def test_store_to_node_styles(): """store_to_node_styles should store style items to node_styles""" node_styles = InheritedDict() node = Style(Mock()) node.items = Mock() parent_node = Mock(node_globals=InheritedDict({STYLES_KEY: node_styles})) store_to_node_styles(node, WxRenderingContext({'parent_node': parent_node})) assert node_styles[node.name] == node.items.values()
def test_does_not_create_if_exist(): """should not change parent node_globals if node_styles exist""" node_styles = InheritedDict() parent_node: Node = Mock( node_globals=InheritedDict({STYLES_KEY: node_styles})) setup_node_styles(Mock(), WxRenderingContext({'parent_node': parent_node})) actual = parent_node.node_globals[STYLES_KEY] assert node_styles == actual
def test_uses_parent_style_items(self, items, parent_items, expected): """apply_parent_items should add parent style items""" node = self._get_style_node(items) parent_node = self._get_style_node(parent_items) apply_parent_items(node, WxRenderingContext({'parent_node': parent_node})) actual = [(item.name, item.value, item.setter) for name, item in node.items.items()] assert actual == expected
def render_sizer_children(node: SizerNode, context: WxRenderingContext): """Renders sizer children""" render_children( node, context, lambda x, n, ctx: WxRenderingContext( { 'parent_node': n, 'parent': ctx.parent, 'node_globals': InheritedDict(node.node_globals), 'sizer': node.instance, 'xml_node': x }))
def test_does_not_calls_parent_set_sizer(): """should not call SetSizer of parent if parent is sizer""" node = Mock() parent = AnySizer() set_sizer_to_parent( node, WxRenderingContext({ 'parent': parent, 'sizer': Mock() })) assert not parent.SetSizer.called
def test_sets_menu_to_bar(): """should call Append on menu baar and pass menu instance""" title = 'some title' node = Mock(properties={}, xml_node=XmlNode('wx', 'Menu', attrs=[XmlAttr('title', title)])) menu_bar = MenuBarStub() set_to_menu_bar(node, WxRenderingContext({'parent': menu_bar})) assert menu_bar.Append.call_args == call(node.instance, title)
def test_creates_style_items_from_attrs(attrs, expected): """apply_style_items should create style item for every attribute""" attrs = [XmlAttr('name', 'hoho') ] + [XmlAttr(attr[0], attr[1], attr[2]) for attr in attrs] xml_node = Mock(attrs=attrs) node = Style(xml_node) apply_style_items(node, WxRenderingContext()) actual = [(item.name, item.value, item.setter) for name, item in node.items.items()] assert actual == expected
def test_store_to_globals(parent_styles, view_styles, expected): """should copy node styles from view root to parent globals""" parent_node = Mock(node_globals=InheritedDict()) if parent_styles: parent_node.node_globals[STYLES_KEY] = InheritedDict(parent_styles) node = StylesView(Mock()) child = Mock(node_globals=InheritedDict()) child.node_globals[STYLES_KEY] = InheritedDict(view_styles) node.add_child(child) store_to_globals(node, WxRenderingContext({'parent_node': parent_node})) actual = parent_node.node_globals[STYLES_KEY].to_dictionary() assert expected == actual
def run_sandbox(): set_container(Container()) register_dependencies() node_globals = InheritedDict( {'views': SandboxViews(['events', 'sizers', 'binding'])}) launch(WxRenderingContext({'node_globals': node_globals}), 'app')