| 1 |
from pytest import mark |
| 2 |
|
| 3 |
from graphql.language import Source, Lexer, TokenKind |
| 4 |
from graphql.language.block_string import ( |
| 5 |
print_block_string, |
| 6 |
is_printable_as_block_string, |
| 7 |
) |
| 8 |
|
| 9 |
from ..utils import dedent, gen_fuzz_strings, timeout_factor |
| 10 |
|
| 11 |
|
| 12 |
def lex_value(s: str) -> str: |
| 13 |
lexer = Lexer(Source(s)) |
| 14 |
value = lexer.advance().value |
| 15 |
assert isinstance(value, str) |
| 16 |
assert lexer.advance().kind == TokenKind.EOF, "Expected EOF" |
| 17 |
return value |
| 18 |
|
| 19 |
|
| 20 |
def assert_printable_block_string(test_value: str, minimize: bool = False) -> None: |
| 21 |
block_string = print_block_string(test_value, minimize=minimize) |
| 22 |
printed_value = lex_value(block_string) |
| 23 |
assert test_value == printed_value, dedent(f""" |
| 24 |
Expected lexValue({block_string!r}) |
| 25 |
to equal {test_value!r} |
| 26 |
but got {printed_value!r} |
| 27 |
""") |
| 28 |
|
| 29 |
|
| 30 |
def assert_non_printable_block_string(test_value: str) -> None: |
| 31 |
block_string = print_block_string(test_value) |
| 32 |
printed_value = lex_value(block_string) |
| 33 |
assert test_value != printed_value, dedent(f""" |
| 34 |
Expected lexValue({block_string!r}) |
| 35 |
to not equal {test_value!r} |
| 36 |
""") |
| 37 |
|
| 38 |
|
| 39 |
def describe_print_block_string(): |
| 40 |
@mark.slow |
| 41 |
@mark.timeout(80 * timeout_factor) |
| 42 |
def correctly_print_random_strings(): |
| 43 |
# Testing with length >7 is taking exponentially more time. However, it is |
| 44 |
# highly recommended testing with increased limit if you make any change. |
| 45 |
for fuzz_str in gen_fuzz_strings(allowed_chars='\n\t "a\\', max_length=7): |
| 46 |
if not is_printable_as_block_string(fuzz_str): |
| 47 |
assert_non_printable_block_string(fuzz_str) |
| 48 |
continue |
| 49 |
|
| 50 |
assert_printable_block_string(fuzz_str) |
| 51 |
assert_printable_block_string(fuzz_str, minimize=True) |