Skip to content

Commit

Permalink
Use MarshalIndent for better readability (#9)
Browse files Browse the repository at this point in the history
## Summary

<!-- Describe your changes in detail here, if it closes an open issue,
include "Closes #<issue>" -->
  • Loading branch information
FollowTheProcess authored Nov 19, 2024
1 parent 3622259 commit 05aacc5
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ The files will be named automatically after the test:
Because of this, it needs to know how to serialise your value (which could be basically any valid construct in Go) to plain text, so we follow a few basic rules in priority order:

- **`snapshot.Snapper`:** If your type implements the `Snapper` interface, this is preferred over all other potential serialisation, this allows you to have total control over how your type is snapshotted, do whatever you like in the `Snap` method, just return a `[]byte` that you'd like to look at in the snapshot and thats it!
- **[json.Marshaler]:** If your type implements [json.Marshaler], this will be used and the snapshot will be a valid JSON file
- **[json.Marshaler]:** If your type implements [json.Marshaler], this will be used and the snapshot will be a valid JSON file (using MarshalIndent for readability)
- **[encoding.TextMarshaler]:** If your type implements [encoding.TextMarshaler], this will be used to render your value to the snapshot
- **[fmt.Stringer]:** If your type implements the [fmt.Stringer] interface, this is then used instead
- **Primitive Types:** Any primitive type in Go (`bool`, `int`, `string` etc.) is serialised according to the `%v` verb in the [fmt] package
Expand Down
6 changes: 5 additions & 1 deletion snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ func (s *Shotter) Snap(value any) {
}
current.Write(content)
case json.Marshaler:
content, err := val.MarshalJSON()
// TODO(@FollowTheProcess): If it's JSON, I'd like to make the file extension JSON
// for better editor support etc.

// Use MarshalIndent for better readability
content, err := json.MarshalIndent(val, "", " ")
if err != nil {
s.tb.Fatalf("%T implements json.Marshaler but MarshalJSON() returned an error: %v", val, err)
return
Expand Down
2 changes: 1 addition & 1 deletion snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func TestSnap(t *testing.T) {
name: "json marshaler",
value: jsonMarshaler{},
wantFail: false,
existingSnap: `{"key": "value"}`,
existingSnap: "{\n \"key\": \"value\"\n}",
},
{
name: "json marshaler error",
Expand Down
4 changes: 3 additions & 1 deletion testdata/snapshots/TestSnap/json_marshaler.snap.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
{"key": "value"}
{
"key": "value"
}

0 comments on commit 05aacc5

Please sign in to comment.