comparison tests/test_templating_jinjaengine.py @ 189:a333cdadf5b0

tests: Add tests for Jinja template engine.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 04 Jan 2015 21:55:36 -0800
parents
children 4b1019bb2533
comparison
equal deleted inserted replaced
188:31eaffd0afd7 189:a333cdadf5b0
1 import pytest
2 from .mockutil import (
3 mock_fs, mock_fs_scope, get_simple_page, render_simple_page)
4
5
6 app_config = {
7 'site': {
8 'default_format': 'none',
9 'default_template_engine': 'jinja'},
10 'foo': 'bar'}
11 page_config = {'layout': 'none'}
12
13 open_patches = ['jinja2.environment', 'jinja2.utils']
14
15
16 @pytest.mark.parametrize(
17 'contents, expected',
18 [
19 ("Raw text", "Raw text"),
20 ("This is {{foo}}", "This is bar"),
21 ("Info:\nMy URL: {{page.url}}\n",
22 "Info:\nMy URL: /foo")
23 ])
24 def test_simple(contents, expected):
25 fs = (mock_fs()
26 .withConfig(app_config)
27 .withPage('pages/foo', config=page_config, contents=contents))
28 with mock_fs_scope(fs, open_patches=open_patches):
29 app = fs.getApp()
30 page = get_simple_page(app, 'foo.md')
31 output = render_simple_page(page, '/foo')
32 assert output == expected
33
34
35 def test_layout():
36 contents = "Blah\n"
37 layout = "{{content}}\nFor site: {{foo}}\n"
38 expected = "Blah\nFor site: bar"
39 fs = (mock_fs()
40 .withConfig(app_config)
41 .withAsset('templates/blah.jinja', layout)
42 .withPage('pages/foo', config={'layout': 'blah'},
43 contents=contents))
44 with mock_fs_scope(fs, open_patches=open_patches):
45 app = fs.getApp()
46 page = get_simple_page(app, 'foo.md')
47 output = render_simple_page(page, '/foo')
48 assert output == expected
49
50
51 def test_partial():
52 contents = "Info:\n{% include 'page_info.jinja' %}\n"
53 partial = "- URL: {{page.url}}\n- SLUG: {{page.slug}}\n"
54 expected = "Info:\n- URL: /foo\n- SLUG: foo"
55 fs = (mock_fs()
56 .withConfig(app_config)
57 .withAsset('templates/page_info.jinja', partial)
58 .withPage('pages/foo', config=page_config, contents=contents))
59 with mock_fs_scope(fs, open_patches=open_patches):
60 app = fs.getApp()
61 page = get_simple_page(app, 'foo.md')
62 output = render_simple_page(page, '/foo')
63 assert output == expected
64