6 . "github.com/onsi/ginkgo"
7 . "github.com/onsi/gomega"
8 . "github.com/onsi/gomega/matchers"
11 type CustomError struct {
14 func (c CustomError) Error() string {
18 var _ = Describe("MatchErrorMatcher", func() {
19 Context("When asserting against an error", func() {
20 It("should succeed when matching with an error", func() {
21 err := errors.New("an error")
22 fmtErr := fmt.Errorf("an error")
23 customErr := CustomError{}
25 Ω(err).Should(MatchError(errors.New("an error")))
26 Ω(err).ShouldNot(MatchError(errors.New("another error")))
28 Ω(fmtErr).Should(MatchError(errors.New("an error")))
29 Ω(customErr).Should(MatchError(CustomError{}))
32 It("should succeed when matching with a string", func() {
33 err := errors.New("an error")
34 fmtErr := fmt.Errorf("an error")
35 customErr := CustomError{}
37 Ω(err).Should(MatchError("an error"))
38 Ω(err).ShouldNot(MatchError("another error"))
40 Ω(fmtErr).Should(MatchError("an error"))
41 Ω(customErr).Should(MatchError("an error"))
44 Context("when passed a matcher", func() {
45 It("should pass if the matcher passes against the error string", func() {
46 err := errors.New("error 123 abc")
48 Ω(err).Should(MatchError(MatchRegexp(`\d{3}`)))
51 It("should fail if the matcher fails against the error string", func() {
52 err := errors.New("no digits")
53 Ω(err).ShouldNot(MatchError(MatchRegexp(`\d`)))
57 It("should fail when passed anything else", func() {
58 actualErr := errors.New("an error")
59 _, err := (&MatchErrorMatcher{
60 Expected: []byte("an error"),
62 Ω(err).Should(HaveOccurred())
64 _, err = (&MatchErrorMatcher{
67 Ω(err).Should(HaveOccurred())
71 Context("when passed nil", func() {
72 It("should fail", func() {
73 _, err := (&MatchErrorMatcher{
76 Ω(err).Should(HaveOccurred())
80 Context("when passed a non-error", func() {
81 It("should fail", func() {
82 _, err := (&MatchErrorMatcher{
85 Ω(err).Should(HaveOccurred())
87 _, err = (&MatchErrorMatcher{
90 Ω(err).Should(HaveOccurred())