Debian Codegraph @

tests/test_domains/test_domain_js.py

raw
1 """Tests the JavaScript Domain"""
2
3 from __future__ import annotations
4
5 from unittest.mock import Mock
6
7 import docutils.utils
8 import pytest
9 from docutils import nodes
10
11 from sphinx import addnodes
12 from sphinx.addnodes import (
13 desc,
14 desc_annotation,
15 desc_content,
16 desc_name,
17 desc_parameter,
18 desc_parameterlist,
19 desc_sig_keyword,
20 desc_sig_name,
21 desc_sig_space,
22 desc_signature,
23 )
24 from sphinx.domains.javascript import JavaScriptDomain
25 from sphinx.testing import restructuredtext
26 from sphinx.testing.util import assert_node
27 from sphinx.writers.text import STDINDENT
28
29 from tests.utils import extract_node
30
31 TYPE_CHECKING = False
32 if TYPE_CHECKING:
33 from sphinx.testing.util import SphinxTestApp
34
35
36 @pytest.mark.sphinx('dummy', testroot='domain-js')
37 def test_domain_js_xrefs(app):
38 """Domain objects have correct prefixes when looking up xrefs"""
39 app.build(force_all=True)
40
41 def assert_refnode(node, mod_name, prefix, target, reftype=None, domain='js'):
42 attributes = {
43 'refdomain': domain,
44 'reftarget': target,
45 }
46 if reftype is not None:
47 attributes['reftype'] = reftype
48 if mod_name is not False:
49 attributes['js:module'] = mod_name
50 if prefix is not False:
51 attributes['js:object'] = prefix
52 assert_node(node, **attributes)
53
54 doctree = app.env.get_doctree('roles')
55 refnodes = list(doctree.findall(addnodes.pending_xref))
56 assert_refnode(refnodes[0], None, None, 'TopLevel', 'class')
57 assert_refnode(refnodes[1], None, None, 'top_level', 'func')
58 assert_refnode(refnodes[2], None, 'NestedParentA', 'child_1', 'func')
59 assert_refnode(
60 refnodes[3], None, 'NestedParentA', 'NestedChildA.subchild_2', 'func'
61 )
62 assert_refnode(refnodes[4], None, 'NestedParentA', 'child_2', 'func')
63 assert_refnode(refnodes[5], False, 'NestedParentA', 'any_child', domain='')
64 assert_refnode(refnodes[6], None, 'NestedParentA', 'NestedChildA', 'class')
65 assert_refnode(
66 refnodes[7], None, 'NestedParentA.NestedChildA', 'subchild_2', 'func'
67 )
68 assert_refnode(
69 refnodes[8], None, 'NestedParentA.NestedChildA', 'NestedParentA.child_1', 'func'
70 )
71 assert_refnode(
72 refnodes[9], None, 'NestedParentA', 'NestedChildA.subchild_1', 'func'
73 )
74 assert_refnode(refnodes[10], None, 'NestedParentB', 'child_1', 'func')
75 assert_refnode(refnodes[11], None, 'NestedParentB', 'NestedParentB', 'class')
76 assert_refnode(refnodes[12], None, None, 'NestedParentA.NestedChildA', 'class')
77 assert len(refnodes) == 13
78
79 doctree = app.env.get_doctree('module')
80 refnodes = list(doctree.findall(addnodes.pending_xref))
81 assert_refnode(refnodes[0], 'module_a.submodule', None, 'ModTopLevel', 'class')
82 assert_refnode(
83 refnodes[1], 'module_a.submodule', 'ModTopLevel', 'mod_child_1', 'meth'
84 )
85 assert_refnode(
86 refnodes[2],
87 'module_a.submodule',
88 'ModTopLevel',
89 'ModTopLevel.mod_child_1',
90 'meth',
91 )
92 assert_refnode(
93 refnodes[3], 'module_a.submodule', 'ModTopLevel', 'mod_child_2', 'meth'
94 )
95 assert_refnode(
96 refnodes[4],
97 'module_a.submodule',
98 'ModTopLevel',
99 'module_a.submodule.ModTopLevel.mod_child_1',
100 'meth',
101 )
102 assert_refnode(refnodes[5], 'module_b.submodule', None, 'ModTopLevel', 'class')
103 assert_refnode(
104 refnodes[6], 'module_b.submodule', 'ModTopLevel', 'module_a.submodule', 'mod'
105 )
106 assert len(refnodes) == 7
107
108
109 @pytest.mark.sphinx('dummy', testroot='domain-js')
110 def test_domain_js_objects(app):
111 app.build(force_all=True)
112
113 modules = app.env.domains.javascript_domain.data['modules']
114 objects = app.env.domains.javascript_domain.data['objects']
115
116 assert 'module_a.submodule' in modules
117 assert 'module_a.submodule' in objects
118 assert 'module_b.submodule' in modules
119 assert 'module_b.submodule' in objects
120
121 assert objects['module_a.submodule.ModTopLevel'][2] == 'class'
122 assert objects['module_a.submodule.ModTopLevel.mod_child_1'][2] == 'method'
123 assert objects['module_a.submodule.ModTopLevel.mod_child_2'][2] == 'method'
124 assert objects['module_b.submodule.ModTopLevel'][2] == 'class'
125
126 assert objects['TopLevel'][2] == 'class'
127 assert objects['top_level'][2] == 'function'
128 assert objects['NestedParentA'][2] == 'class'
129 assert objects['NestedParentA.child_1'][2] == 'function'
130 assert objects['NestedParentA.any_child'][2] == 'function'
131 assert objects['NestedParentA.NestedChildA'][2] == 'class'
132 assert objects['NestedParentA.NestedChildA.subchild_1'][2] == 'function'
133 assert objects['NestedParentA.NestedChildA.subchild_2'][2] == 'function'
134 assert objects['NestedParentA.child_2'][2] == 'function'
135 assert objects['NestedParentB'][2] == 'class'
136 assert objects['NestedParentB.child_1'][2] == 'function'
137
138
139 @pytest.mark.sphinx('dummy', testroot='domain-js')
140 def test_domain_js_find_obj(app):
141 def find_obj(mod_name, prefix, obj_name, obj_type, searchmode=0):
142 return app.env.domains.javascript_domain.find_obj(
143 app.env, mod_name, prefix, obj_name, obj_type, searchmode
144 )
145
146 app.build(force_all=True)
147
148 assert find_obj(None, None, 'NONEXISTANT', 'class') == (None, None)
149 assert find_obj(None, None, 'NestedParentA', 'class') == (
150 'NestedParentA',
151 ('roles', 'NestedParentA', 'class'),
152 )
153 assert find_obj(None, None, 'NestedParentA.NestedChildA', 'class') == (
154 'NestedParentA.NestedChildA',
155 ('roles', 'NestedParentA.NestedChildA', 'class'),
156 )
157 assert find_obj(None, 'NestedParentA', 'NestedChildA', 'class') == (
158 'NestedParentA.NestedChildA',
159 ('roles', 'NestedParentA.NestedChildA', 'class'),
160 )
161 assert find_obj(None, None, 'NestedParentA.NestedChildA.subchild_1', 'func') == (
162 'NestedParentA.NestedChildA.subchild_1',
163 ('roles', 'NestedParentA.NestedChildA.subchild_1', 'function'),
164 )
165 assert find_obj(None, 'NestedParentA', 'NestedChildA.subchild_1', 'func') == (
166 'NestedParentA.NestedChildA.subchild_1',
167 ('roles', 'NestedParentA.NestedChildA.subchild_1', 'function'),
168 )
169 assert find_obj(None, 'NestedParentA.NestedChildA', 'subchild_1', 'func') == (
170 'NestedParentA.NestedChildA.subchild_1',
171 ('roles', 'NestedParentA.NestedChildA.subchild_1', 'function'),
172 )
173 assert find_obj('module_a.submodule', 'ModTopLevel', 'mod_child_2', 'meth') == (
174 'module_a.submodule.ModTopLevel.mod_child_2',
175 ('module', 'module_a.submodule.ModTopLevel.mod_child_2', 'method'),
176 )
177 assert find_obj(
178 'module_b.submodule', 'ModTopLevel', 'module_a.submodule', 'mod'
179 ) == ('module_a.submodule', ('module', 'module-module_a.submodule', 'module'))
180
181
182 def test_get_full_qualified_name() -> None:
183 env = Mock(domaindata={})
184 domain = JavaScriptDomain(env)
185
186 # non-js references
187 node = nodes.reference()
188 assert domain.get_full_qualified_name(node) is None
189
190 # simple reference
191 node = nodes.reference(reftarget='func')
192 assert domain.get_full_qualified_name(node) == 'func'
193
194 # with js:module context
195 kwargs = {'js:module': 'module1'}
196 node = nodes.reference(reftarget='func', **kwargs)
197 assert domain.get_full_qualified_name(node) == 'module1.func'
198
199 # with js:object context
200 kwargs = {'js:object': 'Class'}
201 node = nodes.reference(reftarget='func', **kwargs)
202 assert domain.get_full_qualified_name(node) == 'Class.func'
203
204 # with both js:module and js:object context
205 kwargs = {'js:module': 'module1', 'js:object': 'Class'}
206 node = nodes.reference(reftarget='func', **kwargs)
207 assert domain.get_full_qualified_name(node) == 'module1.Class.func'
208
209
210 @pytest.mark.sphinx('html', testroot='_blank')
211 def test_js_module(app):
212 text = '.. js:module:: sphinx'
213 doctree = restructuredtext.parse(app, text)
214 assert_node(doctree, (addnodes.index, nodes.target))
215 assert_node(
216 doctree[0],
217 addnodes.index,
218 entries=[('single', 'sphinx (module)', 'module-sphinx', '', None)],
219 )
220 assert_node(doctree[1], nodes.target, ids=['module-sphinx'])
221
222
223 @pytest.mark.sphinx('html', testroot='_blank')
224 def test_js_function(app):
225 text = '.. js:function:: sum(a, b)'
226 doctree = restructuredtext.parse(app, text)
227 assert_node(
228 doctree,
229 (
230 addnodes.index,
231 [
232 desc,
233 (
234 [
235 desc_signature,
236 ([desc_name, ([desc_sig_name, 'sum'])], desc_parameterlist),
237 ],
238 [desc_content, ()],
239 ),
240 ],
241 ),
242 )
243 assert_node(
244 extract_node(doctree, 1, 0, 1),
245 [
246 desc_parameterlist,
247 (
248 [desc_parameter, ([desc_sig_name, 'a'])],
249 [desc_parameter, ([desc_sig_name, 'b'])],
250 ),
251 ],
252 )
253 assert_node(
254 doctree[0],
255 addnodes.index,
256 entries=[('single', 'sum() (built-in function)', 'sum', '', None)],
257 )
258 assert_node(
259 doctree[1], addnodes.desc, domain='js', objtype='function', no_index=False
260 )
261
262
263 @pytest.mark.sphinx('html', testroot='_blank')
264 def test_js_class(app):
265 text = '.. js:class:: Application'
266 doctree = restructuredtext.parse(app, text)
267 assert_node(
268 doctree,
269 (
270 addnodes.index,
271 [
272 desc,
273 (
274 [
275 desc_signature,
276 (
277 [
278 desc_annotation,
279 ([desc_sig_keyword, 'class'], desc_sig_space),
280 ],
281 [desc_name, ([desc_sig_name, 'Application'])],
282 [desc_parameterlist, ()],
283 ),
284 ],
285 [desc_content, ()],
286 ),
287 ],
288 ),
289 )
290 assert_node(
291 doctree[0],
292 addnodes.index,
293 entries=[('single', 'Application() (class)', 'Application', '', None)],
294 )
295 assert_node(doctree[1], addnodes.desc, domain='js', objtype='class', no_index=False)
296
297
298 @pytest.mark.sphinx('html', testroot='_blank')
299 def test_js_data(app):
300 text = '.. js:data:: name'
301 doctree = restructuredtext.parse(app, text)
302 assert_node(
303 doctree,
304 (
305 addnodes.index,
306 [
307 desc,
308 (
309 [desc_signature, ([desc_name, ([desc_sig_name, 'name'])])],
310 [desc_content, ()],
311 ),
312 ],
313 ),
314 )
315 assert_node(
316 doctree[0],
317 addnodes.index,
318 entries=[('single', 'name (global variable or constant)', 'name', '', None)],
319 )
320 assert_node(doctree[1], addnodes.desc, domain='js', objtype='data', no_index=False)
321
322
323 @pytest.mark.sphinx('html', testroot='_blank')
324 def test_no_index_entry(app):
325 text = '.. js:function:: f()\n.. js:function:: g()\n :no-index-entry:\n'
326 doctree = restructuredtext.parse(app, text)
327 assert_node(doctree, (addnodes.index, desc, addnodes.index, desc))
328 assert_node(
329 doctree[0],
330 addnodes.index,
331 entries=[('single', 'f() (built-in function)', 'f', '', None)],
332 )
333 assert_node(doctree[2], addnodes.index, entries=[])
334
335 text = '.. js:class:: f\n.. js:class:: g\n :no-index-entry:\n'
336 doctree = restructuredtext.parse(app, text)
337 assert_node(doctree, (addnodes.index, desc, addnodes.index, desc))
338 assert_node(
339 doctree[0],
340 addnodes.index,
341 entries=[('single', 'f() (class)', 'f', '', None)],
342 )
343 assert_node(doctree[2], addnodes.index, entries=[])
344
345 text = '.. js:module:: f\n.. js:module:: g\n :no-index-entry:\n'
346 doctree = restructuredtext.parse(app, text)
347 assert_node(doctree, (addnodes.index, nodes.target, nodes.target))
348 assert_node(
349 doctree[0],
350 addnodes.index,
351 entries=[('single', 'f (module)', 'module-f', '', None)],
352 )
353
354
355 @pytest.mark.sphinx('html', testroot='_blank')
356 def test_module_content_line_number(app):
357 text = '.. js:module:: foo\n\n Some link here: :ref:`abc`\n'
358 doc = restructuredtext.parse(app, text)
359 xrefs = list(doc.findall(condition=addnodes.pending_xref))
360 assert len(xrefs) == 1
361 source, line = docutils.utils.get_source_line(xrefs[0])
362 assert 'index.rst' in source
363 assert line == 3
364
365
366 @pytest.mark.sphinx(
367 'html',
368 testroot='root',
369 confoverrides={
370 'javascript_maximum_signature_line_length': len('hello(name)'),
371 },
372 )
373 def test_jsfunction_signature_with_javascript_maximum_signature_line_length_equal(app):
374 text = '.. js:function:: hello(name)'
375 doctree = restructuredtext.parse(app, text)
376 assert_node(
377 doctree,
378 (
379 addnodes.index,
380 [
381 desc,
382 (
383 [
384 desc_signature,
385 (
386 [desc_name, ([desc_sig_name, 'hello'])],
387 desc_parameterlist,
388 ),
389 ],
390 desc_content,
391 ),
392 ],
393 ),
394 )
395 assert_node(
396 doctree[1],
397 desc,
398 desctype='function',
399 domain='js',
400 objtype='function',
401 no_index=False,
402 )
403 assert_node(
404 extract_node(doctree, 1, 0, 1),
405 [desc_parameterlist, desc_parameter, ([desc_sig_name, 'name'])],
406 )
407 assert_node(
408 extract_node(doctree, 1, 0, 1),
409 desc_parameterlist,
410 multi_line_parameter_list=False,
411 )
412
413
414 @pytest.mark.sphinx(
415 'html',
416 testroot='root',
417 confoverrides={
418 'javascript_maximum_signature_line_length': len('hello(name)'),
419 },
420 )
421 def test_jsfunction_signature_with_javascript_maximum_signature_line_length_force_single(
422 app,
423 ):
424 text = '.. js:function:: hello(names)\n :single-line-parameter-list:'
425 doctree = restructuredtext.parse(app, text)
426 assert_node(
427 doctree,
428 (
429 addnodes.index,
430 [
431 desc,
432 (
433 [
434 desc_signature,
435 (
436 [desc_name, ([desc_sig_name, 'hello'])],
437 desc_parameterlist,
438 ),
439 ],
440 desc_content,
441 ),
442 ],
443 ),
444 )
445 assert_node(
446 doctree[1],
447 desc,
448 desctype='function',
449 domain='js',
450 objtype='function',
451 no_index=False,
452 )
453 assert_node(
454 extract_node(doctree, 1, 0, 1),
455 [desc_parameterlist, desc_parameter, ([desc_sig_name, 'names'])],
456 )
457 assert_node(
458 extract_node(doctree, 1, 0, 1),
459 desc_parameterlist,
460 multi_line_parameter_list=False,
461 )
462
463
464 @pytest.mark.sphinx(
465 'html',
466 testroot='root',
467 confoverrides={
468 'javascript_maximum_signature_line_length': len('hello(name)'),
469 },
470 )
471 def test_jsfunction_signature_with_javascript_maximum_signature_line_length_break(app):
472 text = '.. js:function:: hello(names)'
473 doctree = restructuredtext.parse(app, text)
474 assert_node(
475 doctree,
476 (
477 addnodes.index,
478 [
479 desc,
480 (
481 [
482 desc_signature,
483 (
484 [desc_name, ([desc_sig_name, 'hello'])],
485 desc_parameterlist,
486 ),
487 ],
488 desc_content,
489 ),
490 ],
491 ),
492 )
493 assert_node(
494 doctree[1],
495 desc,
496 desctype='function',
497 domain='js',
498 objtype='function',
499 no_index=False,
500 )
501 assert_node(
502 extract_node(doctree, 1, 0, 1),
503 [desc_parameterlist, desc_parameter, ([desc_sig_name, 'names'])],
504 )
505 assert_node(
506 extract_node(doctree, 1, 0, 1),
507 desc_parameterlist,
508 multi_line_parameter_list=True,
509 )
510
511
512 @pytest.mark.sphinx(
513 'html',
514 testroot='root',
515 confoverrides={
516 'maximum_signature_line_length': len('hello(name)'),
517 },
518 )
519 def test_jsfunction_signature_with_maximum_signature_line_length_equal(app):
520 text = '.. js:function:: hello(name)'
521 doctree = restructuredtext.parse(app, text)
522 assert_node(
523 doctree,
524 (
525 addnodes.index,
526 [
527 desc,
528 (
529 [
530 desc_signature,
531 (
532 [desc_name, ([desc_sig_name, 'hello'])],
533 desc_parameterlist,
534 ),
535 ],
536 desc_content,
537 ),
538 ],
539 ),
540 )
541 assert_node(
542 doctree[1],
543 desc,
544 desctype='function',
545 domain='js',
546 objtype='function',
547 no_index=False,
548 )
549 assert_node(
550 extract_node(doctree, 1, 0, 1),
551 [desc_parameterlist, desc_parameter, ([desc_sig_name, 'name'])],
552 )
553 assert_node(
554 extract_node(doctree, 1, 0, 1),
555 desc_parameterlist,
556 multi_line_parameter_list=False,
557 )
558
559
560 @pytest.mark.sphinx(
561 'html',
562 testroot='root',
563 confoverrides={
564 'maximum_signature_line_length': len('hello(name)'),
565 },
566 )
567 def test_jsfunction_signature_with_maximum_signature_line_length_force_single(app):
568 text = '.. js:function:: hello(names)\n :single-line-parameter-list:'
569 doctree = restructuredtext.parse(app, text)
570 assert_node(
571 doctree,
572 (
573 addnodes.index,
574 [
575 desc,
576 (
577 [
578 desc_signature,
579 (
580 [desc_name, ([desc_sig_name, 'hello'])],
581 desc_parameterlist,
582 ),
583 ],
584 desc_content,
585 ),
586 ],
587 ),
588 )
589 assert_node(
590 doctree[1],
591 desc,
592 desctype='function',
593 domain='js',
594 objtype='function',
595 no_index=False,
596 )
597 assert_node(
598 extract_node(doctree, 1, 0, 1),
599 [desc_parameterlist, desc_parameter, ([desc_sig_name, 'names'])],
600 )
601 assert_node(
602 extract_node(doctree, 1, 0, 1),
603 desc_parameterlist,
604 multi_line_parameter_list=False,
605 )
606
607
608 @pytest.mark.sphinx(
609 'html',
610 testroot='root',
611 confoverrides={
612 'maximum_signature_line_length': len('hello(name)'),
613 },
614 )
615 def test_jsfunction_signature_with_maximum_signature_line_length_break(app):
616 text = '.. js:function:: hello(names)'
617 doctree = restructuredtext.parse(app, text)
618 assert_node(
619 doctree,
620 (
621 addnodes.index,
622 [
623 desc,
624 (
625 [
626 desc_signature,
627 (
628 [desc_name, ([desc_sig_name, 'hello'])],
629 desc_parameterlist,
630 ),
631 ],
632 desc_content,
633 ),
634 ],
635 ),
636 )
637 assert_node(
638 doctree[1],
639 desc,
640 desctype='function',
641 domain='js',
642 objtype='function',
643 no_index=False,
644 )
645 assert_node(
646 extract_node(doctree, 1, 0, 1),
647 [desc_parameterlist, desc_parameter, ([desc_sig_name, 'names'])],
648 )
649 assert_node(
650 extract_node(doctree, 1, 0, 1),
651 desc_parameterlist,
652 multi_line_parameter_list=True,
653 )
654
655
656 @pytest.mark.sphinx(
657 'html',
658 testroot='root',
659 confoverrides={
660 'javascript_maximum_signature_line_length': len('hello(name)'),
661 'maximum_signature_line_length': 1,
662 },
663 )
664 def test_javascript_maximum_signature_line_length_overrides_global(app):
665 text = '.. js:function:: hello(name)'
666 doctree = restructuredtext.parse(app, text)
667 expected_doctree = (
668 addnodes.index,
669 [
670 desc,
671 (
672 [
673 desc_signature,
674 ([desc_name, ([desc_sig_name, 'hello'])], desc_parameterlist),
675 ],
676 desc_content,
677 ),
678 ],
679 )
680 assert_node(doctree, expected_doctree)
681 assert_node(
682 doctree[1],
683 desc,
684 desctype='function',
685 domain='js',
686 objtype='function',
687 no_index=False,
688 )
689 expected_sig = [desc_parameterlist, desc_parameter, [desc_sig_name, 'name']]
690 assert_node(extract_node(doctree, 1, 0, 1), expected_sig)
691 assert_node(
692 extract_node(doctree, 1, 0, 1),
693 desc_parameterlist,
694 multi_line_parameter_list=False,
695 )
696
697
698 @pytest.mark.sphinx(
699 'html',
700 testroot='domain-js-javascript_maximum_signature_line_length',
701 )
702 def test_domain_js_javascript_maximum_signature_line_length_in_html(app):
703 app.build()
704 content = (app.outdir / 'index.html').read_text(encoding='utf8')
705 expected_parameter_list_hello = """\
706
707 <dl>
708 <dd>\
709 <em class="sig-param">\
710 <span class="n"><span class="pre">name</span></span>\
711 </em>,\
712 </dd>
713 </dl>
714
715 <span class="sig-paren">)</span>\
716 <a class="headerlink" href="#hello" title="Link to this definition">¶</a>\
717 </dt>\
718 """
719 assert expected_parameter_list_hello in content
720
721 param_line_fmt = '<dd>{}</dd>\n'
722 param_name_fmt = (
723 '<em class="sig-param"><span class="n"><span class="pre">{}</span></span></em>'
724 )
725 optional_fmt = '<span class="optional">{}</span>'
726
727 expected_a = param_line_fmt.format(
728 optional_fmt.format('[')
729 + param_name_fmt.format('a')
730 + ','
731 + optional_fmt.format('['),
732 )
733 assert expected_a in content
734
735 expected_b = param_line_fmt.format(
736 param_name_fmt.format('b')
737 + ','
738 + optional_fmt.format(']')
739 + optional_fmt.format(']'),
740 )
741 assert expected_b in content
742
743 expected_c = param_line_fmt.format(param_name_fmt.format('c') + ',')
744 assert expected_c in content
745
746 expected_d = param_line_fmt.format(
747 param_name_fmt.format('d') + optional_fmt.format('[') + ','
748 )
749 assert expected_d in content
750
751 expected_e = param_line_fmt.format(param_name_fmt.format('e') + ',')
752 assert expected_e in content
753
754 expected_f = param_line_fmt.format(
755 param_name_fmt.format('f') + ',' + optional_fmt.format(']')
756 )
757 assert expected_f in content
758
759 expected_parameter_list_foo = """\
760
761 <dl>
762 {}{}{}{}{}{}</dl>
763
764 <span class="sig-paren">)</span>\
765 <a class="headerlink" href="#foo" title="Link to this definition">¶</a>\
766 </dt>\
767 """.format(expected_a, expected_b, expected_c, expected_d, expected_e, expected_f)
768 assert expected_parameter_list_foo in content
769
770
771 @pytest.mark.sphinx(
772 'text',
773 testroot='domain-js-javascript_maximum_signature_line_length',
774 )
775 def test_domain_js_javascript_maximum_signature_line_length_in_text(app):
776 app.build()
777 content = (app.outdir / 'index.txt').read_text(encoding='utf8')
778 param_line_fmt = STDINDENT * ' ' + '{}\n'
779
780 expected_parameter_list_hello = '(\n{})'.format(param_line_fmt.format('name,'))
781
782 assert expected_parameter_list_hello in content
783
784 expected_a = param_line_fmt.format('[a,[')
785 assert expected_a in content
786
787 expected_b = param_line_fmt.format('b,]]')
788 assert expected_b in content
789
790 expected_c = param_line_fmt.format('c,')
791 assert expected_c in content
792
793 expected_d = param_line_fmt.format('d[,')
794 assert expected_d in content
795
796 expected_e = param_line_fmt.format('e,')
797 assert expected_e in content
798
799 expected_f = param_line_fmt.format('f,]')
800 assert expected_f in content
801
802 expected_parameter_list_foo = '(\n{}{}{}{}{}{})'.format(
803 expected_a,
804 expected_b,
805 expected_c,
806 expected_d,
807 expected_e,
808 expected_f,
809 )
810 assert expected_parameter_list_foo in content
811
812
813 @pytest.mark.sphinx(
814 'html',
815 testroot='domain-js-javascript_maximum_signature_line_length',
816 confoverrides={'javascript_trailing_comma_in_multi_line_signatures': False},
817 )
818 def test_domain_js_javascript_trailing_comma_in_multi_line_signatures_in_html(app):
819 app.build()
820 content = (app.outdir / 'index.html').read_text(encoding='utf8')
821 expected_parameter_list_hello = """\
822
823 <dl>
824 <dd>\
825 <em class="sig-param">\
826 <span class="n"><span class="pre">name</span></span>\
827 </em>\
828 </dd>
829 </dl>
830
831 <span class="sig-paren">)</span>\
832 <a class="headerlink" href="#hello" title="Link to this definition">¶</a>\
833 </dt>\
834 """
835 assert expected_parameter_list_hello in content
836
837 param_line_fmt = '<dd>{}</dd>\n'
838 param_name_fmt = (
839 '<em class="sig-param"><span class="n"><span class="pre">{}</span></span></em>'
840 )
841 optional_fmt = '<span class="optional">{}</span>'
842
843 expected_a = param_line_fmt.format(
844 optional_fmt.format('[')
845 + param_name_fmt.format('a')
846 + ','
847 + optional_fmt.format('['),
848 )
849 assert expected_a in content
850
851 expected_b = param_line_fmt.format(
852 param_name_fmt.format('b')
853 + ','
854 + optional_fmt.format(']')
855 + optional_fmt.format(']'),
856 )
857 assert expected_b in content
858
859 expected_c = param_line_fmt.format(param_name_fmt.format('c') + ',')
860 assert expected_c in content
861
862 expected_d = param_line_fmt.format(
863 param_name_fmt.format('d') + optional_fmt.format('[') + ','
864 )
865 assert expected_d in content
866
867 expected_e = param_line_fmt.format(param_name_fmt.format('e') + ',')
868 assert expected_e in content
869
870 expected_f = param_line_fmt.format(
871 param_name_fmt.format('f') + optional_fmt.format(']')
872 )
873 assert expected_f in content
874
875 expected_parameter_list_foo = """\
876
877 <dl>
878 {}{}{}{}{}{}</dl>
879
880 <span class="sig-paren">)</span>\
881 <a class="headerlink" href="#foo" title="Link to this definition">¶</a>\
882 </dt>\
883 """.format(expected_a, expected_b, expected_c, expected_d, expected_e, expected_f)
884 assert expected_parameter_list_foo in content
885
886
887 @pytest.mark.sphinx(
888 'text',
889 testroot='domain-js-javascript_maximum_signature_line_length',
890 freshenv=True,
891 confoverrides={'javascript_trailing_comma_in_multi_line_signatures': False},
892 )
893 def test_domain_js_javascript_trailing_comma_in_multi_line_signatures_in_text(app):
894 app.build()
895 content = (app.outdir / 'index.txt').read_text(encoding='utf8')
896 param_line_fmt = STDINDENT * ' ' + '{}\n'
897
898 expected_parameter_list_hello = '(\n{})'.format(param_line_fmt.format('name'))
899
900 assert expected_parameter_list_hello in content
901
902 expected_a = param_line_fmt.format('[a,[')
903 assert expected_a in content
904
905 expected_b = param_line_fmt.format('b,]]')
906 assert expected_b in content
907
908 expected_c = param_line_fmt.format('c,')
909 assert expected_c in content
910
911 expected_d = param_line_fmt.format('d[,')
912 assert expected_d in content
913
914 expected_e = param_line_fmt.format('e,')
915 assert expected_e in content
916
917 expected_f = param_line_fmt.format('f]')
918 assert expected_f in content
919
920 expected_parameter_list_foo = '(\n{}{}{}{}{}{})'.format(
921 expected_a,
922 expected_b,
923 expected_c,
924 expected_d,
925 expected_e,
926 expected_f,
927 )
928 assert expected_parameter_list_foo in content
929
930
931 # See: https://github.com/sphinx-doc/sphinx/issues/13217
932 @pytest.mark.sphinx('html', testroot='_blank')
933 def test_js_function_parentheses_in_arguments_and_errors(app: SphinxTestApp) -> None:
934 text = """\
935 .. js:function:: $.getJSON(href)
936
937 :param string href:
938 :throws err:
939 """
940 doctree = restructuredtext.parse(app, text)
941 refnodes = list(doctree.findall(addnodes.pending_xref))
942 assert len(refnodes) == 2
943 assert_node(refnodes[0], [addnodes.pending_xref, nodes.literal, 'string'])
944 assert_node(refnodes[1], [addnodes.pending_xref, nodes.literal, 'err'])