Mercurial > piecrust2
comparison tests/test_templating_pystacheengine.py @ 974:72f17534d58e
tests: First pass on making unit tests work again.
- Fix all imports
- Add more helper functions to work with mock file-systems
- Simplify some code by running chef directly on the mock FS
- Fix a couple tests
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Tue, 17 Oct 2017 01:07:30 -0700 |
parents | ab5c6a8ae90a |
children | 45ad976712ec |
comparison
equal
deleted
inserted
replaced
973:8419daaa7a0e | 974:72f17534d58e |
---|---|
1 import pytest | 1 import pytest |
2 from .mockutil import ( | 2 from .mockutil import mock_fs, mock_fs_scope |
3 mock_fs, mock_fs_scope, get_simple_page, render_simple_page) | 3 from .rdrutil import render_simple_page |
4 | 4 |
5 | 5 |
6 app_config = { | 6 app_config = { |
7 'site': { | 7 'site': { |
8 'default_format': 'none', | 8 'default_format': 'none', |
9 'default_template_engine': 'mustache'}, | 9 'default_template_engine': 'mustache'}, |
10 'foo': 'bar'} | 10 'foo': 'bar'} |
11 page_config = {'layout': 'none'} | 11 page_config = {'layout': 'none'} |
12 | 12 |
13 open_patches = ['pystache.common'] | 13 open_patches = ['pystache.common'] |
14 | 14 |
15 | 15 |
16 @pytest.mark.parametrize( | 16 @pytest.mark.parametrize( |
17 'contents, expected', | 17 'contents, expected', |
18 [ | 18 [ |
19 ("Raw text", "Raw text"), | 19 ("Raw text", "Raw text"), |
20 ("This is {{foo}}", "This is bar"), | 20 ("This is {{foo}}", "This is bar"), |
21 ("Info:\n{{#page}}\nMy URL: {{url}}\n{{/page}}\n", | 21 ("Info:\n{{#page}}\nMy URL: {{url}}\n{{/page}}\n", |
22 "Info:\nMy URL: /foo.html\n") | 22 "Info:\nMy URL: /foo.html\n") |
23 ]) | 23 ]) |
24 def test_simple(contents, expected): | 24 def test_simple(contents, expected): |
25 fs = (mock_fs() | 25 fs = (mock_fs() |
26 .withConfig(app_config) | 26 .withConfig(app_config) |
27 .withPage('pages/foo', config=page_config, contents=contents)) | 27 .withPage('pages/foo', config=page_config, contents=contents)) |
28 with mock_fs_scope(fs, open_patches=open_patches): | 28 with mock_fs_scope(fs, open_patches=open_patches): |
29 app = fs.getApp() | 29 app = fs.getApp() |
30 page = get_simple_page(app, 'foo.md') | 30 page = fs.getSimplePage('foo.md') |
31 route = app.getSourceRoute('pages', None) | 31 route = app.getSourceRoute('pages', None) |
32 route_metadata = {'slug': 'foo'} | 32 route_metadata = {'slug': 'foo'} |
33 output = render_simple_page(page, route, route_metadata) | 33 output = render_simple_page(page, route, route_metadata) |
34 assert output == expected | 34 assert output == expected |
35 | 35 |
37 def test_layout(): | 37 def test_layout(): |
38 contents = "Blah\n" | 38 contents = "Blah\n" |
39 layout = "{{content}}\nFor site: {{foo}}\n" | 39 layout = "{{content}}\nFor site: {{foo}}\n" |
40 expected = "Blah\n\nFor site: bar\n" | 40 expected = "Blah\n\nFor site: bar\n" |
41 fs = (mock_fs() | 41 fs = (mock_fs() |
42 .withConfig(app_config) | 42 .withConfig(app_config) |
43 .withAsset('templates/blah.mustache', layout) | 43 .withAsset('templates/blah.mustache', layout) |
44 .withPage('pages/foo', config={'layout': 'blah'}, | 44 .withPage('pages/foo', config={'layout': 'blah'}, |
45 contents=contents)) | 45 contents=contents)) |
46 with mock_fs_scope(fs, open_patches=open_patches): | 46 with mock_fs_scope(fs, open_patches=open_patches): |
47 app = fs.getApp() | 47 app = fs.getApp() |
48 page = get_simple_page(app, 'foo.md') | 48 page = fs.getSimplePage('foo.md') |
49 route = app.getSourceRoute('pages', None) | 49 route = app.getSourceRoute('pages', None) |
50 route_metadata = {'slug': 'foo'} | 50 route_metadata = {'slug': 'foo'} |
51 output = render_simple_page(page, route, route_metadata) | 51 output = render_simple_page(page, route, route_metadata) |
52 # On Windows, pystache unexplicably adds `\r` to some newlines... wtf. | 52 # On Windows, pystache unexplicably adds `\r` to some newlines... wtf. |
53 output = output.replace('\r', '') | 53 output = output.replace('\r', '') |
57 def test_partial(): | 57 def test_partial(): |
58 contents = "Info:\n{{#page}}\n{{> page_info}}\n{{/page}}\n" | 58 contents = "Info:\n{{#page}}\n{{> page_info}}\n{{/page}}\n" |
59 partial = "- URL: {{url}}\n- SLUG: {{slug}}\n" | 59 partial = "- URL: {{url}}\n- SLUG: {{slug}}\n" |
60 expected = "Info:\n- URL: /foo.html\n- SLUG: foo\n" | 60 expected = "Info:\n- URL: /foo.html\n- SLUG: foo\n" |
61 fs = (mock_fs() | 61 fs = (mock_fs() |
62 .withConfig(app_config) | 62 .withConfig(app_config) |
63 .withAsset('templates/page_info.mustache', partial) | 63 .withAsset('templates/page_info.mustache', partial) |
64 .withPage('pages/foo', config=page_config, contents=contents)) | 64 .withPage('pages/foo', config=page_config, contents=contents)) |
65 with mock_fs_scope(fs, open_patches=open_patches): | 65 with mock_fs_scope(fs, open_patches=open_patches): |
66 app = fs.getApp() | 66 app = fs.getApp() |
67 page = get_simple_page(app, 'foo.md') | 67 page = fs.getSimplePage('foo.md') |
68 route = app.getSourceRoute('pages', None) | 68 route = app.getSourceRoute('pages', None) |
69 route_metadata = {'slug': 'foo'} | 69 route_metadata = {'slug': 'foo'} |
70 output = render_simple_page(page, route, route_metadata) | 70 output = render_simple_page(page, route, route_metadata) |
71 # On Windows, pystache unexplicably adds `\r` to some newlines... wtf. | 71 # On Windows, pystache unexplicably adds `\r` to some newlines... wtf. |
72 output = output.replace('\r', '') | 72 output = output.replace('\r', '') |