def asserts_that_a_source_was_provided(): with raises(TypeError, match="missing 1 required positional argument: 'source'"): # noinspection PyArgumentList strip_ignored_characters() with raises(TypeError, match="Must provide string or Source. Received: None"): # noinspection PyTypeChecker strip_ignored_characters(None)
def strips_kitchen_sink_sdl_but_maintains_the_exact_same_ast( kitchen_sink_sdl # noqa: F811 ): stripped_sdl = strip_ignored_characters(kitchen_sink_sdl) assert strip_ignored_characters(stripped_sdl) == stripped_sdl sdl_ast = parse(kitchen_sink_sdl, no_location=True) stripped_ast = parse(stripped_sdl, no_location=True) assert stripped_ast == sdl_ast
def strips_kitchen_sink_query_but_maintains_the_exact_same_ast( kitchen_sink_query # noqa: F811 ): stripped_query = strip_ignored_characters(kitchen_sink_query) assert strip_ignored_characters(stripped_query) == stripped_query query_ast = parse(kitchen_sink_query, no_location=True) stripped_ast = parse(stripped_query, no_location=True) assert stripped_ast == query_ast
def to_equal(self, expected: str): doc_string = self.doc_string stripped = strip_ignored_characters(doc_string) assert stripped == expected, ( f"Expected strip_ignored_characters({doc_string!r})\n" f"\tto equal {expected!r}\n\tbut got {stripped!r}") stripped_twice = strip_ignored_characters(stripped) assert stripped == stripped_twice, ( f"Expected strip_ignored_characters({stripped!r})\n" f"\tto equal {stripped!r}\n\tbut got {stripped_twice!r}")
def report_document_with_invalid_token(): with raises(GraphQLSyntaxError) as exc_info: strip_ignored_characters('{ foo(arg: "\n"') assert str(exc_info.value) + "\n" == dedent(""" Syntax Error: Unterminated string. GraphQL request:1:13 1 | { foo(arg: " | ^ 2 | " """)
def strips_ignored_characters_inside_block_strings(): # noinspection PyShadowingNames def expect_stripped_string(block_str: str): original_value = lex_value(block_str) stripped_str = strip_ignored_characters(block_str) stripped_value = lex_value(stripped_str) assert original_value == stripped_value return ExpectStripped(block_str) expect_stripped_string('""""""').to_stay_the_same() expect_stripped_string('""" """').to_equal('""""""') expect_stripped_string('"""a"""').to_stay_the_same() expect_stripped_string('""" a"""').to_equal('""" a"""') expect_stripped_string('""" a """').to_equal('""" a """') expect_stripped_string('"""\n"""').to_equal('""""""') expect_stripped_string('"""a\nb"""').to_equal('"""a\nb"""') expect_stripped_string('"""a\rb"""').to_equal('"""a\nb"""') expect_stripped_string('"""a\r\nb"""').to_equal('"""a\nb"""') expect_stripped_string('"""a\r\n\nb"""').to_equal('"""a\n\nb"""') expect_stripped_string('"""\\\n"""').to_stay_the_same() expect_stripped_string('""""\n"""').to_stay_the_same() expect_stripped_string('"""\\"""\n"""').to_equal('"""\\""""""') expect_stripped_string('"""\na\n b"""').to_stay_the_same() expect_stripped_string('"""\n a\n b"""').to_equal('"""a\nb"""') expect_stripped_string('"""\na\n b\nc"""').to_equal('"""a\n b\nc"""') # Testing with length >5 is taking exponentially more time. However it is # highly recommended to test with increased limit if you make any change. max_combination_length = 5 possible_chars = ["\n", " ", '"', "a", "\\"] num_possible_chars = len(possible_chars) num_combinations = 1 for length in range(1, max_combination_length): num_combinations *= num_possible_chars for combination in range(num_combinations): test_str = '"""' left_over = combination for i in range(length): reminder = left_over % num_possible_chars test_str += possible_chars[reminder] left_over = (left_over - reminder) // num_possible_chars test_str += '"""' try: test_value = lex_value(test_str) except (AssertionError, GraphQLSyntaxError): continue # skip invalid values stripped_str = strip_ignored_characters(test_str) stripped_value = lex_value(stripped_str) assert test_value == stripped_value
def to_equal(self, expected: str): doc_string = self.doc_string stripped = strip_ignored_characters(doc_string) assert stripped == expected, dedent(f""" Expected strip_ignored_characters({doc_string!r}) to equal {expected!r} but got {stripped!r} """) stripped_twice = strip_ignored_characters(stripped) assert stripped == stripped_twice, dedent(f"""" Expected strip_ignored_characters({stripped!r})" to equal {stripped!r} but got {stripped_twice!r} """)
def expect_stripped_string(block_str: str): original_value = lex_value(block_str) stripped_str = strip_ignored_characters(block_str) stripped_value = lex_value(stripped_str) assert original_value == stripped_value return ExpectStripped(block_str)
def expect_stripped_string(block_str: str): original_value = lex_value(block_str) stripped_value = lex_value(strip_ignored_characters(block_str)) assert original_value == stripped_value, dedent(f""" Expected lexValue(stripIgnoredCharacters({block_str!r}) to equal {original_value!r} but got {stripped_value!r} """) return ExpectStripped(block_str)
def strips_ignored_characters_from_graphql_query_document(): query = dedent(""" query SomeQuery($foo: String!, $bar: String) { someField(foo: $foo, bar: $bar) { a b { c d } } } """) assert strip_ignored_characters(query) == ( "query SomeQuery($foo:String!$bar:String)" "{someField(foo:$foo bar:$bar){a b{c d}}}")
def strips_ignored_characters_from_graphql_sdl_document(): sdl = dedent(''' """ Type description """ type Foo { """ Field description """ bar: String } ''') assert strip_ignored_characters(sdl) == ( '"""Type description""" type Foo{"""Field description""" bar:String}' )
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} """)
def asserts_that_a_valid_source_was_provided(): with raises(TypeError, match="Must provide string or Source. Received: {}"): # noinspection PyTypeChecker strip_ignored_characters({})
def to_raise(self, expected_stringify_error): with raises(GraphQLSyntaxError) as exc_info: strip_ignored_characters(self.doc_string) assert str(exc_info.value) == expected_stringify_error