wheezy.template / src / wheezy / template / tests / test_preprocessor.py

""" Unit tests for ``wheezy.templates.preprocessor.Preprocessor``.
"""

import unittest


class PreprocessorTestCase(unittest.TestCase):
    """ Test the ``Preprocessor``.
    """

    def setUp(self):
        from wheezy.template.engine import Engine
        from wheezy.template.preprocessor import Preprocessor
        from wheezy.template.loader import DictLoader
        from wheezy.template.ext.core import CoreExtension

        def runtime_engine_factory(loader):
            engine = Engine(
                loader=loader,
                extensions=[
                    CoreExtension(),
                ])
            return engine

        self.templates = {}
        engine = Engine(
            loader=DictLoader(templates=self.templates),
            extensions=[
                CoreExtension('#', line_join=None),
            ])
        self.engine = Preprocessor(runtime_engine_factory, engine,
                                   key_factory=lambda ctx: '')

    def render(self, name, ctx):
        template = self.engine.get_template(name)
        return template.render(ctx)

    def test_render(self):
        self.templates['test.html'] = """\
#require(_)
@require(username)
#_('Welcome,') @username!"""

        assert 'Welcome, John!' == self.render('test.html', ctx={
            '_': lambda x: x,
            'username': 'John'
        })

    def test_extends(self):
        self.templates.update({
            'master.html': """\
#require(_)
@def say_hi(name):
    #_('Hello,') @name!
@end
@say_hi('John')""",

            'tmpl.html': """\
#require(_)
@extends('master.html')
@def say_hi(name):
    #_('Hi,') @name!
@end
"""
        })

        assert '    Hi, John!\n' == self.render('tmpl.html', ctx={
            '_': lambda x: x,
        })

    def test_remove(self):
        self.templates['test.html'] = 'Hello'
        assert 'Hello' == self.render('test.html', {})
        self.engine.remove('x')
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.