|
| 1 | +package matchers_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + . "github.com/onsi/ginkgo/v2" |
| 8 | + . "github.com/onsi/gomega" |
| 9 | + . "github.com/onsi/gomega/matchers" |
| 10 | +) |
| 11 | + |
| 12 | +type FakeIsError struct { |
| 13 | + isError bool |
| 14 | +} |
| 15 | + |
| 16 | +func (f *FakeIsError) Error() string { |
| 17 | + return fmt.Sprintf("is other error: %T", f.isError) |
| 18 | +} |
| 19 | + |
| 20 | +func (f *FakeIsError) Is(other error) bool { |
| 21 | + return f.isError |
| 22 | +} |
| 23 | + |
| 24 | +var _ = Describe("MatchErrorStrictlyMatcher", func() { |
| 25 | + Context("When asserting against an error", func() { |
| 26 | + When("passed an error", func() { |
| 27 | + It("should succeed when errors.Is returns true", func() { |
| 28 | + err := errors.New("an error") |
| 29 | + fmtErr := fmt.Errorf("an error") |
| 30 | + isError := &FakeIsError{true} |
| 31 | + |
| 32 | + Expect(err).To(MatchErrorStrictly(err)) |
| 33 | + Expect(fmtErr).To(MatchErrorStrictly(fmtErr)) |
| 34 | + Expect(isError).To(MatchErrorStrictly(errors.New("any error should match"))) |
| 35 | + }) |
| 36 | + |
| 37 | + It("should fail when errors.Is returns false", func() { |
| 38 | + err := errors.New("an error") |
| 39 | + fmtErr := fmt.Errorf("an error") |
| 40 | + isNotError := &FakeIsError{false} |
| 41 | + |
| 42 | + Expect(err).ToNot(MatchErrorStrictly(errors.New("another error"))) |
| 43 | + Expect(fmtErr).ToNot(MatchErrorStrictly(fmt.Errorf("an error"))) |
| 44 | + |
| 45 | + // errors.Is first checks if the values equal via ==, so we must point |
| 46 | + // to different instances of otherwise equal FakeIsError |
| 47 | + Expect(isNotError).ToNot(MatchErrorStrictly(&FakeIsError{false})) |
| 48 | + }) |
| 49 | + |
| 50 | + It("should succeed when any error in the chain matches the passed error", func() { |
| 51 | + innerErr := errors.New("inner error") |
| 52 | + outerErr := fmt.Errorf("outer error wrapping: %w", innerErr) |
| 53 | + |
| 54 | + Expect(outerErr).To(MatchErrorStrictly(innerErr)) |
| 55 | + }) |
| 56 | + }) |
| 57 | + }) |
| 58 | + |
| 59 | + When("expected is nil", func() { |
| 60 | + It("should fail with an appropriate error", func() { |
| 61 | + _, err := (&MatchErrorStrictlyMatcher{ |
| 62 | + Expected: nil, |
| 63 | + }).Match(errors.New("an error")) |
| 64 | + Expect(err).To(HaveOccurred()) |
| 65 | + Expect(err.Error()).To(ContainSubstring("ToNot(HaveOccurred())")) |
| 66 | + }) |
| 67 | + }) |
| 68 | + |
| 69 | + When("passed nil", func() { |
| 70 | + It("should fail", func() { |
| 71 | + _, err := (&MatchErrorStrictlyMatcher{ |
| 72 | + Expected: errors.New("an error"), |
| 73 | + }).Match(nil) |
| 74 | + Expect(err).To(HaveOccurred()) |
| 75 | + }) |
| 76 | + }) |
| 77 | + |
| 78 | + When("passed a non-error", func() { |
| 79 | + It("should fail", func() { |
| 80 | + _, err := (&MatchErrorStrictlyMatcher{ |
| 81 | + Expected: errors.New("an error"), |
| 82 | + }).Match("an error") |
| 83 | + Expect(err).To(HaveOccurred()) |
| 84 | + |
| 85 | + _, err = (&MatchErrorStrictlyMatcher{ |
| 86 | + Expected: errors.New("an error"), |
| 87 | + }).Match(3) |
| 88 | + Expect(err).To(HaveOccurred()) |
| 89 | + }) |
| 90 | + }) |
| 91 | + |
| 92 | + It("shows failure message", func() { |
| 93 | + failuresMessages := InterceptGomegaFailures(func() { |
| 94 | + Expect(errors.New("foo")).To(MatchErrorStrictly(errors.New("bar"))) |
| 95 | + }) |
| 96 | + Expect(failuresMessages[0]).To(ContainSubstring("foo\n {s: \"foo\"}\nto match error\n <*errors.errorString")) |
| 97 | + }) |
| 98 | + |
| 99 | + It("shows negated failure message", func() { |
| 100 | + err := errors.New("foo") |
| 101 | + failuresMessages := InterceptGomegaFailures(func() { |
| 102 | + Expect(err).ToNot(MatchErrorStrictly(err)) |
| 103 | + }) |
| 104 | + Expect(failuresMessages[0]).To(ContainSubstring("foo\n {s: \"foo\"}\nnot to match error\n <*errors.errorString")) |
| 105 | + }) |
| 106 | + |
| 107 | +}) |
0 commit comments