/
test_reprexpy.py
126 lines (91 loc) · 3.21 KB
/
test_reprexpy.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
import textwrap
import re
import os
import os.path
import pyperclip
import pytest
from reprexpy.reprex import reprex
skip_on_travis = pytest.mark.skipif(
'TRAVIS' in os.environ and os.environ['TRAVIS'] == 'true',
reason='Skipping this test on Travis CI.'
)
def _read_ex_fi(file):
with open(file) as fi:
lns = fi.read()
return lns.rstrip('\n')
def _read_ex_fi_pair(pref):
x = os.path.join('tests', 'reprexes', pref)
return [_read_ex_fi(x + i) for i in ['.py', '.md']]
def _ptxt(txt):
dind = textwrap.dedent(txt)
return dind.strip('\n')
def _all_match(out, regex_lst):
lns = out.splitlines()
return [any([re.search(rgx, line) for line in lns]) for rgx in regex_lst]
def _reprex_basic(*args, **kargs):
return reprex(si=False, advertise=False, *args, **kargs)
def test_spliting_txt_output():
ex = _read_ex_fi_pair('txt-outputs')
out = _reprex_basic(ex[0])
assert out == ex[1]
def test_two_statements_per_line():
ex = _read_ex_fi_pair('two-statements-per-line')
out = _reprex_basic(ex[0])
assert out == ex[1]
def test_plot_outputs():
ex = _read_ex_fi('tests/reprexes/plot-output.py')
out = _reprex_basic(ex)
assert len(re.findall('https://i\.imgur\.com', out)) == 3
def test_exception_handling():
out = _reprex_basic('10 / 0')
one_t = _all_match(out, ['ZeroDivisionError'])
assert one_t[0], 'ZeroDivisionError not found in output'
@skip_on_travis
def test_input_types():
ex = _read_ex_fi('tests/reprexes/txt-outputs.py')
out_x = _reprex_basic(ex)
out_infile = _reprex_basic(code_file='tests/reprexes/txt-outputs.py')
pyperclip.copy(ex)
out_clip = _reprex_basic()
assert len(set([out_x, out_infile, out_clip])) == 1
@skip_on_travis
def test_output_to_clipboard():
_reprex_basic('x = "hi there"; print(x)')
assert pyperclip.paste() == '```python\nx = "hi there"; ' \
'print(x)\n#> hi there\n```'
def test_misc_params():
code = """
var = 'some var'
var
"""
out = reprex(_ptxt(code), venue='so', comment='#<>', advertise=True)
mlst = [
' var = \'some var\'', '#<>',
'Created on.*by the \[reprexpy package\]'
]
mout = _all_match(out, mlst)
for i, j in zip(mout, mlst):
assert i, '%r not found in output' % j
def test_si_imports():
x = _read_ex_fi('tests/reprexes/imports.py')
out = reprex(code=x, si=True)
x_in = [
'nbconvert', 'asttokens', 'pyimgur', 'stdlib-list', 'ipython', 'pyzmq'
]
x_in = [i + '==' for i in x_in]
mout = _all_match(out, x_in)
for i, j in zip(mout, x_in):
assert i, '%r not found in output' % j
def test_si_non_imports():
x = _read_ex_fi('tests/reprexes/non-imports.py')
out = reprex(code=x, si=True)
not_in_x = ['pickledb', 'matplotlib', 'ipython']
not_in_x = [i + '==' for i in not_in_x]
mout = _all_match(out, not_in_x)
for i, j in zip(mout, not_in_x):
assert not i, '%r found in session info' % j
def test_sphinx_venue():
x = _read_ex_fi_pair('sphinx-venue')
out = reprex(code=x[0], venue='sx')
assert out.splitlines()[0:12] == x[1].splitlines()[0:12]
assert re.search(' \.\. image:: https://i\.imgur\.com', out)