|
8 | 8 | from graphql.utilities import strip_ignored_characters |
9 | 9 |
|
10 | 10 | from ..fixtures import kitchen_sink_query, kitchen_sink_sdl # noqa: F401 |
11 | | -from ..utils import dedent |
| 11 | +from ..utils import dedent, gen_fuzz_strings |
12 | 12 |
|
13 | 13 | ignored_tokens = [ |
14 | 14 | # UnicodeBOM |
@@ -374,39 +374,26 @@ def expect_stripped_string(block_str: str): |
374 | 374 | expect_stripped_string('"""\n a\n b"""').to_equal('"""a\nb"""') |
375 | 375 | expect_stripped_string('"""\na\n b\nc"""').to_equal('"""a\n b\nc"""') |
376 | 376 |
|
| 377 | + def strips_ignored_characters_inside_random_block_strings(): |
377 | 378 | # Testing with length >5 is taking exponentially more time. However it is |
378 | 379 | # highly recommended to test with increased limit if you make any change. |
379 | | - max_combination_length = 5 |
380 | | - possible_chars = ["\n", " ", '"', "a", "\\"] |
381 | | - num_possible_chars = len(possible_chars) |
382 | | - num_combinations = 1 |
383 | | - for length in range(1, max_combination_length): |
384 | | - num_combinations *= num_possible_chars |
385 | | - for combination in range(num_combinations): |
386 | | - test_str = '"""' |
387 | | - |
388 | | - left_over = combination |
389 | | - for i in range(length): |
390 | | - reminder = left_over % num_possible_chars |
391 | | - test_str += possible_chars[reminder] |
392 | | - left_over = (left_over - reminder) // num_possible_chars |
393 | | - |
394 | | - test_str += '"""' |
395 | | - |
396 | | - try: |
397 | | - test_value = lex_value(test_str) |
398 | | - except (AssertionError, GraphQLSyntaxError): |
399 | | - continue # skip invalid values |
400 | | - |
401 | | - stripped_value = lex_value(strip_ignored_characters(test_str)) |
402 | | - |
403 | | - assert test_value == stripped_value, dedent( |
404 | | - f""" |
405 | | - Expected lexValue(stripIgnoredCharacters({test_str!r}) |
406 | | - to equal {test_value!r} |
407 | | - but got {stripped_value!r} |
408 | | - """ |
409 | | - ) |
| 380 | + for fuzz_str in gen_fuzz_strings(allowed_chars='\n\t "a\\', max_length=5): |
| 381 | + test_str = f'"""{fuzz_str}"""' |
| 382 | + |
| 383 | + try: |
| 384 | + test_value = lex_value(test_str) |
| 385 | + except (AssertionError, GraphQLSyntaxError): |
| 386 | + continue # skip invalid values |
| 387 | + |
| 388 | + stripped_value = lex_value(strip_ignored_characters(test_str)) |
| 389 | + |
| 390 | + assert test_value == stripped_value, dedent( |
| 391 | + f""" |
| 392 | + Expected lexValue(stripIgnoredCharacters({test_str!r}) |
| 393 | + to equal {test_value!r} |
| 394 | + but got {stripped_value!r} |
| 395 | + """ |
| 396 | + ) |
410 | 397 |
|
411 | 398 | # noinspection PyShadowingNames |
412 | 399 | def strips_kitchen_sink_query_but_maintains_the_exact_same_ast( |
|
0 commit comments