Last active
August 29, 2015 14:19
-
-
Save rob-smallshire/cfb6a5117a0a1e9bc37e to your computer and use it in GitHub Desktop.
A Hypothesis 1.2 strategy for creating multiline strings.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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