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