forked from quantumlib/Cirq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnippets_test.py
811 lines (631 loc) · 20.5 KB
/
snippets_test.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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
# Copyright 2018 The Cirq Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for executable snippets in documentation.
This tests runs code snippets that are executable in `.md` and `.rst`
documentation. It covers all such files under the docs directory, as well as
the top level README file.
In addition to checking that the code executes:
* The test looks for comments of the form `# prints` and then the test
checks that the result of the code snippets commented out code after
that print statement. So if the snippet is
print('foo')
# prints
# foo
Then this checks that the print statement indeed prints 'foo'. Note that
leading spaces are ignored. If there are any characters after `# prints`,
like for instance `# prints something like` then this comparison is
not done. This is useful for documenting code that does print but
the output is non-deterministic.
* The test looks for substitutions that will be applied to the snippets
before running the code. This is useful if a documentation example has
a very long runtime, but can be made shorter by changing some variables
(like number of qubits or number of repetitions). For `.md` files the
substitution is of the form
<!---test_substitution
pattern
substitution
--->
and for `.rst` the substitution is of the form
.. test-substitution::
pattern
substitution
where pattern is the regex matching pattern (passed to re.compile) and
substitution is the replacement string.
"""
import inspect
import sys
from typing import Any, Dict, List, Pattern, Tuple, Iterator
import os
import pathlib
import re
import pytest
import cirq
def test_can_run_readme_code_snippets():
# Get the contents of the README.md file at the project root.
readme_path = 'README.rst'
assert readme_path is not None
assert_file_has_working_code_snippets(readme_path, assume_import=False)
def find_docs_code_snippets_paths() -> Iterator[str]:
docs_folder = pathlib.Path(__file__).parent
for filename in docs_folder.rglob('*.md'):
yield str(filename.relative_to(docs_folder))
for filename in docs_folder.rglob('*.rst'):
yield str(filename.relative_to(docs_folder))
@pytest.mark.parametrize('path', find_docs_code_snippets_paths())
def test_can_run_docs_code_snippets(path):
docs_folder = os.path.dirname(__file__)
assert_file_has_working_code_snippets(os.path.join(docs_folder, path), assume_import=True)
def find_code_snippets(pattern: str, content: str) -> List[Tuple[str, int]]:
matches = re.finditer(pattern, content, re.MULTILINE | re.DOTALL)
newlines = re.finditer("\n", content)
snippets = []
current_line = 1
for match in matches:
for newline in newlines:
current_line += 1
if newline.start() >= match.start():
snippets.append((match.group(1), current_line))
break
return snippets
def find_markdown_code_snippets(content: str) -> List[Tuple[str, int]]:
return find_code_snippets("\n```python(.*?)\n```\n", content)
def find_markdown_test_overrides(content: str) -> List[Tuple[Pattern, str]]:
test_sub_text = find_code_snippets("<!---test_substitution\n(.*?)--->", content)
substitutions = [line.split('\n')[:-1] for line, _ in test_sub_text]
return [(re.compile(match), sub) for match, sub in substitutions]
def apply_overrides(content: str, overrides: List[Tuple[Pattern, str]]) -> str:
override_content = content
for pattern, sub in overrides:
override_content = re.sub(pattern, sub, override_content)
return override_content
def deindent_snippet(snippet: str) -> str:
deindented_lines = []
indentation_amount = None
for line in snippet.split('\n'):
# The first non-empty line determines the indentation level.
if indentation_amount is None and re.match(r'\s*\S', line):
leading_whitespace = re.match(r'\s*', line)
if leading_whitespace:
indentation_amount = len(leading_whitespace.group(0))
if line:
deindented_lines.append(line[indentation_amount:])
else:
deindented_lines.append(line)
return '\n'.join(deindented_lines)
def find_rst_code_snippets(content: str) -> List[Tuple[str, int]]:
snippets = find_code_snippets(
r'\n.. code-block:: python\n(?:\s+:.*?\n)*\n(.*?)(?:\n\S|\Z)', content
)
return [(deindent_snippet(content), line_number) for content, line_number in snippets]
def find_rst_test_overrides(content: str) -> List[Tuple[Pattern, str]]:
# Find ".. test-substitution::"
test_sub_text = find_code_snippets(r'.. test-substitution::\n(([^\n]*\n){2})', content)
substitutions = [line.split('\n')[:-1] for line, _ in test_sub_text]
return [(re.compile(match.lstrip()), sub.lstrip()) for match, sub in substitutions]
def test_find_rst_code_snippets():
snippets = find_rst_code_snippets(
"""
A 3 by 3 grid of qubits using
.. code-block:: python
print("hello world")
The next level up.
.. code-block:: python
:emphasize-lines: 3,5
print("hello 1")
for i in range(10):
print(f"hello {i}")
More text.
.. code-block:: python
print("last line")
"""
)
assert snippets == [
('print("hello world")\n', 4),
('print("hello 1")\n\nfor i in range(10):\n print(f"hello {i}")\n', 10),
('print("last line")\n', 20),
]
def test_find_rst_overrides():
overrides = find_rst_test_overrides(
"""
A 3 by 3 grid of qubits using
.. code-block:: python
print("hello world")
print("golden")
.. test-substitution::
hello world
goodbye cruel world
.. test-substitution::
golden
yellow
"""
)
assert len(overrides) == 2
assert overrides[0][0].match('hello world')
assert overrides[1][0].match('golden')
assert overrides[0][1] == 'goodbye cruel world'
assert overrides[1][1] == 'yellow'
def test_apply_rst_overrides():
content = """
A 3 by 3 grid of qubits using
.. code-block:: python
print("hello world")
print("golden")
.. test-substitution::
hello world
goodbye cruel world
.. test-substitution::
golden
yellow
"""
overrides = find_rst_test_overrides(content)
print(overrides)
assert (
apply_overrides(content, overrides)
== """
A 3 by 3 grid of qubits using
.. code-block:: python
print("goodbye cruel world")
print("yellow")
.. test-substitution::
goodbye cruel world
goodbye cruel world
.. test-substitution::
yellow
yellow
"""
)
def test_find_markdown_code_snippets():
snippets = find_markdown_code_snippets(
"""
A 3 by 3 grid of qubits using
```python
print("hello world")
```
The next level up.
```python
print("hello 1")
for i in range(10):
print(f"hello {i}")
```
More text.
```python
print("last line")
```
"""
)
assert snippets == [
('\nprint("hello world")', 4),
('\nprint("hello 1")\n\nfor i in range(10):\n print(f"hello {i}")', 10),
('\nprint("last line")', 19),
]
def test_find_markdown_test_overrides():
overrides = find_markdown_test_overrides(
"""
A 3 by 3 grid of qubits using
```python
print("hello world")
```
<!---test_substitution
hello
goodbye
--->
<!---test_substitution
world
universe
--->
"""
)
assert len(overrides) == 2
assert overrides[0][0].match('hello')
assert overrides[1][0].match('world')
assert overrides[0][1] == 'goodbye'
assert overrides[1][1] == 'universe'
def test_apply_overrides_markdown():
content = """
A 3 by 3 grid of qubits using
```python
print("hello world")
```
<!---test_substitution
hello
goodbye
--->
<!---test_substitution
world
universe
--->
"""
overrides = find_markdown_test_overrides(content)
assert (
apply_overrides(content, overrides)
== """
A 3 by 3 grid of qubits using
```python
print("goodbye universe")
```
<!---test_substitution
goodbye
goodbye
--->
<!---test_substitution
universe
universe
--->
"""
)
def assert_file_has_working_code_snippets(path: str, assume_import: bool):
"""Checks that code snippets in a file actually run."""
with open(path, encoding='utf-8') as f:
content = f.read()
# Find snippets of code, and execute them. They should finish.
if path.endswith('.md'):
overrides = find_markdown_test_overrides(content)
content = apply_overrides(content, overrides)
snippets = find_markdown_code_snippets(content)
else:
overrides = find_rst_test_overrides(content)
content = apply_overrides(content, overrides)
snippets = find_rst_code_snippets(content)
assert_code_snippets_run_in_sequence(snippets, assume_import)
def assert_code_snippets_run_in_sequence(snippets: List[Tuple[str, int]], assume_import: bool):
"""Checks that a sequence of code snippets actually run.
State is kept between snippets. Imports and variables defined in one
snippet will be visible in later snippets.
"""
state: Dict[str, Any] = {}
if assume_import:
exec('import cirq', state)
for content, line_number in snippets:
assert_code_snippet_executes_correctly(content, state, line_number)
def _canonicalize_printed_line_chunk(chunk: str) -> str:
chunk = ' ' + chunk + ' '
# Reduce trailing '.0' at end of number.
chunk = chunk.replace('.0-', '. -')
chunk = chunk.replace('.0+', '. +')
# Remove leading spacing.
while '[ ' in chunk:
chunk = chunk.replace('[ ', '[')
# Remove sign before zero.
chunk = chunk.replace('-0 ', '+0 ')
chunk = chunk.replace('-0. ', '+0. ')
chunk = chunk.replace('-0j', '+0j')
chunk = chunk.replace('-0.j', '+0.j')
# Remove possibly-redundant + sign.
chunk = chunk.replace(' +0. ', ' 0. ')
chunk = chunk.replace(' +0.j', ' 0.j')
# Remove double-spacing.
while ' ' in chunk:
chunk = chunk.replace(' ', ' ')
# Remove spaces before imaginary unit.
while ' j' in chunk:
chunk = chunk.replace(' j', 'j')
# Remove padding spaces.
chunk = chunk.strip()
if chunk.startswith('+'):
chunk = chunk[1:]
return chunk
def canonicalize_printed_line(line: str) -> str:
"""Remove minor variations between outputs on some systems.
Basically, numpy is extremely inconsistent about where it puts spaces and
minus signs on 0s. This method goes through the line looking for stuff
that looks like it came from numpy, and if so then strips out spacing and
turns signed zeroes into just zeroes.
Args:
line: The line to canonicalize.
Returns:
The canonicalized line.
"""
prev_end = 0
result = []
for match in re.finditer(r"\[([^\]]+\.[^\]]*)\]", line):
start = match.start() + 1
end = match.end() - 1
result.append(line[prev_end:start])
result.append(_canonicalize_printed_line_chunk(line[start:end]))
prev_end = end
result.append(line[prev_end:])
return ''.join(result).rstrip()
def test_canonicalize_printed_line():
x = 'first [-0.5-0.j 0. -0.5j] then [-0. 0.]'
assert canonicalize_printed_line(x) == ('first [-0.5+0.j 0. -0.5j] then [0. 0.]')
a = '[-0.5-0.j 0. -0.5j 0. -0.5j -0.5+0.j ]'
b = '[-0.5-0. j 0. -0.5j 0. -0.5j -0.5+0. j]'
assert canonicalize_printed_line(a) == canonicalize_printed_line(b)
assert len({canonicalize_printed_line(e) for e in ['[2.2]', '[+2.2]', '[ 2.2]']}) == 1
assert len({canonicalize_printed_line(e) for e in ['[-0.]', '[+0.]', '[ 0.]', '[0.]']}) == 1
a = '[[ 0.+0.j 1.+0.j]'
b = '[[0.+0.j 1.+0.j]'
assert canonicalize_printed_line(a) == canonicalize_printed_line(b)
def assert_code_snippet_executes_correctly(snippet: str, state: Dict, line_number: int = None):
"""Executes a snippet and compares output / errors to annotations."""
raises_annotation = re.search(r"# raises\s*(\S*)", snippet)
if raises_annotation is None:
before = snippet
after = None
expected_failure = None
else:
before = snippet[: raises_annotation.start()]
after = snippet[raises_annotation.start() :]
expected_failure = raises_annotation.group(1)
if not expected_failure:
raise AssertionError('No error type specified for # raises line.')
assert_code_snippet_runs_and_prints_expected(before, state, line_number)
if expected_failure is not None:
assert after is not None
assert_code_snippet_fails(after, state, expected_failure)
def assert_code_snippet_runs_and_prints_expected(
snippet: str, state: Dict, line_number: int = None
):
"""Executes a snippet and compares captured output to annotated output."""
output_lines: List[str] = []
expected_outputs = find_expected_outputs(snippet)
def print_capture(*values, sep=' '):
output_lines.extend(sep.join(str(e) for e in values).split('\n'))
state['print'] = print_capture
try:
exec(snippet, state)
assert_expected_lines_present_in_order(expected_outputs, output_lines)
except AssertionError as ex:
new_msg = ex.args[0] + '\n\nIn snippet{}:\n{}'.format(
"" if line_number is None else " (line {})".format(line_number), _indent([snippet])
)
ex.args = (new_msg,) + tuple(ex.args[1:])
raise
def assert_code_snippet_fails(snippet: str, state: Dict, expected_failure_type: str):
try:
exec(snippet, state)
except Exception as ex:
actual_failure_types = [e.__name__ for e in inspect.getmro(type(ex))]
if expected_failure_type not in actual_failure_types:
raise AssertionError(
'Expected snippet to raise a {}, but it raised a {}.'.format(
expected_failure_type, ' -> '.join(actual_failure_types)
)
)
return
raise AssertionError('Expected snippet to fail, but it ran to completion.')
def assert_expected_lines_present_in_order(expected_lines: List[str], actual_lines: List[str]):
"""Checks that all expected lines are present.
It is permitted for there to be extra actual lines between expected lines.
"""
expected_lines = [canonicalize_printed_line(e) for e in expected_lines]
actual_lines = [canonicalize_printed_line(e) for e in actual_lines]
i = 0
for expected in expected_lines:
while i < len(actual_lines) and actual_lines[i] != expected:
i += 1
assert i < len(actual_lines), (
'Missing expected line: {!r}\n'
'\n'
'Actual lines:\n'
'{}\n'
'\n'
'Expected lines:\n'
'{}\n'
'\n'
'Highlighted Differences:\n'
'{}\n'
''.format(
expected,
_indent(actual_lines),
_indent(expected_lines),
_indent(
[
cirq.testing.highlight_text_differences(
'\n'.join(actual_lines), '\n'.join(expected_lines)
)
]
),
)
)
i += 1
def find_expected_outputs(snippet: str) -> List[str]:
"""Finds expected output lines within a snippet.
Expected output must be annotated with a leading '# prints'.
Lines below '# prints' must start with '# ' or be just '#' and not indent
any more than that in order to add an expected line. As soon as a line
breaks this pattern, expected output recording cuts off.
Adding words after '# prints' causes the expected output lines to be
skipped instead of included. For example, for random output say
'# prints something like' to avoid checking the following lines.
"""
continue_key = '# '
expected = []
printing = False
for line in snippet.split('\n'):
if printing:
if line.startswith(continue_key) or line == continue_key.strip():
rest = line[len(continue_key) :]
expected.append(rest)
else:
printing = False
# Matches '# print', '# prints', '# print:', and '# prints:'
elif re.match(r'^#\s*prints?:?\s*$', line):
printing = True
return expected
def _indent(lines: List[str]) -> str:
return '\t' + '\n'.join(lines).replace('\n', '\n\t')
def test_find_expected_outputs():
assert (
find_expected_outputs(
"""
# print
# abc
# def
"""
)
== ['abc']
)
assert (
find_expected_outputs(
"""
# prints
# abc
# def
"""
)
== ['abc']
)
assert (
find_expected_outputs(
"""
# print:
# abc
# def
"""
)
== ['abc']
)
assert (
find_expected_outputs(
"""
#print:
# abc
# def
"""
)
== ['abc']
)
assert (
find_expected_outputs(
"""
# prints:
# abc
# def
"""
)
== ['abc']
)
assert (
find_expected_outputs(
"""
# prints:
# abc
# def
"""
)
== ['abc']
)
assert (
find_expected_outputs(
"""
lorem ipsum
# prints
# abc
a wondrous collection
# prints
# def
# ghi
"""
)
== [' abc', 'def', 'ghi']
)
assert (
find_expected_outputs(
"""
a wandering adventurer
# prints something like
# prints
#prints
# pants
# trance
"""
)
== []
)
def test_assert_expected_lines_present_in_order():
assert_expected_lines_present_in_order(expected_lines=[], actual_lines=[])
assert_expected_lines_present_in_order(expected_lines=[], actual_lines=['abc'])
assert_expected_lines_present_in_order(expected_lines=['abc'], actual_lines=['abc'])
with pytest.raises(AssertionError):
assert_expected_lines_present_in_order(expected_lines=['abc'], actual_lines=[])
assert_expected_lines_present_in_order(
expected_lines=['abc', 'def'], actual_lines=['abc', 'def']
)
assert_expected_lines_present_in_order(
expected_lines=['abc', 'def'], actual_lines=['abc', 'interruption', 'def']
)
with pytest.raises(AssertionError):
assert_expected_lines_present_in_order(
expected_lines=['abc', 'def'], actual_lines=['def', 'abc']
)
assert_expected_lines_present_in_order(expected_lines=['abc '], actual_lines=['abc'])
assert_expected_lines_present_in_order(expected_lines=['abc'], actual_lines=['abc '])
def test_assert_code_snippet_executes_correctly():
assert_code_snippet_executes_correctly("a = 1", {})
assert_code_snippet_executes_correctly("a = b", {'b': 1})
s = {}
assert_code_snippet_executes_correctly("a = 1", s)
assert s['a'] == 1
with pytest.raises(NameError):
assert_code_snippet_executes_correctly("a = b", {})
with pytest.raises(SyntaxError):
assert_code_snippet_executes_correctly("a = ;", {})
assert_code_snippet_executes_correctly(
"""
print("abc")
# prints
# abc
""",
{},
)
if sys.version_info[0] >= 3: # Our print capture only works in python 3.
with pytest.raises(AssertionError):
assert_code_snippet_executes_correctly(
"""
print("abc")
# prints
# def
""",
{},
)
assert_code_snippet_executes_correctly(
"""
# raises ZeroDivisionError
a = 1 / 0
""",
{},
)
assert_code_snippet_executes_correctly(
"""
# raises ArithmeticError
a = 1 / 0
""",
{},
)
assert_code_snippet_executes_correctly(
"""
# prints 123
print("123")
# raises SyntaxError
print "abc")
""",
{},
)
with pytest.raises(AssertionError):
assert_code_snippet_executes_correctly(
"""
# raises ValueError
a = 1 / 0
""",
{},
)
with pytest.raises(AssertionError):
assert_code_snippet_executes_correctly(
"""
# raises
a = 1
""",
{},
)