Path: blob/main/vendor/github.com/onsi/gomega/internal/gomega.go
2880 views
package internal12import (3"context"4"time"56"github.com/onsi/gomega/types"7)89type Gomega struct {10Fail types.GomegaFailHandler11THelper func()12DurationBundle DurationBundle13}1415func NewGomega(bundle DurationBundle) *Gomega {16return &Gomega{17Fail: nil,18THelper: nil,19DurationBundle: bundle,20}21}2223func (g *Gomega) IsConfigured() bool {24return g.Fail != nil && g.THelper != nil25}2627func (g *Gomega) ConfigureWithFailHandler(fail types.GomegaFailHandler) *Gomega {28g.Fail = fail29g.THelper = func() {}30return g31}3233func (g *Gomega) ConfigureWithT(t types.GomegaTestingT) *Gomega {34g.Fail = func(message string, _ ...int) {35t.Helper()36t.Fatalf("\n%s", message)37}38g.THelper = t.Helper39return g40}4142func (g *Gomega) Ω(actual any, extra ...any) types.Assertion {43return g.ExpectWithOffset(0, actual, extra...)44}4546func (g *Gomega) Expect(actual any, extra ...any) types.Assertion {47return g.ExpectWithOffset(0, actual, extra...)48}4950func (g *Gomega) ExpectWithOffset(offset int, actual any, extra ...any) types.Assertion {51return NewAssertion(actual, g, offset, extra...)52}5354func (g *Gomega) Eventually(actualOrCtx any, args ...any) types.AsyncAssertion {55return g.makeAsyncAssertion(AsyncAssertionTypeEventually, 0, actualOrCtx, args...)56}5758func (g *Gomega) EventuallyWithOffset(offset int, actualOrCtx any, args ...any) types.AsyncAssertion {59return g.makeAsyncAssertion(AsyncAssertionTypeEventually, offset, actualOrCtx, args...)60}6162func (g *Gomega) Consistently(actualOrCtx any, args ...any) types.AsyncAssertion {63return g.makeAsyncAssertion(AsyncAssertionTypeConsistently, 0, actualOrCtx, args...)64}6566func (g *Gomega) ConsistentlyWithOffset(offset int, actualOrCtx any, args ...any) types.AsyncAssertion {67return g.makeAsyncAssertion(AsyncAssertionTypeConsistently, offset, actualOrCtx, args...)68}6970func (g *Gomega) makeAsyncAssertion(asyncAssertionType AsyncAssertionType, offset int, actualOrCtx any, args ...any) types.AsyncAssertion {71baseOffset := 372timeoutInterval := -time.Duration(1)73pollingInterval := -time.Duration(1)74intervals := []any{}75var ctx context.Context7677actual := actualOrCtx78startingIndex := 079if _, isCtx := actualOrCtx.(context.Context); isCtx && len(args) > 0 {80// the first argument is a context, we should accept it as the context _only if_ it is **not** the only argument **and** the second argument is not a parseable duration81// this is due to an unfortunate ambiguity in early version of Gomega in which multi-type durations are allowed after the actual82if _, err := toDuration(args[0]); err != nil {83ctx = actualOrCtx.(context.Context)84actual = args[0]85startingIndex = 186}87}8889for _, arg := range args[startingIndex:] {90switch v := arg.(type) {91case context.Context:92ctx = v93default:94intervals = append(intervals, arg)95}96}97var err error98if len(intervals) > 0 {99timeoutInterval, err = toDuration(intervals[0])100if err != nil {101g.Fail(err.Error(), offset+baseOffset)102}103}104if len(intervals) > 1 {105pollingInterval, err = toDuration(intervals[1])106if err != nil {107g.Fail(err.Error(), offset+baseOffset)108}109}110111return NewAsyncAssertion(asyncAssertionType, actual, g, timeoutInterval, pollingInterval, 1, ctx, offset)112}113114func (g *Gomega) SetDefaultEventuallyTimeout(t time.Duration) {115g.DurationBundle.EventuallyTimeout = t116}117118func (g *Gomega) SetDefaultEventuallyPollingInterval(t time.Duration) {119g.DurationBundle.EventuallyPollingInterval = t120}121122func (g *Gomega) SetDefaultConsistentlyDuration(t time.Duration) {123g.DurationBundle.ConsistentlyDuration = t124}125126func (g *Gomega) SetDefaultConsistentlyPollingInterval(t time.Duration) {127g.DurationBundle.ConsistentlyPollingInterval = t128}129130func (g *Gomega) EnforceDefaultTimeoutsWhenUsingContexts() {131g.DurationBundle.EnforceDefaultTimeoutsWhenUsingContexts = true132}133134func (g *Gomega) DisableDefaultTimeoutsWhenUsingContext() {135g.DurationBundle.EnforceDefaultTimeoutsWhenUsingContexts = false136}137138139