diff --git a/v2/README.md b/v2/README.md index 0e51542..a15815d 100644 --- a/v2/README.md +++ b/v2/README.md @@ -136,6 +136,7 @@ func TestNewExample(t *testing.T) { | `WithNameSuffix` | Suffix for fixture files. | `.golden` | `WithDirPerms` | Directory permissions for fixtures | `0755` | `WithFilePerms` | File permissions for fixtures | `0644` +| `WithEqualFn` | Custom equal logic to be used | None | `WithDiffEngine` | Diff engine to use for diff output | `ClassicDiff` | `WithDiffFn` | Custom diff logic to be used | None | `WithIgnoreTemplateErrors` | Ignore errors from templates | `false` diff --git a/v2/assertions.go b/v2/assertions.go index ab3901d..d9b931c 100644 --- a/v2/assertions.go +++ b/v2/assertions.go @@ -156,7 +156,7 @@ func (g *Goldie) compare(t *testing.T, name string, actualData []byte) error { return fmt.Errorf("expected %s to be nil", err.Error()) } - if !bytes.Equal(actualData, expectedData) { + if !g.equal(actualData, expectedData) { msg := "Result did not match the golden fixture. Diff is below:\n\n" actual := string(actualData) expected := string(expectedData) @@ -202,7 +202,7 @@ func (g *Goldie) compareTemplate(t *testing.T, name string, data interface{}, ac return newErrMissingKey(fmt.Sprintf("Template error: %s", err.Error())) } - if !bytes.Equal(actualData, expectedData.Bytes()) { + if !g.equal(actualData, expectedData.Bytes()) { msg := "Result did not match the golden fixture. Diff is below:\n\n" actual := string(actualData) expected := expectedData.String() @@ -218,3 +218,10 @@ func (g *Goldie) compareTemplate(t *testing.T, name string, data interface{}, ac return nil } + +func (g *Goldie) equal(actual, expected []byte) bool { + if g.equalFn != nil { + return g.equalFn(actual, expected) + } + return bytes.Equal(actual, expected) +} diff --git a/v2/assertions_test.go b/v2/assertions_test.go index 508b206..a4c70c1 100644 --- a/v2/assertions_test.go +++ b/v2/assertions_test.go @@ -14,6 +14,7 @@ func TestCompare(t *testing.T) { actualData []byte expectedData []byte update bool + equalfn EqualFn err error }{ { @@ -21,6 +22,7 @@ func TestCompare(t *testing.T) { actualData: []byte("abc"), expectedData: []byte("abc"), update: true, + equalfn: nil, err: nil, }, { @@ -28,6 +30,7 @@ func TestCompare(t *testing.T) { actualData: []byte("abc"), expectedData: []byte("abc"), update: false, + equalfn: nil, err: &errFixtureNotFound{}, }, { @@ -35,13 +38,25 @@ func TestCompare(t *testing.T) { actualData: []byte("bc"), expectedData: []byte("abc"), update: true, + equalfn: nil, err: &errFixtureMismatch{}, }, + { + name: "custom equalfn", + actualData: []byte("ab"), + expectedData: []byte("abc"), + update: true, + equalfn: func(actual, expected []byte) bool { + return actual[0] == expected[0] + }, + err: nil, + }, { name: "nil", actualData: nil, expectedData: nil, update: true, + equalfn: nil, err: nil, }, } @@ -54,6 +69,7 @@ func TestCompare(t *testing.T) { assert.Nil(t, err) } + g.equalFn = test.equalfn err := g.compare(t, test.name, test.actualData) assert.IsType(t, test.err, err) diff --git a/v2/goldie.go b/v2/goldie.go index 78e3f27..f9a4c15 100644 --- a/v2/goldie.go +++ b/v2/goldie.go @@ -80,6 +80,7 @@ type Goldie struct { filePerms os.FileMode dirPerms os.FileMode + equalFn EqualFn diffEngine DiffEngine diffFn DiffFn ignoreTemplateErrors bool diff --git a/v2/interface.go b/v2/interface.go index 8f6a378..0f7017a 100644 --- a/v2/interface.go +++ b/v2/interface.go @@ -23,6 +23,9 @@ type Tester interface { GoldenFileName(t *testing.T, name string) string } +// EqualFn compares if actual and expected are equal. +type EqualFn func(actual []byte, expected []byte) bool + // DiffFn takes in an actual and expected and will return a diff string // representing the differences between the two. type DiffFn func(actual string, expected string) string @@ -68,6 +71,7 @@ type OptionProcessor interface { WithFilePerms(mode os.FileMode) error WithDirPerms(mode os.FileMode) error + WithEqualFn(fn EqualFn) error WithDiffEngine(engine DiffEngine) error WithDiffFn(fn DiffFn) error WithIgnoreTemplateErrors(ignoreErrors bool) error @@ -117,6 +121,15 @@ func WithDirPerms(mode os.FileMode) Option { } } +// WithEqualFn sets the customized equality comapre function that implements +// the EqualFn signature. +//noinspection GoUnusedExportedFunction +func WithEqualFn(fn EqualFn) Option { + return func(o OptionProcessor) error { + return o.WithEqualFn(fn) + } +} + // WithDiffEngine sets the `diff` engine that will be used to generate the // `diff` text. // diff --git a/v2/options.go b/v2/options.go index 8f30b64..faff6b4 100644 --- a/v2/options.go +++ b/v2/options.go @@ -36,6 +36,13 @@ func (g *Goldie) WithDirPerms(mode os.FileMode) error { return nil } +// WithEqualFn sets the customized equality comapre function that implements +// the EqualFn signature. +func (g *Goldie) WithEqualFn(fn EqualFn) error { + g.equalFn = fn + return nil +} + // WithDiffEngine sets the `diff` engine that will be used to generate the // `diff` text. func (g *Goldie) WithDiffEngine(engine DiffEngine) error {