github.com/PandaGoAdmin/utils@v0.0.0-20211208134815-d5461603a00f/debug_test.go (about)

     1  package kgo
     2  
     3  import (
     4  	"github.com/stretchr/testify/assert"
     5  	"testing"
     6  )
     7  
     8  func TestDebug_DumpPrint(t *testing.T) {
     9  	defer func() {
    10  		r := recover()
    11  		assert.Empty(t, r)
    12  	}()
    13  
    14  	KDbug.DumpPrint(Version)
    15  }
    16  
    17  func TestDebug_DumpStacks(t *testing.T) {
    18  	defer func() {
    19  		r := recover()
    20  		assert.Empty(t, r)
    21  	}()
    22  
    23  	KDbug.DumpStacks()
    24  }
    25  
    26  func TestDebug_GetCallName(t *testing.T) {
    27  	defer func() {
    28  		r := recover()
    29  		assert.NotEmpty(t, r)
    30  	}()
    31  
    32  	var res string
    33  
    34  	res = KDbug.GetCallName(nil, false)
    35  	assert.Contains(t, res, "TestDebug_GetCallName")
    36  
    37  	res = KDbug.GetCallName(nil, true)
    38  	assert.Equal(t, "TestDebug_GetCallName", res)
    39  
    40  	res = KDbug.GetCallName("", false)
    41  	assert.Empty(t, res)
    42  
    43  	res = KDbug.GetCallName(KArr.ArrayRand, false)
    44  	assert.Contains(t, res, "ArrayRand")
    45  
    46  	res = KDbug.GetCallName(KArr.ArrayRand, true)
    47  	assert.Equal(t, "ArrayRand-fm", res)
    48  
    49  	//未实现的方法
    50  	KDbug.GetCallName(itfObj.noRealize, false)
    51  }
    52  
    53  func BenchmarkDebug_GetCallName(b *testing.B) {
    54  	b.ResetTimer()
    55  	for i := 0; i < b.N; i++ {
    56  		KDbug.GetCallName(KArr.ArrayRand, false)
    57  	}
    58  }
    59  
    60  func TestDebug_GetCallFile(t *testing.T) {
    61  	res := KDbug.GetCallFile()
    62  	assert.NotEmpty(t, res)
    63  }
    64  
    65  func BenchmarkDebug_GetCallFile(b *testing.B) {
    66  	b.ResetTimer()
    67  	for i := 0; i < b.N; i++ {
    68  		KDbug.GetCallFile()
    69  	}
    70  }
    71  
    72  func TestDebug_GetCallDir(t *testing.T) {
    73  	res := KDbug.GetCallDir()
    74  	assert.NotEmpty(t, res)
    75  }
    76  
    77  func BenchmarkDebug_GetCallDir(b *testing.B) {
    78  	b.ResetTimer()
    79  	for i := 0; i < b.N; i++ {
    80  		KDbug.GetCallDir()
    81  	}
    82  }
    83  
    84  func TestDebug_GetCallLine(t *testing.T) {
    85  	res := KDbug.GetCallLine()
    86  	assert.Greater(t, res, 1)
    87  }
    88  
    89  func BenchmarkDebug_GetCallLine(b *testing.B) {
    90  	b.ResetTimer()
    91  	for i := 0; i < b.N; i++ {
    92  		KDbug.GetCallLine()
    93  	}
    94  }
    95  
    96  func TestDebug_GetCallPackage(t *testing.T) {
    97  	var res string
    98  
    99  	res = KDbug.GetCallPackage()
   100  	assert.Equal(t, "kgo", res)
   101  
   102  	res = KDbug.GetCallPackage(KDbug.GetCallFile())
   103  	assert.Equal(t, "kgo", res)
   104  
   105  	res = KDbug.GetCallPackage(strHello)
   106  	assert.Empty(t, res)
   107  }
   108  
   109  func BenchmarkDebug_GetCallPackage(b *testing.B) {
   110  	b.ResetTimer()
   111  	for i := 0; i < b.N; i++ {
   112  		KDbug.GetCallPackage()
   113  	}
   114  }
   115  
   116  func TestDebug_HasMethod(t *testing.T) {
   117  	var tests = []struct {
   118  		input    interface{}
   119  		method   string
   120  		expected bool
   121  	}{
   122  		{intSpeedLight, "", false},
   123  		{strHello, "toString", false},
   124  		{KArr, "InIntSlice", true},
   125  		{&KArr, "InInt64Slice", true},
   126  		{&KArr, strHello, false},
   127  	}
   128  	for _, test := range tests {
   129  		actual := KDbug.HasMethod(test.input, test.method)
   130  		assert.Equal(t, actual, test.expected)
   131  	}
   132  }
   133  
   134  func BenchmarkDebug_HasMethod(b *testing.B) {
   135  	b.ResetTimer()
   136  	for i := 0; i < b.N; i++ {
   137  		KDbug.HasMethod(KArr, "SliceFill")
   138  	}
   139  }
   140  
   141  func TestDebug_GetMethod(t *testing.T) {
   142  	var res interface{}
   143  
   144  	res = KDbug.GetMethod(intSpeedLight, "")
   145  	assert.Nil(t, res)
   146  
   147  	res = KDbug.GetMethod(&KArr, "InIntSlice")
   148  	assert.NotNil(t, res)
   149  
   150  	res = KDbug.GetMethod(KArr, "InIntSlice")
   151  	assert.NotNil(t, res)
   152  
   153  	res = KDbug.GetMethod(KArr, strHello)
   154  	assert.Nil(t, res)
   155  }
   156  
   157  func BenchmarkDebug_GetMethod(b *testing.B) {
   158  	b.ResetTimer()
   159  	for i := 0; i < b.N; i++ {
   160  		KDbug.GetMethod(&KArr, "InIntSlice")
   161  	}
   162  }
   163  
   164  func TestDebug_CallMethod(t *testing.T) {
   165  	var res interface{}
   166  	var err error
   167  
   168  	//调用不存在的方法
   169  	res, err = KDbug.CallMethod(KArr, strHello)
   170  	assert.NotNil(t, err)
   171  
   172  	//有参数调用
   173  	res, err = KDbug.CallMethod(&KConv, "BaseConvert", "123456", 10, 16)
   174  	assert.Nil(t, err)
   175  	assert.NotEmpty(t, res)
   176  
   177  	//无参数调用
   178  	res, err = KDbug.CallMethod(&KOS, "Getcwd")
   179  	assert.Nil(t, err)
   180  	assert.NotEmpty(t, res)
   181  }
   182  
   183  func BenchmarkDebug_CallMethod(b *testing.B) {
   184  	b.ResetTimer()
   185  	for i := 0; i < b.N; i++ {
   186  		_, _ = KDbug.CallMethod(&KOS, "Getcwd")
   187  	}
   188  }
   189  
   190  func TestDebug_GetFuncNames(t *testing.T) {
   191  	var res []string
   192  
   193  	//空字符串
   194  	res = KDbug.GetFuncNames("")
   195  	assert.Empty(t, res)
   196  
   197  	//空变量
   198  	res = KDbug.GetFuncNames(nil)
   199  	assert.Empty(t, res)
   200  
   201  	//非指针变量
   202  	res = KDbug.GetFuncNames(KStr)
   203  	assert.NotEmpty(t, res)
   204  
   205  	//指针变量
   206  	res = KDbug.GetFuncNames(&KConv)
   207  	assert.NotEmpty(t, res)
   208  
   209  	n1 := KDbug.GetFuncNames(KFile)
   210  	n2 := KDbug.GetFuncNames(KStr)
   211  	n3 := KDbug.GetFuncNames(KNum)
   212  	n4 := KDbug.GetFuncNames(KArr)
   213  	n5 := KDbug.GetFuncNames(KTime)
   214  	n6 := KDbug.GetFuncNames(KConv)
   215  	n7 := KDbug.GetFuncNames(KOS)
   216  	n8 := KDbug.GetFuncNames(KEncr)
   217  	n9 := KDbug.GetFuncNames(KDbug)
   218  	funTotal := len(n1) + len(n2) + len(n3) + len(n4) + len(n5) + len(n6) + len(n7) + len(n8) + len(n9)
   219  	dumpPrint("the package function total:", funTotal)
   220  }
   221  
   222  func BenchmarkDebug_GetFuncNames(b *testing.B) {
   223  	b.ResetTimer()
   224  	for i := 0; i < b.N; i++ {
   225  		KDbug.GetFuncNames(&KConv)
   226  	}
   227  }