| 1 |
# This file is part of Hypothesis, which may be found at |
| 2 |
# https://github.com/HypothesisWorks/hypothesis/ |
| 3 |
# |
| 4 |
# Copyright the Hypothesis Authors. |
| 5 |
# Individual contributors are listed in AUTHORS.rst and the git log. |
| 6 |
# |
| 7 |
# This Source Code Form is subject to the terms of the Mozilla Public License, |
| 8 |
# v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 9 |
# obtain one at https://mozilla.org/MPL/2.0/. |
| 10 |
|
| 11 |
import string |
| 12 |
from encodings.aliases import aliases |
| 13 |
|
| 14 |
import pytest |
| 15 |
|
| 16 |
from hypothesis import given, settings, strategies as st |
| 17 |
|
| 18 |
from tests.common.utils import Why, xfail_on_crosshair |
| 19 |
|
| 20 |
IDENTIFIER_CHARS = string.ascii_letters + string.digits + "_" |
| 21 |
|
| 22 |
|
| 23 |
@given(st.characters(exclude_characters=IDENTIFIER_CHARS)) |
| 24 |
def test_large_blacklist(c): |
| 25 |
assert c not in IDENTIFIER_CHARS |
| 26 |
|
| 27 |
|
| 28 |
@xfail_on_crosshair(Why.symbolic_outside_context) # seems like a crosshair bug here |
| 29 |
@given(st.data()) |
| 30 |
def test_arbitrary_blacklist(data): |
| 31 |
blacklist = data.draw(st.text(st.characters(max_codepoint=1000), min_size=1)) |
| 32 |
ords = list(map(ord, blacklist)) |
| 33 |
c = data.draw( |
| 34 |
st.characters( |
| 35 |
exclude_characters=blacklist, |
| 36 |
min_codepoint=max(0, min(ords) - 1), |
| 37 |
max_codepoint=max(0, max(ords) + 1), |
| 38 |
) |
| 39 |
) |
| 40 |
assert c not in blacklist |
| 41 |
|
| 42 |
|
| 43 |
def _enc(cdc): |
| 44 |
try: |
| 45 |
"".encode(cdc) |
| 46 |
return True |
| 47 |
except Exception: |
| 48 |
return False |
| 49 |
|
| 50 |
|
| 51 |
lots_of_encodings = sorted(x for x in set(aliases).union(aliases.values()) if _enc(x)) |
| 52 |
assert len(lots_of_encodings) > 100 # sanity-check |
| 53 |
|
| 54 |
|
| 55 |
@pytest.mark.skipif( |
| 56 |
settings.get_current_profile_name() == "crosshair", |
| 57 |
reason="takes 2000s; large & slow symbolic strings", |
| 58 |
) |
| 59 |
@given(data=st.data(), codec=st.sampled_from(lots_of_encodings)) |
| 60 |
@settings(max_examples=5) |
| 61 |
def test_can_constrain_characters_to_codec(data, codec): |
| 62 |
s = data.draw(st.text(st.characters(codec=codec), min_size=25)) |
| 63 |
s.encode(codec) |