-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathtest_typeerror-274.py
More file actions
95 lines (78 loc) · 2.8 KB
/
test_typeerror-274.py
File metadata and controls
95 lines (78 loc) · 2.8 KB
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
import sys
import pytest
import semver
import semver.version
PY2 = sys.version_info[0] == 2
PY3 = sys.version_info[0] == 3
def ensure_binary(s, encoding="utf-8", errors="strict"):
"""
Coerce ``s`` to bytes.
* `str` -> encoded to `bytes`
* `bytes` -> `bytes`
:param s: the string to convert
:type s: str | bytes
:param encoding: the encoding to apply, defaults to "utf-8"
:type encoding: str
:param errors: set a different error handling scheme;
other possible values are `ignore`, `replace`, and
`xmlcharrefreplace` as well as any other name
registered with :func:`codecs.register_error`.
Defaults to "strict".
:type errors: str
:raises TypeError: if ``s`` is not str or bytes type
:return: the converted string
:rtype: str
"""
if isinstance(s, str):
return s.encode(encoding, errors)
elif isinstance(s, bytes):
return s
else:
raise TypeError("not expecting type '%s'" % type(s))
def test_should_work_with_string_and_unicode():
result = semver.compare("1.1.0", b"1.2.2")
assert result == -1
result = semver.compare(b"1.1.0", "1.2.2")
assert result == -1
class TestEnsure:
# From six project
# grinning face emoji
UNICODE_EMOJI = "\U0001F600"
BINARY_EMOJI = b"\xf0\x9f\x98\x80"
def test_ensure_binary_raise_type_error(self):
with pytest.raises(TypeError):
semver.version.ensure_str(8)
def test_errors_and_encoding(self):
ensure_binary(self.UNICODE_EMOJI, encoding="latin-1", errors="ignore")
with pytest.raises(UnicodeEncodeError):
ensure_binary(self.UNICODE_EMOJI, encoding="latin-1", errors="strict")
def test_ensure_binary_raise(self):
converted_unicode = ensure_binary(
self.UNICODE_EMOJI, encoding="utf-8", errors="strict"
)
converted_binary = ensure_binary(
self.BINARY_EMOJI, encoding="utf-8", errors="strict"
)
# PY3: str -> bytes
assert converted_unicode == self.BINARY_EMOJI and isinstance(
converted_unicode, bytes
)
# PY3: bytes -> bytes
assert converted_binary == self.BINARY_EMOJI and isinstance(
converted_binary, bytes
)
def test_ensure_str(self):
converted_unicode = semver.version.ensure_str(
self.UNICODE_EMOJI, encoding="utf-8", errors="strict"
)
converted_binary = semver.version.ensure_str(
self.BINARY_EMOJI, encoding="utf-8", errors="strict"
)
# PY3: str -> str
assert converted_unicode == self.UNICODE_EMOJI and isinstance(
converted_unicode, str
)
# PY3: bytes -> str
assert converted_binary == self.UNICODE_EMOJI and isinstance(
converted_unicode, str
)