Skip to content

Commit 18a4723

Browse files
authoredNov 2, 2021
separate out offsets and timeouts (#478)
1 parent e001fab commit 18a4723

9 files changed

+91
-52
lines changed
 

Diff for: ‎gomega_dsl.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,8 @@ func Expect(actual interface{}, extra ...interface{}) Assertion {
204204
// ExpectWithOffset(1, "foo").To(Equal("foo"))
205205
//
206206
// Unlike `Expect` and `Ω`, `ExpectWithOffset` takes an additional integer argument
207-
// that is used to modify the call-stack offset when computing line numbers.
207+
// that is used to modify the call-stack offset when computing line numbers. It is
208+
// the same as `Expect(...).WithOffset`.
208209
//
209210
// This is most useful in helper functions that make assertions. If you want Gomega's
210211
// error message to refer to the calling line in the test (as opposed to the line in the helper function)
@@ -300,6 +301,9 @@ For example:
300301
}).Should(Succeed())
301302
302303
will rerun the function until all assertions pass.
304+
305+
`Eventually` specifying a timeout interval (and an optional polling interval) are
306+
the same as `Eventually(...).WithTimeout` or `Eventually(...).WithTimeout(...).WithPolling`.
303307
*/
304308
func Eventually(actual interface{}, intervals ...interface{}) AsyncAssertion {
305309
ensureDefaultGomegaIsConfigured()
@@ -309,6 +313,12 @@ func Eventually(actual interface{}, intervals ...interface{}) AsyncAssertion {
309313
// EventuallyWithOffset operates like Eventually but takes an additional
310314
// initial argument to indicate an offset in the call stack. This is useful when building helper
311315
// functions that contain matchers. To learn more, read about `ExpectWithOffset`.
316+
//
317+
// `EventuallyWithOffset` is the same as `Eventually(...).WithOffset`.
318+
//
319+
// `EventuallyWithOffset` specifying a timeout interval (and an optional polling interval) are
320+
// the same as `Eventually(...).WithOffset(...).WithTimeout` or
321+
// `Eventually(...).WithOffset(...).WithTimeout(...).WithPolling`.
312322
func EventuallyWithOffset(offset int, actual interface{}, intervals ...interface{}) AsyncAssertion {
313323
ensureDefaultGomegaIsConfigured()
314324
return Default.EventuallyWithOffset(offset, actual, intervals...)
@@ -337,6 +347,9 @@ func Consistently(actual interface{}, intervals ...interface{}) AsyncAssertion {
337347
// ConsistentlyWithOffset operates like Consistently but takes an additional
338348
// initial argument to indicate an offset in the call stack. This is useful when building helper
339349
// functions that contain matchers. To learn more, read about `ExpectWithOffset`.
350+
//
351+
// `ConsistentlyWithOffset` is the same as `Consistently(...).WithOffset` and
352+
// optional `WithTimeout` and `WithPolling`.
340353
func ConsistentlyWithOffset(offset int, actual interface{}, intervals ...interface{}) AsyncAssertion {
341354
ensureDefaultGomegaIsConfigured()
342355
return Default.ConsistentlyWithOffset(offset, actual, intervals...)

Diff for: ‎internal/assertion.go

+5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ func NewAssertion(actualInput interface{}, g *Gomega, offset int, extra ...inter
2323
}
2424
}
2525

26+
func (assertion *Assertion) WithOffset(offset int) types.Assertion {
27+
assertion.offset = offset
28+
return assertion
29+
}
30+
2631
func (assertion *Assertion) Should(matcher types.GomegaMatcher, optionalDescription ...interface{}) bool {
2732
assertion.g.THelper()
2833
return assertion.vetExtras(optionalDescription...) && assertion.match(matcher, true, optionalDescription...)

Diff for: ‎internal/async_assertion.go

+15
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,21 @@ func NewAsyncAssertion(asyncType AsyncAssertionType, actualInput interface{}, g
8787
return out
8888
}
8989

90+
func (assertion *AsyncAssertion) WithOffset(offset int) types.AsyncAssertion {
91+
assertion.offset = offset
92+
return assertion
93+
}
94+
95+
func (assertion *AsyncAssertion) WithTimeout(interval time.Duration) types.AsyncAssertion {
96+
assertion.timeoutInterval = interval
97+
return assertion
98+
}
99+
100+
func (assertion *AsyncAssertion) WithPolling(interval time.Duration) types.AsyncAssertion {
101+
assertion.pollingInterval = interval
102+
return assertion
103+
}
104+
90105
func (assertion *AsyncAssertion) Should(matcher types.GomegaMatcher, optionalDescription ...interface{}) bool {
91106
assertion.g.THelper()
92107
return assertion.match(matcher, true, optionalDescription...)

Diff for: ‎internal/async_assertion_test.go

+45-45
Large diffs are not rendered by default.

Diff for: ‎internal/dsl_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,9 @@ var _ = Describe("Gomega DSL", func() {
215215
doubleNested := func(eventually bool) {
216216
func() {
217217
if eventually {
218-
EventuallyWithOffset(2, true, "10ms", "5ms").Should(BeFalse())
218+
Eventually(true, "10ms", "5ms").WithOffset(2).Should(BeFalse())
219219
} else {
220-
ExpectWithOffset(2, true).To(BeFalse())
220+
Expect(true).WithOffset(2).To(BeFalse())
221221
}
222222
}()
223223
}

Diff for: ‎internal/gomega_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ var _ = Describe("Gomega", func() {
6161
doubleNested := func(g Gomega, eventually bool) {
6262
func() {
6363
if eventually {
64-
g.EventuallyWithOffset(2, true, "10ms", "5ms").Should(BeFalse())
64+
g.Eventually(true, "10ms", "5ms").WithOffset(2).Should(BeFalse())
6565
} else {
66-
g.ExpectWithOffset(2, true).To(BeFalse())
66+
g.Expect(true).WithOffset(2).To(BeFalse())
6767
}
6868
}()
6969
}

Diff for: ‎matchers/satisfy_matcher_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var _ = Describe("SatisfyMatcher", func() {
1313

1414
Context("Panic if predicate is invalid", func() {
1515
panicsWithPredicate := func(predicate interface{}) {
16-
ExpectWithOffset(1, func() { Satisfy(predicate) }).To(Panic())
16+
Expect(func() { Satisfy(predicate) }).WithOffset(1).To(Panic())
1717
}
1818
It("nil", func() {
1919
panicsWithPredicate(nil)

Diff for: ‎matchers/with_transform_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var _ = Describe("WithTransformMatcher", func() {
1414

1515
Context("Panic if transform function invalid", func() {
1616
panicsWithTransformer := func(transform interface{}) {
17-
ExpectWithOffset(1, func() { WithTransform(transform, nil) }).To(Panic())
17+
Expect(func() { WithTransform(transform, nil) }).WithOffset(1).To(Panic())
1818
}
1919
It("nil", func() {
2020
panicsWithTransformer(nil)

Diff for: ‎types/types.go

+6
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ func MatchMayChangeInTheFuture(matcher GomegaMatcher, value interface{}) bool {
6666
type AsyncAssertion interface {
6767
Should(matcher GomegaMatcher, optionalDescription ...interface{}) bool
6868
ShouldNot(matcher GomegaMatcher, optionalDescription ...interface{}) bool
69+
70+
WithOffset(offset int) AsyncAssertion
71+
WithTimeout(interval time.Duration) AsyncAssertion
72+
WithPolling(interval time.Duration) AsyncAssertion
6973
}
7074

7175
// Assertions are returned by Ω and Expect and enable assertions against Gomega matchers
@@ -76,4 +80,6 @@ type Assertion interface {
7680
To(matcher GomegaMatcher, optionalDescription ...interface{}) bool
7781
ToNot(matcher GomegaMatcher, optionalDescription ...interface{}) bool
7882
NotTo(matcher GomegaMatcher, optionalDescription ...interface{}) bool
83+
84+
WithOffset(offset int) Assertion
7985
}

0 commit comments

Comments
 (0)
Please sign in to comment.