Skip to content

Commit

Permalink
Improve text strategy reprs
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed Nov 16, 2021
1 parent 6cd8dd3 commit 153c6a2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
24 changes: 20 additions & 4 deletions hypothesis-python/src/hypothesis/strategies/_internal/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,32 @@ def __init__(
include_characters=whitelist_characters,
exclude_characters=blacklist_characters,
)
if not intervals:
arguments = [
self._arg_repr = ", ".join(
f"{k}={v!r}"
for k, v in [
("whitelist_categories", whitelist_categories),
("blacklist_categories", blacklist_categories),
("whitelist_characters", whitelist_characters),
("blacklist_characters", blacklist_characters),
("min_codepoint", min_codepoint),
("max_codepoint", max_codepoint),
]
if not (v in (None, "") or (k == "blacklist_categories" and v == ("Cs",)))
)
if not intervals:
raise InvalidArgument(
"No characters are allowed to be generated by this "
"combination of arguments: "
+ ", ".join("%s=%r" % arg for arg in arguments if arg[1] is not None)
f"combination of arguments: {self._arg_repr}"
)
self.intervals = IntervalSet(intervals)
self.zero_point = self.intervals.index_above(ord("0"))
self.Z_point = min(
self.intervals.index_above(ord("Z")), len(self.intervals) - 1
)

def __repr__(self):
return f"characters({self._arg_repr})"

def do_draw(self, data):
if len(self.intervals) > 256:
if biased_coin(data, 0.2):
Expand Down Expand Up @@ -107,6 +113,16 @@ class TextStrategy(ListStrategy):
def do_draw(self, data):
return "".join(super().do_draw(data))

def __repr__(self):
args = []
if repr(self.element_strategy) != "characters()":
args.append(repr(self.element_strategy))
if self.min_size:
args.append(f"min_size={self.min_size}")
if self.max_size < float("inf"):
args.append(f"max_size={self.max_size}")
return f"text({', '.join(args)})"

# See https://docs.python.org/3/library/stdtypes.html#string-methods
# These methods always return Truthy values for any nonempty string.
_nonempty_filters = ListStrategy._nonempty_filters + (
Expand Down
10 changes: 5 additions & 5 deletions hypothesis-python/tests/ghostwriter/recorded/magic_builtins.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_fuzz_chr(i):

@given(
source=st.nothing(),
filename=st.nothing(),
filename=st.text(),
mode=st.nothing(),
flags=st.just(0),
dont_inherit=st.booleans(),
Expand All @@ -69,7 +69,7 @@ def test_fuzz_complex(real, imag):
complex(real=real, imag=imag)


@given(obj=st.nothing(), name=st.nothing())
@given(obj=st.nothing(), name=st.text())
def test_fuzz_delattr(obj, name):
delattr(obj, name)

Expand Down Expand Up @@ -112,12 +112,12 @@ def test_fuzz_format(value, format_spec):
format(value, format_spec)


@given(object=st.builds(object), name=st.nothing(), default=st.nothing())
@given(object=st.builds(object), name=st.text(), default=st.nothing())
def test_fuzz_getattr(object, name, default):
getattr(object, name, default)


@given(obj=st.nothing(), name=st.nothing())
@given(obj=st.nothing(), name=st.text())
def test_fuzz_hasattr(obj, name):
hasattr(obj, name)

Expand Down Expand Up @@ -253,7 +253,7 @@ def test_fuzz_round(number, ndigits):
round(number=number, ndigits=ndigits)


@given(obj=st.nothing(), name=st.nothing(), value=st.nothing())
@given(obj=st.nothing(), name=st.text(), value=st.nothing())
def test_fuzz_setattr(obj, name, value):
setattr(obj, name, value)

Expand Down

0 comments on commit 153c6a2

Please sign in to comment.