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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
"""test BlockParser."""
import os
import tempfile
from textwrap import dedent
import pytest
from sphinx_gallery.block_parser import BlockParser
from sphinx_gallery.gen_gallery import DEFAULT_GALLERY_CONF
CXX_BODY = dedent(
"""
int x = 3;
return;
"""
)
@pytest.mark.parametrize(
"comment, expected_docstring",
[
pytest.param(
dedent(
"""\
// Title
// =====
//
// description
"""
),
dedent(
"""\
Title
=====
description
"""
),
id="single-line style",
),
# A simple multiline header
pytest.param(
dedent(
"""\
/*
Title
=====
*/
"""
),
dedent(
"""\
Title
=====
"""
),
id="simple multiline",
),
# A multiline comment with aligned decorations on intermediate lines
pytest.param(
dedent(
"""\
/*
* Title
* =====
*/
"""
),
dedent(
"""\
Title
=====
"""
),
id="decorated-multiline",
),
# A multiline comment that starts on the same line as the start symbol
pytest.param(
dedent(
"""\
/* Title
* =====
*
*/
"""
),
dedent(
"""\
Title
=====
"""
),
id="early-multiline",
),
],
)
def test_cxx_titles(comment, expected_docstring):
doc = comment + CXX_BODY
parser = BlockParser("*.cpp", DEFAULT_GALLERY_CONF)
file_conf, blocks, _ = parser._split_content(doc)
assert len(blocks) == 2
assert blocks[0][0] == "text"
assert blocks[0][1] == expected_docstring
@pytest.mark.parametrize(
"filetype, title, special, expected",
[
pytest.param(
"*.py",
"""# Title""",
"# %%\n# A simple comment\n# with two lines",
"A simple comment\nwith two lines\n",
id="simple",
),
pytest.param(
"*.f90",
"""! Title""",
" !%%\n ! Indented single-line comment",
"Indented single-line comment\n",
id="single-line-indented",
),
pytest.param(
"*.cpp",
"""// Title""",
(" // %%\n // First comment line\n // Second comment line\n"),
"First comment line\nSecond comment line\n",
id="indented-separate-sentinel",
),
pytest.param(
"*.cs",
"""// Title""",
(
" //////////////////////////////\n"
" // Indented multi-line comment\n"
" //\n"
" // with a blank line\n"
),
"Indented multi-line comment\n\nwith a blank line\n",
id="block-two-paragraphs",
),
pytest.param(
"*.c",
"""/* Title */""",
(
" /* %% \n"
" Indented multi-line comment\n"
" continued on a second line */\n"
),
"Indented multi-line comment\ncontinued on a second line\n",
id="multiline-block-comment",
),
pytest.param(
"*.c",
"""/* Title */""",
(
" /*%%\n"
" * * List item\n"
" * * Another item\n"
" * * Item 3\n"
" */"
),
("* List item\n* Another item\n* Item 3\n"),
id="multiline-comment-short-form",
),
pytest.param(
"*.jl",
"# Title",
(" #=%%\n * list item 1\n * list item 2\n * subitem\n =#"),
"* list item 1\n* list item 2\n * subitem\n",
id="julia-multiline",
),
pytest.param(
"*.m",
"% Title",
"%% Heading\n% Extended description",
"Heading\n-------\n\nExtended description\n",
id="matlab-autoheading",
),
],
)
def test_rst_blocks(filetype, title, special, expected):
doc = f"{title}\n{CXX_BODY}\n\n{special}\n{CXX_BODY}"
gallery_conf = DEFAULT_GALLERY_CONF.copy()
gallery_conf["filetype_parsers"] = {".m": "Matlab"}
parser = BlockParser(filetype, gallery_conf)
file_conf, blocks, _ = parser._split_content(doc)
assert len(blocks) == 4
assert blocks[2][0] == "text"
assert blocks[2][1] == expected
def test_cpp_file_to_rst():
CODE = """\
// Do stuff
// --------
int main(int argc, char** argv) {
//%% First heading
//This is the start of ``main``
// This is just a comment because of the preceding blank line
int y = 4;
// sphinx_gallery_start_ignore
y = 5; // don't look: this is a secret!
// sphinx_gallery_end_ignore
if (y == 4) {
return 1;
}
// sphinx_gallery_foobar = 14
}
"""
with tempfile.NamedTemporaryFile("wb", suffix=".cpp", delete=False) as f:
f.write(CODE.encode())
try:
parser = BlockParser(f.name, DEFAULT_GALLERY_CONF)
file_conf, blocks, _ = parser.split_code_and_text_blocks(f.name)
finally:
os.remove(f.name)
assert parser.language == "C++"
assert file_conf == {"foobar": 14}
assert "First heading\n-------------\n\nThis is the start" in blocks[2][1]
assert "// This is just a comment" in blocks[3][1]
assert "secret" in blocks[3][1]
assert "sphinx_gallery_foobar" in blocks[3][1]
assert "sphinx_gallery_foobar" not in parser.remove_config_comments(blocks[3][1])
cleaned = parser.remove_ignore_blocks(blocks[3][1])
assert "secret" not in cleaned
assert "y == 4" in cleaned
|