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 ... )
}
2023-01-30 20:05:01 +00:00
func ( g * Gomega ) Eventually ( actualOrCtx interface { } , args ... interface { } ) types . AsyncAssertion {
return g . makeAsyncAssertion ( AsyncAssertionTypeEventually , 0 , actualOrCtx , args ... )
2021-09-01 13:50:18 +00:00
}
2023-01-30 20:05:01 +00:00
func ( g * Gomega ) EventuallyWithOffset ( offset int , actualOrCtx interface { } , args ... interface { } ) types . AsyncAssertion {
return g . makeAsyncAssertion ( AsyncAssertionTypeEventually , offset , actualOrCtx , args ... )
2022-11-01 15:23:27 +00:00
}
2021-09-01 13:50:18 +00:00
2023-01-30 20:05:01 +00:00
func ( g * Gomega ) Consistently ( actualOrCtx interface { } , args ... interface { } ) types . AsyncAssertion {
return g . makeAsyncAssertion ( AsyncAssertionTypeConsistently , 0 , actualOrCtx , args ... )
2021-09-01 13:50:18 +00:00
}
2023-01-30 20:05:01 +00:00
func ( g * Gomega ) ConsistentlyWithOffset ( offset int , actualOrCtx interface { } , args ... interface { } ) types . AsyncAssertion {
return g . makeAsyncAssertion ( AsyncAssertionTypeConsistently , offset , actualOrCtx , args ... )
2021-09-01 13:50:18 +00:00
}
2023-01-30 20:05:01 +00:00
func ( g * Gomega ) makeAsyncAssertion ( asyncAssertionType AsyncAssertionType , offset int , actualOrCtx interface { } , args ... interface { } ) types . AsyncAssertion {
2022-11-01 15:23:27 +00:00
baseOffset := 3
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
2022-11-01 15:23:27 +00:00
2023-01-30 20:05:01 +00:00
actual := actualOrCtx
startingIndex := 0
if _ , isCtx := actualOrCtx . ( context . Context ) ; isCtx && len ( args ) > 0 {
2022-11-01 15:23:27 +00:00
// the first argument is a context, we should accept it as the context _only if_ it is **not** the only argumnent **and** the second argument is not a parseable duration
// this is due to an unfortunate ambiguity in early version of Gomega in which multi-type durations are allowed after the actual
2023-01-30 20:05:01 +00:00
if _ , err := toDuration ( args [ 0 ] ) ; err != nil {
ctx = actualOrCtx . ( context . Context )
actual = args [ 0 ]
startingIndex = 1
2022-11-01 15:23:27 +00:00
}
}
for _ , arg := range args [ startingIndex : ] {
2022-10-17 17:38:08 +00:00
switch v := arg . ( type ) {
case context . Context :
ctx = v
default :
intervals = append ( intervals , arg )
}
}
2022-11-01 15:23:27 +00:00
var err error
2021-09-01 13:50:18 +00:00
if len ( intervals ) > 0 {
2022-11-01 15:23:27 +00:00
timeoutInterval , err = toDuration ( intervals [ 0 ] )
if err != nil {
g . Fail ( err . Error ( ) , offset + baseOffset )
}
2021-09-01 13:50:18 +00:00
}
if len ( intervals ) > 1 {
2022-11-01 15:23:27 +00:00
pollingInterval , err = toDuration ( intervals [ 1 ] )
if err != nil {
g . Fail ( err . Error ( ) , offset + baseOffset )
}
2021-09-01 13:50:18 +00:00
}
2023-01-30 20:05:01 +00:00
return NewAsyncAssertion ( asyncAssertionType , actual , g , timeoutInterval , pollingInterval , 1 , 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
}
2024-11-04 20:10:45 +00:00
func ( g * Gomega ) EnforceDefaultTimeoutsWhenUsingContexts ( ) {
g . DurationBundle . EnforceDefaultTimeoutsWhenUsingContexts = true
}
func ( g * Gomega ) DisableDefaultTimeoutsWhenUsingContext ( ) {
g . DurationBundle . EnforceDefaultTimeoutsWhenUsingContexts = false
}