File: test_strip_ignored_characters_fuzz.py

package info (click to toggle)
graphql-core 3.2.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,384 kB
  • sloc: python: 45,812; makefile: 26; sh: 13
file content (41 lines) | stat: -rw-r--r-- 1,395 bytes parent folder | download
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
from typing import Optional

from pytest import mark

from graphql.error import GraphQLSyntaxError
from graphql.language import Lexer, Source, TokenKind
from graphql.utilities import strip_ignored_characters

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_strip_ignored_characters():
    @mark.slow
    @mark.timeout(20)
    def strips_ignored_characters_inside_random_block_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

            stripped_value = lex_value(strip_ignored_characters(test_str))

            assert test_value == stripped_value, dedent(
                f"""
                Expected lexValue(stripIgnoredCharacters({test_str!r})
                  to equal {test_value!r}
                  but got {stripped_value!r}
                """
            )