2021-09-01 13:50:18 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
2022-10-17 17:38:08 +00:00
|
|
|
"context"
|
2021-09-01 13:50:18 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/onsi/gomega/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Gomega struct {
|
|
|
|
Fail types.GomegaFailHandler
|
|
|
|
THelper func()
|
|
|
|
DurationBundle DurationBundle
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewGomega(bundle DurationBundle) *Gomega {
|
|
|
|
return &Gomega{
|
|
|
|
Fail: nil,
|
|
|
|
THelper: nil,
|
|
|
|
DurationBundle: bundle,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Gomega) IsConfigured() bool {
|
|
|
|
return g.Fail != nil && g.THelper != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Gomega) ConfigureWithFailHandler(fail types.GomegaFailHandler) *Gomega {
|
|
|
|
g.Fail = fail
|
|
|
|
g.THelper = func() {}
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Gomega) ConfigureWithT(t types.GomegaTestingT) *Gomega {
|
|
|
|
g.Fail = func(message string, _ ...int) {
|
|
|
|
t.Helper()
|
|
|
|
t.Fatalf("\n%s", message)
|
|
|
|
}
|
|
|
|
g.THelper = t.Helper
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
2021-11-08 20:26:59 +00:00
|
|
|
func (g *Gomega) Ω(actual interface{}, extra ...interface{}) types.Assertion {
|
|
|
|
return g.ExpectWithOffset(0, actual, extra...)
|
2021-09-01 13:50:18 +00:00
|
|
|
}
|
|
|
|
|
2021-11-08 20:26:59 +00:00
|
|
|
func (g *Gomega) Expect(actual interface{}, extra ...interface{}) types.Assertion {
|
|
|
|
return g.ExpectWithOffset(0, actual, extra...)
|
2021-09-01 13:50:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Gomega) ExpectWithOffset(offset int, actual interface{}, extra ...interface{}) types.Assertion {
|
|
|
|
return NewAssertion(actual, g, offset, extra...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Gomega) Eventually(actual interface{}, intervals ...interface{}) types.AsyncAssertion {
|
|
|
|
return g.EventuallyWithOffset(0, actual, intervals...)
|
|
|
|
}
|
|
|
|
|
2022-10-17 17:38:08 +00:00
|
|
|
func (g *Gomega) EventuallyWithOffset(offset int, actual interface{}, args ...interface{}) types.AsyncAssertion {
|
2022-10-20 09:06:44 +00:00
|
|
|
timeoutInterval := -time.Duration(1)
|
|
|
|
pollingInterval := -time.Duration(1)
|
2022-10-17 17:38:08 +00:00
|
|
|
intervals := []interface{}{}
|
|
|
|
var ctx context.Context
|
|
|
|
for _, arg := range args {
|
|
|
|
switch v := arg.(type) {
|
|
|
|
case context.Context:
|
|
|
|
ctx = v
|
|
|
|
default:
|
|
|
|
intervals = append(intervals, arg)
|
|
|
|
}
|
|
|
|
}
|
2021-09-01 13:50:18 +00:00
|
|
|
if len(intervals) > 0 {
|
|
|
|
timeoutInterval = toDuration(intervals[0])
|
|
|
|
}
|
|
|
|
if len(intervals) > 1 {
|
|
|
|
pollingInterval = toDuration(intervals[1])
|
|
|
|
}
|
|
|
|
|
2022-10-17 17:38:08 +00:00
|
|
|
return NewAsyncAssertion(AsyncAssertionTypeEventually, actual, g, timeoutInterval, pollingInterval, ctx, offset)
|
2021-09-01 13:50:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Gomega) Consistently(actual interface{}, intervals ...interface{}) types.AsyncAssertion {
|
|
|
|
return g.ConsistentlyWithOffset(0, actual, intervals...)
|
|
|
|
}
|
|
|
|
|
2022-10-17 17:38:08 +00:00
|
|
|
func (g *Gomega) ConsistentlyWithOffset(offset int, actual interface{}, args ...interface{}) types.AsyncAssertion {
|
2022-10-20 09:06:44 +00:00
|
|
|
timeoutInterval := -time.Duration(1)
|
|
|
|
pollingInterval := -time.Duration(1)
|
2022-10-17 17:38:08 +00:00
|
|
|
intervals := []interface{}{}
|
|
|
|
var ctx context.Context
|
|
|
|
for _, arg := range args {
|
|
|
|
switch v := arg.(type) {
|
|
|
|
case context.Context:
|
|
|
|
ctx = v
|
|
|
|
default:
|
|
|
|
intervals = append(intervals, arg)
|
|
|
|
}
|
|
|
|
}
|
2021-09-01 13:50:18 +00:00
|
|
|
if len(intervals) > 0 {
|
|
|
|
timeoutInterval = toDuration(intervals[0])
|
|
|
|
}
|
|
|
|
if len(intervals) > 1 {
|
|
|
|
pollingInterval = toDuration(intervals[1])
|
|
|
|
}
|
|
|
|
|
2022-10-17 17:38:08 +00:00
|
|
|
return NewAsyncAssertion(AsyncAssertionTypeConsistently, actual, g, timeoutInterval, pollingInterval, ctx, offset)
|
2021-09-01 13:50:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Gomega) SetDefaultEventuallyTimeout(t time.Duration) {
|
|
|
|
g.DurationBundle.EventuallyTimeout = t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Gomega) SetDefaultEventuallyPollingInterval(t time.Duration) {
|
|
|
|
g.DurationBundle.EventuallyPollingInterval = t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Gomega) SetDefaultConsistentlyDuration(t time.Duration) {
|
|
|
|
g.DurationBundle.ConsistentlyDuration = t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Gomega) SetDefaultConsistentlyPollingInterval(t time.Duration) {
|
|
|
|
g.DurationBundle.ConsistentlyPollingInterval = t
|
|
|
|
}
|