2018-04-27 21:39:13 -05:00
|
|
|
"""Test sphinx.roles"""
|
|
|
|
|
2024-11-22 15:54:26 -06:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-03-07 08:35:36 -06:00
|
|
|
from unittest.mock import Mock
|
|
|
|
|
2024-10-05 22:29:11 -05:00
|
|
|
import pytest
|
2018-04-27 21:39:13 -05:00
|
|
|
from docutils import nodes
|
|
|
|
|
2024-10-05 22:29:11 -05:00
|
|
|
from sphinx.roles import EmphasizedLiteral, _format_rfc_target
|
2018-04-27 21:39:13 -05:00
|
|
|
from sphinx.testing.util import assert_node
|
|
|
|
|
|
|
|
|
2025-02-15 17:23:56 -06:00
|
|
|
def test_samp() -> None:
|
2019-03-02 23:23:03 -06:00
|
|
|
emph_literal_role = EmphasizedLiteral()
|
|
|
|
|
2018-04-27 21:39:13 -05:00
|
|
|
# normal case
|
|
|
|
text = 'print 1+{variable}'
|
|
|
|
ret, msg = emph_literal_role('samp', text, text, 0, Mock())
|
2024-08-11 08:58:56 -05:00
|
|
|
assert_node(
|
|
|
|
ret[0],
|
|
|
|
[
|
|
|
|
nodes.literal,
|
|
|
|
(
|
|
|
|
'print 1+',
|
|
|
|
[nodes.emphasis, 'variable'],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2018-04-27 21:39:13 -05:00
|
|
|
assert msg == []
|
|
|
|
|
|
|
|
# two emphasis items
|
|
|
|
text = 'print {1}+{variable}'
|
|
|
|
ret, msg = emph_literal_role('samp', text, text, 0, Mock())
|
2024-08-11 08:58:56 -05:00
|
|
|
assert_node(
|
|
|
|
ret[0],
|
|
|
|
[
|
|
|
|
nodes.literal,
|
|
|
|
(
|
|
|
|
'print ',
|
|
|
|
[nodes.emphasis, '1'],
|
|
|
|
'+',
|
|
|
|
[nodes.emphasis, 'variable'],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2018-04-27 21:39:13 -05:00
|
|
|
assert msg == []
|
|
|
|
|
|
|
|
# empty curly brace
|
|
|
|
text = 'print 1+{}'
|
|
|
|
ret, msg = emph_literal_role('samp', text, text, 0, Mock())
|
2024-08-11 08:58:56 -05:00
|
|
|
assert_node(ret[0], [nodes.literal, 'print 1+{}'])
|
2018-04-27 21:39:13 -05:00
|
|
|
assert msg == []
|
|
|
|
|
|
|
|
# half-opened variable
|
|
|
|
text = 'print 1+{variable'
|
|
|
|
ret, msg = emph_literal_role('samp', text, text, 0, Mock())
|
2024-08-11 08:58:56 -05:00
|
|
|
assert_node(ret[0], [nodes.literal, 'print 1+{variable'])
|
2018-04-27 21:39:13 -05:00
|
|
|
assert msg == []
|
|
|
|
|
|
|
|
# nested
|
|
|
|
text = 'print 1+{{variable}}'
|
|
|
|
ret, msg = emph_literal_role('samp', text, text, 0, Mock())
|
2024-08-11 08:58:56 -05:00
|
|
|
assert_node(
|
|
|
|
ret[0],
|
|
|
|
[
|
|
|
|
nodes.literal,
|
|
|
|
(
|
|
|
|
'print 1+',
|
|
|
|
[nodes.emphasis, '{variable'],
|
|
|
|
'}',
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2018-04-27 21:39:13 -05:00
|
|
|
assert msg == []
|
|
|
|
|
|
|
|
# emphasized item only
|
|
|
|
text = '{variable}'
|
|
|
|
ret, msg = emph_literal_role('samp', text, text, 0, Mock())
|
2024-08-11 08:58:56 -05:00
|
|
|
assert_node(ret[0], [nodes.literal, nodes.emphasis, 'variable'])
|
2018-04-27 21:39:13 -05:00
|
|
|
assert msg == []
|
|
|
|
|
|
|
|
# escaping
|
|
|
|
text = r'print 1+\{variable}'
|
|
|
|
ret, msg = emph_literal_role('samp', text, text, 0, Mock())
|
2024-08-11 08:58:56 -05:00
|
|
|
assert_node(ret[0], [nodes.literal, 'print 1+{variable}'])
|
2018-04-27 21:39:13 -05:00
|
|
|
assert msg == []
|
|
|
|
|
|
|
|
# escaping (2)
|
|
|
|
text = r'print 1+\{{variable}\}'
|
|
|
|
ret, msg = emph_literal_role('samp', text, text, 0, Mock())
|
2024-08-11 08:58:56 -05:00
|
|
|
assert_node(
|
|
|
|
ret[0],
|
|
|
|
[
|
|
|
|
nodes.literal,
|
|
|
|
(
|
|
|
|
'print 1+{',
|
|
|
|
[nodes.emphasis, 'variable'],
|
|
|
|
'}',
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2018-04-27 21:39:13 -05:00
|
|
|
assert msg == []
|
|
|
|
|
|
|
|
# escape a backslash
|
|
|
|
text = r'print 1+\\{variable}'
|
|
|
|
ret, msg = emph_literal_role('samp', text, text, 0, Mock())
|
2024-08-11 08:58:56 -05:00
|
|
|
assert_node(
|
|
|
|
ret[0],
|
|
|
|
[
|
|
|
|
nodes.literal,
|
|
|
|
(
|
|
|
|
'print 1+\\',
|
|
|
|
[nodes.emphasis, 'variable'],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2018-04-27 21:39:13 -05:00
|
|
|
assert msg == []
|
2024-10-05 22:29:11 -05:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
('target', 'expected_output'),
|
|
|
|
[
|
|
|
|
('123', 'RFC 123'),
|
|
|
|
('123#', 'RFC 123#'),
|
|
|
|
('123#id1', 'RFC 123#id1'),
|
|
|
|
('123#section', 'RFC 123 Section'),
|
|
|
|
('123#section-1', 'RFC 123 Section 1'),
|
|
|
|
('123#section-2.5.3', 'RFC 123 Section 2.5.3'),
|
|
|
|
('123#page-13', 'RFC 123 Page 13'),
|
|
|
|
('123#appendix-B', 'RFC 123 Appendix B'),
|
|
|
|
# Section names are also present in some RFC anchors. Until we come up with a reliable way
|
|
|
|
# to reconstruct the section names from the corresponding anchors with the correct
|
|
|
|
# capitalization it's probably better to leave them alone.
|
|
|
|
('9076#name-risks-in-the-dns-data', 'RFC 9076#name-risks-in-the-dns-data'),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_format_rfc_target(target: str, expected_output: str) -> None:
|
|
|
|
assert _format_rfc_target(target) == expected_output
|