Add the ability to supply a formatter for strings #29
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Context
When receiving a string value to
expect
, the matcher previouslyassumed 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 stringvalues 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 itinto 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 thematch_snapshot
call as needed.
For the formatter itself, I opted to rely on
call
instead of anothermethod name like
format
, and for it to be passed as an object insteadof 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.