-
Notifications
You must be signed in to change notification settings - Fork 7
/
tests.py
293 lines (244 loc) · 7.96 KB
/
tests.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
import pasteboard
import pytest
import mypy.api
from hypothesis import given, strategies as st
from pasteboard import Pasteboard, PasteboardType
from pathlib import Path
from typing import Tuple
STRING_TYPES = (
pasteboard.String,
pasteboard.RTF,
pasteboard.HTML,
pasteboard.TabularText,
)
BINARY_TYPES = (pasteboard.PDF, pasteboard.PNG, pasteboard.TIFF)
# no null characters, no control characters, no surrogate characters
TEXT = st.characters(min_codepoint=1, blacklist_categories=("Cc", "Cs"))
@given(TEXT)
def test_get_set_contents_default(s: str) -> None:
pb = Pasteboard()
assert pb.set_contents(s)
assert pb.get_contents() == s
@given(TEXT)
def test_get_contents_diff_not_none_after_set(s: str) -> None:
pb = Pasteboard()
assert pb.set_contents(s)
assert pb.get_contents(diff=True) == s
assert pb.get_contents(diff=True) is None
@pytest.mark.parametrize("type", STRING_TYPES)
@given(TEXT)
def test_get_set_contents_string(type: PasteboardType, s: str) -> None:
pb = Pasteboard()
assert pb.set_contents(s, type=type)
assert pb.get_contents(type=type) == s
assert pb.get_contents(type=type, diff=True) is None
@pytest.mark.parametrize("type", BINARY_TYPES)
@given(st.binary())
def test_get_set_contents_data(type: PasteboardType, b: bytes) -> None:
pb = Pasteboard()
assert pb.set_contents(b, type=type)
assert pb.get_contents(type=type) == b
assert pb.get_contents(type=type, diff=True) is None
def test_get_set_contents_with_null_char() -> None:
pb = Pasteboard()
assert pb.set_contents("abc\x00def")
assert pb.get_contents() == "abc"
def test_get_set_contents_with_emoji_santa() -> None:
s = "\x1f385"
pb = Pasteboard()
assert pb.set_contents(s)
assert pb.get_contents() == s
@pytest.mark.parametrize(
"type,name",
[
(pasteboard.String, "public.utf8-plain-text"),
(pasteboard.RTF, "public.rtf"),
(pasteboard.HTML, "public.html"),
(pasteboard.TabularText, "public.utf8-tab-separated-values-text"),
(pasteboard.PDF, "com.adobe.pdf"),
(pasteboard.PNG, "public.png"),
(pasteboard.TIFF, "public.tiff"),
],
)
def test_types_repr(type: PasteboardType, name: str) -> None:
assert repr(type) == f"<PasteboardType {name}>"
def test_file_urls() -> None:
pb = Pasteboard()
assert pb.set_contents("abc\x00def")
assert pb.get_file_urls() is None
assert pb.get_file_urls(diff=True) is None
def mypy_run(tmp_path: Path, content: str) -> Tuple[str, str, int]:
py = tmp_path / "test.py"
py.write_text(content)
filename = str(py)
normal_report, error_report, exit_status = mypy.api.run([filename, "--strict"])
return normal_report.replace(filename, "test.py"), error_report, exit_status
def test_type_hints_pasteboard_valid(tmp_path: Path) -> None:
normal_report, error_report, exit_status = mypy_run(
tmp_path,
"""
from pasteboard import Pasteboard
pb = Pasteboard()
""",
)
assert exit_status == 0, normal_report
def test_type_hints_pasteboard_invalid_args(tmp_path: Path) -> None:
normal_report, error_report, exit_status = mypy_run(
tmp_path,
"""
from pasteboard import Pasteboard
pb = Pasteboard("bar")
""",
)
assert exit_status == 1, normal_report
assert 'Too many arguments for "Pasteboard"' in normal_report
def test_type_hints_pasteboard_invalid_kwargs(tmp_path: Path) -> None:
normal_report, error_report, exit_status = mypy_run(
tmp_path,
"""
from pasteboard import Pasteboard
pb = Pasteboard(foo="bar")
""",
)
assert exit_status == 1, normal_report
assert 'Unexpected keyword argument "foo" for "Pasteboard"' in normal_report
def test_type_hints_get_contents_valid_no_args(tmp_path: Path) -> None:
normal_report, error_report, exit_status = mypy_run(
tmp_path,
"""
from pasteboard import Pasteboard
from typing import Optional
pb = Pasteboard()
s: Optional[str] = pb.get_contents()
""",
)
assert exit_status == 0, normal_report
def test_type_hints_get_contents_valid_diff_arg(tmp_path: Path) -> None:
normal_report, error_report, exit_status = mypy_run(
tmp_path,
"""
from pasteboard import Pasteboard
pb = Pasteboard()
s = pb.get_contents(diff=True)
if s:
s += "foo"
""",
)
assert exit_status == 0, normal_report
def test_type_hints_get_contents_valid_type_args(tmp_path: Path) -> None:
normal_report, error_report, exit_status = mypy_run(
tmp_path,
"""
from pasteboard import Pasteboard, PNG
from typing import Union
pb = Pasteboard()
s = pb.get_contents(type=PNG)
if s:
if isinstance(s, str):
s += "foo"
else:
s += b"foo"
""",
)
assert exit_status == 0, normal_report
def test_type_hints_get_contents_valid_both_args(tmp_path: Path) -> None:
normal_report, error_report, exit_status = mypy_run(
tmp_path,
"""
from pasteboard import Pasteboard, PNG
from typing import Union
pb = Pasteboard()
s = pb.get_contents(type=PNG, diff=True)
if s:
if isinstance(s, str):
s += "foo"
else:
s += b"foo"
""",
)
assert exit_status == 0, normal_report
@pytest.mark.parametrize("arg", ['"bar"', 'foo="bar"', 'type="bar"', 'diff="bar"'])
def test_type_hints_get_contents_invalid_arg(arg: str, tmp_path: Path) -> None:
normal_report, error_report, exit_status = mypy_run(
tmp_path,
f"""
from pasteboard import Pasteboard
pb = Pasteboard()
pb.get_contents({arg})
""",
)
assert exit_status == 1, normal_report
assert "No overload variant" in normal_report
@pytest.mark.parametrize("arg", ['"bar"', 'b"bar"'])
def test_type_hints_set_contents_valid_no_args(arg: str, tmp_path: Path) -> None:
normal_report, error_report, exit_status = mypy_run(
tmp_path,
f"""
from pasteboard import Pasteboard
pb = Pasteboard()
result: bool = pb.set_contents({arg})
""",
)
assert exit_status == 0, normal_report
@pytest.mark.parametrize("arg", ['"bar"', 'b"bar"'])
def test_type_hints_set_contents_valid_type_args(arg: str, tmp_path: Path) -> None:
normal_report, error_report, exit_status = mypy_run(
tmp_path,
f"""
from pasteboard import Pasteboard, PNG
pb = Pasteboard()
result: bool = pb.set_contents({arg}, type=PNG)
""",
)
assert exit_status == 0, normal_report
def test_type_hints_set_contents_invalid_arg(tmp_path: Path) -> None:
normal_report, error_report, exit_status = mypy_run(
tmp_path,
f"""
from pasteboard import Pasteboard
pb = Pasteboard()
result: bool = pb.set_contents(0)
""",
)
assert exit_status == 1, normal_report
assert '"set_contents" of "Pasteboard" cannot be "int"' in normal_report
def test_type_hints_set_contents_invalid_type_arg(tmp_path: Path) -> None:
normal_report, error_report, exit_status = mypy_run(
tmp_path,
f"""
from pasteboard import Pasteboard
pb = Pasteboard()
result: bool = pb.set_contents("", type="bar")
""",
)
assert exit_status == 1, normal_report
msg = 'Argument "type" to "set_contents" of "Pasteboard" has incompatible type "str"; expected "PasteboardType'
assert msg in normal_report
def test_type_hints_set_contents_invalid_kwarg(tmp_path: Path) -> None:
normal_report, error_report, exit_status = mypy_run(
tmp_path,
f"""
from pasteboard import Pasteboard
pb = Pasteboard()
result: bool = pb.set_contents("", foo="bar")
""",
)
assert exit_status == 1, normal_report
assert (
'Unexpected keyword argument "foo" for "set_contents" of "Pasteboard"'
in normal_report
)
def test_type_hints_set_contents_invalid_result(tmp_path: Path) -> None:
normal_report, error_report, exit_status = mypy_run(
tmp_path,
f"""
from pasteboard import Pasteboard
pb = Pasteboard()
result: str = pb.set_contents("")
""",
)
assert exit_status == 1, normal_report
assert (
'Incompatible types in assignment (expression has type "bool", variable has type "str")'
in normal_report
)