Skip to content

Instantly share code, notes, and snippets.

@rob-smallshire
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rob-smallshire/cfb6a5117a0a1e9bc37e to your computer and use it in GitHub Desktop.
Save rob-smallshire/cfb6a5117a0a1e9bc37e to your computer and use it in GitHub Desktop.
A Hypothesis 1.2 strategy for creating multiline strings.
PRINTABLE_ASCII_RANGE = (32, 127)
def multiline_ascii_encodable_text(min_num_lines, max_num_lines):
"""A Hypothesis strategy to produce a multiline Unicode string.
Args:
min_num_lines: The minimum number of lines in the produced strings.
max_num_lines: The maximum number of lines in the produced strings.
Returns:
A strategy for generating Unicode strings containing only newlines
and characters which are encodable as printable 7-bit ASCII characters.
"""
return strategy(integers_in_range(min_num_lines, max_num_lines)) \
.flatmap(lambda n: ([integers_in_range(*PRINTABLE_ASCII_RANGE)],) * n) \
.map(lambda xs: '\n'.join(bytes(x).decode('ascii') for x in xs))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment