@@ -2,7 +2,7 @@ package fixenv
22
33import (
44 "errors"
5- "fmt "
5+ "github.com/rekby/fixenv/internal "
66 "runtime"
77 "sync"
88 "testing"
@@ -11,25 +11,6 @@ import (
1111 "github.com/stretchr/testify/require"
1212)
1313
14- type testMock struct {
15- TestName string
16- SkipGoexit bool
17-
18- m sync.Mutex
19- cleanups []func ()
20- logs []struct {
21- format string
22- args []interface {}
23- resultString string
24- }
25- fatals []struct {
26- format string
27- args []interface {}
28- resultString string
29- }
30- skipCount int
31- }
32-
3314func (e * EnvT ) cloneWithTest (t T ) * EnvT {
3415 e2 := newEnv (t , e .c , e .m , e .scopes )
3516 e2 .onCreate ()
@@ -42,80 +23,13 @@ func newTestEnv(t T) *EnvT {
4223 return e
4324}
4425
45- func (t * testMock ) callCleanup () {
46- for i := len (t .cleanups ) - 1 ; i >= 0 ; i -- {
47- t .cleanups [i ]()
48- }
49- }
50-
51- func (t * testMock ) Cleanup (f func ()) {
52- t .m .Lock ()
53- defer t .m .Unlock ()
54-
55- t .cleanups = append (t .cleanups , f )
56- }
57-
58- func (t * testMock ) Fatalf (format string , args ... interface {}) {
59- t .m .Lock ()
60- defer t .m .Unlock ()
61-
62- t .fatals = append (t .fatals , struct {
63- format string
64- args []interface {}
65- resultString string
66- }{
67- format : format ,
68- args : args ,
69- resultString : fmt .Sprintf (format , args ... ),
70- })
71-
72- if ! t .SkipGoexit {
73- runtime .Goexit ()
74- }
75- }
76-
77- func (t * testMock ) Logf (format string , args ... interface {}) {
78- t .m .Lock ()
79- defer t .m .Unlock ()
80-
81- t .logs = append (t .logs , struct {
82- format string
83- args []interface {}
84- resultString string
85- }{format : format , args : args , resultString : fmt .Sprintf (format , args ... )})
86- }
87-
88- func (t * testMock ) Name () string {
89- if t .TestName == "" {
90- return "mock"
91- }
92- return t .TestName
93- }
94-
95- func (t * testMock ) SkipNow () {
96- t .m .Lock ()
97- t .skipCount ++
98- t .m .Unlock ()
99-
100- if ! t .SkipGoexit {
101- runtime .Goexit ()
102- }
103- }
104-
105- func (t * testMock ) Skipped () bool {
106- t .m .Lock ()
107- defer t .m .Unlock ()
108-
109- return t .skipCount > 0
110- }
111-
11226func Test_Env__NewEnv (t * testing.T ) {
11327 t .Run ("create_new_env" , func (t * testing.T ) {
11428 initGlobalState ()
11529 at := assert .New (t )
11630
117- tMock := & testMock {TestName : "mock" }
118- defer tMock .callCleanup ()
31+ tMock := & internal. TestMock {TestName : "mock" }
32+ defer tMock .CallCleanup ()
11933
12034 e := New (tMock )
12135 at .Equal (tMock , e .t )
@@ -124,7 +38,7 @@ func Test_Env__NewEnv(t *testing.T) {
12438 at .Equal (globalScopeInfo , e .scopes )
12539 at .Len (globalCache .store , 0 )
12640 at .Len (globalScopeInfo , 1 )
127- at .Len (tMock .cleanups , 1 )
41+ at .Len (tMock .Cleanups , 1 )
12842 })
12943
13044 t .Run ("global_info_cleaned" , func (t * testing.T ) {
@@ -136,16 +50,16 @@ func Test_Env__NewEnv(t *testing.T) {
13650 t .Run ("double_env_same_scope_same_time" , func (t * testing.T ) {
13751 at := assert .New (t )
13852
139- tMock := & testMock {TestName : "mock" }
140- defer tMock .callCleanup ()
53+ tMock := & internal. TestMock {TestName : "mock" }
54+ defer tMock .CallCleanup ()
14155
14256 _ = New (tMock )
143- at .Len (tMock .fatals , 0 )
57+ at .Len (tMock .Fatals , 0 )
14458
14559 runUntilFatal (func () {
14660 _ = New (tMock )
14761 })
148- at .Len (tMock .fatals , 1 )
62+ at .Len (tMock .Fatals , 1 )
14963 })
15064
15165 t .Run ("double_env_similar_scope_different_time" , func (t * testing.T ) {
@@ -245,19 +159,19 @@ func Test_Env_Cache(t *testing.T) {
245159 t .Run ("fail_on_fixture_err" , func (t * testing.T ) {
246160 at := assert .New (t )
247161
248- tMock := & testMock {TestName : "mock" }
249- defer tMock .callCleanup ()
162+ tMock := & internal. TestMock {TestName : "mock" }
163+ defer tMock .CallCleanup ()
250164
251165 e := newTestEnv (tMock )
252- at .Len (tMock .fatals , 0 )
166+ at .Len (tMock .Fatals , 0 )
253167
254168 runUntilFatal (func () {
255169 testFailedFixture (e )
256170 })
257- at .Len (tMock .fatals , 1 )
171+ at .Len (tMock .Fatals , 1 )
258172
259173 // log message contains fixture name
260- at .Contains (tMock .fatals [0 ].resultString , "testFailedFixture" )
174+ at .Contains (tMock .Fatals [0 ].ResultString , "testFailedFixture" )
261175 })
262176
263177 t .Run ("not_serializable_param" , func (t * testing.T ) {
@@ -267,20 +181,20 @@ func Test_Env_Cache(t *testing.T) {
267181 F func () // can't serialize func to json
268182 }
269183 param := paramT {}
270- tMock := & testMock {TestName : "mock" }
271- defer tMock .callCleanup ()
184+ tMock := & internal. TestMock {TestName : "mock" }
185+ defer tMock .CallCleanup ()
272186 e := newTestEnv (tMock )
273187 runUntilFatal (func () {
274188 e .Cache (param , nil , func () (res interface {}, err error ) {
275189 return nil , nil
276190 })
277191 })
278- at .Len (tMock .fatals , 1 )
192+ at .Len (tMock .Fatals , 1 )
279193 })
280194
281195 t .Run ("cache_by_caller_func" , func (t * testing.T ) {
282196 at := assert .New (t )
283- tMock := & testMock {TestName : "mock" }
197+ tMock := & internal. TestMock {TestName : "mock" }
284198 e := newTestEnv (tMock )
285199
286200 cnt := 0
@@ -299,7 +213,7 @@ func Test_Env_Cache(t *testing.T) {
299213
300214 t .Run ("different_cache_for_diff_anonim_function" , func (t * testing.T ) {
301215 at := assert .New (t )
302- tMock := & testMock {TestName : "mock" }
216+ tMock := & internal. TestMock {TestName : "mock" }
303217 e := newTestEnv (tMock )
304218
305219 cnt := 0
@@ -324,20 +238,20 @@ func Test_Env_Cache(t *testing.T) {
324238 t .Run ("check_unreachable_code" , func (t * testing.T ) {
325239 at := assert .New (t )
326240
327- tMock := & testMock {TestName : "mock" , SkipGoexit : true }
241+ tMock := & internal. TestMock {TestName : "mock" , SkipGoexit : true }
328242 e := New (tMock )
329243 at .Panics (func () {
330244 e .Cache (nil , nil , func () (res interface {}, err error ) {
331245 return nil , ErrSkipTest
332246 })
333247 })
334- at .Equal (1 , tMock .skipCount )
248+ at .Equal (1 , tMock .SkipCount )
335249 })
336250}
337251
338252func Test_Env_CacheWithCleanup (t * testing.T ) {
339253 t .Run ("NilCleanup" , func (t * testing.T ) {
340- tMock := & testMock {TestName : t .Name ()}
254+ tMock := & internal. TestMock {TestName : t .Name ()}
341255 env := newTestEnv (tMock )
342256
343257 callbackCalled := 0
@@ -357,7 +271,7 @@ func Test_Env_CacheWithCleanup(t *testing.T) {
357271 })
358272
359273 t .Run ("WithCleanup" , func (t * testing.T ) {
360- tMock := & testMock {TestName : t .Name ()}
274+ tMock := & internal. TestMock {TestName : t .Name ()}
361275 env := newTestEnv (tMock )
362276
363277 callbackCalled := 0
@@ -381,7 +295,7 @@ func Test_Env_CacheWithCleanup(t *testing.T) {
381295 require .Equal (t , 1 , callbackCalled )
382296 require .Equal (t , cleanupCalled , 0 )
383297
384- tMock .callCleanup ()
298+ tMock .CallCleanup ()
385299 require .Equal (t , 1 , callbackCalled )
386300 require .Equal (t , 1 , cleanupCalled )
387301 })
@@ -391,8 +305,8 @@ func Test_FixtureWrapper(t *testing.T) {
391305 t .Run ("ok" , func (t * testing.T ) {
392306 at := assert .New (t )
393307
394- tMock := & testMock {TestName : "mock" }
395- defer tMock .callCleanup ()
308+ tMock := & internal. TestMock {TestName : "mock" }
309+ defer tMock .CallCleanup ()
396310
397311 e := newTestEnv (tMock )
398312 key := cacheKey ("asd" )
@@ -413,24 +327,24 @@ func Test_FixtureWrapper(t *testing.T) {
413327
414328 cnt = 0
415329 key2 := cacheKey ("asd" )
416- cleanupsLen := len (tMock .cleanups )
330+ cleanupsLen := len (tMock .Cleanups )
417331 w = e .fixtureCallWrapper (key2 , func () (res interface {}, err error ) {
418332 cnt ++
419333 return cnt , nil
420334 }, & FixtureOptions {cleanupFunc : func () {
421335
422336 }})
423- at .Len (tMock .cleanups , cleanupsLen )
337+ at .Len (tMock .Cleanups , cleanupsLen )
424338 _ , _ = w ()
425339 at .Equal ([]cacheKey {key , key2 }, si .cacheKeys )
426- at .Len (tMock .cleanups , cleanupsLen + 1 )
340+ at .Len (tMock .Cleanups , cleanupsLen + 1 )
427341 })
428342
429343 t .Run ("unknown_scope_info" , func (t * testing.T ) {
430344 at := assert .New (t )
431345
432- tMock := & testMock {TestName : "mock" }
433- defer tMock .callCleanup ()
346+ tMock := & internal. TestMock {TestName : "mock" }
347+ defer tMock .CallCleanup ()
434348 e := newTestEnv (tMock )
435349
436350 tMock .TestName = "mock2"
@@ -443,13 +357,13 @@ func Test_FixtureWrapper(t *testing.T) {
443357
444358 // revert test name for good cleanup
445359 tMock .TestName = "mock"
446- at .Len (tMock .fatals , 1 )
360+ at .Len (tMock .Fatals , 1 )
447361 })
448362}
449363
450364func Test_Env_Skip (t * testing.T ) {
451365 at := assert .New (t )
452- tm := & testMock {TestName : "mock" }
366+ tm := & internal. TestMock {TestName : "mock" }
453367 tEnv := newTestEnv (tm )
454368
455369 skipFixtureCallTimes := 0
@@ -524,7 +438,7 @@ func Test_Env_TearDown(t *testing.T) {
524438 t .Run ("ok" , func (t * testing.T ) {
525439 at := assert .New (t )
526440
527- t1 := & testMock {TestName : "mock" }
441+ t1 := & internal. TestMock {TestName : "mock" }
528442 // defer t1.callCleanup - direct call e1.tearDown - for test
529443
530444 e1 := newTestEnv (t1 )
@@ -542,7 +456,7 @@ func Test_Env_TearDown(t *testing.T) {
542456 at .Len (e1 .scopes [makeScopeName (t1 .TestName , ScopeTest )].Keys (), 2 )
543457 at .Len (e1 .c .store , 2 )
544458
545- t2 := & testMock {TestName : "mock2" }
459+ t2 := & internal. TestMock {TestName : "mock2" }
546460 // defer t2.callCleanup - direct call e2.tearDown - for test
547461
548462 e2 := e1 .cloneWithTest (t2 )
@@ -573,7 +487,7 @@ func Test_Env_TearDown(t *testing.T) {
573487
574488 t .Run ("tearDown on unexisted scope" , func (t * testing.T ) {
575489 at := assert .New (t )
576- tMock := & testMock {TestName : "mock" }
490+ tMock := & internal. TestMock {TestName : "mock" }
577491 // defer tMock.callCleanups. e.tearDown will call directly for test
578492 e := newTestEnv (tMock )
579493
@@ -583,7 +497,7 @@ func Test_Env_TearDown(t *testing.T) {
583497
584498 runUntilFatal (e .tearDown )
585499
586- at .Len (tMock .fatals , 1 )
500+ at .Len (tMock .Fatals , 1 )
587501 })
588502}
589503
@@ -733,12 +647,13 @@ func Test_ScopeName(t *testing.T) {
733647}
734648
735649func TestNewEnv (t * testing.T ) {
736- tm := & testMock {}
650+ tm := & internal. TestMock {}
737651 tm .SkipGoexit = true
738652 New (tm )
739653
654+ //goland:noinspection GoDeprecation
740655 NewEnv (tm )
741- if len (tm .fatals ) == 0 {
656+ if len (tm .Fatals ) == 0 {
742657 t .Fatal ("bad double login between new and NewEnv" )
743658 }
744659}
0 commit comments