Skip to content

Commit

Permalink
fix: gcustom.MakeMatcher accepts nil as actual value (#666)
Browse files Browse the repository at this point in the history
  • Loading branch information
houz42 committed May 18, 2023
1 parent 7cadcf6 commit 57054d5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion gcustom/make_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
15 changes: 15 additions & 0 deletions gcustom/make_matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 57054d5

Please sign in to comment.