Ejemplo n.º 1
0
 def test_template_data(self, env):
     env = Environment(autoescape=True)
     t = env.from_string("{% macro say_hello(name) %}"
                         "<p>Hello {{ name }}!</p>{% endmacro %}"
                         '{{ say_hello("<blink>foo</blink>") }}')
     escaped_out = "<p>Hello &lt;blink&gt;foo&lt;/blink&gt;!</p>"
     assert t.render() == escaped_out
     assert text_type(t.module) == escaped_out
     assert escape(t.module) == escaped_out
     assert t.module.say_hello("<blink>foo</blink>") == escaped_out
     assert (escape(
         t.module.say_hello(EvalContext(env),
                            "<blink>foo</blink>")) == escaped_out)
     assert escape(t.module.say_hello("<blink>foo</blink>")) == escaped_out
Ejemplo n.º 2
0
    def test_markup_operations(self, env):
        # adding two strings should escape the unsafe one
        unsafe = '<script type="application/x-some-script">alert("foo");</script>'
        safe = Markup("<em>username</em>")
        assert unsafe + safe == text_type(escape(unsafe)) + text_type(safe)

        # string interpolations are safe to use too
        assert Markup(
            "<em>%s</em>") % "<bad user>" == "<em>&lt;bad user&gt;</em>"
        assert (Markup("<em>%(username)s</em>") % {
            "username": "******"
        } == "<em>&lt;bad user&gt;</em>")

        # an escaped object is markup too
        assert type(Markup("foo") + "bar") is Markup

        # and it implements __html__ by returning itself
        x = Markup("foo")
        assert x.__html__() is x

        # it also knows how to treat __html__ objects
        class Foo(object):
            def __html__(self):
                return "<em>awesome</em>"

            def __unicode__(self):
                return "awesome"

        assert Markup(Foo()) == "<em>awesome</em>"
        assert (Markup("<strong>%s</strong>") %
                Foo() == "<strong><em>awesome</em></strong>")

        # escaping and unescaping
        assert escape("\"<>&'") == "&#34;&lt;&gt;&amp;&#39;"
        assert Markup("<em>Foo &amp; Bar</em>").striptags() == "Foo & Bar"
        assert Markup("&lt;test&gt;").unescape() == "<test>"
Ejemplo n.º 3
0
    def test_loop_call_loop(self, env):
        tmpl = env.from_string("""

        {% macro test() %}
            {{ caller() }}
        {% endmacro %}

        {% for num1 in range(5) %}
            {% call test() %}
                {% for num2 in range(10) %}
                    {{ loop.index }}
                {% endfor %}
            {% endcall %}
        {% endfor %}

        """)

        assert tmpl.render().split() == [text_type(x)
                                         for x in range(1, 11)] * 5
Ejemplo n.º 4
0
 def test_operator_precedence(self, env):
     tmpl = env.from_string("""{{ 2 * 3 + 4 % 2 + 1 - 2 }}""")
     assert tmpl.render() == text_type(2 * 3 + 4 % 2 + 1 - 2)
Ejemplo n.º 5
0
 def test_string(self, env):
     x = [1, 2, 3, 4, 5]
     tmpl = env.from_string("""{{ obj|string }}""")
     assert tmpl.render(obj=x) == text_type(x)
Ejemplo n.º 6
0
 def __str__(self):
     return u"(%s,%s)" % (text_type(self.value1), text_type(self.value2))
Ejemplo n.º 7
0
 def __str__(self):
     return text_type(self.value)