Skip to content

Commit

Permalink
Add SourceLocation.__eq__ to make it comparable to the formatted form
Browse files Browse the repository at this point in the history
  • Loading branch information
ktosiek committed Sep 25, 2019
1 parent 837ee66 commit de0b14f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/graphql/language/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ class SourceLocation(NamedTuple):
def formatted(self):
return dict(line=self.line, column=self.column)

def __eq__(self, other):
if isinstance(other, dict):
return other == self.formatted
return super().__eq__(other)

def __ne__(self, other):
return not self == other


def get_location(source: "Source", position: int) -> SourceLocation:
"""Get the line and column for a character position in the source.
Expand Down
12 changes: 12 additions & 0 deletions tests/language/test_location.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from graphql import SourceLocation


def describe_SourceLocation():
def equals_with_itself():
assert SourceLocation(1, 2) == SourceLocation(1, 2)
assert (SourceLocation(1, 2) != SourceLocation(1, 2)) is False

def equals_with_formatted_form():
sl = SourceLocation(1, 2)
assert SourceLocation(1, 2) == sl.formatted
assert (SourceLocation(1, 2) != sl.formatted) is False

0 comments on commit de0b14f

Please sign in to comment.