| 1 |
"""Tests parsers module.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from typing import TYPE_CHECKING |
| 6 |
from unittest.mock import Mock, patch |
| 7 |
|
| 8 |
import pytest |
| 9 |
|
| 10 |
from sphinx.parsers import RSTParser |
| 11 |
from sphinx.util.docutils import new_document |
| 12 |
|
| 13 |
if TYPE_CHECKING: |
| 14 |
from sphinx.testing.util import SphinxTestApp |
| 15 |
|
| 16 |
|
| 17 |
@pytest.mark.sphinx('html', testroot='basic') |
| 18 |
@patch('docutils.parsers.rst.states.RSTStateMachine') |
| 19 |
def test_RSTParser_prolog_epilog(RSTStateMachine: Mock, app: SphinxTestApp) -> None: |
| 20 |
document = new_document('dummy.rst') |
| 21 |
document.settings = Mock(tab_width=8, language_code='') |
| 22 |
parser = RSTParser() |
| 23 |
parser._config = app.config |
| 24 |
parser._env = app.env |
| 25 |
|
| 26 |
# normal case |
| 27 |
text = 'hello Sphinx world\nSphinx is a document generator' |
| 28 |
parser.parse(text, document) |
| 29 |
(content, _), _ = RSTStateMachine().run.call_args |
| 30 |
|
| 31 |
assert list(content.xitems()) == [ |
| 32 |
('dummy.rst', 0, 'hello Sphinx world'), |
| 33 |
('dummy.rst', 1, 'Sphinx is a document generator'), |
| 34 |
] |
| 35 |
|
| 36 |
# with rst_prolog |
| 37 |
app.config.rst_prolog = 'this is rst_prolog\nhello reST!' |
| 38 |
parser.parse(text, document) |
| 39 |
(content, _), _ = RSTStateMachine().run.call_args |
| 40 |
assert list(content.xitems()) == [ |
| 41 |
('<rst_prologue>', 0, 'this is rst_prolog'), |
| 42 |
('<rst_prologue>', 1, 'hello reST!'), |
| 43 |
('<generated>', 0, ''), |
| 44 |
('dummy.rst', 0, 'hello Sphinx world'), |
| 45 |
('dummy.rst', 1, 'Sphinx is a document generator'), |
| 46 |
] |
| 47 |
|
| 48 |
# with rst_epilog |
| 49 |
app.config.rst_prolog = None |
| 50 |
app.config.rst_epilog = 'this is rst_epilog\ngood-bye reST!' |
| 51 |
parser.parse(text, document) |
| 52 |
(content, _), _ = RSTStateMachine().run.call_args |
| 53 |
assert list(content.xitems()) == [ |
| 54 |
('dummy.rst', 0, 'hello Sphinx world'), |
| 55 |
('dummy.rst', 1, 'Sphinx is a document generator'), |
| 56 |
('dummy.rst', 2, ''), |
| 57 |
('<rst_epilogue>', 0, 'this is rst_epilog'), |
| 58 |
('<rst_epilogue>', 1, 'good-bye reST!'), |
| 59 |
] |
| 60 |
|
| 61 |
# expandtabs / convert whitespaces |
| 62 |
app.config.rst_prolog = None |
| 63 |
app.config.rst_epilog = None |
| 64 |
text = '\thello Sphinx world\n\v\fSphinx is a document generator' |
| 65 |
parser.parse(text, document) |
| 66 |
(content, _), _ = RSTStateMachine().run.call_args |
| 67 |
assert list(content.xitems()) == [ |
| 68 |
('dummy.rst', 0, ' hello Sphinx world'), |
| 69 |
('dummy.rst', 1, ' Sphinx is a document generator'), |
| 70 |
] |