syrusakbary / snapshottest

Snapshot Testing utils for Python 📸
MIT License
525 stars 102 forks source link

Consider formatting multiline strings differently #164

Open RazerM opened 1 year ago

RazerM commented 1 year ago

Here's an example of how the snapshot is currently formatted:

snapshots['test_foo 1'] = '''ID                                   Name         Description
------------------------------------ ------------ -------------------------------
32fd8c27-9110-463f-89c0-eb676814923a Item 1       An item with a long description
32fd8c27-9110-463f-89c0-eb676814923a Item 2
32fd8c27-9110-463f-89c0-eb676814923a Another item
'''

I think the following is better for reviewing the snapshots, and is the exact same string:

snapshots['test_foo 1'] = '''\
ID                                   Name         Description
------------------------------------ ------------ -------------------------------
32fd8c27-9110-463f-89c0-eb676814923a Item 1       An item with a long description
32fd8c27-9110-463f-89c0-eb676814923a Item 2
32fd8c27-9110-463f-89c0-eb676814923a Another item
'''

I can provide a PR if this would be accepted

In the meantime, I'm using

from snapshottest.formatter import Formatter
from snapshottest.formatters import TypeFormatter, format_str

def format_str_custom(value, indent, formatter):
    formatted = format_str(value, indent, formatter)
    return re.sub(r"^(['\"]{3})(.*)", r"\1\\\n\2", formatted)

Formatter.register_formatter(TypeFormatter((str,), format_str_custom))