comparison tests/test_templating_pystacheengine.py @ 185:139179dc7abd

render: Add support for a Mustache template engine. Add unit tests for the new class.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 04 Jan 2015 14:59:12 -0800
parents
children 4b1019bb2533
comparison
equal deleted inserted replaced
184:27d623a241c6 185:139179dc7abd
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': 'mustache'},
10 'foo': 'bar'}
11 page_config = {'layout': 'none'}
12
13 open_patches = ['pystache.common']
14
15
16 @pytest.mark.parametrize(
17 'contents, expected',
18 [
19 ("Raw text", "Raw text"),
20 ("This is {{foo}}", "This is bar"),
21 ("Info:\n{{#page}}\nMy URL: {{url}}\n{{/page}}\n",
22 "Info:\nMy URL: /foo\n")
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\n\nFor site: bar\n"
39 fs = (mock_fs()
40 .withConfig(app_config)
41 .withAsset('templates/blah.mustache', 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{{#page}}\n{{> page_info}}\n{{/page}}\n"
53 partial = "- URL: {{url}}\n- SLUG: {{slug}}\n"
54 expected = "Info:\n- URL: /foo\n- SLUG: foo\n"
55 fs = (mock_fs()
56 .withConfig(app_config)
57 .withAsset('templates/page_info.mustache', 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