Debian Codegraph @

tests/test_text.py

raw
1 # -*- coding: utf-8 -*-
2 # Copyright: (c) 2020, Jordan Borean (@jborean93) <[email protected]>
3 # MIT License (see LICENSE or https://opensource.org/licenses/MIT)
4 import typing
5
6 import pytest
7
8 import spnego._text as text
9
10
11 class Obj:
12 def __init__(self, str: typing.Optional[str] = None, repr: typing.Optional[str] = None) -> None:
13 self.str = str
14 self.repr = repr
15
16 def __eq__(self, other: typing.Any) -> bool:
17 return isinstance(other, Obj) and self.str == other.str and self.repr == other.repr
18
19 def __str__(self) -> str:
20 return self.str # type: ignore[return-value] # Testing this scenario
21
22 def __repr__(self) -> str:
23 return self.repr # type: ignore[return-value] # Testing this scenario
24
25
26 class ObjRepr:
27 def __init__(self, repr: typing.Optional[str] = None) -> None:
28 self.repr = repr
29
30 def __repr__(self) -> str:
31 return self.repr # type: ignore[return-value] # Testing this scenario
32
33
34 class ObjUnicodeError:
35 def __str__(self) -> str:
36 return "café".encode("ascii") # type: ignore[return-value] # Testing this scenario
37
38 def __repr__(self) -> str:
39 return "café".encode("ascii") # type: ignore[return-value] # Testing this scenario
40
41
42 def test_to_bytes_from_bytes():
43 actual = text.to_bytes(b"\xFF\x00\x7F\x80")
44
45 assert actual == b"\xFF\x00\x7F\x80"
46
47
48 @pytest.mark.parametrize(
49 "value, expected",
50 [
51 ("cafe", b"cafe"),
52 ("café", b"caf\xc3\xa9"),
53 ("ÜseӜ", b"\x55\xCC\x88\x73\x65\xD3\x9C"),
54 ],
55 )
56 def test_to_bytes_from_text(value, expected):
57 actual = text.to_bytes(value)
58
59 assert actual == expected
60
61
62 def test_to_bytes_encoding():
63 actual = text.to_bytes("café", encoding="windows-1252")
64
65 assert actual == b"caf\xe9"
66
67
68 def test_to_bytes_errors():
69 with pytest.raises(UnicodeEncodeError, match="codec can't encode character"):
70 text.to_bytes("café", encoding="ascii")
71
72 actual = text.to_bytes("café", encoding="ascii", errors="replace")
73
74 assert actual == b"caf?"
75
76
77 def test_to_bytes_nonstr():
78 actual = text.to_bytes(Obj(str="café"))
79
80 assert actual == b"caf\xc3\xa9"
81
82
83 def test_to_bytes_nonstr_default():
84 actual = text.to_bytes(Obj())
85
86 assert actual == b""
87
88
89 def test_to_bytes_nonstr_repr():
90 actual = text.to_bytes(ObjRepr(repr="café"))
91
92 assert actual == b"caf\xc3\xa9"
93
94
95 def test_to_bytes_nonstr_unicode_error():
96 actual = text.to_bytes(ObjUnicodeError())
97
98 assert actual == b""
99
100
101 def test_to_bytes_nonstr_passthru():
102 actual = text.to_bytes(Obj(), nonstring="passthru")
103
104 assert actual == Obj()
105
106
107 def test_to_bytes_nonstr_empty():
108 actual = text.to_bytes(Obj(), nonstring="empty")
109
110 assert actual == b""
111
112
113 def test_to_bytes_nonstr_invalid():
114 with pytest.raises(ValueError, match="Invalid nonstring value"):
115 text.to_bytes(Obj(), nonstring="invalid")
116
117
118 def test_to_text_from_text():
119 actual = text.to_text("café")
120
121 assert actual == "café"
122
123
124 @pytest.mark.parametrize(
125 "value, expected",
126 [
127 (b"cafe", "cafe"),
128 (b"caf\xc3\xa9", "café"),
129 (b"\x55\xCC\x88\x73\x65\xD3\x9C", "ÜseӜ"),
130 ],
131 )
132 def test_to_text_from_bytes(value, expected):
133 actual = text.to_text(value)
134
135 assert actual == expected
136
137
138 def test_to_text_encoding():
139 actual = text.to_text(b"caf\xe9", encoding="windows-1252")
140
141 assert actual == "café"
142
143
144 def test_to_text_errors():
145 with pytest.raises(UnicodeError, match="codec can't decode byte 0xff"):
146 text.to_text(b"caf\xFF")
147
148 actual = text.to_text(b"caf\xFF", errors="replace")
149
150 assert actual == "caf�"
151
152
153 def test_to_text_nonstr():
154 actual = text.to_text(Obj(str="café"))
155
156 assert actual == "café"
157
158
159 def test_to_text_nonstr_unicode():
160 class ObjUnicode:
161 def __unicode__(self):
162 return "café"
163
164 actual = text.to_text(ObjUnicode())
165
166 assert actual == "café"
167
168
169 def test_to_text_nonstr_default():
170 actual = text.to_text(Obj())
171
172 assert actual == ""
173
174
175 def test_to_text_nonstr_repr():
176 actual = text.to_text(ObjRepr(repr="café"))
177
178 assert actual == "café"
179
180
181 def test_to_text_nonstr_unicode_error():
182 actual = text.to_text(ObjUnicodeError())
183
184 assert actual == ""
185
186
187 def test_to_text_nonstr_passthru():
188 actual = text.to_text(Obj(), nonstring="passthru")
189
190 assert actual == Obj()
191
192
193 def test_to_text_nonstr_empty():
194 actual = text.to_text(Obj(), nonstring="empty")
195
196 assert actual == ""
197
198
199 def test_to_text_nonstr_invalid():
200 with pytest.raises(ValueError, match="Invalid nonstring value"):
201 text.to_text(Obj(), nonstring="invalid")