| 1 |
|
| 2 |
import pytest |
| 3 |
import sys |
| 4 |
from hypothesis import given, strategies, settings, example |
| 5 |
|
| 6 |
from rpython.rlib import rutf8, runicode |
| 7 |
from rpython.rlib.unicodedata import unicodedb_12_1_0 |
| 8 |
|
| 9 |
|
| 10 |
@given(strategies.characters(), strategies.booleans()) |
| 11 |
def test_unichr_as_utf8(c, allow_surrogates): |
| 12 |
i = ord(c) |
| 13 |
if not allow_surrogates and 0xD800 <= i <= 0xDFFF: |
| 14 |
with pytest.raises(rutf8.OutOfRange): |
| 15 |
rutf8.unichr_as_utf8(i, allow_surrogates) |
| 16 |
else: |
| 17 |
u = rutf8.unichr_as_utf8(i, allow_surrogates) |
| 18 |
assert u == c.encode('utf8') |
| 19 |
|
| 20 |
@given(strategies.binary()) |
| 21 |
def test_check_ascii(s): |
| 22 |
raised = False |
| 23 |
try: |
| 24 |
s.decode('ascii') |
| 25 |
except UnicodeDecodeError as e: |
| 26 |
raised = True |
| 27 |
try: |
| 28 |
rutf8.check_ascii(s) |
| 29 |
except rutf8.CheckError: |
| 30 |
assert raised |
| 31 |
else: |
| 32 |
assert not raised |
| 33 |
|
| 34 |
@given(strategies.binary(), strategies.booleans()) |
| 35 |
@example('\xf1\x80\x80\x80', False) |
| 36 |
def test_check_utf8(s, allow_surrogates): |
| 37 |
_test_check_utf8(s, allow_surrogates) |
| 38 |
|
| 39 |
@given(strategies.text(), strategies.booleans()) |
| 40 |
def test_check_utf8_valid(u, allow_surrogates): |
| 41 |
_test_check_utf8(u.encode('utf-8'), allow_surrogates) |
| 42 |
|
| 43 |
@given(strategies.binary(), strategies.text(), strategies.binary()) |
| 44 |
def test_check_utf8_slice(a, b, c): |
| 45 |
start = len(a) |
| 46 |
b_utf8 = b.encode('utf-8') |
| 47 |
end = start + len(b_utf8) |
| 48 |
assert rutf8.check_utf8(a + b_utf8 + c, False, start, end) == len(b) |
| 49 |
|
| 50 |
def _has_surrogates(s): |
| 51 |
for u in s.decode('utf8'): |
| 52 |
if 0xD800 <= ord(u) <= 0xDFFF: |
| 53 |
return True |
| 54 |
return False |
| 55 |
|
| 56 |
def _test_check_utf8(s, allow_surrogates): |
| 57 |
try: |
| 58 |
u, _ = runicode.str_decode_utf_8(s, len(s), None, final=True, |
| 59 |
allow_surrogates=allow_surrogates) |
| 60 |
valid = True |
| 61 |
except UnicodeDecodeError as e: |
| 62 |
valid = False |
| 63 |
length = rutf8._check_utf8(s, allow_surrogates, 0, len(s)) |
| 64 |
if length < 0: |
| 65 |
assert not valid |
| 66 |
assert ~(length) == e.start |
| 67 |
else: |
| 68 |
assert valid |
| 69 |
if sys.maxunicode == 0x10FFFF or not _has_surrogates(s): |
| 70 |
assert length == len(u) |
| 71 |
|
| 72 |
@given(strategies.characters()) |
| 73 |
def test_next_pos(uni): |
| 74 |
skips = [] |
| 75 |
for elem in uni: |
| 76 |
skips.append(len(elem.encode('utf8'))) |
| 77 |
pos = 0 |
| 78 |
i = 0 |
| 79 |
utf8 = uni.encode('utf8') |
| 80 |
while pos < len(utf8): |
| 81 |
new_pos = rutf8.next_codepoint_pos(utf8, pos) |
| 82 |
assert new_pos - pos == skips[i] |
| 83 |
i += 1 |
| 84 |
pos = new_pos |
| 85 |
|
| 86 |
def test_check_newline_utf8(): |
| 87 |
for i in xrange(sys.maxunicode): |
| 88 |
if runicode.unicodedb.islinebreak(i): |
| 89 |
assert rutf8.islinebreak(unichr(i).encode('utf8'), 0) |
| 90 |
else: |
| 91 |
assert not rutf8.islinebreak(unichr(i).encode('utf8'), 0) |
| 92 |
|
| 93 |
def test_isspace_utf8(): |
| 94 |
for i in xrange(sys.maxunicode): |
| 95 |
if runicode.unicodedb.isspace(i): |
| 96 |
assert rutf8.isspace(unichr(i).encode('utf8'), 0) |
| 97 |
else: |
| 98 |
assert not rutf8.isspace(unichr(i).encode('utf8'), 0) |
| 99 |
|
| 100 |
@given(strategies.characters(), strategies.text()) |
| 101 |
def test_utf8_in_chars(ch, txt): |
| 102 |
response = rutf8.utf8_in_chars(ch.encode('utf8'), 0, txt.encode('utf8')) |
| 103 |
r = (ch in txt) |
| 104 |
assert r == response |
| 105 |
|
| 106 |
@given(strategies.text(), strategies.integers(min_value=0), |
| 107 |
strategies.integers(min_value=0)) |
| 108 |
def test_codepoints_in_utf8(u, start, len1): |
| 109 |
end = start + len1 |
| 110 |
if end > len(u): |
| 111 |
extra = end - len(u) |
| 112 |
else: |
| 113 |
extra = 0 |
| 114 |
count = rutf8.codepoints_in_utf8(u.encode('utf8'), |
| 115 |
len(u[:start].encode('utf8')), |
| 116 |
len(u[:end].encode('utf8')) + extra) |
| 117 |
assert count == len(u[start:end]) |
| 118 |
|
| 119 |
@given(strategies.text()) |
| 120 |
def test_utf8_index_storage(u): |
| 121 |
index = rutf8.create_utf8_index_storage(u.encode('utf8'), len(u)) |
| 122 |
for i, item in enumerate(u): |
| 123 |
assert (rutf8.codepoint_at_index(u.encode('utf8'), index, i) == |
| 124 |
ord(item)) |
| 125 |
|
| 126 |
@given(strategies.text(min_size=63)) |
| 127 |
@example(u'x' * 64 * 5) |
| 128 |
@example(u'x' * (64 * 5 - 1)) |
| 129 |
def test_codepoint_position_at_index(u): |
| 130 |
index = rutf8.create_utf8_index_storage(u.encode('utf8'), len(u)) |
| 131 |
for i in range(len(u) + 1): |
| 132 |
assert (rutf8.codepoint_position_at_index(u.encode('utf8'), index, i) == |
| 133 |
len(u[:i].encode('utf8'))) |
| 134 |
|
| 135 |
@given(strategies.text(min_size=63)) |
| 136 |
@example(u'x' * 64 * 5) |
| 137 |
@example(u'x' * (64 * 5 - 1)) |
| 138 |
@example(u'ä' + u'x«' * 1000 + u'–' + u'y' * 100) |
| 139 |
def test_codepoint_index_at_byte_position(u): |
| 140 |
b = u.encode('utf8') |
| 141 |
storage = rutf8.create_utf8_index_storage(b, len(u)) |
| 142 |
for i in range(len(u) + 1): |
| 143 |
bytepos = len(u[:i].encode('utf8')) |
| 144 |
assert rutf8.codepoint_index_at_byte_position( |
| 145 |
b, storage, bytepos, len(u)) == i |
| 146 |
|
| 147 |
@given(strategies.text(min_size=63)) |
| 148 |
def test_codepoint_position_at_index_inverse(u): |
| 149 |
b = u.encode('utf8') |
| 150 |
storage = rutf8.create_utf8_index_storage(b, len(u)) |
| 151 |
for i in range(len(u) + 1): |
| 152 |
bytepos = rutf8.codepoint_position_at_index(b, storage, i) |
| 153 |
assert rutf8.codepoint_index_at_byte_position( |
| 154 |
b, storage, bytepos, len(u)) == i |
| 155 |
|
| 156 |
|
| 157 |
repr_func = rutf8.make_utf8_escape_function(prefix='u', pass_printable=False, |
| 158 |
quotes=True) |
| 159 |
|
| 160 |
@given(strategies.text()) |
| 161 |
def test_repr(u): |
| 162 |
assert repr(u) == repr_func(u.encode('utf8')) |
| 163 |
|
| 164 |
@given(strategies.lists(strategies.characters())) |
| 165 |
@example([u'\ud800', u'\udc00']) |
| 166 |
def test_surrogate_in_utf8(unichars): |
| 167 |
uni = ''.join([u.encode('utf8') for u in unichars]) |
| 168 |
result = rutf8.surrogate_in_utf8(uni) >= 0 |
| 169 |
expected = any(uch for uch in unichars if u'\ud800' <= uch <= u'\udfff') |
| 170 |
assert result == expected |
| 171 |
|
| 172 |
|
| 173 |
def test_utf8_string_builder(): |
| 174 |
s = rutf8.Utf8StringBuilder() |
| 175 |
s.append("foo") |
| 176 |
s.append_char("x") |
| 177 |
assert s.getlength() == 4 |
| 178 |
assert s.build() == "foox" |
| 179 |
s.append(u"\u1234".encode("utf8")) |
| 180 |
assert s.getlength() == 5 |
| 181 |
assert s.build().decode("utf8") == u"foox\u1234" |
| 182 |
s.append("foo") |
| 183 |
s.append_char("x") |
| 184 |
assert s.getlength() == 9 |
| 185 |
assert s.build().decode("utf8") == u"foox\u1234foox" |
| 186 |
|
| 187 |
s = rutf8.Utf8StringBuilder() |
| 188 |
s.append_code(0x1234) |
| 189 |
assert s.build().decode("utf8") == u"\u1234" |
| 190 |
assert s.getlength() == 1 |
| 191 |
s.append_code(0xD800) |
| 192 |
assert s.getlength() == 2 |
| 193 |
|
| 194 |
s = rutf8.Utf8StringBuilder() |
| 195 |
s.append_utf8("abc", 3) |
| 196 |
assert s.getlength() == 3 |
| 197 |
assert s.build().decode("utf8") == u"abc" |
| 198 |
|
| 199 |
s.append_utf8(u"\u1234".encode("utf8"), 1) |
| 200 |
assert s.build().decode("utf8") == u"abc\u1234" |
| 201 |
assert s.getlength() == 4 |
| 202 |
|
| 203 |
s.append_code(0xD800) |
| 204 |
assert s.getlength() == 5 |
| 205 |
|
| 206 |
s.append_utf8_slice(u"äöüß".encode("utf-8"), 2, 6, 2) |
| 207 |
assert s.getlength() == 7 |
| 208 |
assert s.build().decode("utf-8") == u"abc\u1234\ud800öü" |
| 209 |
|
| 210 |
def test_utf8_string_builder_bad_code(): |
| 211 |
s = rutf8.Utf8StringBuilder() |
| 212 |
with pytest.raises(rutf8.OutOfRange): |
| 213 |
s.append_code(0x110000) |
| 214 |
assert s.build() == '' |
| 215 |
assert s.getlength() == 0 |
| 216 |
|
| 217 |
@given(strategies.text()) |
| 218 |
def test_utf8_iterator(arg): |
| 219 |
u = rutf8.Utf8StringIterator(arg.encode('utf8')) |
| 220 |
l = [] |
| 221 |
for c in u: |
| 222 |
l.append(unichr(c)) |
| 223 |
assert list(arg) == l |
| 224 |
|
| 225 |
@given(strategies.text()) |
| 226 |
def test_utf8_iterator_pos(arg): |
| 227 |
utf8s = arg.encode('utf8') |
| 228 |
u = rutf8.Utf8StringPosIterator(utf8s) |
| 229 |
l = [] |
| 230 |
i = 0 |
| 231 |
for c, pos in u: |
| 232 |
l.append(unichr(c)) |
| 233 |
assert c == rutf8.codepoint_at_pos(utf8s, pos) |
| 234 |
assert pos == i |
| 235 |
i = rutf8.next_codepoint_pos(utf8s, i) |
| 236 |
assert list(arg) == l |
| 237 |
|
| 238 |
|
| 239 |
@given(strategies.text(), strategies.integers(0xd800, 0xdfff)) |
| 240 |
def test_has_surrogates(arg, surrogate): |
| 241 |
b = (arg + unichr(surrogate) + arg).encode("utf-8") |
| 242 |
assert not rutf8.has_surrogates(arg.encode("utf-8")) |
| 243 |
assert rutf8.has_surrogates(unichr(surrogate).encode("utf-8")) |
| 244 |
assert rutf8.has_surrogates(b) |
| 245 |
|
| 246 |
def test_has_surrogate_xed_no_surrogate(): |
| 247 |
u = unichr(55217) + unichr(54990) |
| 248 |
b = u.encode("utf-8") |
| 249 |
assert b.startswith(b"\xed") |
| 250 |
assert not rutf8.has_surrogates(b) |
| 251 |
|
| 252 |
printable_repr_func = rutf8.make_utf8_escape_function(pass_printable=True, |
| 253 |
quotes=True, |
| 254 |
unicodedb=unicodedb_12_1_0) |
| 255 |
|
| 256 |
def test_printable_repr_func(): |
| 257 |
s = u'\U0001f42a'.encode("utf-8") |
| 258 |
assert printable_repr_func(s) == "'" + s + "'" |
| 259 |
|