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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
"""
Test cases for Nikola ReST extensions.
A sample will be rendered by CompileRest.
One method is provided for checking the resulting HTML:
* assert_html_contains(html, element, attributes=None, text=None)
"""
from io import StringIO
import pytest
from lxml import html as lxml_html
import nikola.plugins.compile.rest
import nikola.plugins.compile.rest.listing
from nikola.plugins.compile.rest import vimeo
from nikola.utils import _reload, LocaleBorg
from .helper import FakeSite
def test_ReST_extension():
sample = '.. raw:: html\n\n <iframe src="foo" height="bar">spam</iframe>'
html = get_html_from_rst(sample)
assert_html_contains(html, "iframe", attributes={"src": "foo"}, text="spam")
with pytest.raises(Exception):
assert_html_contains("eggs", {})
def test_math_extension_outputs_tex():
"""Test that math is outputting TeX code."""
sample = r":math:`e^{ix} = \cos x + i\sin x`"
html = get_html_from_rst(sample)
assert_html_contains(
html,
"span",
attributes={"class": "math"},
text=r"\(e^{ix} = \cos x + i\sin x\)",
)
def test_soundcloud_iframe():
"""Test SoundCloud iframe tag generation"""
sample = ".. soundcloud:: SID\n :height: 400\n :width: 600"
html = get_html_from_rst(sample)
assert_html_contains(
html,
"iframe",
attributes={
"src": (
"https://w.soundcloud.com/player/"
"?url=http://api.soundcloud.com/"
"tracks/SID"
),
"height": "400",
"width": "600",
},
)
def test_youtube_iframe():
"""Test Youtube iframe tag generation"""
sample = ".. youtube:: YID\n :height: 400\n :width: 600"
html = get_html_from_rst(sample)
assert_html_contains(
html,
"iframe",
attributes={
"src": (
"https://www.youtube-nocookie.com"
"/embed/YID?rel=0&"
"wmode=transparent"
),
"height": "400",
"width": "600",
"frameborder": "0",
"allowfullscreen": "",
"allow": "encrypted-media",
},
)
def test_vimeo(disable_vimeo_api_query):
"""Test Vimeo iframe tag generation"""
sample = ".. vimeo:: VID\n :height: 400\n :width: 600"
html = get_html_from_rst(sample)
assert_html_contains(
html,
"iframe",
attributes={
"src": ("https://player.vimeo.com/" "video/VID"),
"height": "400",
"width": "600",
},
)
@pytest.mark.parametrize(
"sample",
[
".. code-block:: python\n\n import antigravity",
".. sourcecode:: python\n\n import antigravity",
],
)
def test_rendering_codeblock_alias(sample):
"""Test CodeBlock aliases"""
get_html_from_rst(sample)
def test_doc_doesnt_exist():
with pytest.raises(Exception):
assert_html_contains("anything", {})
def test_doc():
sample = "Sample for testing my :doc:`fake-post`"
html = get_html_from_rst(sample)
assert_html_contains(
html, "a", text="Fake post", attributes={"href": "/posts/fake-post"}
)
def test_doc_titled():
sample = "Sample for testing my :doc:`titled post <fake-post>`"
html = get_html_from_rst(sample)
assert_html_contains(
html, "a", text="titled post", attributes={"href": "/posts/fake-post"}
)
@pytest.fixture(autouse=True, scope="module")
def localeborg_base():
"""A base config of LocaleBorg."""
LocaleBorg.reset()
assert not LocaleBorg.initialized
LocaleBorg.initialize({}, "en")
assert LocaleBorg.initialized
assert LocaleBorg().current_lang == "en"
try:
yield
finally:
LocaleBorg.reset()
assert not LocaleBorg.initialized
def get_html_from_rst(rst):
"""Create html output from rst string"""
compiler = nikola.plugins.compile.rest.CompileRest()
compiler.set_site(FakeSite())
return compiler.compile_string(rst)[0]
class FakePost:
def __init__(self, outfile):
self._depfile = {outfile: []}
def assert_html_contains(html, element, attributes=None, text=None):
"""
Test if HTML document includes an element with the given attributes
and text content.
The HTML is parsed with lxml for checking against the data you
provide. The method takes an element argument, a string representing
the *name* of an HTML tag, like "script" or "iframe".
We will try to find this tag in the document and perform the tests
on it. You can pass a dictionary to the attributes kwarg
representing the name and the value of the tag attributes. The text
kwarg takes a string argument, which will be tested against the
contents of the HTML element.
One last caveat: you need to url unquote your urls if you are going
to test attributes like "src" or "link", since the HTML rendered by
docutils will be always unquoted.
"""
html_doc = lxml_html.parse(StringIO(html))
try:
tag = next(html_doc.iter(element))
except StopIteration:
raise Exception("<{0}> not in {1}".format(element, html))
if attributes:
arg_attrs = set(attributes.items())
tag_attrs = set(tag.items())
assert arg_attrs.issubset(tag_attrs)
if text:
assert text in tag.text
@pytest.fixture
def disable_vimeo_api_query():
"""
Disable query of the vimeo api over the wire
Set Vimeo.request_size to False for avoiding querying the Vimeo api
over the network.
"""
before = vimeo.Vimeo.request_size
vimeo.Vimeo.request_size = False
try:
_reload(nikola.plugins.compile.rest)
yield
finally:
vimeo.Vimeo.request_size = before
|