Skip to content

Commit

Permalink
Merge fix cache values if use generic helper
Browse files Browse the repository at this point in the history
  • Loading branch information
rekby authored Jun 12, 2022
2 parents 5f7f77c + 39f16da commit 7f21018
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 28 deletions.
2 changes: 1 addition & 1 deletion env.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func makeCacheKey(testname string, params interface{}, opt *FixtureOptions, test
externalCallerLevel := 5
var pc = make([]uintptr, externalCallerLevel)
var extCallerFrame runtime.Frame
if externalCallerLevel == runtime.Callers(0, pc) {
if externalCallerLevel == runtime.Callers(opt.additionlSkipExternalCalls, pc) {
frames := runtime.CallersFrames(pc)
frames.Next() // callers
frames.Next() // the function
Expand Down
9 changes: 9 additions & 0 deletions env_generic_sugar.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package fixenv

func Cache[TRes any](env Env, params any, opt *FixtureOptions, f func() (TRes, error)) TRes {
addSkipLevel(&opt)
callbackResult := env.Cache(params, opt, func() (res interface{}, err error) {
return f()
})
Expand All @@ -16,6 +17,7 @@ func Cache[TRes any](env Env, params any, opt *FixtureOptions, f func() (TRes, e
}

func CacheWithCleanup[TRes any](env Env, params any, opt *FixtureOptions, f func() (TRes, FixtureCleanupFunc, error)) TRes {
addSkipLevel(&opt)
callbackResult := env.CacheWithCleanup(params, opt, func() (res interface{}, cleanup FixtureCleanupFunc, err error) {
return f()
})
Expand All @@ -26,3 +28,10 @@ func CacheWithCleanup[TRes any](env Env, params any, opt *FixtureOptions, f func
}
return res
}

func addSkipLevel(optspp **FixtureOptions) {
if *optspp == nil {
*optspp = &FixtureOptions{}
}
(*optspp).additionlSkipExternalCalls++
}
96 changes: 69 additions & 27 deletions env_generic_sugar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,84 @@ import (
)

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

env := envMock{onCache: func(params interface{}, opt *FixtureOptions, f FixtureCallbackFunc) interface{} {
require.Equal(t, inParams, params)
require.Equal(t, inOpt, opt)
res, _ := f()
return res
}}

res := Cache(env, inParams, inOpt, func() (int, error) {
return 2, nil
t.Run("PassParams", func(t *testing.T) {
inParams := 123
inOpt := &FixtureOptions{Scope: ScopeTest}

env := envMock{onCache: func(params interface{}, opt *FixtureOptions, f FixtureCallbackFunc) interface{} {
opt.additionlSkipExternalCalls--
require.Equal(t, inParams, params)
require.Equal(t, inOpt, opt)
res, _ := f()
return res
}}

res := Cache(env, inParams, inOpt, func() (int, error) {
return 2, nil
})
require.Equal(t, 2, res)
})
t.Run("SkipAdditionalCache", func(t *testing.T) {
test := &testMock{name: 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
})
}

require.Equal(t, 1, f1())
require.Equal(t, 2, f2())
})
require.Equal(t, 2, res)
}

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

cleanupCalledBack := 0

cleanupCalledBack := 0
env := envMock{onCacheWithCleanup: func(params interface{}, opt *FixtureOptions, f FixtureCallbackWithCleanupFunc) interface{} {
opt.additionlSkipExternalCalls--
require.Equal(t, inParams, params)
require.Equal(t, inOpt, opt)
res, _, _ := f()
return res
}}

env := envMock{onCacheWithCleanup: func(params interface{}, opt *FixtureOptions, f FixtureCallbackWithCleanupFunc) interface{} {
require.Equal(t, inParams, params)
require.Equal(t, inOpt, opt)
res, _, _ := f()
return res
}}
res := CacheWithCleanup(env, inParams, inOpt, func() (int, FixtureCleanupFunc, error) {
cleanup := func() {
cleanupCalledBack++
}
return 2, cleanup, nil
})
require.Equal(t, 2, res)
})
t.Run("SkipAdditionalCache", func(t *testing.T) {
test := &testMock{name: t.Name()}
env := newTestEnv(test)

res := CacheWithCleanup(env, inParams, inOpt, func() (int, FixtureCleanupFunc, error) {
cleanup := func() {
cleanupCalledBack++
f1 := func() int {
return CacheWithCleanup(env, nil, nil, func() (int, FixtureCleanupFunc, error) {
return 1, nil, nil
})
}
return 2, cleanup, nil
f2 := func() int {
return CacheWithCleanup(env, nil, nil, func() (int, FixtureCleanupFunc, error) {
return 2, nil, nil
})
}

require.Equal(t, 1, f1())
require.Equal(t, 2, f2())
})
require.Equal(t, 2, res)

}

Expand Down
2 changes: 2 additions & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ type FixtureOptions struct {
// Scope for cache result
Scope CacheScope

additionlSkipExternalCalls int

// cleanupFunc if not nil - called for cleanup fixture results
// internal implementation details
cleanupFunc FixtureCleanupFunc
Expand Down

0 comments on commit 7f21018

Please sign in to comment.