Debian Codegraph @

tests/utilities/test_strip_ignored_characters_fuzz.py

raw
1 from typing import Optional
2
3 from pytest import mark
4
5 from graphql.error import GraphQLSyntaxError
6 from graphql.language import Lexer, Source, TokenKind
7 from graphql.utilities import strip_ignored_characters
8
9 from ..utils import dedent, gen_fuzz_strings, timeout_factor
10
11
12 def lex_value(s: str) -> Optional[str]:
13 lexer = Lexer(Source(s))
14 value = lexer.advance().value
15 assert lexer.advance().kind == TokenKind.EOF, "Expected EOF"
16 return value
17
18
19 def describe_strip_ignored_characters():
20 @mark.slow
21 @mark.timeout(80 * timeout_factor)
22 def strips_ignored_characters_inside_random_block_strings():
23 # Testing with length >7 is taking exponentially more time. However it is
24 # highly recommended to test with increased limit if you make any change.
25 for fuzz_str in gen_fuzz_strings(allowed_chars='\n\t "a\\', max_length=7):
26 test_str = f'"""{fuzz_str}"""'
27
28 try:
29 test_value = lex_value(test_str)
30 except (AssertionError, GraphQLSyntaxError):
31 continue # skip invalid values
32
33 stripped_value = lex_value(strip_ignored_characters(test_str))
34
35 assert test_value == stripped_value, dedent(f"""
36 Expected lexValue(stripIgnoredCharacters({test_str!r})
37 to equal {test_value!r}
38 but got {stripped_value!r}
39 """)