-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenv_generic_sugar_test.go
222 lines (191 loc) · 5.53 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//go:build go1.18
// +build go1.18
package fixenv
import (
"fmt"
"github.com/rekby/fixenv/internal"
"github.com/stretchr/testify/assert"
"math/rand"
"testing"
"github.com/stretchr/testify/require"
)
func TestCacheGeneric(t *testing.T) {
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 := &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
})
}
require.Equal(t, 1, f1())
require.Equal(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{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
}}
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 := &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
})
}
require.Equal(t, 1, f1())
require.Equal(t, 2, f2())
})
}
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--
require.Equal(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)
require.Equal(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
})
}
require.Equal(t, 1, f1())
require.Equal(t, 2, f2())
})
}
func TestCacheResultPanic(t *testing.T) {
t.Run("Simple", func(t *testing.T) {
at := assert.New(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)
at.Equal(first, second)
})
t.Run("Options", func(t *testing.T) {
at := assert.New(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")
at.Equal(first1, first2)
at.Equal(second1, second2)
at.NotEqual(first1, second1)
})
t.Run("Panic", func(t *testing.T) {
at := assert.New(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})
}
at.Panics(func() {
rndFix(e, "first")
})
})
}
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{}
}
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) {
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)
}