|
14 | 14 |
|
15 | 15 | """Tests for executable snippets in documentation. |
16 | 16 |
|
17 | | -This tests runs code snippets that are executable in `.md` and `.rst` |
18 | | -documentation. It covers all such files under the docs directory, as well as |
19 | | -the top level README file. |
| 17 | +This tests code snippets that are executable in `.md` documentation. It covers |
| 18 | +all such files under the docs directory, as well as the top-level README file. |
20 | 19 |
|
21 | 20 | In addition to checking that the code executes: |
22 | 21 |
|
|
45 | 44 | substitution |
46 | 45 | ---> |
47 | 46 |
|
48 | | - and for `.rst` the substitution is of the form |
49 | | -
|
50 | | - .. test-substitution:: |
51 | | - pattern |
52 | | - substitution |
53 | | -
|
54 | 47 | where pattern is the regex matching pattern (passed to re.compile) and |
55 | 48 | substitution is the replacement string. |
56 | 49 | """ |
@@ -79,8 +72,6 @@ def find_docs_code_snippets_paths() -> Iterator[str]: |
79 | 72 | docs_folder = pathlib.Path(__file__).parent |
80 | 73 | for filename in docs_folder.rglob('*.md'): |
81 | 74 | yield str(filename.relative_to(docs_folder)) |
82 | | - for filename in docs_folder.rglob('*.rst'): |
83 | | - yield str(filename.relative_to(docs_folder)) |
84 | 75 |
|
85 | 76 |
|
86 | 77 | @pytest.mark.parametrize('path', find_docs_code_snippets_paths()) |
@@ -138,120 +129,6 @@ def deindent_snippet(snippet: str) -> str: |
138 | 129 | return '\n'.join(deindented_lines) |
139 | 130 |
|
140 | 131 |
|
141 | | -def find_rst_code_snippets(content: str) -> List[Tuple[str, int]]: |
142 | | - snippets = find_code_snippets( |
143 | | - r'\n.. code-block:: python\n(?:\s+:.*?\n)*\n(.*?)(?:\n\S|\Z)', content |
144 | | - ) |
145 | | - return [(deindent_snippet(content), line_number) for content, line_number in snippets] |
146 | | - |
147 | | - |
148 | | -def find_rst_test_overrides(content: str) -> List[Tuple[Pattern, str]]: |
149 | | - # Find ".. test-substitution::" |
150 | | - test_sub_text = find_code_snippets(r'.. test-substitution::\n(([^\n]*\n){2})', content) |
151 | | - substitutions = [line.split('\n')[:-1] for line, _ in test_sub_text] |
152 | | - return [(re.compile(match.lstrip()), sub.lstrip()) for match, sub in substitutions] |
153 | | - |
154 | | - |
155 | | -def test_find_rst_code_snippets(): |
156 | | - snippets = find_rst_code_snippets( |
157 | | - """ |
158 | | -A 3 by 3 grid of qubits using |
159 | | -
|
160 | | -.. code-block:: python |
161 | | -
|
162 | | - print("hello world") |
163 | | -
|
164 | | -The next level up. |
165 | | -
|
166 | | -.. code-block:: python |
167 | | - :emphasize-lines: 3,5 |
168 | | -
|
169 | | - print("hello 1") |
170 | | -
|
171 | | - for i in range(10): |
172 | | - print(f"hello {i}") |
173 | | -
|
174 | | -More text. |
175 | | -
|
176 | | -.. code-block:: python |
177 | | -
|
178 | | - print("last line") |
179 | | -""" |
180 | | - ) |
181 | | - |
182 | | - assert snippets == [ |
183 | | - ('print("hello world")\n', 4), |
184 | | - ('print("hello 1")\n\nfor i in range(10):\n print(f"hello {i}")\n', 10), |
185 | | - ('print("last line")\n', 20), |
186 | | - ] |
187 | | - |
188 | | - |
189 | | -def test_find_rst_overrides(): |
190 | | - overrides = find_rst_test_overrides( |
191 | | - """ |
192 | | -A 3 by 3 grid of qubits using |
193 | | -
|
194 | | -.. code-block:: python |
195 | | -
|
196 | | - print("hello world") |
197 | | - print("golden") |
198 | | -
|
199 | | -.. test-substitution:: |
200 | | - hello world |
201 | | - goodbye cruel world |
202 | | -
|
203 | | -.. test-substitution:: |
204 | | - golden |
205 | | - yellow |
206 | | -""" |
207 | | - ) |
208 | | - assert len(overrides) == 2 |
209 | | - assert overrides[0][0].match('hello world') |
210 | | - assert overrides[1][0].match('golden') |
211 | | - assert overrides[0][1] == 'goodbye cruel world' |
212 | | - assert overrides[1][1] == 'yellow' |
213 | | - |
214 | | - |
215 | | -def test_apply_rst_overrides(): |
216 | | - content = """ |
217 | | -A 3 by 3 grid of qubits using |
218 | | -
|
219 | | -.. code-block:: python |
220 | | -
|
221 | | - print("hello world") |
222 | | - print("golden") |
223 | | -
|
224 | | -.. test-substitution:: |
225 | | - hello world |
226 | | - goodbye cruel world |
227 | | -
|
228 | | -.. test-substitution:: |
229 | | - golden |
230 | | - yellow |
231 | | -""" |
232 | | - overrides = find_rst_test_overrides(content) |
233 | | - print(overrides) |
234 | | - assert ( |
235 | | - apply_overrides(content, overrides) |
236 | | - == """ |
237 | | -A 3 by 3 grid of qubits using |
238 | | -
|
239 | | -.. code-block:: python |
240 | | -
|
241 | | - print("goodbye cruel world") |
242 | | - print("yellow") |
243 | | -
|
244 | | -.. test-substitution:: |
245 | | - goodbye cruel world |
246 | | - goodbye cruel world |
247 | | -
|
248 | | -.. test-substitution:: |
249 | | - yellow |
250 | | - yellow |
251 | | -""" |
252 | | - ) |
253 | | - |
254 | | - |
255 | 132 | def test_find_markdown_code_snippets(): |
256 | 133 | snippets = find_markdown_code_snippets( |
257 | 134 | """ |
@@ -359,10 +236,6 @@ def assert_file_has_working_code_snippets(path: str, assume_import: bool): |
359 | 236 | overrides = find_markdown_test_overrides(content) |
360 | 237 | content = apply_overrides(content, overrides) |
361 | 238 | snippets = find_markdown_code_snippets(content) |
362 | | - else: |
363 | | - overrides = find_rst_test_overrides(content) |
364 | | - content = apply_overrides(content, overrides) |
365 | | - snippets = find_rst_code_snippets(content) |
366 | 239 | assert_code_snippets_run_in_sequence(snippets, assume_import) |
367 | 240 |
|
368 | 241 |
|
|
0 commit comments