Skip to content

Commit 7f21018

Browse files
authored
Merge fix cache values if use generic helper
2 parents 5f7f77c + 39f16da commit 7f21018

File tree

4 files changed

+81
-28
lines changed

4 files changed

+81
-28
lines changed

env.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func makeCacheKey(testname string, params interface{}, opt *FixtureOptions, test
159159
externalCallerLevel := 5
160160
var pc = make([]uintptr, externalCallerLevel)
161161
var extCallerFrame runtime.Frame
162-
if externalCallerLevel == runtime.Callers(0, pc) {
162+
if externalCallerLevel == runtime.Callers(opt.additionlSkipExternalCalls, pc) {
163163
frames := runtime.CallersFrames(pc)
164164
frames.Next() // callers
165165
frames.Next() // the function

env_generic_sugar.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package fixenv
55

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

1819
func CacheWithCleanup[TRes any](env Env, params any, opt *FixtureOptions, f func() (TRes, FixtureCleanupFunc, error)) TRes {
20+
addSkipLevel(&opt)
1921
callbackResult := env.CacheWithCleanup(params, opt, func() (res interface{}, cleanup FixtureCleanupFunc, err error) {
2022
return f()
2123
})
@@ -26,3 +28,10 @@ func CacheWithCleanup[TRes any](env Env, params any, opt *FixtureOptions, f func
2628
}
2729
return res
2830
}
31+
32+
func addSkipLevel(optspp **FixtureOptions) {
33+
if *optspp == nil {
34+
*optspp = &FixtureOptions{}
35+
}
36+
(*optspp).additionlSkipExternalCalls++
37+
}

env_generic_sugar_test.go

Lines changed: 69 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,84 @@ import (
1010
)
1111

1212
func TestCacheGeneric(t *testing.T) {
13-
inParams := 123
14-
inOpt := &FixtureOptions{Scope: ScopeTest}
15-
16-
env := envMock{onCache: func(params interface{}, opt *FixtureOptions, f FixtureCallbackFunc) interface{} {
17-
require.Equal(t, inParams, params)
18-
require.Equal(t, inOpt, opt)
19-
res, _ := f()
20-
return res
21-
}}
22-
23-
res := Cache(env, inParams, inOpt, func() (int, error) {
24-
return 2, nil
13+
t.Run("PassParams", func(t *testing.T) {
14+
inParams := 123
15+
inOpt := &FixtureOptions{Scope: ScopeTest}
16+
17+
env := envMock{onCache: func(params interface{}, opt *FixtureOptions, f FixtureCallbackFunc) interface{} {
18+
opt.additionlSkipExternalCalls--
19+
require.Equal(t, inParams, params)
20+
require.Equal(t, inOpt, opt)
21+
res, _ := f()
22+
return res
23+
}}
24+
25+
res := Cache(env, inParams, inOpt, func() (int, error) {
26+
return 2, nil
27+
})
28+
require.Equal(t, 2, res)
29+
})
30+
t.Run("SkipAdditionalCache", func(t *testing.T) {
31+
test := &testMock{name: t.Name()}
32+
env := newTestEnv(test)
33+
34+
f1 := func() int {
35+
return Cache(env, nil, nil, func() (int, error) {
36+
return 1, nil
37+
})
38+
}
39+
f2 := func() int {
40+
return Cache(env, nil, nil, func() (int, error) {
41+
return 2, nil
42+
})
43+
}
44+
45+
require.Equal(t, 1, f1())
46+
require.Equal(t, 2, f2())
2547
})
26-
require.Equal(t, 2, res)
2748
}
2849

2950
func TestCacheWithCleanupGeneric(t *testing.T) {
30-
inParams := 123
31-
inOpt := &FixtureOptions{Scope: ScopeTest}
51+
t.Run("PassParams", func(t *testing.T) {
52+
inParams := 123
53+
inOpt := &FixtureOptions{Scope: ScopeTest}
54+
55+
cleanupCalledBack := 0
3256

33-
cleanupCalledBack := 0
57+
env := envMock{onCacheWithCleanup: func(params interface{}, opt *FixtureOptions, f FixtureCallbackWithCleanupFunc) interface{} {
58+
opt.additionlSkipExternalCalls--
59+
require.Equal(t, inParams, params)
60+
require.Equal(t, inOpt, opt)
61+
res, _, _ := f()
62+
return res
63+
}}
3464

35-
env := envMock{onCacheWithCleanup: func(params interface{}, opt *FixtureOptions, f FixtureCallbackWithCleanupFunc) interface{} {
36-
require.Equal(t, inParams, params)
37-
require.Equal(t, inOpt, opt)
38-
res, _, _ := f()
39-
return res
40-
}}
65+
res := CacheWithCleanup(env, inParams, inOpt, func() (int, FixtureCleanupFunc, error) {
66+
cleanup := func() {
67+
cleanupCalledBack++
68+
}
69+
return 2, cleanup, nil
70+
})
71+
require.Equal(t, 2, res)
72+
})
73+
t.Run("SkipAdditionalCache", func(t *testing.T) {
74+
test := &testMock{name: t.Name()}
75+
env := newTestEnv(test)
4176

42-
res := CacheWithCleanup(env, inParams, inOpt, func() (int, FixtureCleanupFunc, error) {
43-
cleanup := func() {
44-
cleanupCalledBack++
77+
f1 := func() int {
78+
return CacheWithCleanup(env, nil, nil, func() (int, FixtureCleanupFunc, error) {
79+
return 1, nil, nil
80+
})
4581
}
46-
return 2, cleanup, nil
82+
f2 := func() int {
83+
return CacheWithCleanup(env, nil, nil, func() (int, FixtureCleanupFunc, error) {
84+
return 2, nil, nil
85+
})
86+
}
87+
88+
require.Equal(t, 1, f1())
89+
require.Equal(t, 2, f2())
4790
})
48-
require.Equal(t, 2, res)
4991

5092
}
5193

interface.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ type FixtureOptions struct {
7272
// Scope for cache result
7373
Scope CacheScope
7474

75+
additionlSkipExternalCalls int
76+
7577
// cleanupFunc if not nil - called for cleanup fixture results
7678
// internal implementation details
7779
cleanupFunc FixtureCleanupFunc

0 commit comments

Comments
 (0)