-
Notifications
You must be signed in to change notification settings - Fork 1
/
env_generic_sugar_test.go
125 lines (108 loc) · 2.93 KB
/
env_generic_sugar_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//go:build go1.18
// +build go1.18
package fixenv
import (
"fmt"
"github.com/rekby/fixenv/internal"
"math/rand"
"testing"
)
func TestCacheResultGeneric(t *testing.T) {
t.Run("PassParams", func(t *testing.T) {
inOpt := CacheOptions{
CacheKey: 123,
Scope: ScopeTest,
}
cleanupCalledBack := 0
env := envMock{onCacheResult: func(opt CacheOptions, f FixtureFunction) interface{} {
opt.additionlSkipExternalCalls--
requireEquals(t, inOpt, opt)
res, _ := f()
return res.Value
}}
f := func() (*GenericResult[int], error) {
cleanup := func() {
cleanupCalledBack++
}
return NewGenericResultWithCleanup(2, cleanup), nil
}
res := CacheResult(env, f, inOpt)
requireEquals(t, 2, res)
})
t.Run("SkipAdditionalCache", func(t *testing.T) {
test := &internal.TestMock{TestName: t.Name()}
env := newTestEnv(test)
f1 := func() int {
return CacheResult(env, func() (*GenericResult[int], error) {
return NewGenericResult(1), nil
})
}
f2 := func() int {
return CacheResult(env, func() (*GenericResult[int], error) {
return NewGenericResult(2), nil
})
}
requireEquals(t, 1, f1())
requireEquals(t, 2, f2())
})
}
func TestCacheResultPanic(t *testing.T) {
t.Run("Simple", func(t *testing.T) {
tMock := &internal.TestMock{TestName: "mock", SkipGoexit: true}
e := New(tMock)
rndFix := func(e Env) int {
return CacheResult(e, func() (*GenericResult[int], error) {
return NewGenericResult(rand.Int()), nil
})
}
first := rndFix(e)
second := rndFix(e)
requireEquals(t, first, second)
})
t.Run("Options", func(t *testing.T) {
tMock := &internal.TestMock{TestName: "mock", SkipGoexit: true}
e := New(tMock)
rndFix := func(e Env, name string) int {
return CacheResult(e, func() (*GenericResult[int], error) {
return NewGenericResult(rand.Int()), nil
}, CacheOptions{CacheKey: name})
}
first1 := rndFix(e, "first")
first2 := rndFix(e, "first")
second1 := rndFix(e, "second")
second2 := rndFix(e, "second")
requireEquals(t, first1, first2)
requireEquals(t, second1, second2)
requireNotEquals(t, first1, second1)
})
t.Run("Panic", func(t *testing.T) {
tMock := &internal.TestMock{TestName: "mock", SkipGoexit: true}
e := New(tMock)
rndFix := func(e Env, name string) int {
return CacheResult(e, func() (*GenericResult[int], error) {
return NewGenericResult(rand.Int()), nil
}, CacheOptions{CacheKey: name}, CacheOptions{CacheKey: name})
}
requirePanic(t, func() {
rndFix(e, "first")
})
})
}
type envMock struct {
onCacheResult func(opts CacheOptions, f FixtureFunction) interface{}
}
func (e envMock) T() T {
panic("not implemented")
}
func (e envMock) CacheResult(f FixtureFunction, options ...CacheOptions) interface{} {
var opts CacheOptions
switch len(options) {
case 0:
// pass
case 1:
opts = options[0]
default:
panic(fmt.Errorf("max options len is 1, given: %v", len(options)))
}
return e.onCacheResult(opts, f)
}