/
test_gen_rst.py
64 lines (48 loc) · 1.91 KB
/
test_gen_rst.py
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
# -*- coding: utf-8 -*-
# Author: Óscar Nájera
# License: 3-clause BSD
"""
Testing the rst files generator
"""
from __future__ import division, absolute_import, print_function
import tempfile
import sphinxgallery.gen_rst as sg
from nose.tools import assert_equals, assert_false
import ast
def test_split_code_and_text_blocks():
"""Test if a known example file gets properly split"""
blocks = sg.split_code_and_text_blocks('examples/just_code.py')
assert_equals(blocks[0][0], 'text')
assert_equals(blocks[1][0], 'code')
def test_bug_cases_of_notebook_syntax():
"""Test over the known requirements of supported syntax in the
notebook styled comments"""
with open('sphinxgallery/tests/reference_parse.txt') as reference:
ref_blocks = ast.literal_eval(reference.read())
blocks = sg.split_code_and_text_blocks('tutorials/plot_parse.py')
assert_equals(blocks, ref_blocks)
def test_codestr2rst():
"""Test the correct translation of a code block into rst"""
output = sg.codestr2rst('print("hello world")')
reference = """
.. code-block:: python
print("hello world")"""
assert_equals(reference, output)
def test_extract_intro():
with tempfile.NamedTemporaryFile() as f:
f.write(b'\n'.join([b'"""'
b'Docstring header',
b'================',
b'',
b'This is the explanation of the example'
b'which goes on and on',
b'', b'',
b'And this is a second paragraph',
b'"""']))
f.flush()
result = sg.extract_intro(f.name)
assert_false('Docstring' in result)
assert_equals(
result,
'This is the explanation of the example which goes on and on')
assert_false('second paragraph' in result)