File: test_backend.py

package info (click to toggle)
pybtex-docutils 1.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 252 kB
  • sloc: python: 460; makefile: 144
file content (197 lines) | stat: -rw-r--r-- 5,611 bytes parent folder | download | duplicates (2)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import docutils
import docutils.nodes
import docutils.utils
import pytest
import pybtex.plugin
import pybtex.database
from pybtex.richtext import HRef, Tag, Text

from pybtex_docutils import Backend


def render_str(richtext):
    return "".join(str(node) for node in richtext.render(Backend()))


# may remove this test when new pybtex is out
def test_text():
    assert Backend().format_text('hi') == Backend().format_str('hi')


def test_tag():
    tag = Tag('em', 'hello')
    assert render_str(tag) == '<emphasis>hello</emphasis>'


def test_tag_text():
    tag = Tag('em', Text('hello', ' world'))
    assert render_str(tag) == '<emphasis>hello world</emphasis>'


def test_tag_strong():
    tag = Tag('strong', 'hello')
    assert render_str(tag) == '<strong>hello</strong>'


def test_tag_i():
    tag = Tag('i', 'hello')
    assert render_str(tag) == '<emphasis>hello</emphasis>'


def test_tag_b():
    tag = Tag('b', 'hello')
    assert render_str(tag) == '<strong>hello</strong>'


def test_tag_tt():
    tag = Tag('tt', 'hello')
    assert render_str(tag) == '<literal>hello</literal>'


def test_tag_sup():
    tag = Tag('sup', 'hello')
    assert render_str(tag) == '<superscript>hello</superscript>'


def test_tag_sub():
    tag = Tag('sub', 'hello')
    assert render_str(tag) == '<subscript>hello</subscript>'


def test_tag_unknown():
    tag = Tag('***unknown***', 'hello')
    assert render_str(tag) == 'hello'


def test_href():
    href = HRef('http://www.example.com', 'hyperlinked text')
    assert render_str(href) == (
        '<reference refuri="http://www.example.com">'
        'hyperlinked text'
        '</reference>')


def test_href_text():
    href = HRef('http://www.example.com', Text('hyperlinked', ' text'))
    assert render_str(href) == (
        '<reference refuri="http://www.example.com">'
        'hyperlinked text'
        '</reference>')


def test_render_sequence():
    text = Text('hello ', Tag('em', 'world'))
    assert render_str(text) == 'hello <emphasis>world</emphasis>'


@pytest.fixture
def entry():
    data = pybtex.database.BibliographyData({
        'hongquin1997': pybtex.database.Entry(
            'article',
            fields={
                'language': 'english',
                'title': 'Predicting the Diffusion Coefficient'
                         ' in Supercritical Fluids',
                'journal': 'Ind. Eng. Chem. Res.',
                'volume': '36',
                'year': '1997',
                'pages': '888-895',
            },
            persons={'author': [
                pybtex.database.Person('Liu, Hongquin'),
                pybtex.database.Person('Ruckenstein, Eli')]},
            )})
    style = pybtex.plugin.find_plugin('pybtex.style.formatting', 'plain')()
    entries = list(style.format_entries(data.entries.values()))
    return entries[0]


@pytest.fixture
def document():
    return docutils.utils.new_document('test.rst')


def test_citation(entry, document):
    node = Backend().citation(entry, document)
    assert str(node) == (
        '<citation ids="hongquin1997" names="hongquin1997">'
        '<label>hongquin1997</label>'
        '<paragraph>'
        'Hongquin Liu and Eli Ruckenstein. '
        'Predicting the diffusion coefficient in supercritical fluids. '
        '<emphasis>Ind. Eng. Chem. Res.</emphasis>, '
        '36:888–895, 1997.'
        '</paragraph>'
        '</citation>')


def test_citation_reference(entry, document):
    node = Backend().citation_reference(entry, document)
    id_ = (
        "id1" if docutils.__version_info__ < (0, 18) else
        "citation-reference-1"
    )
    assert str(node) == (
        f'<citation_reference ids="{id_}" '
        f'refname="hongquin1997">'
        f'hongquin1997'
        f'</citation_reference>')


def test_citation_use_label(entry, document):
    node = Backend().citation(
        entry, document, use_key_as_label=False)
    assert str(node) == (
        '<citation ids="hongquin1997" names="hongquin1997">'
        '<label>1</label>'
        '<paragraph>'
        'Hongquin Liu and Eli Ruckenstein. '
        'Predicting the diffusion coefficient in supercritical fluids. '
        '<emphasis>Ind. Eng. Chem. Res.</emphasis>, '
        '36:888–895, 1997.'
        '</paragraph>'
        '</citation>')


def test_citation_reference_use_label(entry, document):
    node = Backend().citation_reference(
        entry, document, use_key_as_label=False)
    id_ = (
        "id1" if docutils.__version_info__ < (0, 18) else
        "citation-reference-1"
    )
    assert str(node) == (
        f'<citation_reference ids="{id_}" '
        f'refname="hongquin1997">'
        f'1'
        f'</citation_reference>')


def test_footnote(entry, document):
    node = Backend().footnote(entry, document)
    assert str(node) == (
        '<footnote auto="1" ids="hongquin1997" names="hongquin1997">'
        '<paragraph>'
        'Hongquin Liu and Eli Ruckenstein. '
        'Predicting the diffusion coefficient in supercritical fluids. '
        '<emphasis>Ind. Eng. Chem. Res.</emphasis>, '
        '36:888–895, 1997.'
        '</paragraph>'
        '</footnote>')


def test_footnote_reference(entry, document):
    node = Backend().footnote_reference(entry, document)
    id_ = (
        "id1" if docutils.__version_info__ < (0, 18) else
        "footnote-reference-1"
    )
    assert str(node) == (
        f'<footnote_reference auto="1" ids="[\'{id_}\']" '
        f'refname="hongquin1997"/>')


def test_write_entry():
    with pytest.raises(NotImplementedError):
        Backend().write_entry(None, None, None)