@@ -66,7 +66,7 @@ def test_thumbnail_div(content, tooltip, is_backref):
66
66
assert html_div == reference
67
67
68
68
69
- def test_identify_names (unicode_sample ):
69
+ def test_identify_names (unicode_sample , gallery_conf ):
70
70
"""Test name identification."""
71
71
expected = {
72
72
'os.path.join' :
@@ -95,12 +95,13 @@ def test_identify_names(unicode_sample):
95
95
}],
96
96
}
97
97
_ , script_blocks = split_code_and_text_blocks (unicode_sample )
98
- res = sg .identify_names (script_blocks )
98
+ ref_regex = sg ._make_ref_regex (gallery_conf ['app' ].config )
99
+ res = sg .identify_names (script_blocks , ref_regex )
99
100
assert expected == res
100
101
101
102
102
- def test_identify_names2 (tmpdir ):
103
- """Test more name identification."""
103
+ def test_identify_names_implicit (tmpdir , gallery_conf ):
104
+ """Test implicit name identification."""
104
105
code_str = b"""
105
106
'''
106
107
Title
@@ -148,26 +149,52 @@ def test_identify_names2(tmpdir):
148
149
fname .write (code_str , 'wb' )
149
150
150
151
_ , script_blocks = split_code_and_text_blocks (fname .strpath )
151
- res = sg .identify_names (script_blocks )
152
+ ref_regex = sg ._make_ref_regex (gallery_conf ['app' ].config )
153
+ res = sg .identify_names (script_blocks , ref_regex )
152
154
153
155
assert expected == res
154
156
155
- code_str = b"""
156
- '''
157
- Title
158
- -----
159
-
160
- This example uses :func:`k.l` and :meth:`~m.n`.
161
- '''
162
- """ + code_str .split (b"'''" )[- 1 ]
163
- expected ['k.l' ] = [{'module' : 'k' , 'module_short' : 'k' , 'name' : 'l' ,
164
- 'is_class' : False , 'is_explicit' : True }]
165
- expected ['m.n' ] = [{'module' : 'm' , 'module_short' : 'm' , 'name' : 'n' ,
166
- 'is_class' : False , 'is_explicit' : True }]
167
157
168
- fname = tmpdir .join ("identify_names.py" )
169
- fname .write (code_str , 'wb' )
170
- _ , script_blocks = split_code_and_text_blocks (fname .strpath )
171
- res = sg .identify_names (script_blocks )
172
-
173
- assert expected == res
158
+ cobj = dict (
159
+ module = "m" , module_short = "m" , name = "n" , is_class = False , is_explicit = True
160
+ )
161
+
162
+
163
+ @pytest .mark .parametrize (
164
+ 'text, default_role, ref, cobj' ,
165
+ [
166
+ (':func:`m.n`' , None , 'm.n' , cobj ),
167
+ (':func:`~m.n`' , 'obj' , 'm.n' , cobj ),
168
+ (':func:`Title <m.n>`' , None , 'm.n' , cobj ),
169
+ (':func:`!m.n` or `!t <~m.n>`' , None , None , None ),
170
+ ('`m.n`' , 'obj' , 'm.n' , cobj ),
171
+ ('`m.n`' , None , None , None ), # see comment below
172
+ (':ref:`m.n`' , None , None , None ),
173
+ ('`m.n`' , 'ref' , None , None ),
174
+ ('``literal``' , 'obj' , None , None ),
175
+ ],
176
+ ids = [
177
+ 'regular' ,
178
+ 'show only last component' ,
179
+ 'with title' ,
180
+ 'no link for !' ,
181
+ 'default_role obj' ,
182
+ 'no default_role' , # see comment below
183
+ 'non-python role' ,
184
+ 'non-python default_role' ,
185
+ 'literal' ,
186
+ ],
187
+ )
188
+ # the sphinx default value for default_role is None = no change, the docutils
189
+ # default is title-reference (set by the default-role directive), see
190
+ # www.sphinx-doc.org/en/master/usage/configuration.html#confval-default_role
191
+ # and docutils.sourceforge.io/docs/ref/rst/roles.html
192
+ def test_identify_names_explicit (text , default_role , ref , cobj , gallery_conf ):
193
+ """Test explicit name identification."""
194
+ if default_role :
195
+ gallery_conf ['app' ].config ['default_role' ] = default_role
196
+ script_blocks = [('text' , text , 1 )]
197
+ expected = {ref : [cobj ]} if ref else {}
198
+ ref_regex = sg ._make_ref_regex (gallery_conf ['app' ].config )
199
+ actual = sg .identify_names (script_blocks , ref_regex )
200
+ assert expected == actual
0 commit comments