2022-02-19 21:05:56 -06:00
|
|
|
"""Tests sphinx.util.rst functions."""
|
2018-11-25 05:23:04 -06:00
|
|
|
|
2024-08-12 16:34:03 -05:00
|
|
|
import pytest
|
2018-11-25 05:23:04 -06:00
|
|
|
from docutils.statemachine import StringList
|
2019-05-01 23:46:10 -05:00
|
|
|
from jinja2 import Environment
|
2018-11-25 05:23:04 -06:00
|
|
|
|
2020-11-11 05:00:27 -06:00
|
|
|
from sphinx.util.rst import append_epilog, escape, heading, prepend_prolog, textwidth
|
2016-08-29 10:37:30 -05:00
|
|
|
|
|
|
|
|
|
|
|
def test_escape():
|
2017-02-12 11:02:51 -06:00
|
|
|
assert escape(':ref:`id`') == r'\:ref\:\`id\`'
|
|
|
|
assert escape('footnote [#]_') == r'footnote \[\#\]\_'
|
2018-01-28 03:58:58 -06:00
|
|
|
assert escape('sphinx.application') == r'sphinx.application'
|
|
|
|
assert escape('.. toctree::') == r'\.. toctree\:\:'
|
2018-11-25 05:23:04 -06:00
|
|
|
|
|
|
|
|
2024-08-12 16:34:03 -05:00
|
|
|
@pytest.mark.sphinx('html', testroot='root')
|
2018-11-25 05:23:04 -06:00
|
|
|
def test_append_epilog(app):
|
|
|
|
epilog = 'this is rst_epilog\ngood-bye reST!'
|
2024-08-11 08:58:56 -05:00
|
|
|
content = StringList(
|
|
|
|
['hello Sphinx world', 'Sphinx is a document generator'],
|
|
|
|
'dummy.rst',
|
|
|
|
)
|
2018-11-25 05:23:04 -06:00
|
|
|
append_epilog(content, epilog)
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
assert list(content.xitems()) == [
|
|
|
|
('dummy.rst', 0, 'hello Sphinx world'),
|
|
|
|
('dummy.rst', 1, 'Sphinx is a document generator'),
|
|
|
|
('dummy.rst', 2, ''),
|
|
|
|
('<rst_epilog>', 0, 'this is rst_epilog'),
|
|
|
|
('<rst_epilog>', 1, 'good-bye reST!'),
|
|
|
|
]
|
2018-11-25 05:23:04 -06:00
|
|
|
|
|
|
|
|
2024-08-12 16:34:03 -05:00
|
|
|
@pytest.mark.sphinx('html', testroot='root')
|
2018-11-25 05:23:04 -06:00
|
|
|
def test_prepend_prolog(app):
|
|
|
|
prolog = 'this is rst_prolog\nhello reST!'
|
2024-08-11 08:58:56 -05:00
|
|
|
content = StringList(
|
|
|
|
[
|
|
|
|
':title: test of SphinxFileInput',
|
|
|
|
':author: Sphinx team',
|
|
|
|
'',
|
|
|
|
'hello Sphinx world',
|
|
|
|
'Sphinx is a document generator',
|
|
|
|
],
|
|
|
|
'dummy.rst',
|
|
|
|
)
|
2018-11-25 05:23:04 -06:00
|
|
|
prepend_prolog(content, prolog)
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
assert list(content.xitems()) == [
|
|
|
|
('dummy.rst', 0, ':title: test of SphinxFileInput'),
|
|
|
|
('dummy.rst', 1, ':author: Sphinx team'),
|
|
|
|
('<generated>', 0, ''),
|
|
|
|
('<rst_prolog>', 0, 'this is rst_prolog'),
|
|
|
|
('<rst_prolog>', 1, 'hello reST!'),
|
|
|
|
('<generated>', 0, ''),
|
|
|
|
('dummy.rst', 2, ''),
|
|
|
|
('dummy.rst', 3, 'hello Sphinx world'),
|
|
|
|
('dummy.rst', 4, 'Sphinx is a document generator'),
|
|
|
|
]
|
2018-11-25 05:23:04 -06:00
|
|
|
|
|
|
|
|
2024-08-12 16:34:03 -05:00
|
|
|
@pytest.mark.sphinx('html', testroot='root')
|
2018-11-25 05:23:04 -06:00
|
|
|
def test_prepend_prolog_with_CR(app):
|
|
|
|
# prolog having CR at tail
|
|
|
|
prolog = 'this is rst_prolog\nhello reST!\n'
|
2024-08-11 08:58:56 -05:00
|
|
|
content = StringList(
|
|
|
|
['hello Sphinx world', 'Sphinx is a document generator'],
|
|
|
|
'dummy.rst',
|
|
|
|
)
|
2018-11-25 05:23:04 -06:00
|
|
|
prepend_prolog(content, prolog)
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
assert list(content.xitems()) == [
|
|
|
|
('<rst_prolog>', 0, 'this is rst_prolog'),
|
|
|
|
('<rst_prolog>', 1, 'hello reST!'),
|
|
|
|
('<generated>', 0, ''),
|
|
|
|
('dummy.rst', 0, 'hello Sphinx world'),
|
|
|
|
('dummy.rst', 1, 'Sphinx is a document generator'),
|
|
|
|
]
|
2018-11-25 05:23:04 -06:00
|
|
|
|
|
|
|
|
2024-08-12 16:34:03 -05:00
|
|
|
@pytest.mark.sphinx('html', testroot='root')
|
2018-11-25 05:23:04 -06:00
|
|
|
def test_prepend_prolog_without_CR(app):
|
|
|
|
# prolog not having CR at tail
|
|
|
|
prolog = 'this is rst_prolog\nhello reST!'
|
2024-08-11 08:58:56 -05:00
|
|
|
content = StringList(
|
|
|
|
['hello Sphinx world', 'Sphinx is a document generator'],
|
|
|
|
'dummy.rst',
|
|
|
|
)
|
2018-11-25 05:23:04 -06:00
|
|
|
prepend_prolog(content, prolog)
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
assert list(content.xitems()) == [
|
|
|
|
('<rst_prolog>', 0, 'this is rst_prolog'),
|
|
|
|
('<rst_prolog>', 1, 'hello reST!'),
|
|
|
|
('<generated>', 0, ''),
|
|
|
|
('dummy.rst', 0, 'hello Sphinx world'),
|
|
|
|
('dummy.rst', 1, 'Sphinx is a document generator'),
|
|
|
|
]
|
2019-05-01 23:46:10 -05:00
|
|
|
|
|
|
|
|
2024-08-12 16:34:03 -05:00
|
|
|
@pytest.mark.sphinx('html', testroot='root')
|
2023-07-23 11:14:01 -05:00
|
|
|
def test_prepend_prolog_with_roles_in_sections(app):
|
|
|
|
prolog = 'this is rst_prolog\nhello reST!'
|
2024-08-11 08:58:56 -05:00
|
|
|
content = StringList(
|
|
|
|
[
|
|
|
|
':title: test of SphinxFileInput',
|
|
|
|
':author: Sphinx team',
|
|
|
|
'', # this newline is required
|
|
|
|
':mod:`foo`',
|
|
|
|
'----------',
|
|
|
|
'',
|
|
|
|
'hello',
|
|
|
|
],
|
|
|
|
'dummy.rst',
|
|
|
|
)
|
2023-07-23 11:14:01 -05:00
|
|
|
prepend_prolog(content, prolog)
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
assert list(content.xitems()) == [
|
|
|
|
('dummy.rst', 0, ':title: test of SphinxFileInput'),
|
|
|
|
('dummy.rst', 1, ':author: Sphinx team'),
|
|
|
|
('<generated>', 0, ''),
|
|
|
|
('<rst_prolog>', 0, 'this is rst_prolog'),
|
|
|
|
('<rst_prolog>', 1, 'hello reST!'),
|
|
|
|
('<generated>', 0, ''),
|
|
|
|
('dummy.rst', 2, ''),
|
|
|
|
('dummy.rst', 3, ':mod:`foo`'),
|
|
|
|
('dummy.rst', 4, '----------'),
|
|
|
|
('dummy.rst', 5, ''),
|
|
|
|
('dummy.rst', 6, 'hello'),
|
|
|
|
]
|
2023-07-23 11:14:01 -05:00
|
|
|
|
|
|
|
|
2024-08-12 16:34:03 -05:00
|
|
|
@pytest.mark.sphinx('html', testroot='root')
|
2023-07-23 11:14:01 -05:00
|
|
|
def test_prepend_prolog_with_roles_in_sections_with_newline(app):
|
|
|
|
# prologue with trailing line break
|
|
|
|
prolog = 'this is rst_prolog\nhello reST!\n'
|
|
|
|
content = StringList([':mod:`foo`', '-' * 10, '', 'hello'], 'dummy.rst')
|
|
|
|
prepend_prolog(content, prolog)
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
assert list(content.xitems()) == [
|
|
|
|
('<rst_prolog>', 0, 'this is rst_prolog'),
|
|
|
|
('<rst_prolog>', 1, 'hello reST!'),
|
|
|
|
('<generated>', 0, ''),
|
|
|
|
('dummy.rst', 0, ':mod:`foo`'),
|
|
|
|
('dummy.rst', 1, '----------'),
|
|
|
|
('dummy.rst', 2, ''),
|
|
|
|
('dummy.rst', 3, 'hello'),
|
|
|
|
]
|
2023-07-23 11:14:01 -05:00
|
|
|
|
|
|
|
|
2024-08-12 16:34:03 -05:00
|
|
|
@pytest.mark.sphinx('html', testroot='root')
|
2023-07-23 11:14:01 -05:00
|
|
|
def test_prepend_prolog_with_roles_in_sections_without_newline(app):
|
|
|
|
# prologue with no trailing line break
|
|
|
|
prolog = 'this is rst_prolog\nhello reST!'
|
|
|
|
content = StringList([':mod:`foo`', '-' * 10, '', 'hello'], 'dummy.rst')
|
|
|
|
prepend_prolog(content, prolog)
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
assert list(content.xitems()) == [
|
|
|
|
('<rst_prolog>', 0, 'this is rst_prolog'),
|
|
|
|
('<rst_prolog>', 1, 'hello reST!'),
|
|
|
|
('<generated>', 0, ''),
|
|
|
|
('dummy.rst', 0, ':mod:`foo`'),
|
|
|
|
('dummy.rst', 1, '----------'),
|
|
|
|
('dummy.rst', 2, ''),
|
|
|
|
('dummy.rst', 3, 'hello'),
|
|
|
|
]
|
2023-07-23 11:14:01 -05:00
|
|
|
|
|
|
|
|
2019-05-01 23:46:10 -05:00
|
|
|
def test_textwidth():
|
|
|
|
assert textwidth('Hello') == 5
|
|
|
|
assert textwidth('русский язык') == 12
|
|
|
|
assert textwidth('русский язык', 'WFA') == 23 # Cyrillic are ambiguous chars
|
|
|
|
|
|
|
|
|
|
|
|
def test_heading():
|
|
|
|
env = Environment()
|
|
|
|
env.extend(language=None)
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
assert heading(env, 'Hello') == 'Hello\n====='
|
|
|
|
assert heading(env, 'Hello', 1) == 'Hello\n====='
|
|
|
|
assert heading(env, 'Hello', 2) == 'Hello\n-----'
|
|
|
|
assert heading(env, 'Hello', 3) == 'Hello\n~~~~~'
|
|
|
|
assert heading(env, 'русский язык', 1) == 'русский язык\n============'
|
2019-05-01 23:46:10 -05:00
|
|
|
|
|
|
|
# language=ja: ambiguous
|
|
|
|
env.language = 'ja'
|
2024-08-11 08:58:56 -05:00
|
|
|
assert heading(env, 'русский язык', 1) == 'русский язык\n======================='
|