From 57054d54add42b5aa5c499d4a5b3667a02f61ec1 Mon Sep 17 00:00:00 2001 From: Hou 42 Date: Thu, 18 May 2023 23:27:44 +0800 Subject: [PATCH] fix: gcustom.MakeMatcher accepts nil as actual value (#666) --- gcustom/make_matcher.go | 4 +++- gcustom/make_matcher_test.go | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/gcustom/make_matcher.go b/gcustom/make_matcher.go index 0c782e464..5372fa441 100644 --- a/gcustom/make_matcher.go +++ b/gcustom/make_matcher.go @@ -97,7 +97,9 @@ func MakeMatcher(matchFunc any, args ...any) CustomGomegaMatcher { finalMatchFunc = reflect.MakeFunc(reflect.TypeOf(finalMatchFunc), func(args []reflect.Value) []reflect.Value { actual := args[0].Interface() - if reflect.TypeOf(actual).AssignableTo(t.In(0)) { + if actual == nil && reflect.TypeOf(actual) == reflect.TypeOf(nil) { + return matchFuncValue.Call([]reflect.Value{reflect.New(t.In(0)).Elem()}) + } else if reflect.TypeOf(actual).AssignableTo(t.In(0)) { return matchFuncValue.Call([]reflect.Value{reflect.ValueOf(actual)}) } else { return []reflect.Value{ diff --git a/gcustom/make_matcher_test.go b/gcustom/make_matcher_test.go index 9516282ad..6c45419eb 100644 --- a/gcustom/make_matcher_test.go +++ b/gcustom/make_matcher_test.go @@ -148,6 +148,21 @@ var _ = Describe("MakeMatcher", func() { }) }) + + Context("when the match func accepts a nil-able type", func() { + It("ensure nil matches the type", func() { + var passedIn any + m := gcustom.MakeMatcher(func(a *someType) (bool, error) { + passedIn = a + return true, nil + }) + + success, err := m.Match(nil) + Ω(success).Should(BeTrue()) + Ω(err).ShouldNot(HaveOccurred()) + Ω(passedIn).Should(BeNil()) + }) + }) }) It("calls the matchFunc and returns whatever it returns when Match is called", func() {