Skip to content

Commit

Permalink
feat(amber): indent multiline strings, close #193 (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
Noah authored and noahnu committed Apr 18, 2020
1 parent fad9e90 commit 8e6a522
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/syrupy/extensions/amber.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
Expand Down
17 changes: 14 additions & 3 deletions tests/__snapshots__/test_extension_amber.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@
'd': ...,
}
---
# name: test_deeply_nested_multiline_string_in_dict
<class 'dict'> {
'value_a': <class 'dict'> {
'value_b': '
line 1
line 2
line 3
',
},
}
---
# name: test_dict[actual0]
<class 'dict'> {
'a': <class 'dict'> {
Expand Down Expand Up @@ -139,11 +150,11 @@
},
]
---
# name: test_list_in_dict
# name: test_multiline_string_in_dict
<class 'dict'> {
'value': '
line 1
line 2
line 1
line 2
',
}
---
Expand Down
8 changes: 7 additions & 1 deletion tests/test_extension_amber.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8e6a522

Please sign in to comment.