77 "testing"
88
99 "github.com/stretchr/testify/assert"
10+ "github.com/stretchr/testify/require"
1011)
1112
1213type testMock struct {
@@ -278,6 +279,58 @@ func Test_Env_Cache(t *testing.T) {
278279 })
279280}
280281
282+ func Test_Env_CacheWithCleanup (t * testing.T ) {
283+ t .Run ("NilCleanup" , func (t * testing.T ) {
284+ tMock := & testMock {name : t .Name ()}
285+ env := newTestEnv (tMock )
286+
287+ callbackCalled := 0
288+ var callbackFunc FixtureCallbackWithCleanupFunc = func () (res interface {}, cleanup FixtureCleanupFunc , err error ) {
289+ callbackCalled ++
290+ return callbackCalled , nil , nil
291+ }
292+
293+ res := env .CacheWithCleanup (nil , nil , callbackFunc )
294+ require .Equal (t , 1 , res )
295+ require .Equal (t , 1 , callbackCalled )
296+
297+ // got value from cache
298+ res = env .CacheWithCleanup (nil , nil , callbackFunc )
299+ require .Equal (t , 1 , res )
300+ require .Equal (t , 1 , callbackCalled )
301+ })
302+
303+ t .Run ("WithCleanup" , func (t * testing.T ) {
304+ tMock := & testMock {name : t .Name ()}
305+ env := newTestEnv (tMock )
306+
307+ callbackCalled := 0
308+ cleanupCalled := 0
309+ var callbackFunc FixtureCallbackWithCleanupFunc = func () (res interface {}, cleanup FixtureCleanupFunc , err error ) {
310+ callbackCalled ++
311+ cleanup = func () {
312+ cleanupCalled ++
313+ }
314+ return callbackCalled , cleanup , nil
315+ }
316+
317+ res := env .CacheWithCleanup (nil , nil , callbackFunc )
318+ require .Equal (t , 1 , res )
319+ require .Equal (t , 1 , callbackCalled )
320+ require .Equal (t , cleanupCalled , 0 )
321+
322+ // got value from cache
323+ res = env .CacheWithCleanup (nil , nil , callbackFunc )
324+ require .Equal (t , 1 , res )
325+ require .Equal (t , 1 , callbackCalled )
326+ require .Equal (t , cleanupCalled , 0 )
327+
328+ tMock .callCleanup ()
329+ require .Equal (t , 1 , callbackCalled )
330+ require .Equal (t , 1 , cleanupCalled )
331+ })
332+ }
333+
281334func Test_FixtureWrapper (t * testing.T ) {
282335 t .Run ("ok" , func (t * testing.T ) {
283336 at := assert .New (t )
@@ -293,7 +346,7 @@ func Test_FixtureWrapper(t *testing.T) {
293346 cnt ++
294347 return cnt , errors .New ("test" )
295348 }, & FixtureOptions {})
296- si := e .scopes [scopeName (tMock .Name (), ScopeTest )]
349+ si := e .scopes [makeScopeName (tMock .Name (), ScopeTest )]
297350 at .Equal (0 , cnt )
298351 at .Len (si .cacheKeys , 0 )
299352 res1 , err := w ()
@@ -308,7 +361,7 @@ func Test_FixtureWrapper(t *testing.T) {
308361 w = e .fixtureCallWrapper (key2 , func () (res interface {}, err error ) {
309362 cnt ++
310363 return cnt , nil
311- }, & FixtureOptions {CleanupFunc : func () {
364+ }, & FixtureOptions {cleanupFunc : func () {
312365
313366 }})
314367 at .Len (tMock .cleanups , cleanupsLen )
@@ -416,7 +469,7 @@ func Test_Env_TearDown(t *testing.T) {
416469
417470 e1 := newTestEnv (t1 )
418471 at .Len (e1 .scopes , 1 )
419- at .Len (e1 .scopes [scopeName (t1 .name , ScopeTest )].Keys (), 0 )
472+ at .Len (e1 .scopes [makeScopeName (t1 .name , ScopeTest )].Keys (), 0 )
420473 at .Len (e1 .c .store , 0 )
421474
422475 e1 .Cache (1 , nil , func () (res interface {}, err error ) {
@@ -426,31 +479,31 @@ func Test_Env_TearDown(t *testing.T) {
426479 return nil , nil
427480 })
428481 at .Len (e1 .scopes , 1 )
429- at .Len (e1 .scopes [scopeName (t1 .name , ScopeTest )].Keys (), 2 )
482+ at .Len (e1 .scopes [makeScopeName (t1 .name , ScopeTest )].Keys (), 2 )
430483 at .Len (e1 .c .store , 2 )
431484
432485 t2 := & testMock {name : "mock2" }
433486 // defer t2.callCleanup - direct call e2.tearDown - for test
434487
435488 e2 := e1 .cloneWithTest (t2 )
436489 at .Len (e1 .scopes , 2 )
437- at .Len (e1 .scopes [scopeName (t1 .name , ScopeTest )].Keys (), 2 )
438- at .Len (e1 .scopes [scopeName (t2 .name , ScopeTest )].Keys (), 0 )
490+ at .Len (e1 .scopes [makeScopeName (t1 .name , ScopeTest )].Keys (), 2 )
491+ at .Len (e1 .scopes [makeScopeName (t2 .name , ScopeTest )].Keys (), 0 )
439492 at .Len (e1 .c .store , 2 )
440493
441494 e2 .Cache (1 , nil , func () (res interface {}, err error ) {
442495 return nil , nil
443496 })
444497
445498 at .Len (e1 .scopes , 2 )
446- at .Len (e1 .scopes [scopeName (t1 .name , ScopeTest )].Keys (), 2 )
447- at .Len (e1 .scopes [scopeName (t2 .name , ScopeTest )].Keys (), 1 )
499+ at .Len (e1 .scopes [makeScopeName (t1 .name , ScopeTest )].Keys (), 2 )
500+ at .Len (e1 .scopes [makeScopeName (t2 .name , ScopeTest )].Keys (), 1 )
448501 at .Len (e1 .c .store , 3 )
449502
450503 // finish first test and tearDown e1
451504 e1 .tearDown ()
452505 at .Len (e1 .scopes , 1 )
453- at .Len (e1 .scopes [scopeName (t2 .name , ScopeTest )].Keys (), 1 )
506+ at .Len (e1 .scopes [makeScopeName (t2 .name , ScopeTest )].Keys (), 1 )
454507 at .Len (e1 .c .store , 1 )
455508
456509 e2 .tearDown ()
@@ -479,10 +532,14 @@ func Test_MakeCacheKey(t *testing.T) {
479532 var res cacheKey
480533 var err error
481534
482- envFunc := func () {
535+ privateEnvFunc := func () {
483536 res , err = makeCacheKey ("asdf" , 222 , globalEmptyFixtureOptions , true )
484537 }
485- envFunc ()
538+
539+ publicEnvFunc := func () {
540+ privateEnvFunc ()
541+ }
542+ publicEnvFunc () // external caller
486543 at .NoError (err )
487544
488545 expected := cacheKey (`{"func":"github.com/rekby/fixenv.Test_MakeCacheKey","fname":".../env_test.go","scope":0,"scope_name":"asdf","params":222}` )
@@ -601,15 +658,15 @@ func Test_ScopeName(t *testing.T) {
601658 for _ , c := range table {
602659 t .Run (c .name , func (t * testing.T ) {
603660 at := assert .New (t )
604- at .Equal (c .result , scopeName (c .testName , c .scope ))
661+ at .Equal (c .result , makeScopeName (c .testName , c .scope ))
605662 })
606663 }
607664 })
608665
609666 t .Run ("unexpected_scope" , func (t * testing.T ) {
610667 at := assert .New (t )
611668 at .Panics (func () {
612- scopeName ("asd" , - 1 )
669+ makeScopeName ("asd" , - 1 )
613670 })
614671 })
615672}
0 commit comments