Mercurial > piecrust2
annotate tests/test_templating_pystacheengine.py @ 1037:89d94955b818
serve: Fix infinite loop in asset processing when a change is detected.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Tue, 16 Jan 2018 08:41:25 -0800 |
parents | 45ad976712ec |
children |
rev | line source |
---|---|
185
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
1 import pytest |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
2 from .mockutil import mock_fs, mock_fs_scope |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
3 from .rdrutil import render_simple_page |
185
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
4 |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
5 |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
6 app_config = { |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
7 'site': { |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
8 'default_format': 'none', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
9 'default_template_engine': 'mustache'}, |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
10 'foo': 'bar'} |
185
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
11 page_config = {'layout': 'none'} |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
12 |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
13 open_patches = ['pystache.common'] |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
14 |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
15 |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
16 @pytest.mark.parametrize( |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
17 'contents, expected', |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
18 [ |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
19 ("Raw text", "Raw text"), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
20 ("This is {{foo}}", "This is bar"), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
21 ("Info:\n{{#page}}\nMy URL: {{url}}\n{{/page}}\n", |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
22 "Info:\nMy URL: /foo.html\n") |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
23 ]) |
185
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
24 def test_simple(contents, expected): |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
25 fs = (mock_fs() |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
26 .withConfig(app_config) |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
27 .withPage('pages/foo', config=page_config, contents=contents)) |
185
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
28 with mock_fs_scope(fs, open_patches=open_patches): |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
29 page = fs.getSimplePage('foo.md') |
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
974
diff
changeset
|
30 output = render_simple_page(page) |
185
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
31 assert output == expected |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
32 |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
33 |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
34 def test_layout(): |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
35 contents = "Blah\n" |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
36 layout = "{{content}}\nFor site: {{foo}}\n" |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
37 expected = "Blah\n\nFor site: bar\n" |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
38 fs = (mock_fs() |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
39 .withConfig(app_config) |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
40 .withAsset('templates/blah.mustache', layout) |
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
974
diff
changeset
|
41 .withPage('pages/foo', config={'layout': 'blah.mustache'}, |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
42 contents=contents)) |
185
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
43 with mock_fs_scope(fs, open_patches=open_patches): |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
44 page = fs.getSimplePage('foo.md') |
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
974
diff
changeset
|
45 output = render_simple_page(page) |
494
2537fe95d771
tests: Fix the Mustache tests on Windows.
Ludovic Chabant <ludovic@chabant.com>
parents:
369
diff
changeset
|
46 # On Windows, pystache unexplicably adds `\r` to some newlines... wtf. |
2537fe95d771
tests: Fix the Mustache tests on Windows.
Ludovic Chabant <ludovic@chabant.com>
parents:
369
diff
changeset
|
47 output = output.replace('\r', '') |
185
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
48 assert output == expected |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
49 |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
50 |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
51 def test_partial(): |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
52 contents = "Info:\n{{#page}}\n{{> page_info}}\n{{/page}}\n" |
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
974
diff
changeset
|
53 partial = "- URL: {{url}}\n- SLUG: {{route.slug}}\n" |
369
4b1019bb2533
serve: Giant refactor to change how we handle data when serving pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
185
diff
changeset
|
54 expected = "Info:\n- URL: /foo.html\n- SLUG: foo\n" |
185
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
55 fs = (mock_fs() |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
56 .withConfig(app_config) |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
57 .withAsset('templates/page_info.mustache', partial) |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
58 .withPage('pages/foo', config=page_config, contents=contents)) |
185
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
59 with mock_fs_scope(fs, open_patches=open_patches): |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
60 page = fs.getSimplePage('foo.md') |
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
974
diff
changeset
|
61 output = render_simple_page(page) |
494
2537fe95d771
tests: Fix the Mustache tests on Windows.
Ludovic Chabant <ludovic@chabant.com>
parents:
369
diff
changeset
|
62 # On Windows, pystache unexplicably adds `\r` to some newlines... wtf. |
2537fe95d771
tests: Fix the Mustache tests on Windows.
Ludovic Chabant <ludovic@chabant.com>
parents:
369
diff
changeset
|
63 output = output.replace('\r', '') |
185
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
64 assert output == expected |
139179dc7abd
render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
65 |