def test_raises_style_error(): """should raise StyleError if style is not found""" node = Mock(node_globals=InheritedDict()) node.node_globals[STYLES_KEY] = InheritedDict() with raises(StyleError): apply_styles(node, '', ['key'])
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, TkRenderingContext({'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(), TkRenderingContext({'parent_node': parent_node})) actual = parent_node.node_globals[STYLES_KEY] assert node_styles == actual
def test_applies_style_items(style_keys, expected_keys): """should apply style items""" node = Mock(node_globals=InheritedDict()) node_styles = {key: [Mock(apply=Mock())] for key in expected_keys} node.node_globals[STYLES_KEY] = InheritedDict(source=node_styles) apply_styles(node, '', style_keys) called = True for item in chain(*node_styles.values()): called = item.apply.called and called assert called
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(), Mock()) child = Mock(node_globals=InheritedDict()) child.node_globals[STYLES_KEY] = InheritedDict(view_styles) node.add_child(child) store_to_globals(node, TkRenderingContext({'parent_node': parent_node})) actual = parent_node.node_globals[STYLES_KEY].to_dictionary() assert expected == actual
def test_render_attribute(node_globals, xml_attr, setter, value): """should return setter and value""" node = Node(Mock(), node_globals=InheritedDict(node_globals)) actual = render_attribute(node, xml_attr) assert actual == (setter, value)
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 render_child_styles(node: Style, context: TkRenderingContext): """Renders child styles""" render_children(node, context, lambda x, n, ctx: TkRenderingContext({ 'parent_node': n, 'node_globals': InheritedDict(node.node_globals), 'node_styles': _get_styles(context), 'xml_node': x }))
def get_init_value(attr: XmlAttr, node_globals: InheritedDict) -> Any: """Evaluates attribute value and returns it""" stripped_value = attr.value.strip() if attr.value else '' if is_expression(stripped_value): body = parse_expression(stripped_value)[1] parameters = node_globals.to_dictionary() if node_globals else {} return execute(body, parameters) return attr.value
def _get_child_context(xml_node: XmlNode, node: WidgetNode, _: TkRenderingContext): """Renders child widgets""" child_context = TkRenderingContext() child_context.xml_node = xml_node child_context.parent_node = node child_context.master = node.instance child_context.node_globals = InheritedDict(node.node_globals) return child_context
def test_creates_node_styles(): """should add node_styles to parent globals""" parent_node: Node = Mock(node_globals=InheritedDict()) setup_node_styles(Mock(), TkRenderingContext({'parent_node': parent_node})) actual = parent_node.node_globals.get(STYLES_KEY) assert isinstance(actual, InheritedDict)
def store_to_globals(view: StylesView, context: TkRenderingContext): """Stores styles to parent node globals""" child: Node = view.children[0] styles: InheritedDict = child.node_globals[STYLES_KEY] if STYLES_KEY in context.parent_node.node_globals: parent_styles = context.parent_node.node_globals[STYLES_KEY] merged_styles = {**parent_styles.to_dictionary(), **styles.to_dictionary()} styles = InheritedDict(merged_styles) context.parent_node.node_globals[STYLES_KEY] = styles
def get_tk_child_context(child_xml_node: XmlNode, node: Node, context: TkRenderingContext) -> TkRenderingContext: """Return rendering context for child node""" return TkRenderingContext({ 'parent_node': node, 'master': context.master, 'node_globals': InheritedDict(node.node_globals), 'xml_node': child_xml_node })
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 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_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 check_binding_fixture(request): checkbox, vm = CheckBoxStub(), CheckViewModel() node = WxNode(checkbox, Mock(), InheritedDict({'vm': vm})) context = BindingContext() context.node = node context.xml_attr = XmlAttr('Value') context.setter = call_set_attr context.expression_body = "vm.value" request.cls.context = context request.cls.checkbox = checkbox request.cls.vm = vm
def text_binding_fixture(request): text, vm = TextEntryStub(), TextViewModel() node = WxNode(text, Mock(), InheritedDict({'vm': vm})) context = BindingContext() context.node = node context.xml_attr = XmlAttr('Value') context.setter = call_set_attr context.expression_body = "vm.text_value" request.cls.context = context request.cls.text = text request.cls.vm = vm
def get_wx_child_context(xml_node: XmlNode, parent_node: Node, context: WxRenderingContext) -> WxRenderingContext: """Return child node context""" return WxRenderingContext({ 'parent_node': parent_node, 'parent': context.parent, 'node_globals': InheritedDict(parent_node.node_globals), 'sizer': context.sizer, 'xml_node': xml_node })
def setup_node_styles(_: Style, context: TkRenderingContext): """Initializes node styles""" if STYLES_KEY not in context.parent_node.node_globals: context.parent_node.node_globals[STYLES_KEY] = InheritedDict()
def run_sandbox(): set_container(Container()) register_dependencies() node_globals = InheritedDict( {'views': SandboxViews(['events', 'sizers', 'binding'])}) launch(WxRenderingContext({'node_globals': node_globals}), 'app')