1"""
2    test_sphinx_parsers
3    ~~~~~~~~~~~~~~~~~~~
4
5    Tests parsers module.
6
7    :copyright: Copyright 2007-2021 by the Sphinx team, see AUTHORS.
8    :license: BSD, see LICENSE for details.
9"""
10
11from unittest.mock import Mock, patch
12
13import pytest
14
15from sphinx.parsers import RSTParser
16from sphinx.util.docutils import new_document
17
18
19@pytest.mark.sphinx(testroot='basic')
20@patch('docutils.parsers.rst.states.RSTStateMachine')
21def test_RSTParser_prolog_epilog(RSTStateMachine, app):
22    document = new_document('dummy.rst')
23    document.settings = Mock(tab_width=8, language_code='')
24    parser = RSTParser()
25    parser.set_application(app)
26
27    # normal case
28    text = ('hello Sphinx world\n'
29            'Sphinx is a document generator')
30    parser.parse(text, document)
31    (content, _), _ = RSTStateMachine().run.call_args
32
33    assert list(content.xitems()) == [('dummy.rst', 0, 'hello Sphinx world'),
34                                      ('dummy.rst', 1, 'Sphinx is a document generator')]
35
36    # with rst_prolog
37    app.env.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()) == [('<rst_prolog>', 0, 'this is rst_prolog'),
41                                      ('<rst_prolog>', 1, 'hello reST!'),
42                                      ('<generated>', 0, ''),
43                                      ('dummy.rst', 0, 'hello Sphinx world'),
44                                      ('dummy.rst', 1, 'Sphinx is a document generator')]
45
46    # with rst_epilog
47    app.env.config.rst_prolog = None
48    app.env.config.rst_epilog = 'this is rst_epilog\ngood-bye reST!'
49    parser.parse(text, document)
50    (content, _), _ = RSTStateMachine().run.call_args
51    assert list(content.xitems()) == [('dummy.rst', 0, 'hello Sphinx world'),
52                                      ('dummy.rst', 1, 'Sphinx is a document generator'),
53                                      ('dummy.rst', 2, ''),
54                                      ('<rst_epilog>', 0, 'this is rst_epilog'),
55                                      ('<rst_epilog>', 1, 'good-bye reST!')]
56
57    # expandtabs / convert whitespaces
58    app.env.config.rst_prolog = None
59    app.env.config.rst_epilog = None
60    text = ('\thello Sphinx world\n'
61            '\v\fSphinx is a document generator')
62    parser.parse(text, document)
63    (content, _), _ = RSTStateMachine().run.call_args
64    assert list(content.xitems()) == [('dummy.rst', 0, '        hello Sphinx world'),
65                                      ('dummy.rst', 1, '  Sphinx is a document generator')]
66