def test_context_inheritance(self): base = Template("HEAD {% for i in '123' %}" "{%slot%}xxx{%endslot%}" "{%endfor%} FOOTER") final = Template("body {{i}}", parent=base) res = final.render() assert res == "HEAD body 1body 2body 3 FOOTER"
def test_2levels(self): base = Template("HEAD {%slot%}xxx{%endslot%} FOOTER") first = Template("This is the body {% slot %} yyy {% endslot %}", parent=base) second = Template("The end...", parent=first) res = second.render() assert res == "HEAD This is the body The end... FOOTER"
def test_multi_fill_sideeffect(self): """ if the child does not explicitly define a fill, it will be used for any / all slots """ base = Template("HEAD {%slot main%}xxx{%endslot%} - " "{% slot bar %}yyy{%endslot%} FOOTER") final = Template("Hello", parent=base) res = final.render() assert res == "HEAD Hello - Hello FOOTER"
def test_explicit3(self): base = Template("HEAD {%slot main%}xxx{%endslot%} FOOTER") final = Template( "{%fill main%}This is the body{%endfill%}" "{%fill other%}Other fill{%endfill%}", parent=base) res = final.render() assert res == "HEAD This is the body FOOTER"
def test_multi_fill(self): base = Template("HEAD {%slot main%}xxx{%endslot%} - " "{% slot bar %}yyy{%endslot%} FOOTER") final = Template( "Hello " "{%fill main %}This is the main block" "{% endfill %} and " "{%fill bar %}this is the bar block" "{%endfill %}", parent=base) res = final.render() assert res == "HEAD This is the main block - this is the bar block FOOTER"
def test_loop(self): tpl = Template("{% for i in seq%}" "{%if loop.first%}{{i}} is first{%endif%}" "{%if loop.last%}{{i}} is last{%endif%}" "{{i}}{{loop.index}}-{{loop.index0}}" "{%endfor%}") res = tpl.render(seq="abcde") assert res == "a is firsta0-1b1-2c2-3d3-4e is laste4-5"
def test_total_madness1(self): base = Template("HEAD {%slot a%} aa {% endslot %}" "{%for i in '123'%}" "{%slot b %}{%endslot%}" "{%endfor%}" "FOOTER") c1 = Template( "{%fill a%}A{%endfill%}" "noise" "{%fill b%}" "{{i}}{%slot c%}..{%endslot%}" "{%endfill%}", parent=base) c2 = Template("Default", parent=c1) res = c2.render() assert res == "HEAD A1Default2Default3DefaultFOOTER"
def test_accolade(self): tpl = """if {} { {! {% for i in '123' %}{{i}}{%endfor%} { }} {=""" template = Template(tpl) assert isinstance(template.mainnode.nodes[0], TextNode) assert template.mainnode.nodes[0].text == "if {} { {! " assert isinstance(template.mainnode.nodes[1], ForBlockStatementNode) assert isinstance(template.mainnode.nodes[2], TextNode) assert template.mainnode.nodes[2].text == " { }} {="
def test_attraction_marker2(self): base = Template(""" <html> <style> p, th, td { font-family: 'Open Sans', sans-serif; } </style> <body> {% slot content %}{% endslot %} </body> </html> """) final = Template("<<MARKER>>", parent=base) res = final.render() assert '<<MARKER>>' in res
def test_multiline(self): tpl = """Hello world bye!""" template = Template(tpl) assert isinstance(template.mainnode, MainNode) assert isinstance(template.mainnode.nodes[0], TextNode) assert template.mainnode.nodes[0].text == tpl
def test_complex(self): tpl = """Hello World {% for i in abc%} x {{i}} {% endfor %} That's all!""" t = Template(tpl) t.render(abc="123")
def test_css(self): tpl = """<html> <style> p, th, td { font-family: 'Open Sans', sans-serif; }</style> </html> """ template = Template(tpl) assert isinstance(template.mainnode, MainNode) assert isinstance(template.mainnode.nodes[0], TextNode) assert template.mainnode.nodes[0].text == tpl
def test_render_text(self): tpl = Template("Hello World\n" "\n" "{% for i in abc%}\n" " x {{i}}\n" "{% endfor %}\n" "\n" "That's all!") rendered = tpl.render_nested(i=1, abc=[1]) assert rendered[0] == "Hello World\n\n" assert rendered[1][0] == "\n x " assert rendered[1][2] == "\n" assert rendered[2] == "\n\nThat's all!"
def test_complex(self): tpl = """Hello World {% for i in abc%} x {{i}} {% endfor %} That's all!""" t = Template(tpl) assert len(t.mainnode.nodes) == 3 nodes = t.mainnode.nodes assert isinstance(nodes[0], TextNode) assert isinstance(nodes[1], BlockStatementNode) assert isinstance(nodes[2], TextNode)
def test_multi_line_indent(self): tpl = """Hello World this is a test a {%if } more noise ... """ with pytest.raises(ParseError) as e: Template(tpl) exc = e.value assert exc.pc.offset == 39 assert exc.pc.code.startswith("{%if") line, col = exc.pc.position() assert line == 4 assert col == 11
def test_string_expression_special2(self): tpl = Template("{{'{%hello%}'}}") assert tpl.render() == '{%hello%}'
def test_simple(self): tpl = Template("{%for i in j%}{{i}}{% endfor %}") assert tpl.render(j="a") == "a" assert tpl.render(j=[1, 2, 3]) == "123"
def test_expression(self): tpl = Template("{{i + j}}") assert tpl.render(i=10, j=5) == "15" assert tpl.render(i="Hello", j="World") == "HelloWorld"
def test_string_expression(self): tpl = Template("{{'hello'}}") assert tpl.render() == 'hello'
def test_empty(self): template = Template("") assert isinstance(template.mainnode, MainNode) assert len(template.mainnode.nodes) == 0
def test_interpolation(self): tpl = Template("{{i}}") assert tpl.render(i=10) == "10" assert tpl.render(i=0) == "0" assert tpl.render(i=-10) == "-10" assert tpl.render(i="hello") == "hello"
def test_no_child(self): """ rendering a base template with slot without a child providing a fill """ tpl = "1\n\n{% slot %}hello{%endslot%}" res = Template(tpl).render() assert res == "1\n\nhello"
def test_nondefault(self): base = Template("HEAD {%slot foo%}xxx{%endslot%} FOOTER") final = Template("{%fill foo%}This is the body{%endfill%}", parent=base) res = final.render() assert res == "HEAD This is the body FOOTER"
def test_missing(self): base = Template("HEAD {%slot foo%}xxx{%endslot%} FOOTER") final = Template("{%fill main%}This is the body{%endfill%}", parent=base) res = final.render() assert res == "HEAD xxx FOOTER"
def test_simple(self): base = Template("HEAD {%slot%}xxx{%endslot%} FOOTER") final = Template("This is the body", parent=base) res = final.render() assert res == "HEAD This is the body FOOTER"
def test_toplevel_else(self): """ else cannot be used by itself """ with pytest.raises(ParseError) as e: Template("{%else%}").render() assert e.value.pc.tag == 'else'
def test_else(self): tpl = Template("{%if bool%}TRUE{%else%}FALSE{%endif%}") assert tpl.render(bool=True) == "TRUE" assert tpl.render(bool=False) == "FALSE"
def test_context_stacking(self): """ this is more of a context feature """ tpl = Template("{%for i in j%}{{i}}{%endfor%}{{i}}") assert tpl.render(i="z", j=["a"]) == "az"
def test_simple(self): tpl = Template("{%if bool%}TRUE{%endif%}") assert tpl.render(bool=True) == "TRUE" assert tpl.render(bool=False) == ""
def test_attraction_marker(self): base = Template("HEAD {% slot content %}xxx{% endslot %} FOOTER") final = Template("<<MARKER>>", parent=base) res = final.render() assert res == "HEAD <<MARKER>> FOOTER"