Skip to content

Commit

Permalink
Merge pull request #82 from 15r10nk/fix_quoting_update
Browse files Browse the repository at this point in the history
fix: changeing string quoting is no update
  • Loading branch information
15r10nk authored May 2, 2024
2 parents 17b52ff + 1aee2f6 commit 9060935
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
14 changes: 11 additions & 3 deletions inline_snapshot/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ def normalize_strings(token_sequence):
continue

if current_string is not None:
yield (token.STRING, repr(current_string))
yield simple_token(token.STRING, repr(current_string))
current_string = None

yield t

if current_string is not None:
yield (token.STRING, repr(current_string))
yield simple_token(token.STRING, repr(current_string))


ignore_tokens = (token.NEWLINE, token.ENDMARKER, token.NL)
Expand Down Expand Up @@ -90,7 +90,15 @@ def triple_quote(string):
return f"{quote_type}{string}{quote_type}"


simple_token = namedtuple("simple_token", "type,string")
class simple_token(namedtuple("simple_token", "type,string")):

def __eq__(self, other):
if self.type == other.type == 3:
return ast.literal_eval(self.string) == ast.literal_eval(
other.string
) and self.string.replace("'", '"') == other.string.replace("'", '"')
else:
return super().__eq__(other)


def value_to_token(value):
Expand Down
52 changes: 52 additions & 0 deletions tests/test_inline_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,3 +877,55 @@ def test_different_snapshot_name(check_update):
"""
)
)


def test_quoting_change_is_no_update(source):

s = source(
"""\
from inline_snapshot import external,snapshot
class X:
def __init__(self,a):
self.a=a
pass
def __repr__(self):
return f'X("{self.a}")'
def __eq__(self,other):
if not hasattr(other,"a"):
return NotImplemented
return other.a==self.a
assert X("a") == snapshot()
"""
)
assert s.flags == snapshot({"create"})

s = s.run("create")

assert s.source == snapshot(
"""\
from inline_snapshot import external,snapshot
class X:
def __init__(self,a):
self.a=a
pass
def __repr__(self):
return f'X("{self.a}")'
def __eq__(self,other):
if not hasattr(other,"a"):
return NotImplemented
return other.a==self.a
assert X("a") == snapshot(X("a"))
"""
)

assert s.flags == snapshot({"create"})
s = s.run()
assert s.flags == snapshot(set())

0 comments on commit 9060935

Please sign in to comment.