-
-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathtest_block_string_fuzz.py
54 lines (42 loc) · 1.79 KB
/
test_block_string_fuzz.py
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
from typing import Optional
from pytest import mark
from graphql.error import GraphQLSyntaxError
from graphql.language import Source, Lexer, TokenKind
from graphql.language.block_string import print_block_string
from ..utils import dedent, gen_fuzz_strings
def lex_value(s: str) -> Optional[str]:
lexer = Lexer(Source(s))
value = lexer.advance().value
assert lexer.advance().kind == TokenKind.EOF, "Expected EOF"
return value
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 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}
"""
)