approvals / go-approval-tests

Apache License 2.0
86 stars 22 forks source link

verifyOptions.WithCustomScrubber: ability to pass a custom scrubber func #43

Open bardzusny opened 2 years ago

bardzusny commented 2 years ago

A proof-of-concept PR implementing ability to pass a custom scrubbing function.

It received the verified string and a replacer, returns a scrubbed verified string.

Seems like a legit shortcut to make scrubbing API much more flexible. Got this idea when working on https://github.com/approvals/go-approval-tests/pull/42 .

I'd appreciate a check on the public API more than anything, and if the idea seems valid overall. Then we can work out the details (+ things like the preferred commit notation/format).

Description

Usage examples:

// replaces "Hello" with the replacer string:
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)
}