Skip to content

Commit

Permalink
fix: pass cmp.Options to cmp.Diff in BeComparableToMatcher (#563)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomiej-grzegorek committed Jul 20, 2022
1 parent 293dd88 commit a9a8414
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 25 deletions.
3 changes: 2 additions & 1 deletion matchers/be_comparable_to_matcher.go
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
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\""))
})
})
})
})

0 comments on commit a9a8414

Please sign in to comment.