def correctly_print_random_strings(): # Testing with length >7 is taking exponentially more time. However it is # highly recommended to test with increased limit if you make any change. for fuzz_str in gen_fuzz_strings(allowed_chars='\n\t "a\\', max_length=7): test_str = f'"""{fuzz_str}"""' try: test_value = lex_value(test_str) except (AssertionError, GraphQLSyntaxError): continue # skip invalid values assert isinstance(test_value, str) printed_value = lex_value(print_block_string(test_value)) assert test_value == printed_value, dedent(f""" Expected lex_value(print_block_string({test_value!r}) to equal {test_value!r} but got {printed_value!r} """) printed_multiline_string = lex_value( print_block_string(test_value, " ", True)) assert test_value == printed_multiline_string, dedent(f""" Expected lex_value(print_block_string({test_value!r}, ' ', True) to equal {test_value!r} but got {printed_multiline_string!r} """)
def correctly_prints_single_line_with_leading_space_and_quotation(): s = ' space-led value "quoted string"' assert print_block_string( s) == '""" space-led value "quoted string"\n"""' assert (print_block_string( s, "", True) == '""" space-led value "quoted string"\n"""')
def assert_non_printable_block_string(test_value: str) -> None: block_string = print_block_string(test_value) printed_value = lex_value(block_string) assert test_value != printed_value, dedent(f""" Expected lexValue({block_string!r}) to not equal {test_value!r} """)
def correctly_prints_string_with_a_first_line_indentation(): s = join_lines(" first ", " line ", "indentation", " string") assert print_block_string(s) == join_lines('"""', " first ", " line ", "indentation", " string", '"""')
def assert_printable_block_string(test_value: str, minimize: bool = False) -> None: block_string = print_block_string(test_value, minimize=minimize) printed_value = lex_value(block_string) assert test_value == printed_value, dedent(f""" Expected lexValue({block_string!r}) to equal {test_value!r} but got {printed_value!r} """)
def print_description( def_: Union[GraphQLArgument, GraphQLDirective, GraphQLEnumValue, GraphQLNamedType], indentation="", first_in_block=True, adding='', ) -> str: if not def_.description: return "" lines = description_lines(def_.description, 120 - len(indentation)) if adding: lines += ['\n' + adding + '\n'] text = "\n".join(lines) prefer_multiple_lines = len(text) > 70 block_string = print_block_string(text, "", prefer_multiple_lines) prefix = "\n" + indentation if indentation and not first_in_block else indentation return prefix + block_string.replace("\n", "\n" + indentation) + "\n"
def by_default_print_block_strings_as_single_line(): s = "one liner" assert print_block_string(s) == '"""one liner"""' assert print_block_string(s, "", True) == '"""\none liner\n"""'
def correctly_prints_single_line_with_trailing_backslash(): s = "backslash \\" assert print_block_string(s) == '"""\nbackslash \\\n"""' assert print_block_string(s, "", True) == '"""\nbackslash \\\n"""'
def correctly_prints_single_line_with_leading_space(): s = " space-led string" assert print_block_string(s) == '""" space-led string"""' assert print_block_string(s, "", True) == '""" space-led string\n"""'
def _assert_block_string(s: str, readable: str, minimize: Optional[str] = None) -> None: assert print_block_string(s) == readable assert print_block_string(s, minimize=True) == minimize or readable
def do_not_escape_characters(): s = '" \\ / \b \f \n \r \t' assert print_block_string(s) == f'"""\n{s}\n"""'