summaryrefslogtreecommitdiff
path: root/tests/test_build_text.py
blob: d65135047976570af0592da3113cafea504be5fb (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# -*- coding: utf-8 -*-
"""
    test_build_text
    ~~~~~~~~~~~~~~~

    Test the build process with Text builder with the test root.

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

from docutils.utils import column_width
from sphinx.writers.text import MAXWIDTH

from util import with_app


def with_text_app(*args, **kw):
    default_kw = {
        'buildername': 'text',
        'srcdir': '(empty)',
        'confoverrides': {
            'project': 'text',
            'master_doc': 'contents',
        },
    }
    default_kw.update(kw)
    return with_app(*args, **default_kw)


@with_text_app()
def test_maxwitdh_with_prefix(app):
    long_string = u' '.join([u"ham"] * 30)
    contents = (
            u".. seealso:: %(long_string)s\n\n"
            u"* %(long_string)s\n"
            u"* %(long_string)s\n"
            u"\nspam egg\n"
            ) % locals()

    (app.srcdir / 'contents.rst').write_text(contents, encoding='utf-8')
    app.builder.build_all()
    result = (app.outdir / 'contents.txt').text(encoding='utf-8')

    lines = result.splitlines()
    line_widths = [column_width(line) for line in lines]
    assert max(line_widths) < MAXWIDTH
    assert lines[0].startswith('See also: ham')
    assert lines[1].startswith('  ham')
    assert lines[2] == ''
    assert lines[3].startswith('* ham')
    assert lines[4].startswith('  ham')
    assert lines[5] == ''
    assert lines[6].startswith('* ham')
    assert lines[7].startswith('  ham')
    assert lines[8] == ''
    assert lines[9].startswith('spam egg')


@with_text_app()
def test_lineblock(app):
    # regression test for #1109: need empty line after line block
    contents = (
            u"* one\n"
            u"\n"
            u"  | line-block 1\n"
            u"  | line-block 2\n"
            u"\n"
            u"followed paragraph.\n"
            )

    (app.srcdir / 'contents.rst').write_text(contents, encoding='utf-8')
    app.builder.build_all()
    result = (app.outdir / 'contents.txt').text(encoding='utf-8')

    expect = (
            u"* one\n"
            u"\n"
            u"     line-block 1\n"
            u"     line-block 2\n"
            u"\n"
            u"followed paragraph.\n"
            )

    assert result == expect


@with_text_app()
def test_nonascii_title_line(app):
    title = u'\u65e5\u672c\u8a9e'
    underline = u'=' * column_width(title)
    content = u'\n'.join((title, underline, u''))

    (app.srcdir / 'contents.rst').write_text(content, encoding='utf-8')
    app.builder.build_all()
    result = (app.outdir / 'contents.txt').text(encoding='utf-8')

    expect_underline = underline.replace('=', '*')
    result_underline = result.splitlines()[2].strip()
    assert expect_underline == result_underline


@with_text_app()
def test_nonascii_table(app):
    text = u'\u65e5\u672c\u8a9e'
    contents = (u"\n.. list-table::"
                 "\n"
                 "\n   - - spam"
                 "\n     - egg"
                 "\n"
                 "\n   - - %(text)s"
                 "\n     - %(text)s"
                 "\n" % locals())

    (app.srcdir / 'contents.rst').write_text(contents, encoding='utf-8')
    app.builder.build_all()
    result = (app.outdir / 'contents.txt').text(encoding='utf-8')

    lines = [line.strip() for line in result.splitlines() if line.strip()]
    line_widths = [column_width(line) for line in lines]
    assert len(set(line_widths)) == 1  # same widths


@with_text_app()
def test_nonascii_maxwidth(app):
    sb_text = u'abc'  #length=3
    mb_text = u'\u65e5\u672c\u8a9e'  #length=3

    sb_line = ' '.join([sb_text] * int(MAXWIDTH / 3))
    mb_line = ' '.join([mb_text] * int(MAXWIDTH / 3))
    mix_line = ' '.join([sb_text, mb_text] * int(MAXWIDTH / 6))

    contents = u'\n\n'.join((sb_line, mb_line, mix_line))

    (app.srcdir / 'contents.rst').write_text(contents, encoding='utf-8')
    app.builder.build_all()
    result = (app.outdir / 'contents.txt').text(encoding='utf-8')

    lines = [line.strip() for line in result.splitlines() if line.strip()]
    line_widths = [column_width(line) for line in lines]
    assert max(line_widths) < MAXWIDTH