From 38340964efd9ac4969995a3a8970795365b44239 Mon Sep 17 00:00:00 2001 From: Frank Hoffmann <15r10nk-git@polarbit.de> Date: Mon, 25 Mar 2024 20:13:31 +0100 Subject: [PATCH] refactor: use _get_changes api for CollectionValue --- inline_snapshot/_inline_snapshot.py | 37 +++++++++++++++++++++++++++++ tests/test_inline_snapshot.py | 2 +- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/inline_snapshot/_inline_snapshot.py b/inline_snapshot/_inline_snapshot.py index c7090c23..58be23fb 100644 --- a/inline_snapshot/_inline_snapshot.py +++ b/inline_snapshot/_inline_snapshot.py @@ -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]" diff --git a/tests/test_inline_snapshot.py b/tests/test_inline_snapshot.py index 483d3ad8..24324e72 100644 --- a/tests/test_inline_snapshot.py +++ b/tests/test_inline_snapshot.py @@ -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(