Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(amber): Do not add empty line to empty iterables #287

Merged
merged 2 commits into from
Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/syrupy/extensions/amber/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ def __serialize_lines(
include_type: bool = True,
ends: str = "\n",
) -> str:
lines = ends.join(lines)
lines_end = "\n" if lines else ""
return (
f"{cls.with_indent(cls.object_type(data), depth)} " if include_type else ""
) + f"{open_tag}\n{ends.join(lines)}\n{cls.with_indent(close_tag, depth)}"
) + f"{open_tag}\n{lines}{lines_end}{cls.with_indent(close_tag, depth)}"
34 changes: 33 additions & 1 deletion tests/__snapshots__/test_extension_amber.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@
],
}
---
# name: test_dict[actual3]
<class 'dict'> {
}
---
# name: test_doubly_parametrized[bar-foo]
'foo'
---
Expand All @@ -167,7 +171,27 @@
# name: test_empty_snapshot.1
''
---
# name: test_list
# name: test_list[actual0]
<class 'list'> [
]
---
# name: test_list[actual1]
<class 'list'> [
'this',
'is',
'a',
'list',
]
---
# name: test_list[actual2]
<class 'list'> [
'contains',
'empty',
<class 'list'> [
],
]
---
# name: test_list[actual3]
<class 'list'> [
1,
2,
Expand Down Expand Up @@ -294,6 +318,10 @@
),
}
---
# name: test_set[actual4]
<class 'set'> {
}
---
# name: test_string[0]
''
---
Expand Down Expand Up @@ -356,3 +384,7 @@
},
)
---
# name: test_tuple.2
<class 'tuple'> (
)
---
16 changes: 14 additions & 2 deletions tests/test_extension_amber.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def test_multiple_snapshots(snapshot):
def test_tuple(snapshot):
assert snapshot == ("this", "is", ("a", "tuple"))
assert snapshot == ExampleTuple(a="this", b="is", c="a", d={"named", "tuple"})
assert snapshot == ()


@pytest.mark.parametrize(
Expand All @@ -85,6 +86,7 @@ def test_tuple(snapshot):
{"contains", "frozen", frozenset({"1", "2"})},
{"contains", "tuple", (1, 2)},
{"contains", "namedtuple", ExampleTuple(a=1, b=2, c=3, d=4)},
set(),
],
)
def test_set(snapshot, actual):
Expand All @@ -102,6 +104,7 @@ def test_set(snapshot, actual):
frozenset({"1", "2"}): ["1", 2],
ExampleTuple(a=1, b=2, c=3, d=4): {"e": False},
},
{},
],
)
def test_dict(snapshot, actual):
Expand All @@ -114,8 +117,17 @@ def test_numbers(snapshot):
assert snapshot == 2 / 6


def test_list(snapshot):
assert snapshot == [1, 2, "string", {"key": "value"}]
@pytest.mark.parametrize(
"actual",
[
[],
["this", "is", "a", "list"],
["contains", "empty", []],
[1, 2, "string", {"key": "value"}],
],
)
def test_list(snapshot, actual):
assert actual == snapshot


list_cycle = [1, 2, 3]
Expand Down