diff --git a/approvals.go b/approvals.go index edcdf70..9525055 100644 --- a/approvals.go +++ b/approvals.go @@ -286,6 +286,15 @@ func (v verifyOptions) WithRegexScrubber(scrubber *regexp.Regexp, replacer strin return v } +// WithCustomScrubber allows you to 'scrub' dynamic data such as timestamps within your test input +// using a custom function and replace it with a static placeholder +func (v verifyOptions) WithCustomScrubber(scrubFunc func(s, r string) string, replacer string) verifyOptions { + v.scrubbers = append(v.scrubbers, func(s string) string { + return scrubFunc(s, replacer) + }) + return v +} + // WithExtension overrides the default file extension (.txt) for approval files. func (v verifyOptions) WithExtension(extension string) verifyOptions { v.extWithDot = extension diff --git a/scrubber_test.go b/scrubber_test.go index b94beb4..6ae231c 100644 --- a/scrubber_test.go +++ b/scrubber_test.go @@ -86,3 +86,14 @@ func TestVerifyAllWithRegexScrubber(t *testing.T) { xs := []string{"Christopher", "Llewellyn"} approvals.VerifyAll(t, "uppercase", xs, func(x interface{}) string { return fmt.Sprintf("%s => %s", x, strings.ToUpper(x.(string))) }, opts) } + +func scrubFunc(s, replacer string) string { + return regexp.MustCompile("Hello").ReplaceAllString(s, replacer) +} + +func TestVerifyJSONBytesWithCustomScrubber(t *testing.T) { + opts := approvals.Options().WithCustomScrubber(scrubFunc, "Hi") + + jb := []byte("{ \"Greeting\": \"Hello\" }") + approvals.VerifyJSONBytes(t, jb, opts) +}