github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/test.go (about)

     1  package zlsgo
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"runtime"
     7  	"strconv"
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  // TestUtil Test aid
    13  type TestUtil struct {
    14  	t testing.TB
    15  }
    16  
    17  // NewTest testing object
    18  func NewTest(t testing.TB) *TestUtil {
    19  	return &TestUtil{
    20  		t: t,
    21  	}
    22  }
    23  
    24  // GetCallerInfo GetCallerInfo
    25  func (u *TestUtil) GetCallerInfo() string {
    26  	var info string
    27  
    28  	for i := 0; ; i++ {
    29  		pc, file, line, ok := runtime.Caller(i)
    30  		if !ok {
    31  			break
    32  		}
    33  
    34  		basename := file
    35  		if !strings.HasSuffix(basename, "_test.go") {
    36  			continue
    37  		}
    38  
    39  		funcName := runtime.FuncForPC(pc).Name()
    40  		index := strings.LastIndex(funcName, ".Test")
    41  		if index == -1 {
    42  			index = strings.LastIndex(funcName, ".Benchmark")
    43  			if index == -1 {
    44  				continue
    45  			}
    46  		}
    47  		funcName = funcName[index+1:]
    48  
    49  		if index := strings.IndexByte(funcName, '.'); index > -1 {
    50  			// funcName = funcName[:index]
    51  			// info = funcName + "(" + basename + ":" + strconv.Itoa(line) + ")"
    52  			info = basename + ":" + strconv.Itoa(line)
    53  			continue
    54  		}
    55  
    56  		info = basename + ":" + strconv.Itoa(line)
    57  		break
    58  	}
    59  
    60  	if info == "" {
    61  		info = "<Unable to get information>"
    62  	}
    63  	return info
    64  }
    65  
    66  // Equal Equal
    67  func (u *TestUtil) Equal(expected, actual interface{}) bool {
    68  	if !reflect.DeepEqual(expected, actual) {
    69  		u.t.Helper()
    70  		fmt.Printf("        %s 期待:%v (type %v) - 结果:%v (type %v)\n", u.PrintMyName(), expected, reflect.TypeOf(expected), actual, reflect.TypeOf(actual))
    71  		u.t.Fail()
    72  		return false
    73  	}
    74  	return true
    75  }
    76  
    77  // EqualTrue EqualTrue
    78  func (u *TestUtil) EqualTrue(actual interface{}) {
    79  	u.Equal(true, actual)
    80  }
    81  
    82  // EqualNil EqualNil
    83  func (u *TestUtil) EqualNil(actual interface{}) {
    84  	u.Equal(nil, actual)
    85  }
    86  
    87  // NoError NoError
    88  func (u *TestUtil) NoError(err error, exit ...bool) bool {
    89  	if err == nil {
    90  		return true
    91  	}
    92  
    93  	fmt.Printf("    %s Error: %s\n", u.PrintMyName(), err)
    94  
    95  	if len(exit) > 0 && exit[0] {
    96  		u.t.Fatal()
    97  	} else {
    98  		u.t.Fail()
    99  	}
   100  	return false
   101  }
   102  
   103  // EqualExit EqualExit
   104  func (u *TestUtil) EqualExit(expected, actual interface{}) {
   105  	if !reflect.DeepEqual(expected, actual) {
   106  		fmt.Printf("        %s 期待:%v (type %v) - 结果:%v (type %v)\n", u.PrintMyName(), expected, reflect.TypeOf(expected), actual, reflect.TypeOf(actual))
   107  		u.t.Fatal()
   108  	}
   109  }
   110  
   111  // Log log
   112  func (u *TestUtil) Log(v ...interface{}) {
   113  	u.t.Helper()
   114  	u.t.Log(v...)
   115  }
   116  
   117  // Logf Logf
   118  func (u *TestUtil) Logf(format string, args ...interface{}) {
   119  	u.t.Helper()
   120  	u.t.Logf(format, args...)
   121  }
   122  
   123  // Fatal Fatal
   124  func (u *TestUtil) Fatal(v ...interface{}) {
   125  	u.t.Helper()
   126  	u.t.Fatal(v...)
   127  }
   128  
   129  // PrintMyName PrintMyName
   130  func (u *TestUtil) PrintMyName() string {
   131  	return u.GetCallerInfo()
   132  }
   133  
   134  func (u *TestUtil) Run(name string, f func(tt *TestUtil)) {
   135  	u.t.Helper()
   136  	u.t.(*testing.T).Run(name, func(t *testing.T) {
   137  		f(NewTest(t))
   138  	})
   139  }
   140  
   141  func (u *TestUtil) T() *testing.T {
   142  	return u.t.(*testing.T)
   143  }