Skip to content

Commit

Permalink
feat: add WithReplacement except opt to snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
hperl committed Mar 21, 2024
1 parent 6511eb1 commit 7a286d3
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions snapshotx/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package snapshotx

import (
"bytes"
"encoding/json"
"fmt"
"strings"
Expand All @@ -25,6 +26,7 @@ type (
}
exceptPaths []string
exceptNestedKeys []string
replacement struct{ str, replacement string }
)

func (e exceptPaths) apply(t *testing.T, raw []byte) []byte {
Expand All @@ -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)
}
Expand All @@ -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 {
Expand Down

0 comments on commit 7a286d3

Please sign in to comment.