2018-01-29 19:44:18 +00:00
|
|
|
package matchers_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
. "github.com/onsi/gomega/matchers"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Erroring() error {
|
|
|
|
return errors.New("bam")
|
|
|
|
}
|
|
|
|
|
|
|
|
func NotErroring() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type AnyType struct{}
|
|
|
|
|
|
|
|
func Invalid() *AnyType {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ = Describe("Succeed", func() {
|
|
|
|
It("should succeed if the function succeeds", func() {
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(NotErroring()).Should(Succeed())
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("should succeed (in the negated) if the function errored", func() {
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(Erroring()).ShouldNot(Succeed())
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("should not if passed a non-error", func() {
|
|
|
|
success, err := (&SucceedMatcher{}).Match(Invalid())
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(success).Should(BeFalse())
|
|
|
|
Expect(err).Should(MatchError("Expected an error-type. Got:\n <*matchers_test.AnyType | 0x0>: nil"))
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("doesn't support non-error type", func() {
|
|
|
|
success, err := (&SucceedMatcher{}).Match(AnyType{})
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(success).Should(BeFalse())
|
|
|
|
Expect(err).Should(MatchError("Expected an error-type. Got:\n <matchers_test.AnyType>: {}"))
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("doesn't support non-error pointer type", func() {
|
|
|
|
success, err := (&SucceedMatcher{}).Match(&AnyType{})
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(success).Should(BeFalse())
|
|
|
|
Expect(err).Should(MatchError(MatchRegexp(`Expected an error-type. Got:\n <*matchers_test.AnyType | 0x[[:xdigit:]]+>: {}`)))
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("should not succeed with pointer types that conform to error interface", func() {
|
|
|
|
err := &CustomErr{"ohai"}
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(err).ShouldNot(Succeed())
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("should succeed with nil pointers to types that conform to error interface", func() {
|
|
|
|
var err *CustomErr = nil
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(err).Should(Succeed())
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
})
|