Files
imgproxy/testutil/lazy_obj.go
Victor Sokolov 2d9ad5c250 IMG-49: Introduced instance (#1512)
* Introduced instance

* Makefile changes
2025-09-09 15:11:37 +02:00

33 lines
622 B
Go

package testutil
import (
"testing"
"github.com/stretchr/testify/require"
)
// LazyObj is a function that returns an object of type T.
type LazyObj[T any] func() T
// LazyObjInit is a function that initializes and returns an object of type T and an error if any.
type LazyObjInit[T any] func() (T, error)
// NewLazyObj creates a new LazyObj that initializes the object on the first call.
func NewLazyObj[T any](t *testing.T, init LazyObjInit[T]) LazyObj[T] {
t.Helper()
var obj *T
return func() T {
if obj != nil {
return *obj
}
o, err := init()
require.NoError(t, err)
obj = &o
return o
}
}