Codebase list golang-gomega / f22b915
Produce more descriptive failure messages when PanicWith() fails. James Harris authored 4 years ago William Martin committed 4 years ago
2 changed file(s) with 131 addition(s) and 30 deletion(s). Raw diff Collapse all Expand all
5252 }
5353
5454 func (matcher *PanicMatcher) FailureMessage(actual interface{}) (message string) {
55 if matcher.Expected == nil {
56 // We wanted any panic to occur, but none did.
57 return format.Message(actual, "to panic")
58 }
59
60 if matcher.object == nil {
61 // We wanted a panic with a specific value to occur, but none did.
62 switch matcher.Expected.(type) {
63 case omegaMatcher:
64 return format.Message(actual, "to panic with a value matching", matcher.Expected)
65 default:
66 return format.Message(actual, "to panic with", matcher.Expected)
67 }
68 }
69
70 // We got a panic, but the value isn't what we expected.
5571 switch matcher.Expected.(type) {
56 case nil:
57 return format.Message(actual, "to panic")
5872 case omegaMatcher:
59 return format.Message(actual, "to panic with a value matching", matcher.Expected)
73 return format.Message(
74 actual,
75 fmt.Sprintf(
76 "to panic with a value matching\n%s\nbut panicked with\n%s",
77 format.Object(matcher.Expected, 1),
78 format.Object(matcher.object, 1),
79 ),
80 )
6081 default:
61 return format.Message(actual, "to panic with", matcher.Expected)
82 return format.Message(
83 actual,
84 fmt.Sprintf(
85 "to panic with\n%s\nbut panicked with\n%s",
86 format.Object(matcher.Expected, 1),
87 format.Object(matcher.object, 1),
88 ),
89 )
6290 }
6391 }
6492
6593 func (matcher *PanicMatcher) NegatedFailureMessage(actual interface{}) (message string) {
94 if matcher.Expected == nil {
95 // We didn't want any panic to occur, but one did.
96 return format.Message(actual, fmt.Sprintf("not to panic, but panicked with\n%s", format.Object(matcher.object, 1)))
97 }
98
99 // We wanted a to ensure a panic with a specific value did not occur, but it did.
66100 switch matcher.Expected.(type) {
67 case nil:
68 return format.Message(actual, fmt.Sprintf("not to panic, but panicked with\n%s", format.Object(matcher.object, 1)))
69101 case omegaMatcher:
70102 return format.Message(
71103 actual,
5151 })
5252
5353 var _ = Describe("PanicWith", func() {
54 When("passed a function of the correct type", func() {
55 It("should call the function and pass if the function panics with the expected value", func() {
56 Expect(func() { panic("ack!") }).To(PanicWith("ack!"))
57 Expect(func() {}).NotTo(PanicWith("ack!"))
54 When("a specific panic value is expected", func() {
55 matcher := PanicWith("ack!")
56
57 When("no panic occurs", func() {
58 actual := func() {}
59
60 It("prints a message that includes the expected value", func() {
61 failuresMessages := InterceptGomegaFailures(func() {
62 Expect(actual).To(matcher)
63 })
64 Expect(failuresMessages).To(ConsistOf(
65 MatchRegexp("Expected\n\\s+<func\\(\\)>: .+\nto panic with\\s+<string>: ack!"),
66 ))
67 })
68
69 It("passes when negated", func() {
70 Expect(actual).NotTo(matcher)
71 })
72 })
73
74 When("the panic value matches", func() {
75 actual := func() { panic("ack!") }
76
77 It("passes", func() {
78 Expect(actual).To(matcher)
79 })
80
81 It("prints a message that includes the (un)expected value when negated", func() {
82 failuresMessages := InterceptGomegaFailures(func() {
83 Expect(actual).NotTo(matcher)
84 })
85 Expect(failuresMessages).To(ConsistOf(
86 MatchRegexp("Expected\n\\s+<func\\(\\)>: .+\nnot to panic with\\s+<string>: ack!"),
87 ))
88 })
89 })
90
91 When("the panic value does not match", func() {
92 actual := func() { panic("unexpected!") }
93
94 It("prints a message that includes both the actual and expected values", func() {
95 failuresMessages := InterceptGomegaFailures(func() {
96 Expect(actual).To(matcher)
97 })
98 Expect(failuresMessages).To(ConsistOf(
99 MatchRegexp("Expected\n\\s+<func\\(\\)>: .+\nto panic with\\s+<string>: ack!\nbut panicked with\n\\s+<string>: unexpected!"),
100 ))
101 })
102
103 It("passes when negated", func() {
104 Expect(actual).NotTo(matcher)
105 })
58106 })
59107 })
60108
61 When("assertion fails", func() {
62 It("prints simple message when positive", func() {
63 failuresMessages := InterceptGomegaFailures(func() {
64 Expect(func() {}).To(PanicWith("ack!"))
109 When("the expected value is actually a matcher", func() {
110 matcher := PanicWith(MatchRegexp("ack"))
111
112 When("no panic occurs", func() {
113 actual := func() {}
114
115 It("prints a message that includes the expected value", func() {
116 failuresMessages := InterceptGomegaFailures(func() {
117 Expect(actual).To(matcher)
118 })
119 Expect(failuresMessages).To(ConsistOf(
120 MatchRegexp("Expected\n\\s+<func\\(\\)>: .+\nto panic with a value matching\n.+MatchRegexpMatcher.+ack"),
121 ))
65122 })
66 Expect(failuresMessages).To(ConsistOf(MatchRegexp("Expected\n\\s+<func\\(\\)>: .+\nto panic with\\s+<string>: ack!")))
123
124 It("passes when negated", func() {
125 Expect(actual).NotTo(matcher)
126 })
67127 })
68128
69 It("prints simple message when negative", func() {
70 failuresMessages := InterceptGomegaFailures(func() {
71 Expect(func() { panic("ack!") }).NotTo(PanicWith("ack!"))
129 When("the panic value matches", func() {
130 actual := func() { panic("ack!") }
131
132 It("passes", func() {
133 Expect(actual).To(matcher)
72134 })
73 Expect(failuresMessages).To(ConsistOf(MatchRegexp("Expected\n\\s+<func\\(\\)>: .+\nnot to panic with\\s+<string>: ack!")))
135
136 It("prints a message that includes the (un)expected value when negated", func() {
137 failuresMessages := InterceptGomegaFailures(func() {
138 Expect(actual).NotTo(matcher)
139 })
140 Expect(failuresMessages).To(ConsistOf(
141 MatchRegexp("Expected\n\\s+<func\\(\\)>: .+\nnot to panic with a value matching\n.+MatchRegexpMatcher.+ack"),
142 ))
143 })
74144 })
75145
76 When("the expected value is actually a matcher", func() {
77 It("prints simple message when positive", func() {
146 When("the panic value does not match", func() {
147 actual := func() { panic("unexpected!") }
148
149 It("prints a message that includes both the actual and expected values", func() {
78150 failuresMessages := InterceptGomegaFailures(func() {
79 Expect(func() {}).To(PanicWith(Equal("ack!")))
151 Expect(actual).To(matcher)
80152 })
81 Expect(failuresMessages).To(ConsistOf(MatchRegexp("Expected\n\\s+<func\\(\\)>: .+\nto panic with a value matching\n.+EqualMatcher.+ack!")))
153 Expect(failuresMessages).To(ConsistOf(
154 MatchRegexp("Expected\n\\s+<func\\(\\)>: .+\nto panic with a value matching\n.+MatchRegexpMatcher.+ack.+\nbut panicked with\n\\s+<string>: unexpected!"),
155 ))
82156 })
83157
84 It("prints the object passed to panic() when negative", func() {
85 failuresMessages := InterceptGomegaFailures(func() {
86 Expect(func() { panic("ack!") }).NotTo(PanicWith(MatchRegexp("ack")))
87 })
88 Expect(failuresMessages).To(ConsistOf(
89 MatchRegexp("Expected\n\\s+<func\\(\\)>: .+\nnot to panic with a value matching\n.+MatchRegexpMatcher.+ack.+\nbut panicked with\n <string>: ack!"),
90 ))
158 It("passes when negated", func() {
159 Expect(actual).NotTo(matcher)
91160 })
92161 })
93162 })