Skip to content

Commit

Permalink
Merge pull request #43 remove deprecated code
Browse files Browse the repository at this point in the history
  • Loading branch information
rekby authored Aug 9, 2024
2 parents 3b688c4 + c957670 commit f66bbf7
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 494 deletions.
69 changes: 0 additions & 69 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,6 @@ func New(t T) *EnvT {
return env
}

// NewEnv create EnvT from test
// Deprecated: use New instead
func NewEnv(t T) *EnvT {
return New(t)
}

func newEnv(t T, c *cache, m sync.Locker, scopes map[string]*scopeInfo) *EnvT {
return &EnvT{
t: t,
Expand All @@ -70,69 +64,6 @@ func (e *EnvT) T() T {
return e.t
}

// Cache call from fixture and manage call f and cache it.
// Cache must be called direct from fixture - it use runtime stacktrace for
// detect called method - it is part of cache key.
// params - part of cache key. Usually - parameters, passed to fixture.
//
// it allow use parametrized fixtures with different results.
// params must be json serializable.
//
// opt - fixture options, nil for default options.
// f - callback - fixture body.
// Cache guarantee for call f exactly once for same Cache called and params combination.
// Deprecated: will be removed in next versions.
// Use EnvT.CacheResult instead
func (e *EnvT) Cache(cacheKey interface{}, opt *FixtureOptions, f FixtureCallbackFunc) interface{} {
if opt == nil {
opt = &FixtureOptions{}
}
var fixtureFunc FixtureFunction = func() (*Result, error) {
res, err := f()
return NewResult(res), err
}

options := CacheOptions{
Scope: opt.Scope,
CacheKey: cacheKey,
additionlSkipExternalCalls: opt.additionlSkipExternalCalls,
}
return e.cache(fixtureFunc, options)
}

// CacheWithCleanup call from fixture and manage call f and cache it.
// CacheWithCleanup must be called direct from fixture - it use runtime stacktrace for
// detect called method - it is part of cache key.
// params - part of cache key. Usually - parameters, passed to fixture.
//
// it allow use parametrized fixtures with different results.
// params must be json serializable.
//
// opt - fixture options, nil for default options.
// f - callback - fixture body.
// cleanup, returned from f called while fixture cleanup
// Cache guarantee for call f exactly once for same Cache called and params combination.
// Deprecated: will be removed in next versions.
// Use EnvT.CacheResult instead
func (e *EnvT) CacheWithCleanup(cacheKey interface{}, opt *FixtureOptions, f FixtureCallbackWithCleanupFunc) interface{} {
if opt == nil {
opt = &FixtureOptions{}
}

var fixtureFunc FixtureFunction = func() (*Result, error) {
res, resCleanupFunc, err := f()
return NewResultWithCleanup(res, resCleanupFunc), err
}

options := CacheOptions{
Scope: opt.Scope,
CacheKey: cacheKey,
additionlSkipExternalCalls: opt.additionlSkipExternalCalls,
}

return e.cache(fixtureFunc, options)
}

// CacheResult call f callback once and cache result (ok and error),
// then return same result for all calls of the callback without additional calls
// f with same options calls max once per test (or defined test scope)
Expand Down
44 changes: 0 additions & 44 deletions env_generic_sugar.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,6 @@ package fixenv

import "fmt"

// Cache is call f once per cache scope (default per test) and cache result (success or error).
// All other calls of the f will return same result
// Deprecated: Use CacheResult
func Cache[TRes any](env Env, cacheKey any, opt *FixtureOptions, f func() (TRes, error)) TRes {
addSkipLevel(&opt)

res := CacheResult(env, func() (*GenericResult[TRes], error) {
fRes, err := f()
return NewGenericResult(fRes), err
}, CacheOptions{
Scope: opt.Scope,
CacheKey: cacheKey,
additionlSkipExternalCalls: opt.additionlSkipExternalCalls,
})

return res
}

// CacheWithCleanup is call f once per cache scope (default per test) and cache result (success or error).
// All other calls of the f will return same result.
// Used when fixture need own cleanup after exit from test scope
// Deprecated: Use CacheResult
func CacheWithCleanup[TRes any](env Env, cacheKey any, opt *FixtureOptions, f func() (TRes, FixtureCleanupFunc, error)) TRes {
addSkipLevel(&opt)

res := CacheResult(env, func() (*GenericResult[TRes], error) {
res, cleanup, err := f()
return NewGenericResultWithCleanup(res, cleanup), err
}, CacheOptions{
Scope: opt.Scope,
CacheKey: cacheKey,
additionlSkipExternalCalls: opt.additionlSkipExternalCalls,
})

return res
}

// CacheResult is call f once per cache scope (default per test) and cache result (success or error).
// All other calls of the f will return same result.
func CacheResult[TRes any](env Env, f GenericFixtureFunction[TRes], options ...CacheOptions) TRes {
Expand Down Expand Up @@ -91,13 +54,6 @@ func NewGenericResultWithCleanup[ResT any](res ResT, cleanup FixtureCleanupFunc)
return &GenericResult[ResT]{Value: res, ResultAdditional: ResultAdditional{Cleanup: cleanup}}
}

func addSkipLevel(optspp **FixtureOptions) {
if *optspp == nil {
*optspp = &FixtureOptions{}
}
(*optspp).additionlSkipExternalCalls++
}

func addSkipLevelCache(optspp *CacheOptions) {
(*optspp).additionlSkipExternalCalls++
}
96 changes: 1 addition & 95 deletions env_generic_sugar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,90 +10,6 @@ import (
"testing"
)

func TestCacheGeneric(t *testing.T) {
t.Run("PassParams", func(t *testing.T) {
inParams := 123
inOpt := &FixtureOptions{Scope: ScopeTest}

env := envMock{
onCacheResult: func(opts CacheOptions, f FixtureFunction) interface{} {
opts.additionlSkipExternalCalls--
requireEquals(t, inParams, opts.CacheKey)
requireEquals(t, inOpt.Scope, opts.Scope)
res, _ := f()
return res.Value
},
}

res := Cache(env, inParams, inOpt, func() (int, error) {
return 2, nil
})
requireEquals(t, 2, res)
})
t.Run("SkipAdditionalCache", func(t *testing.T) {
test := &internal.TestMock{TestName: t.Name()}
env := newTestEnv(test)

f1 := func() int {
return Cache(env, nil, nil, func() (int, error) {
return 1, nil
})
}
f2 := func() int {
return Cache(env, nil, nil, func() (int, error) {
return 2, nil
})
}

requireEquals(t, 1, f1())
requireEquals(t, 2, f2())
})
}

func TestCacheWithCleanupGeneric(t *testing.T) {
t.Run("PassParams", func(t *testing.T) {
inParams := 123
inOpt := &FixtureOptions{Scope: ScopeTest}

cleanupCalledBack := 0

env := envMock{
onCacheResult: func(opts CacheOptions, f FixtureFunction) interface{} {
requireEquals(t, inParams, opts.CacheKey)
requireEquals(t, inOpt.Scope, opts.Scope)
res, _ := f()
return res.Value
},
}

res := CacheWithCleanup(env, inParams, inOpt, func() (int, FixtureCleanupFunc, error) {
cleanup := func() {
cleanupCalledBack++
}
return 2, cleanup, nil
})
requireEquals(t, 2, res)
})
t.Run("SkipAdditionalCache", func(t *testing.T) {
test := &internal.TestMock{TestName: t.Name()}
env := newTestEnv(test)

f1 := func() int {
return CacheWithCleanup(env, nil, nil, func() (int, FixtureCleanupFunc, error) {
return 1, nil, nil
})
}
f2 := func() int {
return CacheWithCleanup(env, nil, nil, func() (int, FixtureCleanupFunc, error) {
return 2, nil, nil
})
}

requireEquals(t, 1, f1())
requireEquals(t, 2, f2())
})
}

func TestCacheResultGeneric(t *testing.T) {
t.Run("PassParams", func(t *testing.T) {
inOpt := CacheOptions{
Expand Down Expand Up @@ -188,23 +104,13 @@ func TestCacheResultPanic(t *testing.T) {
}

type envMock struct {
onCache func(params interface{}, opt *FixtureOptions, f FixtureCallbackFunc) interface{}
onCacheWithCleanup func(params interface{}, opt *FixtureOptions, f FixtureCallbackWithCleanupFunc) interface{}
onCacheResult func(opts CacheOptions, f FixtureFunction) interface{}
onCacheResult func(opts CacheOptions, f FixtureFunction) interface{}
}

func (e envMock) T() T {
panic("not implemented")
}

func (e envMock) Cache(params interface{}, opt *FixtureOptions, f FixtureCallbackFunc) interface{} {
return e.onCache(params, opt, f)
}

func (e envMock) CacheWithCleanup(params interface{}, opt *FixtureOptions, f FixtureCallbackWithCleanupFunc) interface{} {
return e.onCacheWithCleanup(params, opt, f)
}

func (e envMock) CacheResult(f FixtureFunction, options ...CacheOptions) interface{} {
var opts CacheOptions
switch len(options) {
Expand Down
Loading

0 comments on commit f66bbf7

Please sign in to comment.