Skip to content

Commit 854f075

Browse files
committed
improve error formatting and remove duplication of error message in Eventually/Consistently
1 parent ccebd9b commit 854f075

File tree

6 files changed

+28
-18
lines changed

6 files changed

+28
-18
lines changed

format/format.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,9 @@ func Object(object interface{}, indentation uint) string {
260260
value := reflect.ValueOf(object)
261261
commonRepresentation := ""
262262
if err, ok := object.(error); ok {
263-
commonRepresentation += "\n" + IndentString(err.Error(), indentation)
263+
commonRepresentation += "\n" + IndentString(err.Error(), indentation) + "\n" + indent
264264
}
265-
return fmt.Sprintf("%s<%s>: %s%s", indent, formatType(value), formatValue(value, indentation), commonRepresentation)
265+
return fmt.Sprintf("%s<%s>: %s%s", indent, formatType(value), commonRepresentation, formatValue(value, indentation))
266266
}
267267

268268
/*

format/format_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -616,14 +616,15 @@ var _ = Describe("Format", func() {
616616
Describe("formatting errors", func() {
617617
It("should include the error() representation", func() {
618618
err := fmt.Errorf("whoops: %w", fmt.Errorf("welp: %w", fmt.Errorf("ruh roh")))
619-
Expect(Object(err, 1)).Should(MatchRegexp(` \<\*fmt\.wrapError \| 0x[0-9a-f]*\>\: \{
619+
Expect(Object(err, 1)).Should(MatchRegexp(` \<\*fmt\.wrapError \| 0x[0-9a-f]*\>\:
620+
whoops\: welp\: ruh roh
621+
\{
620622
msg\: "whoops\: welp\: ruh roh",
621623
err\: \<\*fmt.wrapError \| 0x[0-9a-f]*\>\{
622624
msg\: "welp\: ruh roh",
623625
err\: \<\*errors.errorString \| 0x[0-9a-f]*\>\{s\: "ruh roh"\},
624626
\},
625-
\}
626-
whoops\: welp\: ruh roh`))
627+
\}`))
627628
})
628629
})
629630
})

internal/async_assertion.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ func (assertion *AsyncAssertion) match(matcher types.GomegaMatcher, desiredMatch
412412
message += format.Object(attachment.Object, 1)
413413
}
414414
} else {
415-
message = preamble + "\n" + err.Error() + "\n" + format.Object(err, 1)
415+
message = preamble + "\n" + format.Object(err, 1)
416416
}
417417
return message
418418
}

internal/async_assertion_test.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ var _ = Describe("Asynchronous Assertions", func() {
183183
It("renders the matcher's error if an error occured", func() {
184184
ig.G.Eventually(ERR_MATCH).WithTimeout(50 * time.Millisecond).WithPolling(10 * time.Millisecond).Should(SpecMatch())
185185
Ω(ig.FailureMessage).Should(ContainSubstring("Timed out after"))
186-
Ω(ig.FailureMessage).Should(ContainSubstring("The matcher passed to Eventually returned the following error:\nspec matcher error"))
186+
Ω(ig.FailureMessage).Should(ContainSubstring("The matcher passed to Eventually returned the following error:"))
187+
Ω(ig.FailureMessage).Should(ContainSubstring("spec matcher error"))
187188
})
188189

189190
It("renders the optional description", func() {
@@ -372,7 +373,8 @@ var _ = Describe("Asynchronous Assertions", func() {
372373
}).WithTimeout(50 * time.Millisecond).WithPolling(10 * time.Millisecond).Should(SpecMatch())
373374
Ω(counter).Should(Equal(3))
374375
Ω(ig.FailureMessage).Should(ContainSubstring("Failed after"))
375-
Ω(ig.FailureMessage).Should(ContainSubstring("The matcher passed to Consistently returned the following error:\nspec matcher error"))
376+
Ω(ig.FailureMessage).Should(ContainSubstring("The matcher passed to Consistently returned the following error:"))
377+
Ω(ig.FailureMessage).Should(ContainSubstring("spec matcher error"))
376378
})
377379

378380
It("fails if the matcher doesn't match at any point", func() {
@@ -444,7 +446,8 @@ var _ = Describe("Asynchronous Assertions", func() {
444446
It("renders the matcher's error if an error occured", func() {
445447
ig.G.Consistently(ERR_MATCH).Should(SpecMatch())
446448
Ω(ig.FailureMessage).Should(ContainSubstring("Failed after"))
447-
Ω(ig.FailureMessage).Should(ContainSubstring("The matcher passed to Consistently returned the following error:\nspec matcher error"))
449+
Ω(ig.FailureMessage).Should(ContainSubstring("The matcher passed to Consistently returned the following error:"))
450+
Ω(ig.FailureMessage).Should(ContainSubstring("spec matcher error"))
448451
})
449452

450453
It("renders the optional description", func() {
@@ -588,7 +591,8 @@ var _ = Describe("Asynchronous Assertions", func() {
588591
ig.G.Eventually(func() (int, string, Foo, error) {
589592
return 1, "", Foo{}, errors.New("welp!")
590593
}).WithTimeout(50 * time.Millisecond).WithPolling(10 * time.Millisecond).Should(BeNumerically("<", 100))
591-
Ω(ig.FailureMessage).Should(ContainSubstring("The function passed to Eventually returned the following error:\nwelp!"))
594+
Ω(ig.FailureMessage).Should(ContainSubstring("The function passed to Eventually returned the following error:"))
595+
Ω(ig.FailureMessage).Should(ContainSubstring("welp!"))
592596
})
593597

594598
Context("when making a ShouldNot assertion", func() {
@@ -1529,7 +1533,8 @@ sprocket:
15291533
}).WithTimeout(time.Millisecond*10).Should(QuickMatcher(func(actual any) (bool, error) {
15301534
return false, fmt.Errorf("matcher-error")
15311535
}), "My Description")
1532-
Ω(ig.FailureMessage).Should(ContainSubstring("My Description\nThe matcher passed to Eventually returned the following error:\nmatcher-error\n <*errors.errorString"))
1536+
Ω(ig.FailureMessage).Should(ContainSubstring("My Description\nThe matcher passed to Eventually returned the following error:\n <*errors.errorString"))
1537+
Ω(ig.FailureMessage).Should(ContainSubstring("matcher-error"))
15331538
})
15341539

15351540
When("the matcher error is a StopTrying with attachments", func() {
@@ -1553,7 +1558,8 @@ sprocket:
15531558
}).WithTimeout(time.Millisecond*10).Should(QuickMatcher(func(actual any) (bool, error) {
15541559
return true, nil
15551560
}), "My Description")
1556-
Ω(ig.FailureMessage).Should(ContainSubstring("My Description\nThe function passed to Eventually returned the following error:\nactual-err\n <*errors.errorString"))
1561+
Ω(ig.FailureMessage).Should(ContainSubstring("My Description\nThe function passed to Eventually returned the following error:\n <*errors.errorString"))
1562+
Ω(ig.FailureMessage).Should(ContainSubstring("actual-err"))
15571563
})
15581564
})
15591565

@@ -1608,8 +1614,10 @@ sprocket:
16081614
}
16091615
return false, nil
16101616
}), "My Description")
1611-
Ω(ig.FailureMessage).Should(ContainSubstring("My Description\nThe function passed to Eventually returned the following error:\nactual-err\n <*errors.errorString"))
1612-
Ω(ig.FailureMessage).Should(ContainSubstring("At one point, however, the function did return successfully.\nYet, Eventually failed because the matcher returned the following error:\nmatcher-err"))
1617+
Ω(ig.FailureMessage).Should(ContainSubstring("My Description\nThe function passed to Eventually returned the following error:\n <*errors.errorString"))
1618+
Ω(ig.FailureMessage).Should(ContainSubstring("actual-err"))
1619+
Ω(ig.FailureMessage).Should(ContainSubstring("At one point, however, the function did return successfully.\nYet, Eventually failed because the matcher returned the following error:"))
1620+
Ω(ig.FailureMessage).Should(ContainSubstring("matcher-err"))
16131621
})
16141622
})
16151623

@@ -1627,7 +1635,8 @@ sprocket:
16271635
actualInt := actual.(int)
16281636
return actualInt > 3, nil
16291637
}), "My Description")
1630-
Ω(ig.FailureMessage).Should(ContainSubstring("My Description\nThe function passed to Eventually returned the following error:\nactual-err\n <*errors.errorString"))
1638+
Ω(ig.FailureMessage).Should(ContainSubstring("My Description\nThe function passed to Eventually returned the following error:\n <*errors.errorString"))
1639+
Ω(ig.FailureMessage).Should(ContainSubstring("actual-err"))
16311640
Ω(ig.FailureMessage).Should(ContainSubstring("At one point, however, the function did return successfully.\nYet, Eventually failed because the matcher was not satisfied:\nQM failure message: 3"))
16321641

16331642
})

matchers/match_error_matcher_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,14 @@ var _ = Describe("MatchErrorMatcher", func() {
135135
failuresMessages := InterceptGomegaFailures(func() {
136136
Expect(errors.New("foo")).To(MatchError("bar"))
137137
})
138-
Expect(failuresMessages[0]).To(ContainSubstring("{s: \"foo\"}\n foo\nto match error\n <string>: bar"))
138+
Expect(failuresMessages[0]).To(ContainSubstring("foo\n {s: \"foo\"}\nto match error\n <string>: bar"))
139139
})
140140

141141
It("shows negated failure message", func() {
142142
failuresMessages := InterceptGomegaFailures(func() {
143143
Expect(errors.New("foo")).ToNot(MatchError("foo"))
144144
})
145-
Expect(failuresMessages[0]).To(ContainSubstring("{s: \"foo\"}\n foo\nnot to match error\n <string>: foo"))
145+
Expect(failuresMessages[0]).To(ContainSubstring("foo\n {s: \"foo\"}\nnot to match error\n <string>: foo"))
146146
})
147147

148148
})

matchers/succeed_matcher_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ var _ = Describe("Succeed", func() {
7777
It("builds failure message", func() {
7878
actual := Succeed().FailureMessage(errors.New("oops"))
7979
actual = regexp.MustCompile(" 0x.*>").ReplaceAllString(actual, " 0x00000000>")
80-
Expect(actual).To(Equal("Expected success, but got an error:\n <*errors.errorString | 0x00000000>: {s: \"oops\"}\n oops"))
80+
Expect(actual).To(Equal("Expected success, but got an error:\n <*errors.errorString | 0x00000000>: \n oops\n {s: \"oops\"}"))
8181
})
8282

8383
It("simply returns .Error() for the failure message if the error is an AsyncPolledActualError", func() {

0 commit comments

Comments
 (0)