Skip to content

Commit

Permalink
Merge pull request #149 from 15r10nk/fix_148
Browse files Browse the repository at this point in the history
fix: solved a bug caused by a variable inside a snapshot #148
  • Loading branch information
15r10nk authored Dec 10, 2024
2 parents c3a7a3c + c2c585b commit 066c730
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 35 deletions.
40 changes: 40 additions & 0 deletions changelog.d/20241210_204219_15r10nk-git_fix_148.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!--
A new scriv changelog fragment.
Uncomment the section that is right (remove the HTML comment wrapper).
-->

<!--
### Removed
- A bullet item for the Removed category.
-->
<!--
### Added
- A bullet item for the Added category.
-->
<!--
### Changed
- A bullet item for the Changed category.
-->
<!--
### Deprecated
- A bullet item for the Deprecated category.
-->
### Fixed

- solved a bug caused by a variable inside a snapshot (#148)

<!--
### Security
- A bullet item for the Security category.
-->
7 changes: 7 additions & 0 deletions src/inline_snapshot/_adapter/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ def get_adapter(self, old_value, new_value) -> Adapter:
def assign(self, old_value, old_node, new_value):
raise NotImplementedError(cls)

def value_assign(self, old_value, old_node, new_value):
from .value_adapter import ValueAdapter

adapter = ValueAdapter(self.context)
result = yield from adapter.assign(old_value, old_node, new_value)
return result

@classmethod
def map(cls, value, map_function):
raise NotImplementedError(cls)
Expand Down
7 changes: 5 additions & 2 deletions src/inline_snapshot/_adapter/dict_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@ def items(self, value, node):

def assign(self, old_value, old_node, new_value):
if old_node is not None:
assert isinstance(old_node, ast.Dict)
assert len(old_value) == len(old_node.keys)
if not (
isinstance(old_node, ast.Dict) and len(old_value) == len(old_node.keys)
):
result = yield from self.value_assign(old_value, old_node, new_value)
return result

for key, value in zip(old_node.keys, old_node.values):
if key is None:
Expand Down
12 changes: 3 additions & 9 deletions src/inline_snapshot/_adapter/generic_call_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
from dataclasses import MISSING
from typing import Any

from inline_snapshot._adapter.value_adapter import ValueAdapter

from .._change import CallArg
from .._change import Delete
from ..syntax_warnings import InlineSnapshotSyntaxWarning
Expand Down Expand Up @@ -89,13 +87,9 @@ def items(self, value, node):
]

def assign(self, old_value, old_node, new_value):
if old_node is None:
value = yield from ValueAdapter(self.context).assign(
old_value, old_node, new_value
)
return value

assert isinstance(old_node, ast.Call)
if old_node is None or not isinstance(old_node, ast.Call):
result = yield from self.value_assign(old_value, old_node, new_value)
return result

# positional arguments
for pos_arg in old_node.args:
Expand Down
6 changes: 4 additions & 2 deletions src/inline_snapshot/_adapter/sequence_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ def items(self, value, node):

def assign(self, old_value, old_node, new_value):
if old_node is not None:
assert isinstance(
if not isinstance(
old_node, ast.List if isinstance(old_value, list) else ast.Tuple
)
):
result = yield from self.value_assign(old_value, old_node, new_value)
return result

for e in old_node.elts:
if isinstance(e, ast.Starred):
Expand Down
2 changes: 2 additions & 0 deletions src/inline_snapshot/testing/_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ def run_inline(
assert raises == f"{type(raised_exception).__name__}:\n" + str(
raised_exception
)
else:
assert raises == None

if changed_files is not None:
current_files = {}
Expand Down
43 changes: 37 additions & 6 deletions tests/adapter/test_dataclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,7 @@ def test_something():
"""
}
),
raises=snapshot(
"""\
AssertionError:
not equal\
"""
),
raises=snapshot(None),
)


Expand Down Expand Up @@ -490,3 +485,39 @@ class container(BaseModel):
}
),
).run_inline()


def test_dataclass_var():

Example(
"""\
from inline_snapshot import snapshot,Is
from dataclasses import dataclass,field
@dataclass
class container:
a: int
def test_list():
l=container(5)
assert l == snapshot(l), "not equal"
"""
).run_inline(
["--inline-snapshot=update"],
changed_files=snapshot(
{
"test_something.py": """\
from inline_snapshot import snapshot,Is
from dataclasses import dataclass,field
@dataclass
class container:
a: int
def test_list():
l=container(5)
assert l == snapshot(container(a=5)), "not equal"
"""
}
),
)
28 changes: 28 additions & 0 deletions tests/adapter/test_dict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from inline_snapshot import snapshot
from inline_snapshot.testing import Example


def test_dict_var():

Example(
"""\
from inline_snapshot import snapshot,Is
def test_list():
l={1:2}
assert l == snapshot(l), "not equal"
"""
).run_inline(
["--inline-snapshot=update"],
changed_files=snapshot(
{
"test_something.py": """\
from inline_snapshot import snapshot,Is
def test_list():
l={1:2}
assert l == snapshot({1: 2}), "not equal"
"""
}
),
)
43 changes: 27 additions & 16 deletions tests/adapter/test_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@ def test_list():
"""
}
),
raises=snapshot(
"""\
AssertionError:
not equal\
"""
),
raises=snapshot(None),
)


Expand Down Expand Up @@ -62,12 +57,7 @@ def test_list():
"""
}
),
raises=snapshot(
"""\
AssertionError:
not equal\
"""
),
raises=snapshot(None),
)


Expand All @@ -85,10 +75,31 @@ def test_list():
"""
).run_inline(
changed_files=snapshot({}),
raises=snapshot(
"""\
AssertionError:
not equal\
raises=snapshot(None),
)


def test_list_var():

Example(
"""\
from inline_snapshot import snapshot,Is
def test_list():
l=[1]
assert l == snapshot(l), "not equal"
"""
).run_inline(
["--inline-snapshot=update"],
changed_files=snapshot(
{
"test_something.py": """\
from inline_snapshot import snapshot,Is
def test_list():
l=[1]
assert l == snapshot([1]), "not equal"
"""
}
),
)

0 comments on commit 066c730

Please sign in to comment.