1"""
2    test_directive_patch
3    ~~~~~~~~~~~~~~~~~~~
4
5    Test the patched directives.
6
7    :copyright: Copyright 2007-2021 by the Sphinx team, see AUTHORS.
8    :license: BSD, see LICENSE for details.
9"""
10
11from docutils import nodes
12
13from sphinx.testing import restructuredtext
14from sphinx.testing.util import assert_node
15
16
17def test_code_directive(app):
18    # normal case
19    text = ('.. code::\n'
20            '\n'
21            '   print("hello world")\n')
22
23    doctree = restructuredtext.parse(app, text)
24    assert_node(doctree, [nodes.document, nodes.literal_block, 'print("hello world")'])
25    assert_node(doctree[0], language="default", highlight_args={})
26
27    # with language
28    text = ('.. code:: python\n'
29            '\n'
30            '   print("hello world")\n')
31
32    doctree = restructuredtext.parse(app, text)
33    assert_node(doctree, [nodes.document, nodes.literal_block, 'print("hello world")'])
34    assert_node(doctree[0], language="python", highlight_args={})
35
36    # :number-lines: option
37    text = ('.. code:: python\n'
38            '   :number-lines:\n'
39            '\n'
40            '   print("hello world")\n')
41
42    doctree = restructuredtext.parse(app, text)
43    assert_node(doctree, [nodes.document, nodes.literal_block, 'print("hello world")'])
44    assert_node(doctree[0], language="python", linenos=True, highlight_args={})
45
46    # :number-lines: option
47    text = ('.. code:: python\n'
48            '   :number-lines: 5\n'
49            '\n'
50            '   print("hello world")\n')
51
52    doctree = restructuredtext.parse(app, text)
53    assert_node(doctree, [nodes.document, nodes.literal_block, 'print("hello world")'])
54    assert_node(doctree[0], language="python", linenos=True, highlight_args={'linenostart': 5})
55
56
57def test_math_directive(app):
58    # normal case
59    text = '.. math:: E = mc^2'
60    doctree = restructuredtext.parse(app, text)
61    assert_node(doctree, [nodes.document, nodes.math_block, 'E = mc^2\n\n'])
62
63    # :name: option
64    text = ('.. math:: E = mc^2\n'
65            '   :name: eq1\n')
66    doctree = restructuredtext.parse(app, text)
67    assert_node(doctree, [nodes.document, (nodes.target,
68                                           [nodes.math_block, "E = mc^2\n\n"])])
69    assert_node(doctree[1], nodes.math_block, docname='index', label="eq1", number=1)
70
71    # :label: option
72    text = ('.. math:: E = mc^2\n'
73            '   :label: eq2\n')
74    doctree = restructuredtext.parse(app, text)
75    assert_node(doctree, [nodes.document, (nodes.target,
76                                           [nodes.math_block, 'E = mc^2\n\n'])])
77    assert_node(doctree[1], nodes.math_block, docname='index', label="eq2", number=2)
78
79    # :label: option without value
80    text = ('.. math:: E = mc^2\n'
81            '   :label:\n')
82    doctree = restructuredtext.parse(app, text)
83    assert_node(doctree, [nodes.document, (nodes.target,
84                                           [nodes.math_block, 'E = mc^2\n\n'])])
85    assert_node(doctree[1], nodes.math_block, ids=['equation-index-0'],
86                docname='index', label="index:0", number=3)
87