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

fix: pass cmp.Options to cmp.Diff in BeComparableToMatcher #563

Merged
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
3 changes: 2 additions & 1 deletion matchers/be_comparable_to_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package matchers
import (
"bytes"
"fmt"

"github.com/google/go-cmp/cmp"
"github.com/onsi/gomega/format"
)
Expand Down Expand Up @@ -40,7 +41,7 @@ func (matcher *BeComparableToMatcher) Match(actual interface{}) (success bool, m
}

func (matcher *BeComparableToMatcher) FailureMessage(actual interface{}) (message string) {
return cmp.Diff(matcher.Expected, actual)
return cmp.Diff(matcher.Expected, actual, matcher.Options)
}

func (matcher *BeComparableToMatcher) NegatedFailureMessage(actual interface{}) (message string) {
Expand Down
74 changes: 50 additions & 24 deletions matchers/be_comparable_to_matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package matchers_test

import (
"errors"
"time"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -102,29 +103,54 @@ var _ = Describe("BeComparableTo", func() {
})

Context("When asserting equal between objects", func() {
It("should do the right thing", func() {
Expect(5).Should(BeComparableTo(5))
Expect(5.0).Should(BeComparableTo(5.0))

Expect(5).ShouldNot(BeComparableTo("5"))
Expect(5).ShouldNot(BeComparableTo(5.0))
Expect(5).ShouldNot(BeComparableTo(3))

Expect("5").Should(BeComparableTo("5"))
Expect([]int{1, 2}).Should(BeComparableTo([]int{1, 2}))
Expect([]int{1, 2}).ShouldNot(BeComparableTo([]int{2, 1}))
Expect([]byte{'f', 'o', 'o'}).Should(BeComparableTo([]byte{'f', 'o', 'o'}))
Expect([]byte{'f', 'o', 'o'}).ShouldNot(BeComparableTo([]byte{'b', 'a', 'r'}))
Expect(map[string]string{"a": "b", "c": "d"}).Should(BeComparableTo(map[string]string{"a": "b", "c": "d"}))
Expect(map[string]string{"a": "b", "c": "d"}).ShouldNot(BeComparableTo(map[string]string{"a": "b", "c": "e"}))

Expect(myCustomType{s: "abc", n: 3, f: 2.0, arr: []string{"a", "b"}}).Should(BeComparableTo(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b"}}, cmpopts.IgnoreUnexported(myCustomType{})))

Expect(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b"}}).Should(BeComparableTo(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b"}}, cmp.AllowUnexported(myCustomType{})))
Expect(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b"}}).ShouldNot(BeComparableTo(myCustomType{s: "bar", n: 3, f: 2.0, arr: []string{"a", "b"}}, cmp.AllowUnexported(myCustomType{})))
Expect(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b"}}).ShouldNot(BeComparableTo(myCustomType{s: "foo", n: 2, f: 2.0, arr: []string{"a", "b"}}, cmp.AllowUnexported(myCustomType{})))
Expect(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b"}}).ShouldNot(BeComparableTo(myCustomType{s: "foo", n: 3, f: 3.0, arr: []string{"a", "b"}}, cmp.AllowUnexported(myCustomType{})))
Expect(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b"}}).ShouldNot(BeComparableTo(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b", "c"}}, cmp.AllowUnexported(myCustomType{})))
Context("with no additional cmp.Options", func() {
It("should do the right thing", func() {
Expect(5).Should(BeComparableTo(5))
Expect(5.0).Should(BeComparableTo(5.0))

Expect(5).ShouldNot(BeComparableTo("5"))
Expect(5).ShouldNot(BeComparableTo(5.0))
Expect(5).ShouldNot(BeComparableTo(3))

Expect("5").Should(BeComparableTo("5"))
Expect([]int{1, 2}).Should(BeComparableTo([]int{1, 2}))
Expect([]int{1, 2}).ShouldNot(BeComparableTo([]int{2, 1}))
Expect([]byte{'f', 'o', 'o'}).Should(BeComparableTo([]byte{'f', 'o', 'o'}))
Expect([]byte{'f', 'o', 'o'}).ShouldNot(BeComparableTo([]byte{'b', 'a', 'r'}))
Expect(map[string]string{"a": "b", "c": "d"}).Should(BeComparableTo(map[string]string{"a": "b", "c": "d"}))
Expect(map[string]string{"a": "b", "c": "d"}).ShouldNot(BeComparableTo(map[string]string{"a": "b", "c": "e"}))
})
})

Context("with custom cmp.Options", func() {
It("should do the right thing", func() {
Expect(myCustomType{s: "abc", n: 3, f: 2.0, arr: []string{"a", "b"}}).Should(BeComparableTo(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b"}}, cmpopts.IgnoreUnexported(myCustomType{})))

Expect(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b"}}).Should(BeComparableTo(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b"}}, cmp.AllowUnexported(myCustomType{})))
Expect(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b"}}).ShouldNot(BeComparableTo(myCustomType{s: "bar", n: 3, f: 2.0, arr: []string{"a", "b"}}, cmp.AllowUnexported(myCustomType{})))
Expect(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b"}}).ShouldNot(BeComparableTo(myCustomType{s: "foo", n: 2, f: 2.0, arr: []string{"a", "b"}}, cmp.AllowUnexported(myCustomType{})))
Expect(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b"}}).ShouldNot(BeComparableTo(myCustomType{s: "foo", n: 3, f: 3.0, arr: []string{"a", "b"}}, cmp.AllowUnexported(myCustomType{})))
Expect(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b"}}).ShouldNot(BeComparableTo(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b", "c"}}, cmp.AllowUnexported(myCustomType{})))
})

type structWithUnexportedFields struct {
unexported string
Exported string
}

It("should produce failure message according to paseed cmp.Option", func() {
actual := structWithUnexportedFields{unexported: "xxx", Exported: "exported field value"}
expectedEqual := structWithUnexportedFields{unexported: "yyy", Exported: "exported field value"}
matcherWithEqual := BeComparableTo(expectedEqual, cmpopts.IgnoreUnexported(structWithUnexportedFields{}))

Expect(matcherWithEqual.FailureMessage(actual)).To(BeEmpty())

expectedDiffernt := structWithUnexportedFields{unexported: "xxx", Exported: "other value"}
matcherWithDifference := BeComparableTo(expectedDiffernt, cmpopts.IgnoreUnexported(structWithUnexportedFields{}))
Expect(matcherWithDifference.FailureMessage(actual)).To(ContainSubstring("1 ignored field"))
Expect(matcherWithDifference.FailureMessage(actual)).To(ContainSubstring("Exported: \"other value\""))
Expect(matcherWithDifference.FailureMessage(actual)).To(ContainSubstring("Exported: \"exported field value\""))
})
})
})
})