예제 #1
0
def test_error_when_trying_to_style_non_existent_attribute():
    class Foo:
        @reinvokable
        def __init__(self):
            pass

        def __repr__(self):
            return '<Foo>'

    style = Namespace(something_that_does_not_exist='!!!')

    with pytest.raises(InvalidStyleConfigurationException) as e:
        apply_style_data(style_data=style, obj=Foo())

    assert str(
        e.value
    ) == "Object <Foo> could not be updated with style configuration {'something_that_does_not_exist': '!!!'}"
예제 #2
0
def test_style():
    class A(Traversable):
        @dispatch
        @reinvokable
        def __init__(self, **kwargs):
            super().__init__(**kwargs)

        foo = Refinable()
        bar = Refinable()

        @classmethod
        @class_shortcut
        def shortcut1(cls, call_target, **kwargs):
            return call_target(**kwargs)

        def items(self):
            return dict(foo=self.foo, bar=self.bar)

    class B(A):
        @dispatch
        @reinvokable
        def __init__(self, **kwargs):
            super().__init__(**kwargs)

        @classmethod
        @class_shortcut(call_target__attribute='shortcut1')
        def shortcut2(cls, call_target, **kwargs):
            return call_target(**kwargs)

    base = Style(A=dict(foo=1, ), )

    overrides = Style(
        base,
        A=dict(
            shortcuts=dict(shortcut1__foo=4, ),
            foo=5,
            bar=6,
        ),
        B=dict(bar=7, ),
    )

    # First the unstyled case
    assert items(B()) == dict(foo=None, bar=None)
    assert items(B.shortcut1()) == dict(foo=None, bar=None)
    assert items(B.shortcut2()) == dict(foo=None, bar=None)

    # Now let's add the style
    b = B()
    b = apply_style_data(style_data=overrides.component(b), obj=b)
    assert items(b) == dict(foo=5, bar=7)

    b = B.shortcut1()
    assert overrides.component(b) == dict(foo=4, bar=7)
    assert b.__tri_declarative_shortcut_stack == ['shortcut1']
    b = apply_style_data(style_data=overrides.component(b), obj=b)
    assert items(b) == dict(foo=4, bar=7)

    b = B.shortcut2()
    assert b.__tri_declarative_shortcut_stack == ['shortcut2', 'shortcut1']
    assert overrides.component(b) == dict(foo=4, bar=7)
    b = apply_style_data(style_data=overrides.component(b), obj=b)
    assert items(b) == dict(foo=4, bar=7)
예제 #3
0
def test_apply_style_data_does_not_overwrite():
    foo = Namespace(bar__template='specified')
    style = Namespace(bar__template='style_template')

    foo = apply_style_data(style_data=style, obj=foo)
    assert foo == Namespace(bar__template='specified')