Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add customizable equality compare option #41

Merged
merged 1 commit into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions v2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
11 changes: 9 additions & 2 deletions v2/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -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)
}
16 changes: 16 additions & 0 deletions v2/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,49 @@ func TestCompare(t *testing.T) {
actualData []byte
expectedData []byte
update bool
equalfn EqualFn
err error
}{
{
name: "example",
actualData: []byte("abc"),
expectedData: []byte("abc"),
update: true,
equalfn: nil,
err: nil,
},
{
name: "example",
actualData: []byte("abc"),
expectedData: []byte("abc"),
update: false,
equalfn: nil,
err: &errFixtureNotFound{},
},
{
name: "example",
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,
},
}
Expand All @@ -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)

Expand Down
1 change: 1 addition & 0 deletions v2/goldie.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type Goldie struct {
filePerms os.FileMode
dirPerms os.FileMode

equalFn EqualFn
diffEngine DiffEngine
diffFn DiffFn
ignoreTemplateErrors bool
Expand Down
13 changes: 13 additions & 0 deletions v2/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
//
Expand Down
7 changes: 7 additions & 0 deletions v2/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down