diff --git a/snapshotx/snapshot.go b/snapshotx/snapshot.go index 56071444..5cde9583 100644 --- a/snapshotx/snapshot.go +++ b/snapshotx/snapshot.go @@ -4,6 +4,7 @@ package snapshotx import ( + "bytes" "encoding/json" "fmt" "strings" @@ -25,6 +26,7 @@ type ( } exceptPaths []string exceptNestedKeys []string + replacement struct{ str, replacement string } ) func (e exceptPaths) apply(t *testing.T, raw []byte) []byte { @@ -42,6 +44,10 @@ func (e exceptNestedKeys) apply(t *testing.T, raw []byte) []byte { return deleteMatches(t, "", parsed, e, []string{}, raw) } +func (r *replacement) apply(_ *testing.T, raw []byte) []byte { + return bytes.ReplaceAll(raw, []byte(r.str), []byte(r.replacement)) +} + func ExceptPaths(keys ...string) ExceptOpt { return exceptPaths(keys) } @@ -50,6 +56,10 @@ func ExceptNestedKeys(nestedKeys ...string) ExceptOpt { return exceptNestedKeys(nestedKeys) } +func WithReplacement(str, replace string) ExceptOpt { + return &replacement{str: str, replacement: replace} +} + func SnapshotTJSON(t *testing.T, compare []byte, except ...ExceptOpt) { t.Helper() for _, e := range except {