wheezy.html / src / wheezy / html / ext / tests / test_jinja2.py

""" Unit tests for ``wheezy.html.ext.jinja2``.
"""

import unittest

from wheezy.html.ext.tests.test_lexer import PreprocessorMixin


class Jinja2PreprocessorTestCase(PreprocessorMixin, unittest.TestCase):
    """ Test the ``Jinja2Preprocessor``.
    """

    def assert_render_equal(self, template, expected, **kwargs):
        assert_jinja2_equal({
            'variable_start_string': '{{',
            'variable_end_string': '}}'
        }, template, expected, **kwargs)

    HIDDEN = '{{ model.pref.hidden()|e }}'
    MULTIPLE_HIDDEN = '{{ model.prefs.multiple_hidden() }}'
    LABEL = "{{ model.username.label('<i>*</i>Username:') }}"
    EMPTYBOX = "{{ model.amount.emptybox(class_='x')|e }}"
    TEXTBOX = "{{ model.username.textbox(autocomplete='off')|e }}"
    PASSWORD = "{{ model.pwd.password()|e }}"
    TEXTAREA = "{{ model.comment.textarea()|e }}"
    CHECKBOX = "{{ model.remember_me.checkbox() }}"
    MULTIPLE_CHECKBOX = "{{ model.scm.multiple_checkbox(choices=scm) }}"
    RADIO = "{{ model.scm.radio(choices=scm) }}"
    DROPDOWN = "{{ model.scm.dropdown(choices=scm) }}"
    LISTBOX = "{{ model.scm.listbox(choices=scm, class_='x') }}"
    ERROR = "{{ model.username.error() }}"
    GENERAL_ERROR = "{{ model.error() }}"
    INFO = "{{ model.user_info.info() }}"
    GENERAL_INFO = "{{ message.info()|e }}"
    WARNING = "{{ model.user_info.warning() }}"
    GENERAL_WARNING = "{{ message.warning()|e }}"


class Jinja2PreprocessorTestCase2(PreprocessorMixin, unittest.TestCase):
    """ Test the ``Jinja2Preprocessor``.
    """

    def assert_render_equal(self, template, expected, **kwargs):
        assert_jinja2_equal({
            'variable_start_string': '${',
            'variable_end_string': '}'
        }, template, expected, **kwargs)

    HIDDEN = '${model.pref.hidden()|e}'
    MULTIPLE_HIDDEN = '${model.prefs.multiple_hidden()}'
    LABEL = "${model.username.label('<i>*</i>Username:')}"
    EMPTYBOX = "${model.amount.emptybox(class_='x')|e}"
    TEXTBOX = "${model.username.textbox(autocomplete='off')|e}"
    PASSWORD = "${model.pwd.password()|e}"
    TEXTAREA = "${model.comment.textarea()|e}"
    CHECKBOX = "${model.remember_me.checkbox()}"
    MULTIPLE_CHECKBOX = "${model.scm.multiple_checkbox(choices=scm)}"
    RADIO = "${model.scm.radio(choices=scm)}"
    DROPDOWN = "${model.scm.dropdown(choices=scm)}"
    LISTBOX = "${model.scm.listbox(choices=scm, class_='x')}"
    ERROR = "${model.username.error()}"
    GENERAL_ERROR = "${model.error()}"
    INFO = "${model.user_info.info()}"
    GENERAL_INFO = "${message.info()|e}"
    WARNING = "${model.user_info.warning()}"
    GENERAL_WARNING = "${message.warning()|e}"


class Jinja2WhitespaceExtensionTestCase(unittest.TestCase):
    """ Test the ``WhitespaceExtension``.
    """

    block_start_string = '{%'
    block_end_string = '%}'

    def setUp(self):
        from wheezy.html.ext.jinja2 import WhitespaceExtension
        extension = WhitespaceExtension(self)
        self.preprocess = lambda s: extension.preprocess(s, None)

    def test_whitespace(self):
        """
        """
        assert ' x' == self.preprocess(' x')
        assert 'x' == self.preprocess('  \n x \n  ')
        assert 'x' == self.preprocess('  x')
        assert 'x' == self.preprocess('x  ')
        assert '><' == self.preprocess('  > < ')
        assert '>' + self.block_start_string == self.preprocess(
            '>  ' + self.block_start_string)
        assert self.block_end_string + '<' == self.preprocess(
            self.block_end_string + '  <')


class Jinja2WhitespaceExtensionTestCase2(Jinja2WhitespaceExtensionTestCase):
    """ Test the ``WhitespaceExtension``.
    """

    block_start_string = '<%'
    block_end_string = '%>'


try:
    # from jinja2 import Environment
    Environment = __import__('jinja2', None, None,
                             ['Environment']).Environment
    from wheezy.html.ext.jinja2 import WidgetExtension

    def assert_jinja2_equal(options, text, expected, **kwargs):
        template = Environment(
            extensions=[WidgetExtension],
            **options
        ).from_string(text)
        value = template.render(kwargs)
        assert expected == value

except ImportError:  # pragma: nocover

    def assert_jinja2_equal(text, expected, **kwargs):  # noqa
        pass
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.