def test_text_plugin(): text_elem = MockElement('#text') text_elem.text = "Hello {{ name }}" tp = TextPlugin(text_elem) c = Context({}) elem = tp.bind_ctx(c) assert elem.text == "Hello " c.name = "Jonathan" assert tp._dirty_self is True assert tp.update() is None assert tp._dirty_self is False assert elem.text == "Hello Jonathan" tp2 = tp.clone() c2 = Context({'name':'Ansa'}) elem2 = tp2.bind_ctx(c2) assert elem2.text == "Hello Ansa" assert elem.text == "Hello Jonathan" assert tp._dirty_self is False text_elem = MockElement('#text') text_elem.text = "Hello" tp = _compile(text_elem) c = Context({}) elem = tp.bind_ctx(c) assert elem.text == "Hello" c.name = "Jonathan" assert tp.update() is None assert elem.text == "Hello"
def test_incomplete(): ctx = Context() s = InterpolatedStr("Ahoj {{ name }} {{ surname }}!") assert s.value == "Ahoj !" ctx.name = "Jonathan" s.bind_ctx(ctx) assert s.value == "Ahoj Jonathan !" ctx.surname = "Verner" assert s.value == "Ahoj Jonathan Verner!"
def test_string_interp(): ctx = Context() ctx.name = "James" s = InterpolatedStr("My name is {{ surname }}, {{name}} {{ surname}}.") s.bind_ctx(ctx) t = TObserver(s) assert s.value == "My name is , James ." ctx.surname = "Bond" data = t.events.pop().data assert s.value == "My name is Bond, James Bond." # Should correctly interpolate two immediately succeeding expressions ctx.sur = "B" s = InterpolatedStr('{{name}}{{sur}}') s.bind_ctx(ctx) assert s.value == "JamesB"
def test_parse_interpolated_string(): ctx = Context() ctx.name = 'Name' asts = exp.parse_interpolated_str( 'Test text {{ 1+3 }} other text {{ "ahoj" }} final text.') val = "".join([ast.evalctx(ctx) for ast in asts]) assert val == 'Test text 4 other text ahoj final text.' asts = exp.parse_interpolated_str( 'Test text {{ 1+3 }} other text {{ name }} final text.') val = "".join([ast.evalctx(ctx) for ast in asts]) assert val == 'Test text 4 other text Name final text.' asts = exp.parse_interpolated_str( 'Test text {{ 1+3 }} other text {{ len(name) }} final text.') val = "".join([ast.evalctx(ctx) for ast in asts]) assert val == 'Test text 4 other text 4 final text.' asts = exp.parse_interpolated_str( 'Test text {{ "{{{{}}{}{}}}" }} other }}') val = "".join([ast.evalctx(ctx) for ast in asts]) assert val == 'Test text {{{{}}{}{}}} other }}'
def test_interpolated_attrs_plugin(): text_elem = MockElement('#text') text_elem.text = "{{ name }}" t2_elem = MockElement('#text') t2_elem.text = "ahoj" div_elem = MockElement('div', id="test") div_elem <= text_elem div_elem <= t2_elem plug = InterpolatedAttrsPlugin(div_elem) ctx = Context({}) elem = plug.bind_ctx(ctx) assert elem.attributes.id == "test" div_elem.setAttribute('id', "{{ id }}") plug = InterpolatedAttrsPlugin(div_elem) ctx = Context({}) elem = plug.bind_ctx(ctx) text_elem = elem.children[0] com_elem = elem.children[1] t2_elem = elem.children[2] assert elem.attributes.id == "" assert com_elem.tagName == 'comment' assert text_elem.text == "" assert t2_elem.text == "ahoj" assert plug._dirty_self is False assert plug._dirty_subtree is False ctx.id = "test_id" assert plug._dirty_self is True assert plug._dirty_subtree is False assert plug.update() is None assert elem.attributes.id == "test_id" ctx.name = "Jonathan" assert plug._dirty_self is False assert plug._dirty_subtree is True assert plug.update() is None assert text_elem.text == "Jonathan" assert plug._dirty_subtree is False div_elem.attributes.append(MockAttr('id', '{{ id }}')) plug = _compile(div_elem) ctx = Context({}) elem = plug.bind_ctx(ctx) assert elem.attributes.id == "" ctx.id = "test_id" assert plug.update() is None assert elem.attributes.id == "test_id" div_elem.attributes.extend([ MockAttr('id', '{{ id }}'), MockAttr('style', '{{ css }}'), MockAttr('name', '???') ]) plug = _compile(div_elem) ctx = Context({}) elem = plug.bind_ctx(ctx) assert elem.attributes.id == "" assert elem.attributes.style == "" assert elem.attributes.name == "???" ctx.css = "border:1px;" assert plug.update() is None assert elem.attributes.style == "border:1px;"