def test_it_applies_whitespace_control(s1, s2, s3, s4, w1, w2, w3, w4): source = ( 'A{s1}{{%{w1} if 1 {w2}%}}{s2}B{s3}{{%{w3}end{w4}%}}{s4}C'.format( **locals())) tt = TextTemplate(source) expected = 'A' if w1 == '+': expected += s1 elif w1 == '': expected += re.sub(r'\n[ \t]*\Z', r'\n', s1) if w2 == '+': expected += s2 elif w2 == '': expected += re.sub(r'\A[ \t]*\n', r'', s2) expected += 'B' if w3 == '+': expected += s3 elif w3 == '': expected += re.sub(r'\n[ \t]*\Z', r'\n', s3) if w4 == '+': expected += s4 elif w4 == '': expected += re.sub(r'\A[ \t]*\n', r'', s4) expected += 'C' result = tt.render({}) assert result == expected
def test_it_preserves_whitespace_between_directives(): tt = TextTemplate('{% if 1 %}\n' 'foo\n' '{% end %}\n' '\n' '{% if 2 %}\n' 'bar\n' '{% end %}\n') assert tt.render({}) == 'foo\n\nbar\n'
def test_it_doesnt_html_escape(): tt = TextTemplate('$x') assert tt.render({'x': '&'}) == '&'
def test_it_strips_whitespace_when_directive_tag_alone_on_line(): tt = TextTemplate('A \n\n {% if 1 %} \n B \n {%end%} \n C\n') assert tt.render({}) == 'A \n\n B \n C\n'
def test_it_compiles_for(): tt = TextTemplate('{% for x in xs %}$x! {%end%}') assert tt.render({'xs': [1, 2, 3]}) == '1! 2! 3! '
def test_it_compiles_def(): tt = TextTemplate('{% def foo(x) %}$x $x $x{%end%}${foo("a")}') assert tt.render({}) == 'a a a'
def test_it_compiles_with(): tt = TextTemplate('{% with x = "1"; y = "2"; z=y %}${x + y + z}{% end %}') assert tt.render({}) == '122'
def test_it_compiles_if_else(): tt = TextTemplate('{% if x > 1 %}yes{% else %}no{%end%}') assert tt.render({'x': 2}) == 'yes' assert tt.render({'x': 1}) == 'no'
def test_it_compiles_quoted_if_conditions(): tt = TextTemplate('{% if foo == "bar" %}yes{%end%}') assert tt.render({'foo': 'bar'}) == 'yes' assert tt.render({'foo': 'baz'}) == ''