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

Add the ability to supply a formatter for strings #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

lukearndt
Copy link

@lukearndt lukearndt commented Jul 21, 2021

Context

When receiving a string value to expect, the matcher previously
assumed that it was already in human-readable format and did not
serialize it or apply any additional formatting.

This was problematic for string-based HTML values, because they might
not be easily readable in their raw form.

Human-readable snapshots are a lot easier to maintain, and they produce
better errors when they fail.

Changes

This commit introduces the ability to format the value passed into
expect prior to serializing it and running the comparison. Only string
values will be formatted; other types need to be serialized instead.

The config options have been expanded to accept a formatter at both a
global and an example level. This follows the existing config pattern
defined by the serializer for non-string values.

Considerations

This started out as #28.

While it could be nice to include a default formatter for HTML, that
would be a risky thing to do because the gem can't accurately discern
between HTML strings and other formats. The default case is therefore to
take the string as-is, and not try to apply any formatting.

Some test suites might only use snapshot testing for their HTML
documents. When that's the case, it's more convenient to set the global
snapshot_formatter option on the RSpec configuration than to pass it
into every match_snapshot call.

On the other hand, there are probably cases where people want snapshot
tests for strings that are in a variety of different formats. They can
achieve that by passing a snapshot_formatter into the match_snapshot
call as needed.

For the formatter itself, I opted to rely on call instead of another
method name like format, and for it to be passed as an object instead
of a class. This allows us to define formatters as procs or lambdas,
which I think makes sense becaues they just take a string and return
a formatted one.

The build fails!

The build currently fails because I haven't updated the tests.

If we reach a consensus about the actual functionality, I'll go ahead
and make the build pass so that we can merge the PR.

@lukearndt lukearndt force-pushed the add-support-for-string-formatter branch from 976ab2b to a0597fc Compare July 21, 2021 01:01
Copy link

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant