-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
157 lines (127 loc) · 3.73 KB
/
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
import io
import os
import re
import hypothesis.strategies as st
import pytest
from hypothesis import (
given,
assume
)
from tsp import (
re_format,
file_or_open,
absent_if_none,
pack_args,
call_packed
)
def st_any():
x = st.deferred(
lambda: st_hashable()
| st.lists(x)
| st.dictionaries(st_hashable(), x)
| st.sets(st_hashable())
)
return x
def st_hashable():
x = st.deferred(
lambda: st.text()
| st.binary()
| st.integers()
| st.floats(allow_nan=False)
| st.decimals(allow_nan=False)
| st.fractions()
| st.datetimes()
| st.tuples(x)
| st.frozensets(x)
)
return x
@given(st_any() | st.just(r'\s$^.*'))
def test_re_format(obj):
assert re.match(rf'^{re_format(obj)}$', f'{obj}')
@given(s=st.just('x'),
text=st.text()
| st.binary(),
mode=st.just(None)
| st.just('w')
| st.just('r'))
def test_file_or_open_file_exists(s, text, mode, tmpdir):
binary = isinstance(text, bytes)
if not binary:
assert isinstance(text, str)
assume('\r' not in text)
path = str(tmpdir / s)
try:
os.remove(path)
except FileNotFoundError:
pass
if mode != 'w':
with open(path, 'w' + ('b' if binary else '')) as f:
f.write(text)
if binary:
assume(mode)
mode += 'b'
with file_or_open(path, **absent_if_none(mode=mode)) as f:
if mode and 'b' in mode:
assert isinstance(f, io.BufferedIOBase)
else:
assert isinstance(f, io.TextIOBase)
if mode and 'w' in mode:
f.write(text)
else:
assert mode is None or 'r' in mode
assert text == f.read()
os.path.exists(path)
with open(path, 'r' + ('b' if binary else '')) as f:
assert text == f.read()
@given(s=st.just('x'),
text=st.text()
| st.binary(),
mode=st.just(None)
| st.just('w')
| st.just('r'))
def test_file_or_open_file_absent(s, text, mode, tmpdir):
binary = isinstance(text, bytes)
if not binary:
assert isinstance(text, str)
assume('\r' not in text)
path = str(tmpdir / s)
try:
os.remove(path)
except FileNotFoundError:
pass
if binary:
assume(mode)
mode += 'b'
try:
with file_or_open(path, **absent_if_none(mode=mode)) as f:
if mode and 'b' in mode:
assert isinstance(f, io.BufferedIOBase)
else:
assert isinstance(f, io.TextIOBase)
if mode and 'w' in mode:
f.write(text)
else:
pytest.fail('Should have raised a FileNotFoundError')
except Exception as e:
if mode is None or 'r' in mode:
assert isinstance(e, FileNotFoundError)
else:
pytest.fail('unexpected exception')
if mode and 'w' in mode:
with open(path, 'r' + ('b' if binary else '')) as f:
assert text == f.read()
@given(args=st.tuples(st_any()), kwargs=st.dictionaries(st.text(), st_any()))
def test_pack_args(args, kwargs):
packed = pack_args(*args, **kwargs)
assert packed['args'] == args
assert packed['kwargs'] == kwargs
@given(args=st.tuples(st_any()), kwargs=st.dictionaries(st.text(), st_any()))
def test_call_packed(args, kwargs):
call_count = 0
def f(*actual_args, **actual_kwargs):
nonlocal call_count
call_count += 1
assert actual_args == args
assert actual_kwargs == kwargs
call_packed(f, args, kwargs)
assert call_count == 1