Skip to content

Commit

Permalink
refactor: use _get_changes api for CollectionValue
Browse files Browse the repository at this point in the history
  • Loading branch information
15r10nk committed Mar 25, 2024
1 parent 070e64f commit 3834096
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
37 changes: 37 additions & 0 deletions inline_snapshot/_inline_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,43 @@ def get_result(self, flags):

return self._old_value

def _get_changes(self) -> Iterator[Change]:

assert isinstance(self._ast_node, ast.List)

for old_value, old_node in zip(self._old_value, self._ast_node.elts):
if old_value not in self._new_value:
yield Delete(
flag="trim", source=self._source, node=old_node, old_value=old_value
)
continue

# check for update
new_token = value_to_token(old_value)

if self._token_of_node(old_node) != new_token:
new_code = self._token_to_code(new_token)

yield Replace(
node=old_node,
source=self._source,
new_code=new_code,
flag="update",
old_value=old_value,
new_value=old_value,
)

new_values = [v for v in self._new_value if v not in self._old_value]
if new_values:
yield ListInsert(
flag="fix",
source=self._source,
node=self._ast_node,
position=len(self._ast_node.elts),
new_code=[self._value_to_code(v) for v in new_values],
new_values=new_values,
)


class DictValue(GenericValue):
_current_op = "snapshot[key]"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_inline_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ def test_contains(check_update):

assert check_update(
"for i in range(5): assert i in snapshot([0,1,2,3,4,5,6])", flags="trim"
) == snapshot("for i in range(5): assert i in snapshot([0, 1, 2, 3, 4])")
) == snapshot("for i in range(5): assert i in snapshot([0,1,2,3,4])")

assert (
check_update(
Expand Down

0 comments on commit 3834096

Please sign in to comment.