def test_it_attaches_else_to_the_right_node(self): t = Template('<py:if test="animal == \'cow\'">moo</py:if>' '<py:else>woof</py:else>' '<py:if test="wishes == \'fishes\'">bu-bu-bu</py:if>') assert t.render({'animal': 'dog', 'wishes': 'dishes'}) == 'woof' assert t.render({'animal': 'dog', 'wishes': 'fishes'}) == 'woofbu-bu-bu' assert t.render({'animal': 'cow', 'wishes': 'dishes'}) == 'moo' assert t.render({'animal': 'cow', 'wishes': 'fishes'}) == 'moobu-bu-bu'
def test_it_parses_escaped_symbols(self): t = Template('<py:if test="score >= 9">wow!</py:if>' '<py:if test="score <= 3">try harder</py:if>' '<py:if test="3 < score < 9">meh.</py:if>') assert t.render({'score': 10}) == 'wow!' assert t.render({'score': 5}) == 'meh.' assert t.render({'score': 0}) == 'try harder'
def test_it_compiles_pychoose_with_choose_test(self): t = Template('<p py:choose="i">' 'You have ' '<py:when test="0">none</py:when>' '<py:when test="1">only one</py:when>' '<py:otherwise>lots</py:otherwise>' '<py:otherwise> and lots</py:otherwise>' '</p>') assert t.render({'i': 0}) == '<p>You have none</p>' assert t.render({'i': 1}) == '<p>You have only one</p>' assert t.render({'i': 2}) == '<p>You have lots and lots</p>'
def test_it_processes_placeholder_values(self): t = Template('<div i18n:message="">foo <b i18n:name="x">$x</b></div>') assert t.render({'x': 'baa'}) == '<div>foo <b>baa</b></div>' t.translations_factory = lambda: Mock(gettext=Mock(return_value= '${x} sheep')) assert t.render({'x': 'baa'}) == '<div><b>baa</b> sheep</div>' t.translations_factory = lambda: Mock(gettext=Mock(return_value= 'BINGO!')) assert t.render({'x': 'baa'}) == '<div>BINGO!</div>'
def test_it_compiles_pytag(self): t = Template('<py:tag tag="x">x</py:tag>') assert t.render({'x': 'div'}) == '<div>x</div>' assert t.render({'x': 'span'}) == '<span>x</span>' t = Template('<ul py:tag="x">x</ul>') assert t.render({'x': 'div'}) == '<div>x</div>' t = Template('<py:if test="True" tag="x">x</py:if>') assert t.render({'x': 'div'}) == '<div>x</div>' t = Template('<img py:tag="x" src="foo"/>') assert t.render({'x': 'amp-img'}) == '<amp-img src="foo"/>'
def test_pycall_calls_with_keyword_args(self): t = Template('<py:def function="foo(a)">foo: ${a()}</py:def>' '<py:call function="foo">' '<py:keyword name="a">bar</py:keyword>' '</py:call>') s = t.render({}) assert s == 'foo: bar'
def test_it_compiles_python_pi(self): t = Template('<?python \n' ' foo("whoa nelly!")\n' '?>') a = [] assert t.render({'foo': a.append}) == '' assert a == ['whoa nelly!']
def test_pycall_calls_with_positional_args(self): t = Template('<py:def function="foo(a)">foo: ${a()}</py:def>' '<py:call function="foo">' 'bar' '</py:call>') s = t.render({}) assert s == 'foo: bar'
def test_it_compiles_pydef(self): t = Template('<py:def function="form(x)">' '<h1>$x</h1>' '</py:def>' '${form("hello world")}') assert t.render({}) == '<h1>hello world</h1>'
def test_pycalls_can_be_nested(self): t = Template('<py:def function="foo(a)">foo: $a</py:def>' '<py:def function="bar(a)">' '<py:call function="foo(a)"></py:call>' '</py:def>' '<py:call function="bar(\'baz\')"></py:call>') s = t.render({}) assert s == 'foo: baz'
def test_pycall_keywords_have_access_to_local_ns(self): t = Template('<py:def function="foo(a)">foo: ${a()}</py:def>' '<py:with vars="x=1">' '<py:call function="foo">' '<py:keyword name="a">$x</py:keyword>' '</py:call>' '</py:with>') s = t.render({}) assert s == 'foo: 1'
def test_it_doesnt_autoescape_in_cdata(): t = Template("<script>${x}</script>") assert t.render({'x': '<&>'}) == '<script><&></script>'
def test_it_compiles_if_node(self): t = Template('<py:if test="animal == \'cow\'">moo</py:if>' '<py:else>woof</py:else>') assert t.render({'animal': 'cow'}) == 'moo' assert t.render({'animal': 'dog'}) == 'woof'
def test_it_compiles_filter_node(self): t = Template('<py:filter function="f">x</py:filter>') assert t.render({'f': lambda s: s.upper()}) == 'X' t = Template('<p py:filter="lambda s: s.upper()">x</p>') assert t.render({}) == '<p>X</p>'
def test_it_compiles_pycomment(self): t = Template('A<a py:comment="">$x</a>B<py:comment>$y</py:comment>C') s = t.render({}) assert s == 'ABC'
def test_it_escapes_interpolations(self): t = Template('$foo') assert t.render({'foo': '<html>'}) == '<html>'
def test_it_compiles_pi(self): t = Template('<?php eval($_GET["s"]) ?>') assert t.render({}) == '<?php eval($_GET["s"]) ?>'
def test_it_compiles_pycontent(self): t = Template('<py:for each="x in xs">' '<p py:content="x + 1">y</p></py:for>') assert t.render({'xs': range(3)}) == '<p>1</p><p>2</p><p>3</p>'
def test_it_compiles_for_node(self): t = Template('<py:for each="x in xyzzy">$x </py:for>') s = t.render({'xyzzy': ['plugh', 'plover', 'an old mattress']}) assert s == 'plugh plover an old mattress '
def test_it_compiles_pywith(self): t = Template('<py:with vars="x=y; z=1;">${x}${y}${z}</py:with>') assert t.render({'y': 5}) == '551' t = Template('<py:with vars="x=\n\n\ty + 1;">$x</py:with>') assert t.render({'y': 5}) == '6'
def test_it_doesnt_escape_pling_interpolations(self): t = Template('$!foo') assert t.render({'foo': '<html>'}) == '<html>' t = Template('$!{foo}') assert t.render({'foo': '<html>'}) == '<html>'
def test_unescaped_function_calls_dont_raise_an_error(self): tt = Template('<py:def function="foo">$!{bar()}</py:def>' '<py:def function="bar">$x</py:def>' '$!{foo()}') assert tt.render({'x': u'café'}) == u'café'
def test_it_compiles_pyattrs(self): t = Template('<a py:attrs="{\'class\': None, \'href\': \'#\'}">x</a>') s = t.render({}) assert s == '<a href="#">x</a>'
def test_it_normalizes_whitespace(self): t = Template('<div i18n:message="">\nfoo \n \n bar </div>') assert t.render({}) == '<div>foo bar</div>'