Skip to content

Commit 2e29acf

Browse files
Add a TextFormatter (#50)
# Summary <!-- Describe your changes in detail here, if it closes an open issue, include "Closes #<issue>" -->
1 parent 143aadb commit 2e29acf

File tree

17 files changed

+152
-5
lines changed

17 files changed

+152
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ expression: value.Show() # The expression that generated the snapshot
9292
This format was inspired by [insta], a popular snapshot testing library in rust
9393

9494
> [!TIP]
95-
> If you want a different format, you can implement your own! Just implement the `snapshot.Formatter` interface and pass it in
95+
> If you want a different format, there is also a `TextFormatter` or you can implement your own! Just implement the `snapshot.Formatter` interface and pass it in
9696
> with the `snapshot.WithFormatter` option and you're away!
9797
9898
### 🔄 Automatic Updating

formatter.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
11
package snapshot
22

3+
import (
4+
"go.followtheprocess.codes/snapshot/internal/format/insta"
5+
"go.followtheprocess.codes/snapshot/internal/format/text"
6+
)
7+
38
// Formatter is an interface describing something capable of producing a snapshot.
49
type Formatter interface {
510
// Format returns a formatted version of 'value' as a raw byte slice, these
611
// bytes are interpreted as the snapshot and will be written and read from disk
712
// during snapshot comparisons.
813
Format(value any) ([]byte, error)
914
}
15+
16+
// InstaFormatter returns a [Formatter] that produces snapshots in the [insta]
17+
// yaml format.
18+
//
19+
// It takes a description for the snapshot.
20+
//
21+
// [insta]: https://crates.io/crates/insta
22+
func InstaFormatter(description string) Formatter {
23+
return insta.NewFormatter(description)
24+
}
25+
26+
// TextFormatter returns a [Formatter] that produces snapshots by simply
27+
// dumping the value as plain text.
28+
func TextFormatter() Formatter {
29+
return text.NewFormatter()
30+
}

internal/format/insta/insta.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (s Snapshot) save(w io.Writer) error {
6363
return nil
6464
}
6565

66-
// Formatter implements the [format.Formatter] interface and returns an
66+
// Formatter implements the [snapshot.Formatter] interface and returns an
6767
// insta-compatible snapshot format.
6868
type Formatter struct {
6969
description string

internal/format/insta/insta_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func TestFormatter(t *testing.T) {
3737

3838
for _, tt := range tests {
3939
t.Run(tt.name, func(t *testing.T) {
40-
path := filepath.Join("testdata", "TestSave", tt.name+".snap")
40+
path := filepath.Join("testdata", "TestFormatter", tt.name+".snap")
4141

4242
want, err := os.ReadFile(path)
4343
test.Ok(t, err)
File renamed without changes.
File renamed without changes.

internal/format/insta/testdata/TestSave/string.snap renamed to internal/format/insta/testdata/TestFormatter/string.snap

File renamed without changes.

internal/format/insta/testdata/TestSave/struct.snap renamed to internal/format/insta/testdata/TestFormatter/struct.snap

File renamed without changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<nil>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
42

0 commit comments

Comments
 (0)