From 4491eb54350b2442d066d13bbb1fb26d1b30b047 Mon Sep 17 00:00:00 2001 From: Emmanuel Ogbizi-Ugbe Date: Thu, 9 Jul 2020 09:08:13 -0400 Subject: [PATCH 1/2] test: add tests to show failing cases --- tests/__snapshots__/test_extension_amber.ambr | 40 +++++++++++++++++-- tests/test_extension_amber.py | 16 +++++++- 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/tests/__snapshots__/test_extension_amber.ambr b/tests/__snapshots__/test_extension_amber.ambr index 014a7673..63596fa3 100644 --- a/tests/__snapshots__/test_extension_amber.ambr +++ b/tests/__snapshots__/test_extension_amber.ambr @@ -155,6 +155,10 @@ ], } --- +# name: test_dict[actual3] + { + } +--- # name: test_doubly_parametrized[bar-foo] 'foo' --- @@ -167,7 +171,27 @@ # name: test_empty_snapshot.1 '' --- -# name: test_list +# name: test_list[actual0] + [ + ] +--- +# name: test_list[actual1] + [ + 'this', + 'is', + 'a', + 'list', + ] +--- +# name: test_list[actual2] + [ + 'contains', + 'empty', + [ + ], + ] +--- +# name: test_list[actual3] [ 1, 2, @@ -202,14 +226,14 @@ --- # name: test_newline_control_characters.1 ' - line 1 + line 1 line 2 ' --- # name: test_newline_control_characters.2 ' - line 1 - line 2 + line 1 + line 2 ' --- @@ -294,6 +318,10 @@ ), } --- +# name: test_set[actual4] + { + } +--- # name: test_string[0] '' --- @@ -356,3 +384,7 @@ }, ) --- +# name: test_tuple.2 + ( + ) +--- diff --git a/tests/test_extension_amber.py b/tests/test_extension_amber.py index c772de3b..401025b9 100644 --- a/tests/test_extension_amber.py +++ b/tests/test_extension_amber.py @@ -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( @@ -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): @@ -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): @@ -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] From 84c0be9fd6e91db989f149b47aeba529eb819f92 Mon Sep 17 00:00:00 2001 From: Emmanuel Ogbizi-Ugbe Date: Thu, 9 Jul 2020 09:49:13 -0400 Subject: [PATCH 2/2] fix: do not include new line when iterable is empty --- src/syrupy/extensions/amber/serializer.py | 4 +++- tests/__snapshots__/test_extension_amber.ambr | 6 +++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/syrupy/extensions/amber/serializer.py b/src/syrupy/extensions/amber/serializer.py index c34ad460..f0795bb3 100644 --- a/src/syrupy/extensions/amber/serializer.py +++ b/src/syrupy/extensions/amber/serializer.py @@ -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)}" diff --git a/tests/__snapshots__/test_extension_amber.ambr b/tests/__snapshots__/test_extension_amber.ambr index 63596fa3..6486a07e 100644 --- a/tests/__snapshots__/test_extension_amber.ambr +++ b/tests/__snapshots__/test_extension_amber.ambr @@ -226,14 +226,14 @@ --- # name: test_newline_control_characters.1 ' - line 1 + line 1 line 2 ' --- # name: test_newline_control_characters.2 ' - line 1 - line 2 + line 1 + line 2 ' ---