Debian Codegraph @

tests/test_roles.py

raw
1 """Test sphinx.roles"""
2
3 from __future__ import annotations
4
5 from unittest.mock import Mock
6
7 import pytest
8 from docutils import nodes
9
10 from sphinx.roles import EmphasizedLiteral, _format_rfc_target
11 from sphinx.testing.util import assert_node
12
13
14 def test_samp() -> None:
15 emph_literal_role = EmphasizedLiteral()
16
17 # normal case
18 text = 'print 1+{variable}'
19 ret, msg = emph_literal_role('samp', text, text, 0, Mock())
20 assert_node(
21 ret[0],
22 [
23 nodes.literal,
24 (
25 'print 1+',
26 [nodes.emphasis, 'variable'],
27 ),
28 ],
29 )
30 assert msg == []
31
32 # two emphasis items
33 text = 'print {1}+{variable}'
34 ret, msg = emph_literal_role('samp', text, text, 0, Mock())
35 assert_node(
36 ret[0],
37 [
38 nodes.literal,
39 (
40 'print ',
41 [nodes.emphasis, '1'],
42 '+',
43 [nodes.emphasis, 'variable'],
44 ),
45 ],
46 )
47 assert msg == []
48
49 # empty curly brace
50 text = 'print 1+{}'
51 ret, msg = emph_literal_role('samp', text, text, 0, Mock())
52 assert_node(ret[0], [nodes.literal, 'print 1+{}'])
53 assert msg == []
54
55 # half-opened variable
56 text = 'print 1+{variable'
57 ret, msg = emph_literal_role('samp', text, text, 0, Mock())
58 assert_node(ret[0], [nodes.literal, 'print 1+{variable'])
59 assert msg == []
60
61 # nested
62 text = 'print 1+{{variable}}'
63 ret, msg = emph_literal_role('samp', text, text, 0, Mock())
64 assert_node(
65 ret[0],
66 [
67 nodes.literal,
68 (
69 'print 1+',
70 [nodes.emphasis, '{variable'],
71 '}',
72 ),
73 ],
74 )
75 assert msg == []
76
77 # emphasized item only
78 text = '{variable}'
79 ret, msg = emph_literal_role('samp', text, text, 0, Mock())
80 assert_node(ret[0], [nodes.literal, nodes.emphasis, 'variable'])
81 assert msg == []
82
83 # escaping
84 text = r'print 1+\{variable}'
85 ret, msg = emph_literal_role('samp', text, text, 0, Mock())
86 assert_node(ret[0], [nodes.literal, 'print 1+{variable}'])
87 assert msg == []
88
89 # escaping (2)
90 text = r'print 1+\{{variable}\}'
91 ret, msg = emph_literal_role('samp', text, text, 0, Mock())
92 assert_node(
93 ret[0],
94 [
95 nodes.literal,
96 (
97 'print 1+{',
98 [nodes.emphasis, 'variable'],
99 '}',
100 ),
101 ],
102 )
103 assert msg == []
104
105 # escape a backslash
106 text = r'print 1+\\{variable}'
107 ret, msg = emph_literal_role('samp', text, text, 0, Mock())
108 assert_node(
109 ret[0],
110 [
111 nodes.literal,
112 (
113 'print 1+\\',
114 [nodes.emphasis, 'variable'],
115 ),
116 ],
117 )
118 assert msg == []
119
120
121 @pytest.mark.parametrize(
122 ('target', 'expected_output'),
123 [
124 ('123', 'RFC 123'),
125 ('123#', 'RFC 123#'),
126 ('123#id1', 'RFC 123#id1'),
127 ('123#section', 'RFC 123 Section'),
128 ('123#section-1', 'RFC 123 Section 1'),
129 ('123#section-2.5.3', 'RFC 123 Section 2.5.3'),
130 ('123#page-13', 'RFC 123 Page 13'),
131 ('123#appendix-B', 'RFC 123 Appendix B'),
132 # Section names are also present in some RFC anchors. Until we come up with a reliable way
133 # to reconstruct the section names from the corresponding anchors with the correct
134 # capitalization it's probably better to leave them alone.
135 ('9076#name-risks-in-the-dns-data', 'RFC 9076#name-risks-in-the-dns-data'),
136 ],
137 )
138 def test_format_rfc_target(target: str, expected_output: str) -> None:
139 assert _format_rfc_target(target) == expected_output