Mercurial > piecrust2
diff 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 |
line wrap: on
line diff
--- a/tests/test_page.py Tue Oct 17 01:04:10 2017 -0700 +++ b/tests/test_page.py Tue Oct 17 01:07:30 2017 -0700 @@ -1,72 +1,67 @@ import pytest -from piecrust.page import parse_segments - +from piecrust.page import parse_segments, _count_lines test_parse_segments_data1 = ("", {'content': ''}) test_parse_segments_data2 = ("Foo bar", {'content': 'Foo bar'}) -test_parse_segments_data3 = ("""Something that spans -several lines -like this""", - {'content': """Something that spans +test_parse_segments_data3 = ( + """Something that spans + several lines + like this""", + {'content': """Something that spans several lines like this"""}) -test_parse_segments_data4 = ("""Blah blah ----foo--- -Something else ----bar--- -Last thing -""", - { - 'content': "Blah blah\n", - 'foo': "Something else\n", - 'bar': "Last thing\n"}) -test_parse_segments_data5 = ("""Blah blah -<--textile--> -Here's some textile -""", - { - 'content': [ - ("Blah blah\n", None), - ("Here's some textile\n", 'textile')]}) -test_parse_segments_data6 = ("""Blah blah -Whatever -<--textile--> -Oh well, that's good ----foo--- -Another segment -With another... -<--change--> -...of formatting. -""", - { - 'content': [ - ("Blah blah\nWhatever\n", None), - ("Oh well, that's good\n", 'textile')], - 'foo': [ - ("Another segment\nWith another...\n", None), - ("...of formatting.\n", 'change')]}) +test_parse_segments_data4 = ( + """Blah blah + ---foo--- + Something else + ---bar--- + Last thing + """, + { + 'content': "Blah blah\n", + 'foo': "Something else\n", + 'bar': "Last thing\n"}) + @pytest.mark.parametrize('text, expected', [ - test_parse_segments_data1, - test_parse_segments_data2, - test_parse_segments_data3, - test_parse_segments_data4, - test_parse_segments_data5, - test_parse_segments_data6, - ]) + test_parse_segments_data1, + test_parse_segments_data2, + test_parse_segments_data3, + test_parse_segments_data4, +]) def test_parse_segments(text, expected): actual = parse_segments(text) assert actual is not None assert list(actual.keys()) == list(expected.keys()) for key, val in expected.items(): - if isinstance(val, str): - assert len(actual[key].parts) == 1 - assert actual[key].parts[0].content == val - assert actual[key].parts[0].fmt is None - else: - assert len(actual[key].parts) == len(val) - for i, part in enumerate(val): - assert actual[key].parts[i].content == part[0] - assert actual[key].parts[i].fmt == part[1] + assert actual[key].content == val + assert actual[key].fmt is None + +@pytest.mark.parametrize('text, expected', [ + ('', 1), + ('\n', 2), + ('blah foo', 1), + ('blah foo\n', 2), + ('blah foo\nmore here', 2), + ('blah foo\nmore here\n', 3), + ('\nblah foo\nmore here\n', 3), +]) +def test_count_lines(text, expected): + actual = _count_lines(text) + assert actual == expected + + +@pytest.mark.parametrize('text, start, end, expected', [ + ('', 0, -1, 1), + ('\n', 1, -1, 1), + ('blah foo', 2, 4, 1), + ('blah foo\n', 2, 4, 1), + ('blah foo\nmore here', 4, -1, 2), + ('blah foo\nmore here\n', 10, -1, 2), + ('\nblah foo\nmore here\n', 2, -1, 3), +]) +def test_count_lines_with_offsets(text, start, end, expected): + actual = _count_lines(text) + assert actual == expected