Skip to content

Commit

Permalink
Negative assertions on nil pointers to types that conform to error in…
Browse files Browse the repository at this point in the history
…terface should not panic

Signed-off-by: George Lestaris <glestaris@pivotal.io>
  • Loading branch information
Craig Furman authored and glestaris committed Apr 30, 2015
1 parent a91b9a2 commit c816e74
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion matchers/have_occurred_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type HaveOccurredMatcher struct {
}

func (matcher *HaveOccurredMatcher) Match(actual interface{}) (success bool, err error) {
if actual == nil {
if isNil(actual) {
return false, nil
}

Expand Down
18 changes: 18 additions & 0 deletions matchers/have_occurred_matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ import (
. "github.com/onsi/gomega/matchers"
)

type CustomErr struct {
msg string
}

func (e *CustomErr) Error() string {
return e.msg
}

var _ = Describe("HaveOccurred", func() {
It("should succeed if matching an error", func() {
Ω(errors.New("Foo")).Should(HaveOccurred())
Expand All @@ -25,4 +33,14 @@ var _ = Describe("HaveOccurred", func() {
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})

It("should succeed with pointer types that conform to error interface", func() {
err := &CustomErr{"ohai"}
Ω(err).Should(HaveOccurred())
})

It("should not succeed with nil pointers to types that conform to error interface", func() {
var err *CustomErr = nil
Ω(err).ShouldNot(HaveOccurred())
})
})

0 comments on commit c816e74

Please sign in to comment.