summaryrefslogtreecommitdiff
path: root/tests/test_ext_graphviz.py
blob: 762add6f09fd7d89cd3ce0f911378001bc5fa84b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# -*- coding: utf-8 -*-
"""
    test_ext_graphviz
    ~~~~~~~~~~~~~~~~~

    Test sphinx.ext.graphviz extension.

    :copyright: Copyright 2007-2018 by the Sphinx team, see AUTHORS.
    :license: BSD, see LICENSE for details.
"""

import re

import pytest


@pytest.mark.sphinx('html', testroot='ext-graphviz')
@pytest.mark.usefixtures('if_graphviz_found')
def test_graphviz_png_html(app, status, warning):
    app.builder.build_all()

    content = (app.outdir / 'index.html').text()
    html = (r'<div class="figure" .*?>\s*<img .*?/>\s*<p class="caption">'
            r'<span class="caption-text">caption of graph</span>.*</p>\s*</div>')
    assert re.search(html, content, re.S)

    html = 'Hello <img .*?/>\n graphviz world'
    assert re.search(html, content, re.S)

    html = '<img src=".*?" alt="digraph {\n  bar -&gt; baz\n}" />'
    assert re.search(html, content, re.M)

    html = (r'<div class="figure align-right" .*?>\s*<img .*?/>\s*<p class="caption">'
            r'<span class="caption-text">on right</span>.*</p>\s*</div>')
    assert re.search(html, content, re.S)

    html = (r'<div align=\"center\" class=\"align-center\">'
            r'<img src=\".*\.png\" alt=\"digraph foo {\n'
            r'centered\n'
            r'}\" />\n</div>')
    assert re.search(html, content, re.S)


@pytest.mark.sphinx('html', testroot='ext-graphviz',
                    confoverrides={'graphviz_output_format': 'svg'})
@pytest.mark.usefixtures('if_graphviz_found')
def test_graphviz_svg_html(app, status, warning):
    app.builder.build_all()

    content = (app.outdir / 'index.html').text()

    html = (r'<div class=\"figure\" .*?>\n'
            r'<object data=\".*\.svg\".*>\n'
            r'\s+<p class=\"warning\">digraph foo {\n'
            r'bar -&gt; baz\n'
            r'}</p></object>\n'
            r'<p class=\"caption\"><span class=\"caption-text\">'
            r'caption of graph</span>.*</p>\n</div>')
    assert re.search(html, content, re.S)

    html = (r'Hello <object.*>\n'
            r'\s+<p class=\"warning\">graph</p></object>\n'
            r' graphviz world')
    assert re.search(html, content, re.S)

    html = (r'<div class=\"figure align-right\" .*\>\n'
            r'<object data=\".*\.svg\".*>\n'
            r'\s+<p class=\"warning\">digraph bar {\n'
            r'foo -&gt; bar\n'
            r'}</p></object>\n'
            r'<p class=\"caption\"><span class=\"caption-text\">'
            r'on right</span>.*</p>\n'
            r'</div>')
    assert re.search(html, content, re.S)

    html = (r'<div align=\"center\" class=\"align-center\">'
            r'<object data=\".*\.svg\".*>\n'
            r'\s+<p class=\"warning\">digraph foo {\n'
            r'centered\n'
            r'}</p></object>\n'
            r'</div>')
    assert re.search(html, content, re.S)


@pytest.mark.sphinx('latex', testroot='ext-graphviz')
@pytest.mark.usefixtures('if_graphviz_found')
def test_graphviz_latex(app, status, warning):
    app.builder.build_all()

    content = (app.outdir / 'SphinxTests.tex').text()
    macro = ('\\\\begin{figure}\\[htbp\\]\n\\\\centering\n\\\\capstart\n\n'
             '\\\\includegraphics{graphviz-\\w+.pdf}\n'
             '\\\\caption{caption of graph}\\\\label{.*}\\\\end{figure}')
    assert re.search(macro, content, re.S)

    macro = 'Hello \\\\includegraphics{graphviz-\\w+.pdf} graphviz world'
    assert re.search(macro, content, re.S)

    macro = ('\\\\begin{wrapfigure}{r}{0pt}\n\\\\centering\n'
             '\\\\includegraphics{graphviz-\\w+.pdf}\n'
             '\\\\caption{on right}\\\\label{.*}\\\\end{wrapfigure}')
    assert re.search(macro, content, re.S)

    macro = (r'\{\\hfill'
             r'\\includegraphics{graphviz-.*}'
             r'\\hspace\*{\\fill}}')
    assert re.search(macro, content, re.S)


@pytest.mark.sphinx('html', testroot='ext-graphviz', confoverrides={'language': 'xx'})
@pytest.mark.usefixtures('if_graphviz_found')
def test_graphviz_i18n(app, status, warning):
    app.builder.build_all()

    content = (app.outdir / 'index.html').text()
    html = '<img src=".*?" alt="digraph {\n  BAR -&gt; BAZ\n}" />'
    assert re.search(html, content, re.M)