comparison tests/test_page.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 474c9882decf
children 45ad976712ec
comparison
equal deleted inserted replaced
973:8419daaa7a0e 974:72f17534d58e
1 import pytest 1 import pytest
2 from piecrust.page import parse_segments 2 from piecrust.page import parse_segments, _count_lines
3
4 3
5 4
6 test_parse_segments_data1 = ("", {'content': ''}) 5 test_parse_segments_data1 = ("", {'content': ''})
7 test_parse_segments_data2 = ("Foo bar", {'content': 'Foo bar'}) 6 test_parse_segments_data2 = ("Foo bar", {'content': 'Foo bar'})
8 test_parse_segments_data3 = ("""Something that spans 7 test_parse_segments_data3 = (
9 several lines 8 """Something that spans
10 like this""", 9 several lines
11 {'content': """Something that spans 10 like this""",
11 {'content': """Something that spans
12 several lines 12 several lines
13 like this"""}) 13 like this"""})
14 test_parse_segments_data4 = ("""Blah blah 14 test_parse_segments_data4 = (
15 ---foo--- 15 """Blah blah
16 Something else 16 ---foo---
17 ---bar--- 17 Something else
18 Last thing 18 ---bar---
19 """, 19 Last thing
20 { 20 """,
21 'content': "Blah blah\n", 21 {
22 'foo': "Something else\n", 22 'content': "Blah blah\n",
23 'bar': "Last thing\n"}) 23 'foo': "Something else\n",
24 test_parse_segments_data5 = ("""Blah blah 24 'bar': "Last thing\n"})
25 <--textile--> 25
26 Here's some textile
27 """,
28 {
29 'content': [
30 ("Blah blah\n", None),
31 ("Here's some textile\n", 'textile')]})
32 test_parse_segments_data6 = ("""Blah blah
33 Whatever
34 <--textile-->
35 Oh well, that's good
36 ---foo---
37 Another segment
38 With another...
39 <--change-->
40 ...of formatting.
41 """,
42 {
43 'content': [
44 ("Blah blah\nWhatever\n", None),
45 ("Oh well, that's good\n", 'textile')],
46 'foo': [
47 ("Another segment\nWith another...\n", None),
48 ("...of formatting.\n", 'change')]})
49 26
50 @pytest.mark.parametrize('text, expected', [ 27 @pytest.mark.parametrize('text, expected', [
51 test_parse_segments_data1, 28 test_parse_segments_data1,
52 test_parse_segments_data2, 29 test_parse_segments_data2,
53 test_parse_segments_data3, 30 test_parse_segments_data3,
54 test_parse_segments_data4, 31 test_parse_segments_data4,
55 test_parse_segments_data5, 32 ])
56 test_parse_segments_data6,
57 ])
58 def test_parse_segments(text, expected): 33 def test_parse_segments(text, expected):
59 actual = parse_segments(text) 34 actual = parse_segments(text)
60 assert actual is not None 35 assert actual is not None
61 assert list(actual.keys()) == list(expected.keys()) 36 assert list(actual.keys()) == list(expected.keys())
62 for key, val in expected.items(): 37 for key, val in expected.items():
63 if isinstance(val, str): 38 assert actual[key].content == val
64 assert len(actual[key].parts) == 1 39 assert actual[key].fmt is None
65 assert actual[key].parts[0].content == val
66 assert actual[key].parts[0].fmt is None
67 else:
68 assert len(actual[key].parts) == len(val)
69 for i, part in enumerate(val):
70 assert actual[key].parts[i].content == part[0]
71 assert actual[key].parts[i].fmt == part[1]
72 40
41
42 @pytest.mark.parametrize('text, expected', [
43 ('', 1),
44 ('\n', 2),
45 ('blah foo', 1),
46 ('blah foo\n', 2),
47 ('blah foo\nmore here', 2),
48 ('blah foo\nmore here\n', 3),
49 ('\nblah foo\nmore here\n', 3),
50 ])
51 def test_count_lines(text, expected):
52 actual = _count_lines(text)
53 assert actual == expected
54
55
56 @pytest.mark.parametrize('text, start, end, expected', [
57 ('', 0, -1, 1),
58 ('\n', 1, -1, 1),
59 ('blah foo', 2, 4, 1),
60 ('blah foo\n', 2, 4, 1),
61 ('blah foo\nmore here', 4, -1, 2),
62 ('blah foo\nmore here\n', 10, -1, 2),
63 ('\nblah foo\nmore here\n', 2, -1, 3),
64 ])
65 def test_count_lines_with_offsets(text, start, end, expected):
66 actual = _count_lines(text)
67 assert actual == expected