Debian Codegraph @

tests/language/test_schema_coordinate_lexer.py

raw
1 from typing import Optional, Tuple
2
3 from pytest import raises
4
5 from graphql.error import GraphQLSyntaxError
6 from graphql.language import Source, Token, TokenKind
7 from graphql.language.schema_coordinate_lexer import SchemaCoordinateLexer
8
9 Location = Optional[Tuple[int, int]]
10
11
12 def lex_second(s: str) -> Token:
13 lexer = SchemaCoordinateLexer(Source(s))
14 lexer.advance()
15 return lexer.advance()
16
17
18 def assert_syntax_error(text: str, message: str, location: Location) -> None:
19 with raises(GraphQLSyntaxError) as exc_info:
20 lex_second(text)
21 error = exc_info.value
22 assert error.message == f"Syntax Error: {message}"
23 assert error.description == message
24 assert error.locations == [location]
25
26
27 def describe_schema_coordinate_lexer():
28 def tracks_a_schema_coordinate():
29 lexer = SchemaCoordinateLexer(Source("Name.field"))
30 assert lexer.advance() == Token(TokenKind.NAME, 0, 4, 1, 1, "Name")
31
32 def forbids_ignored_tokens():
33 lexer = SchemaCoordinateLexer(Source("\nName.field"))
34 with raises(GraphQLSyntaxError) as exc_info:
35 lexer.advance()
36 error = exc_info.value
37 assert error.message == "Syntax Error: Invalid character: U+000A."
38 assert error.locations == [(1, 1)]
39
40 def lex_reports_a_useful_syntax_error():
41 assert_syntax_error("Foo .bar", "Invalid character: ' '.", (1, 4))