Skip to content

Commit 57054d5

Browse files
authoredMay 18, 2023
fix: gcustom.MakeMatcher accepts nil as actual value (#666)
1 parent 7cadcf6 commit 57054d5

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed
 

‎gcustom/make_matcher.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ func MakeMatcher(matchFunc any, args ...any) CustomGomegaMatcher {
9797
finalMatchFunc = reflect.MakeFunc(reflect.TypeOf(finalMatchFunc),
9898
func(args []reflect.Value) []reflect.Value {
9999
actual := args[0].Interface()
100-
if reflect.TypeOf(actual).AssignableTo(t.In(0)) {
100+
if actual == nil && reflect.TypeOf(actual) == reflect.TypeOf(nil) {
101+
return matchFuncValue.Call([]reflect.Value{reflect.New(t.In(0)).Elem()})
102+
} else if reflect.TypeOf(actual).AssignableTo(t.In(0)) {
101103
return matchFuncValue.Call([]reflect.Value{reflect.ValueOf(actual)})
102104
} else {
103105
return []reflect.Value{

‎gcustom/make_matcher_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,21 @@ var _ = Describe("MakeMatcher", func() {
148148

149149
})
150150
})
151+
152+
Context("when the match func accepts a nil-able type", func() {
153+
It("ensure nil matches the type", func() {
154+
var passedIn any
155+
m := gcustom.MakeMatcher(func(a *someType) (bool, error) {
156+
passedIn = a
157+
return true, nil
158+
})
159+
160+
success, err := m.Match(nil)
161+
Ω(success).Should(BeTrue())
162+
Ω(err).ShouldNot(HaveOccurred())
163+
Ω(passedIn).Should(BeNil())
164+
})
165+
})
151166
})
152167

153168
It("calls the matchFunc and returns whatever it returns when Match is called", func() {

0 commit comments

Comments
 (0)
Please sign in to comment.