11from pytest import raises # type: ignore
22
3- from graphql .language import Source
3+ from graphql .language import Source , SourceLocation
4+ from graphql .pyutils import dedent
45
56
67def describe_source ():
8+ def accepts_body_and_name ():
9+ source = Source ("foo" , "bar" )
10+ assert source .body == "foo"
11+ assert source .name == "bar"
12+
13+ def accepts_location_offset ():
14+ location_offset = SourceLocation (2 , 3 )
15+ source = Source ("" , "" , location_offset )
16+ assert source .location_offset is location_offset
17+
18+ def accepts_tuple_as_location_offset ():
19+ # noinspection PyTypeChecker
20+ source = Source ("" , "" , (2 , 3 )) # type: ignore
21+ assert isinstance (source .location_offset , SourceLocation )
22+ assert source .location_offset == (2 , 3 )
23+
24+ def uses_default_arguments ():
25+ source = Source ("" )
26+ assert source .name == "GraphQL request"
27+ assert isinstance (source .location_offset , SourceLocation )
28+ assert source .location_offset == (1 , 1 )
29+
30+ def can_get_location ():
31+ body = dedent (
32+ """
33+ line 1
34+ line 2
35+ line 3
36+ """
37+ )
38+ source = Source (body )
39+ assert source .body == body
40+ location = source .get_location (body .find ("2" ))
41+ assert isinstance (location , SourceLocation )
42+ assert location == (2 , 6 )
43+
744 def can_be_stringified ():
845 source = Source ("" )
946 assert str (source ) == "<Source name='GraphQL request'>"
@@ -26,10 +63,33 @@ def can_be_compared():
2663 assert not source == "bar"
2764 assert source != "bar"
2865
66+ def rejects_invalid_body_and_name ():
67+ with raises (TypeError , match = "body must be a string\\ ." ):
68+ # noinspection PyTypeChecker
69+ Source (None ) # type: ignore
70+ with raises (TypeError , match = "body must be a string\\ ." ):
71+ # noinspection PyTypeChecker
72+ Source (1 ) # type: ignore
73+ with raises (TypeError , match = "name must be a string\\ ." ):
74+ # noinspection PyTypeChecker
75+ Source ("" , None ) # type: ignore
76+ with raises (TypeError , match = "name must be a string\\ ." ):
77+ # noinspection PyTypeChecker
78+ Source ("" , 1 ) # type: ignore
79+
2980 def rejects_invalid_location_offset ():
3081 def create_source (location_offset ):
3182 return Source ("" , "" , location_offset )
3283
84+ with raises (TypeError ):
85+ create_source (None )
86+ with raises (TypeError ):
87+ create_source (1 )
88+ with raises (TypeError ):
89+ create_source ((1 ,))
90+ with raises (TypeError ):
91+ create_source ((1 , 2 , 3 ))
92+
3393 with raises (
3494 ValueError ,
3595 match = "line in location_offset is 1-indexed and must be positive\\ ." ,
0 commit comments