1
1
from pytest import raises # type: ignore
2
2
3
- from graphql .language import Source
3
+ from graphql .language import Source , SourceLocation
4
+ from graphql .pyutils import dedent
4
5
5
6
6
7
def 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
+
7
44
def can_be_stringified ():
8
45
source = Source ("" )
9
46
assert str (source ) == "<Source name='GraphQL request'>"
@@ -26,10 +63,33 @@ def can_be_compared():
26
63
assert not source == "bar"
27
64
assert source != "bar"
28
65
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
+
29
80
def rejects_invalid_location_offset ():
30
81
def create_source (location_offset ):
31
82
return Source ("" , "" , location_offset )
32
83
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
+
33
93
with raises (
34
94
ValueError ,
35
95
match = "line in location_offset is 1-indexed and must be positive\\ ." ,
0 commit comments