| 1 |
from hypothesis import strategies as st |
| 2 |
from hypothesis import given, example |
| 3 |
|
| 4 |
st_bytestring = st.binary() | st.binary().map(bytearray) |
| 5 |
|
| 6 |
@given(st_bytestring, st_bytestring, st_bytestring) |
| 7 |
def test_find(u, prefix, suffix): |
| 8 |
s = prefix + u + suffix |
| 9 |
assert 0 <= s.find(u) <= len(prefix) |
| 10 |
assert s.find(u, len(prefix), len(s) - len(suffix)) == len(prefix) |
| 11 |
|
| 12 |
@given(st_bytestring, st_bytestring, st_bytestring) |
| 13 |
def test_index(u, prefix, suffix): |
| 14 |
s = prefix + u + suffix |
| 15 |
assert 0 <= s.index(u) <= len(prefix) |
| 16 |
assert s.index(u, len(prefix), len(s) - len(suffix)) == len(prefix) |
| 17 |
|
| 18 |
@given(st_bytestring, st_bytestring, st_bytestring) |
| 19 |
def test_rfind(u, prefix, suffix): |
| 20 |
s = prefix + u + suffix |
| 21 |
assert s.rfind(u) >= len(prefix) |
| 22 |
assert s.rfind(u, len(prefix), len(s) - len(suffix)) == len(prefix) |
| 23 |
|
| 24 |
@given(st_bytestring, st_bytestring, st_bytestring) |
| 25 |
def test_rindex(u, prefix, suffix): |
| 26 |
s = prefix + u + suffix |
| 27 |
assert s.rindex(u) >= len(prefix) |
| 28 |
assert s.rindex(u, len(prefix), len(s) - len(suffix)) == len(prefix) |
| 29 |
|
| 30 |
def adjust_indices(u, start, end): |
| 31 |
if end < 0: |
| 32 |
end = max(end + len(u), 0) |
| 33 |
else: |
| 34 |
end = min(end, len(u)) |
| 35 |
if start < 0: |
| 36 |
start = max(start + len(u), 0) |
| 37 |
return start, end |
| 38 |
|
| 39 |
@given(st_bytestring, st_bytestring) |
| 40 |
def test_startswith_basic(u, v): |
| 41 |
assert u.startswith(v) is (u[:len(v)] == v) |
| 42 |
|
| 43 |
@example(b'x', b'', 1) |
| 44 |
@example(b'x', b'', 2) |
| 45 |
@given(st_bytestring, st_bytestring, st.integers()) |
| 46 |
def test_startswith_start(u, v, start): |
| 47 |
expected = u[start:].startswith(v) if v else (start <= len(u)) |
| 48 |
assert u.startswith(v, start) is expected |
| 49 |
|
| 50 |
@example(b'x', b'', 1, 0) |
| 51 |
@example(b'xx', b'', -1, 0) |
| 52 |
@given(st_bytestring, st_bytestring, st.integers(), st.integers()) |
| 53 |
def test_startswith_3(u, v, start, end): |
| 54 |
if v: |
| 55 |
expected = u[start:end].startswith(v) |
| 56 |
else: |
| 57 |
start0, end0 = adjust_indices(u, start, end) |
| 58 |
expected = start0 <= len(u) and start0 <= end0 |
| 59 |
assert u.startswith(v, start, end) is expected |
| 60 |
|
| 61 |
@given(st_bytestring, st_bytestring) |
| 62 |
def test_endswith_basic(u, v): |
| 63 |
if len(v) > len(u): |
| 64 |
assert u.endswith(v) is False |
| 65 |
else: |
| 66 |
assert u.endswith(v) is (u[len(u) - len(v):] == v) |
| 67 |
|
| 68 |
@example(b'x', b'', 1) |
| 69 |
@example(b'x', b'', 2) |
| 70 |
@given(st_bytestring, st_bytestring, st.integers()) |
| 71 |
def test_endswith_2(u, v, start): |
| 72 |
expected = u[start:].endswith(v) if v else (start <= len(u)) |
| 73 |
assert u.endswith(v, start) is expected |
| 74 |
|
| 75 |
@example(b'x', b'', 1, 0) |
| 76 |
@example(b'xx', b'', -1, 0) |
| 77 |
@given(st_bytestring, st_bytestring, st.integers(), st.integers()) |
| 78 |
def test_endswith_3(u, v, start, end): |
| 79 |
if v: |
| 80 |
expected = u[start:end].endswith(v) |
| 81 |
else: |
| 82 |
start0, end0 = adjust_indices(u, start, end) |
| 83 |
expected = start0 <= len(u) and start0 <= end0 |
| 84 |
assert u.endswith(v, start, end) is expected |