| 1 |
import weakref |
| 2 |
|
| 3 |
from typing import cast, Tuple |
| 4 |
|
| 5 |
from pytest import raises |
| 6 |
|
| 7 |
from graphql.language import Source, SourceLocation |
| 8 |
|
| 9 |
from ..utils import dedent |
| 10 |
|
| 11 |
|
| 12 |
def describe_source(): |
| 13 |
def accepts_body_and_name(): |
| 14 |
source = Source("foo", "bar") |
| 15 |
assert source.body == "foo" |
| 16 |
assert source.name == "bar" |
| 17 |
|
| 18 |
def accepts_location_offset(): |
| 19 |
location_offset = SourceLocation(2, 3) |
| 20 |
source = Source("", "", location_offset) |
| 21 |
assert source.location_offset is location_offset |
| 22 |
|
| 23 |
def accepts_tuple_as_location_offset(): |
| 24 |
# noinspection PyTypeChecker |
| 25 |
source = Source("", "", (2, 3)) # type: ignore |
| 26 |
assert isinstance(source.location_offset, SourceLocation) |
| 27 |
assert source.location_offset == (2, 3) |
| 28 |
|
| 29 |
def uses_default_arguments(): |
| 30 |
source = Source("") |
| 31 |
assert source.name == "GraphQL request" |
| 32 |
assert isinstance(source.location_offset, SourceLocation) |
| 33 |
assert source.location_offset == (1, 1) |
| 34 |
|
| 35 |
def can_get_location(): |
| 36 |
body = dedent(""" |
| 37 |
line 1 |
| 38 |
line 2 |
| 39 |
line 3 |
| 40 |
""") |
| 41 |
source = Source(body) |
| 42 |
assert source.body == body |
| 43 |
location = source.get_location(body.find("2")) |
| 44 |
assert isinstance(location, SourceLocation) |
| 45 |
assert location == (2, 6) |
| 46 |
|
| 47 |
def can_be_stringified(): |
| 48 |
source = Source("") |
| 49 |
assert str(source) == "<Source name='GraphQL request'>" |
| 50 |
|
| 51 |
source = Source("", "Custom source name") |
| 52 |
assert str(source) == "<Source name='Custom source name'>" |
| 53 |
|
| 54 |
def can_be_compared(): |
| 55 |
source = Source("foo") |
| 56 |
assert source == source |
| 57 |
assert not source != source |
| 58 |
assert source == "foo" |
| 59 |
assert not source != "foo" |
| 60 |
same_source = Source("foo") |
| 61 |
assert source == same_source |
| 62 |
assert not source != same_source |
| 63 |
different_source = Source("bar") |
| 64 |
assert not source == different_source |
| 65 |
assert source != different_source |
| 66 |
assert not source == "bar" |
| 67 |
assert source != "bar" |
| 68 |
|
| 69 |
def can_create_weak_reference(): |
| 70 |
source = Source("foo") |
| 71 |
ref = weakref.ref(source) |
| 72 |
assert ref() is source |
| 73 |
|
| 74 |
def can_create_custom_attribute(): |
| 75 |
node = Source("foo") |
| 76 |
node.custom = "bar" # type: ignore |
| 77 |
assert node.custom == "bar" # type: ignore |
| 78 |
|
| 79 |
def rejects_invalid_location_offset(): |
| 80 |
def create_source(location_offset: Tuple[int, int]) -> Source: |
| 81 |
return Source("", "", cast(SourceLocation, location_offset)) |
| 82 |
|
| 83 |
with raises(TypeError): |
| 84 |
create_source(None) # type: ignore |
| 85 |
with raises(TypeError): |
| 86 |
create_source(1) # type: ignore |
| 87 |
with raises(TypeError): |
| 88 |
create_source((1,)) # type: ignore |
| 89 |
with raises(TypeError): |
| 90 |
create_source((1, 2, 3)) # type: ignore |
| 91 |
|
| 92 |
with raises( |
| 93 |
ValueError, |
| 94 |
match="line in location_offset is 1-indexed and must be positive\\.", |
| 95 |
): |
| 96 |
create_source((0, 1)) |
| 97 |
with raises( |
| 98 |
ValueError, |
| 99 |
match="line in location_offset is 1-indexed and must be positive\\.", |
| 100 |
): |
| 101 |
create_source((-1, 1)) |
| 102 |
|
| 103 |
with raises( |
| 104 |
ValueError, |
| 105 |
match="column in location_offset is 1-indexed and must be positive\\.", |
| 106 |
): |
| 107 |
create_source((1, 0)) |
| 108 |
with raises( |
| 109 |
ValueError, |
| 110 |
match="column in location_offset is 1-indexed and must be positive\\.", |
| 111 |
): |
| 112 |
create_source((1, -1)) |