| 1 | import docutils |
| 2 | import pytest |
| 3 | from rich_rst import RestructuredText |
| 4 | from pathlib import Path |
| 5 | from rich.console import Console |
| 6 | from rich.terminal_theme import TerminalTheme |
| 7 | |
| 8 | test_vectors_path = Path("tests/test_vectors") |
| 9 | rst_paths = sorted(str(x) for x in test_vectors_path.glob("*.rst")) |
| 10 | docutils_0_22_mark = pytest.mark.skipif( |
| 11 | docutils.__version_info__ < (0, 22), |
| 12 | reason="requires docutils 0.22 or higher", |
| 13 | ) |
| 14 | |
| 15 | |
| 16 | def render_to_html(rst): |
| 17 | DRACULA_TERMINAL_THEME = TerminalTheme( |
| 18 | (40, 42, 54), |
| 19 | (248, 248, 242), |
| 20 | [ |
| 21 | (40, 42, 54), |
| 22 | (255, 85, 85), |
| 23 | (80, 250, 123), |
| 24 | (241, 250, 140), |
| 25 | (189, 147, 249), |
| 26 | (255, 121, 198), |
| 27 | (139, 233, 253), |
| 28 | (255, 255, 255), |
| 29 | ], |
| 30 | [ |
| 31 | (40, 42, 54), |
| 32 | (255, 85, 85), |
| 33 | (80, 250, 123), |
| 34 | (241, 250, 140), |
| 35 | (189, 147, 249), |
| 36 | (255, 121, 198), |
| 37 | (139, 233, 253), |
| 38 | (255, 255, 255), |
| 39 | ], |
| 40 | ) |
| 41 | console = Console(force_terminal=True, width=120, record=True) |
| 42 | console.print(rst) |
| 43 | return console.export_html(theme=DRACULA_TERMINAL_THEME) |
| 44 | |
| 45 | |
| 46 | @pytest.mark.parametrize( |
| 47 | "rst_path", |
| 48 | [ |
| 49 | ( |
| 50 | pytest.param(path, marks=docutils_0_22_mark) |
| 51 | if path.endswith(("directives.rst", "specification.rst")) |
| 52 | else path |
| 53 | ) |
| 54 | for path in rst_paths |
| 55 | ], |
| 56 | ) |
| 57 | def test_main(rst_path): |
| 58 | rst_path = Path(rst_path) |
| 59 | actual_html_path = rst_path.parent / (rst_path.stem + "_actual.html") |
| 60 | expected_html_path = rst_path.parent / (rst_path.stem + "_expected.html") |
| 61 | |
| 62 | rst = RestructuredText(rst_path.read_text(), show_errors=True) |
| 63 | actual_html = render_to_html(rst) |
| 64 | actual_html_path.write_text(actual_html) |
| 65 | |
| 66 | expected_html = expected_html_path.read_text() |
| 67 | assert expected_html == actual_html |