From d731fb322e9d3aabd1aad55c8951ee935659af19 Mon Sep 17 00:00:00 2001 From: Noah Date: Fri, 17 Apr 2020 10:10:03 -0400 Subject: [PATCH] feat(amber): indent multiline strings, close #193 (#194) --- src/syrupy/extensions/amber.py | 5 ++++- tests/__snapshots__/test_extension_amber.ambr | 17 ++++++++++++++--- tests/test_extension_amber.py | 8 +++++++- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/syrupy/extensions/amber.py b/src/syrupy/extensions/amber.py index 0ebae2748..b67f560a1 100644 --- a/src/syrupy/extensions/amber.py +++ b/src/syrupy/extensions/amber.py @@ -103,7 +103,10 @@ def serialize_string( if "\n" in data: return ( cls.with_indent("'\n", depth) - + str(data) + + "".join( + cls.with_indent(line, depth + 1 if depth else depth) + for line in str(data).splitlines(keepends=True) + ) + "\n" + cls.with_indent("'", depth) ) diff --git a/tests/__snapshots__/test_extension_amber.ambr b/tests/__snapshots__/test_extension_amber.ambr index 7ab6c8993..c41f354bc 100644 --- a/tests/__snapshots__/test_extension_amber.ambr +++ b/tests/__snapshots__/test_extension_amber.ambr @@ -76,6 +76,17 @@ 'd': ..., } --- +# name: test_deeply_nested_multiline_string_in_dict + { + 'value_a': { + 'value_b': ' + line 1 + line 2 + line 3 + ', + }, + } +--- # name: test_dict[actual0] { 'a': { @@ -139,11 +150,11 @@ }, ] --- -# name: test_list_in_dict +# name: test_multiline_string_in_dict { 'value': ' - line 1 - line 2 + line 1 + line 2 ', } --- diff --git a/tests/test_extension_amber.py b/tests/test_extension_amber.py index e6269dfde..e17f8fd2d 100644 --- a/tests/test_extension_amber.py +++ b/tests/test_extension_amber.py @@ -23,11 +23,17 @@ def test_newline_control_characters(snapshot): assert snapshot == "line 1\r\nline 2\r\n" -def test_list_in_dict(snapshot): +def test_multiline_string_in_dict(snapshot): lines = "\n".join(["line 1", "line 2"]) assert {"value": lines} == snapshot +def test_deeply_nested_multiline_string_in_dict(snapshot): + lines = "\n".join(["line 1", "line 2", "line 3"]) + d = {"value_a": {"value_b": lines}} + assert d == snapshot + + @pytest.mark.parametrize("actual", [False, True]) def test_bool(actual, snapshot): assert actual == snapshot