Mercurial > piecrust2
annotate tests/test_page.py @ 1188:a7c43131d871
bake: Fix file write flushing problem with Python 3.8+
Writing the cache files fails in Python 3.8 because it looks like flushing
behaviour has changed. We need to explicitly flush. And even then, in very
rare occurrences, it looks like it can still run into racing conditions,
so we do a very hacky and ugly "retry" loop when fetching cached data :(
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Tue, 15 Jun 2021 22:36:23 -0700 |
parents | 45ad976712ec |
children |
rev | line source |
---|---|
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
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:
5
diff
changeset
|
2 from piecrust.page import parse_segments, _count_lines |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
3 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
4 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
5 test_parse_segments_data1 = ("", {'content': ''}) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
6 test_parse_segments_data2 = ("Foo bar", {'content': 'Foo bar'}) |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
7 test_parse_segments_data3 = ( |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
8 """Something that spans |
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
974
diff
changeset
|
9 several lines |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
974
diff
changeset
|
10 like this""", |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
11 {'content': """Something that spans |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
12 several lines |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
13 like this"""}) |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
14 test_parse_segments_data4 = ( |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
15 """Blah blah |
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
974
diff
changeset
|
16 ---foo--- |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
974
diff
changeset
|
17 Something else |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
974
diff
changeset
|
18 ---bar--- |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
974
diff
changeset
|
19 Last thing |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
974
diff
changeset
|
20 """, |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
21 { |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
22 'content': "Blah blah\n", |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
23 'foo': "Something else\n", |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
24 'bar': "Last thing\n"}) |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
25 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
26 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
27 @pytest.mark.parametrize('text, expected', [ |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
28 test_parse_segments_data1, |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
29 test_parse_segments_data2, |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
30 test_parse_segments_data3, |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
31 test_parse_segments_data4, |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
32 ]) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
33 def test_parse_segments(text, expected): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
34 actual = parse_segments(text) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
35 assert actual is not None |
5 | 36 assert list(actual.keys()) == list(expected.keys()) |
37 for key, val in expected.items(): | |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
38 assert actual[key].content == val |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
39 assert actual[key].fmt is None |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
40 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
41 |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
42 @pytest.mark.parametrize('text, expected', [ |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
43 ('', 1), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
44 ('\n', 2), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
45 ('blah foo', 1), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
46 ('blah foo\n', 2), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
47 ('blah foo\nmore here', 2), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
48 ('blah foo\nmore here\n', 3), |
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
974
diff
changeset
|
49 ('\nblah foo\nmore here\n', 4), |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
50 ]) |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
51 def test_count_lines(text, expected): |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
52 actual = _count_lines(text) |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
53 assert actual == expected |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
54 |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
55 |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
56 @pytest.mark.parametrize('text, start, end, expected', [ |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
57 ('', 0, -1, 1), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
58 ('\n', 1, -1, 1), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
59 ('blah foo', 2, 4, 1), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
60 ('blah foo\n', 2, 4, 1), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
61 ('blah foo\nmore here', 4, -1, 2), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
62 ('blah foo\nmore here\n', 10, -1, 2), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
63 ('\nblah foo\nmore here\n', 2, -1, 3), |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
64 ]) |
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
65 def test_count_lines_with_offsets(text, start, end, expected): |
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
974
diff
changeset
|
66 actual = _count_lines(text, start, end) |
974
72f17534d58e
tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
67 assert actual == expected |