Skip to content

Commit

Permalink
Add customizable equality compare option
Browse files Browse the repository at this point in the history
  • Loading branch information
takashabe committed Jun 27, 2023
1 parent 9788822 commit 9267d41
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 2 deletions.
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

0 comments on commit 9267d41

Please sign in to comment.