예제 #1
0
 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"
예제 #2
0
 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"
예제 #3
0
 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"
예제 #4
0
 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"
예제 #5
0
 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"
예제 #6
0
 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"
예제 #7
0
    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"
예제 #8
0
 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 == " { }} {="
예제 #9
0
    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
예제 #10
0
    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
예제 #11
0
    def test_complex(self):
        tpl = """Hello World

        {% for i in abc%}
          x {{i}}
        {% endfor %}

        That's all!"""
        t = Template(tpl)
        t.render(abc="123")
예제 #12
0
    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
예제 #13
0
 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!"
예제 #14
0
    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)
예제 #15
0
    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
예제 #16
0
 def test_string_expression_special2(self):
     tpl = Template("{{'{%hello%}'}}")
     assert tpl.render() == '{%hello%}'
예제 #17
0
 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"
예제 #18
0
 def test_expression(self):
     tpl = Template("{{i + j}}")
     assert tpl.render(i=10, j=5) == "15"
     assert tpl.render(i="Hello", j="World") == "HelloWorld"
예제 #19
0
 def test_string_expression(self):
     tpl = Template("{{'hello'}}")
     assert tpl.render() == 'hello'
예제 #20
0
 def test_empty(self):
     template = Template("")
     assert isinstance(template.mainnode, MainNode)
     assert len(template.mainnode.nodes) == 0
예제 #21
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"
예제 #22
0
 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"
예제 #23
0
 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"
예제 #24
0
 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"
예제 #25
0
 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"
예제 #26
0
 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'
예제 #27
0
 def test_else(self):
     tpl = Template("{%if bool%}TRUE{%else%}FALSE{%endif%}")
     assert tpl.render(bool=True) == "TRUE"
     assert tpl.render(bool=False) == "FALSE"
예제 #28
0
 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"
예제 #29
0
 def test_simple(self):
     tpl = Template("{%if bool%}TRUE{%endif%}")
     assert tpl.render(bool=True) == "TRUE"
     assert tpl.render(bool=False) == ""
예제 #30
0
 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"