github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/test/case.go (about)

     1  package test
     2  
     3  import (
     4  	"testing"
     5  	"github.com/v2pro/plz/countlog"
     6  	"context"
     7  	"github.com/v2pro/plz/gls"
     8  	"reflect"
     9  )
    10  
    11  var testingTType = reflect.TypeOf((*testing.T)(nil))
    12  
    13  func Case(testCase func(ctx *countlog.Context)) func(t *testing.T) {
    14  	return func(t *testing.T) {
    15  		goid := gls.GoID()
    16  		gls.ResetGls(goid, map[interface{}]interface{}{
    17  			testingTType: t,
    18  		})
    19  		ctx := countlog.Ctx(context.Background())
    20  		defer func() {
    21  			gls.DeleteGls(goid)
    22  			//if t.Failed() {
    23  			//	ctx.LogAccess("test failed", errors.New(""))
    24  			//}
    25  		}()
    26  		testCase(ctx)
    27  	}
    28  }
    29  
    30  func Skip(args ...interface{}) {
    31  	CurrentT().Skip(args...)
    32  }
    33  
    34  func Skipf(format string, args ...interface{}) {
    35  	CurrentT().Skipf(format, args...)
    36  }
    37  
    38  func CurrentT() *testing.T {
    39  	t, found := gls.Get(testingTType).(*testing.T)
    40  	if !found {
    41  		panic("test not started with check.Case()")
    42  	}
    43  	return t
    44  }