comparison tests/test_data_linker.py @ 237:879fe1457e48

data: `Linker` refactor. * Unify the `Linker` and `RecursiveLinker`. * When a page and a directory share the same name, merge their entries in the returned iterator. * Tentative new templating interface.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 15 Feb 2015 22:46:23 -0800
parents 701591ebfcba
children b51ddb0c260b
comparison
equal deleted inserted replaced
236:eaf18442bff8 237:879fe1457e48
1 import pytest 1 import pytest
2 from piecrust.data.linker import Linker, RecursiveLinker 2 from piecrust.data.linker import Linker
3 from .mockutil import mock_fs, mock_fs_scope 3 from .mockutil import mock_fs, mock_fs_scope
4 4
5 5
6 @pytest.mark.parametrize( 6 @pytest.mark.parametrize(
7 'fs, page_path, expected', 7 'fs, page_path, expected',
8 [ 8 [
9 (mock_fs().withPage('pages/foo'), 'foo.md', 9 (mock_fs().withPage('pages/foo'), 'foo.md',
10 [('/foo', True, False)]), 10 # is_dir, name, is_self, data
11 [(False, 'foo', True, '/foo')]),
11 ((mock_fs() 12 ((mock_fs()
12 .withPage('pages/foo') 13 .withPage('pages/foo')
13 .withPage('pages/bar')), 14 .withPage('pages/bar')),
14 'foo.md', 15 'foo.md',
15 [('/bar', False, False), ('/foo', True, False)]), 16 [(False, 'bar', False, '/bar'), (False, 'foo', True, '/foo')]),
16 ((mock_fs() 17 ((mock_fs()
17 .withPage('pages/baz') 18 .withPage('pages/baz')
19 .withPage('pages/something')
18 .withPage('pages/something/else') 20 .withPage('pages/something/else')
19 .withPage('pages/foo') 21 .withPage('pages/foo')
20 .withPage('pages/bar')), 22 .withPage('pages/bar')),
21 'foo.md', 23 'foo.md',
22 [('/bar', False, False), ('/baz', False, False), 24 [(False, 'bar', False, '/bar'),
23 ('/foo', True, False), ('something', False, True)]), 25 (False, 'baz', False, '/baz'),
26 (False, 'foo', True, '/foo'),
27 (True, 'something', False, '/something')]),
24 ((mock_fs() 28 ((mock_fs()
25 .withPage('pages/something/else') 29 .withPage('pages/something/else')
26 .withPage('pages/foo') 30 .withPage('pages/foo')
27 .withPage('pages/something/good') 31 .withPage('pages/something/good')
28 .withPage('pages/bar')), 32 .withPage('pages/bar')),
29 'something/else.md', 33 'something/else.md',
30 [('/something/else', True, False), 34 [(False, 'else', True, '/something/else'),
31 ('/something/good', False, False)]) 35 (False, 'good', False, '/something/good')])
32 ]) 36 ])
33 def test_linker_iteration(fs, page_path, expected): 37 def test_linker_iteration(fs, page_path, expected):
34 with mock_fs_scope(fs): 38 with mock_fs_scope(fs):
35 app = fs.getApp() 39 app = fs.getApp()
36 src = app.getSource('pages') 40 src = app.getSource('pages')
37 linker = Linker(src, page_path=page_path) 41 linker = Linker(src, page_path=page_path)
38 actual = list(iter(linker)) 42 actual = list(iter(linker))
39 43
40 assert len(actual) == len(expected) 44 assert len(actual) == len(expected)
41 for i, (a, e) in enumerate(zip(actual, expected)): 45 for (a, e) in zip(actual, expected):
42 assert a.is_dir == e[2] 46 is_dir, name, is_self, url = e
43 if a.is_dir: 47 assert a.is_dir == is_dir
44 assert a.name == e[0] 48 assert a.name == name
45 else: 49 assert a.is_self == is_self
46 assert a.url == e[0] 50 assert a.url == url
47 assert a.is_self == e[1]
48 51
49 52
50 @pytest.mark.parametrize( 53 @pytest.mark.parametrize(
51 'fs, page_path, expected', 54 'fs, page_path, expected',
52 [ 55 [
76 ]) 79 ])
77 def test_recursive_linker_iteration(fs, page_path, expected): 80 def test_recursive_linker_iteration(fs, page_path, expected):
78 with mock_fs_scope(fs): 81 with mock_fs_scope(fs):
79 app = fs.getApp() 82 app = fs.getApp()
80 src = app.getSource('pages') 83 src = app.getSource('pages')
81 linker = RecursiveLinker(src, page_path=page_path) 84 linker = Linker(src, page_path=page_path)
82 actual = list(iter(linker)) 85 actual = list(iter(linker.allpages))
83 86
84 assert len(actual) == len(expected) 87 assert len(actual) == len(expected)
85 for i, (a, e) in enumerate(zip(actual, expected)): 88 for i, (a, e) in enumerate(zip(actual, expected)):
86 assert a.is_dir is False 89 assert a.is_dir is False
87 assert a.url == e[0] 90 assert a.url == e[0]