initial commit
[govpp.git] / vendor / github.com / onsi / gomega / matchers / match_error_matcher_test.go
1 package matchers_test
2
3 import (
4         "errors"
5         "fmt"
6         . "github.com/onsi/ginkgo"
7         . "github.com/onsi/gomega"
8         . "github.com/onsi/gomega/matchers"
9 )
10
11 type CustomError struct {
12 }
13
14 func (c CustomError) Error() string {
15         return "an error"
16 }
17
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{}
24
25                         Ω(err).Should(MatchError(errors.New("an error")))
26                         Ω(err).ShouldNot(MatchError(errors.New("another error")))
27
28                         Ω(fmtErr).Should(MatchError(errors.New("an error")))
29                         Ω(customErr).Should(MatchError(CustomError{}))
30                 })
31
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{}
36
37                         Ω(err).Should(MatchError("an error"))
38                         Ω(err).ShouldNot(MatchError("another error"))
39
40                         Ω(fmtErr).Should(MatchError("an error"))
41                         Ω(customErr).Should(MatchError("an error"))
42                 })
43
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")
47
48                                 Ω(err).Should(MatchError(MatchRegexp(`\d{3}`)))
49                         })
50
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`)))
54                         })
55                 })
56
57                 It("should fail when passed anything else", func() {
58                         actualErr := errors.New("an error")
59                         _, err := (&MatchErrorMatcher{
60                                 Expected: []byte("an error"),
61                         }).Match(actualErr)
62                         Ω(err).Should(HaveOccurred())
63
64                         _, err = (&MatchErrorMatcher{
65                                 Expected: 3,
66                         }).Match(actualErr)
67                         Ω(err).Should(HaveOccurred())
68                 })
69         })
70
71         Context("when passed nil", func() {
72                 It("should fail", func() {
73                         _, err := (&MatchErrorMatcher{
74                                 Expected: "an error",
75                         }).Match(nil)
76                         Ω(err).Should(HaveOccurred())
77                 })
78         })
79
80         Context("when passed a non-error", func() {
81                 It("should fail", func() {
82                         _, err := (&MatchErrorMatcher{
83                                 Expected: "an error",
84                         }).Match("an error")
85                         Ω(err).Should(HaveOccurred())
86
87                         _, err = (&MatchErrorMatcher{
88                                 Expected: "an error",
89                         }).Match(3)
90                         Ω(err).Should(HaveOccurred())
91                 })
92         })
93 })