Skip to content

Commit

Permalink
add SnapshotJSON helper
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Oct 26, 2023
1 parent f4ec4ba commit 43c129b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 8 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions .got/snapshots/TestSnapshots/d.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"Val": 20
}
46 changes: 40 additions & 6 deletions snapshots.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package got

import (
"encoding/json"
"os"
"path/filepath"
"regexp"
Expand All @@ -9,7 +10,8 @@ import (
"github.com/ysmood/gop"
)

const snapshotExt = ".got-snap"
const snapshotGopExt = ".gop"
const snapshotJSONExt = ".json"

type snapshot struct {
value string
Expand All @@ -21,9 +23,14 @@ func (g G) snapshotsDir() string {
}

func (g G) loadSnapshots() {
paths, err := filepath.Glob(filepath.Join(g.snapshotsDir(), "*"+snapshotExt))
paths, err := filepath.Glob(filepath.Join(g.snapshotsDir(), "*"+snapshotGopExt))
g.E(err)

jsonPaths, err := filepath.Glob(filepath.Join(g.snapshotsDir(), "*"+snapshotJSONExt))
g.E(err)

paths = append(paths, jsonPaths...)

for _, path := range paths {
g.snapshots.Store(path, snapshot{g.Read(path).String(), false})
}
Expand All @@ -39,18 +46,45 @@ func (g G) loadSnapshots() {
})
}

// Snapshot asserts that x equals the snapshot with the specified name, name should be unique under the same test.
// Snapshot asserts that x equals the snapshot with the specified name, name should be unique under the same test case.
// It will create a new snapshot file if the name is not found.
// The snapshot file will be saved to ".got/snapshots/{TEST_NAME}/{name}.got-snap".
// The snapshot file will be saved to ".got/snapshots/{TEST_NAME}".
// To update the snapshot, just change the name of the snapshot or remove the corresponding snapshot file.
// It will auto-remove the unused snapshot files after the test.
// The snapshot files should be version controlled.
// The format of the snapshot file is the output of [gop.Plain].
func (g G) Snapshot(name string, x interface{}) {
g.Helper()
g.snapshot(name, x, false)
}

// SnapshotJSON is similar to [G.Snapshot], but it will convert x to JSON string before comparing.
// The format of the snapshot file is json.
func (g G) SnapshotJSON(name string, x interface{}) {
g.Helper()
g.snapshot(name, x, true)
}

func (g G) snapshot(name string, x interface{}, jsonType bool) {
g.Helper()

path := filepath.Join(g.snapshotsDir(), escapeFileName(name)+snapshotExt)
var ext string
if jsonType {
ext = snapshotJSONExt
} else {
ext = snapshotGopExt
}

path := filepath.Join(g.snapshotsDir(), escapeFileName(name)+ext)

xs := gop.Plain(x)
var xs string
if jsonType {
b, err := json.MarshalIndent(x, "", " ")
g.E(err)
xs = string(b)
} else {
xs = gop.Plain(x)
}

if data, ok := g.snapshots.Load(path); ok {
s := data.(snapshot)
Expand Down
5 changes: 3 additions & 2 deletions snapshots_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestSnapshots(t *testing.T) {
g.Snapshot("a", "ok")
g.Snapshot("b", 1)
g.Snapshot("c", C{10})
g.SnapshotJSON("d", C{20})

g.Run("sub", func(g got.G) {
g.Snapshot("d", "ok")
Expand All @@ -40,7 +41,7 @@ func TestSnapshots(t *testing.T) {
}

func TestSnapshotsCreate(t *testing.T) {
path := filepath.FromSlash(".got/snapshots/TestSnapshotsCreate/a.got-snap")
path := filepath.FromSlash(".got/snapshots/TestSnapshotsCreate/a.gop")
err := os.RemoveAll(path)
if err != nil {
panic(err)
Expand All @@ -56,7 +57,7 @@ func TestSnapshotsCreate(t *testing.T) {
}

func TestSnapshotsNotUsed(t *testing.T) {
path := filepath.FromSlash(".got/snapshots/TestSnapshotsNotUsed/a.got-snap")
path := filepath.FromSlash(".got/snapshots/TestSnapshotsNotUsed/a.gop")

g := got.T(t)
g.WriteFile(path, []byte(`1`))
Expand Down

0 comments on commit 43c129b

Please sign in to comment.