From a9a841466d3489a8a6bb36237dc2f9ce0a2a016b Mon Sep 17 00:00:00 2001 From: bartlomiej-grzegorek <68281091+bartlomiej-grzegorek@users.noreply.github.com> Date: Wed, 20 Jul 2022 17:44:32 +0200 Subject: [PATCH] fix: pass cmp.Options to cmp.Diff in BeComparableToMatcher (#563) --- matchers/be_comparable_to_matcher.go | 3 +- matchers/be_comparable_to_matcher_test.go | 74 +++++++++++++++-------- 2 files changed, 52 insertions(+), 25 deletions(-) diff --git a/matchers/be_comparable_to_matcher.go b/matchers/be_comparable_to_matcher.go index 2205d8c23..8ab4bb919 100644 --- a/matchers/be_comparable_to_matcher.go +++ b/matchers/be_comparable_to_matcher.go @@ -3,6 +3,7 @@ package matchers import ( "bytes" "fmt" + "github.com/google/go-cmp/cmp" "github.com/onsi/gomega/format" ) @@ -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) { diff --git a/matchers/be_comparable_to_matcher_test.go b/matchers/be_comparable_to_matcher_test.go index 083d3c609..8e3af77e5 100644 --- a/matchers/be_comparable_to_matcher_test.go +++ b/matchers/be_comparable_to_matcher_test.go @@ -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" @@ -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\"")) + }) }) }) })