Source

jinja2-main / tests / test_ifcondition.py

# -*- coding: utf-8 -*-
"""
    unit test for if conditions
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~

    :copyright: (c) 2009 by the Jinja Team.
    :license: BSD, see LICENSE for more details.
"""

from jinja2 import Environment
env = Environment()


SIMPLE = '''{% if true %}...{% endif %}'''
ELIF = '''{% if false %}XXX{% elif true %}...{% else %}XXX{% endif %}'''
ELSE = '''{% if false %}XXX{% else %}...{% endif %}'''
EMPTY = '''[{% if true %}{% else %}{% endif %}]'''


def test_simple():
    tmpl = env.from_string(SIMPLE)
    assert tmpl.render() == '...'


def test_elif():
    tmpl = env.from_string(ELIF)
    assert tmpl.render() == '...'


def test_else():
    tmpl = env.from_string(ELSE)
    assert tmpl.render() == '...'


def test_empty():
    tmpl = env.from_string(EMPTY)
    assert tmpl.render() == '[]'


def test_complete():
    tmpl = env.from_string('{% if a %}A{% elif b %}B{% elif c == d %}'
                           'C{% else %}D{% endif %}')
    assert tmpl.render(a=0, b=False, c=42, d=42.0) == 'C'


def test_no_scope():
    tmpl = env.from_string('{% if a %}{% set foo = 1 %}{% endif %}{{ foo }}')
    assert tmpl.render(a=True) == '1'
    tmpl = env.from_string('{% if true %}{% set foo = 1 %}{% endif %}{{ foo }}')
    assert tmpl.render() == '1'
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.