test(log): add toString unit test (#2373)

* test(log): add toString unit test

* fix lint

* annotation

* fix build&test
pull/2408/head
180909 2 years ago committed by GitHub
parent 0e698b248a
commit b10e067fe9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      contrib/log/aliyun/aliyun.go
  2. 32
      contrib/log/aliyun/aliyun_test.go

@ -127,7 +127,7 @@ func NewAliyunLog(options ...Option) Logger {
}
}
// toString 任意类型转string
// toString convert any type to string
func toString(v interface{}) string {
var key string
if v == nil {

@ -1,6 +1,7 @@
package aliyun
import (
"math"
"testing"
"github.com/go-kratos/kratos/v2/log"
@ -97,3 +98,34 @@ func TestLog(t *testing.T) {
t.Errorf("Log() returns error:%v", err)
}
}
func TestToString(t *testing.T) {
tests := []struct {
in interface{}
out string
}{
{math.MaxFloat64, "17976931348623157000000000000000000000000000000000000" +
"000000000000000000000000000000000000000000000000000000000000000000000000000" +
"000000000000000000000000000000000000000000000000000000000000000000000000000" +
"000000000000000000000000000000000000000000000000000000000000000000000000000" +
"0000000000000000000000000000000"},
{math.MaxFloat32, "340282346638528860000000000000000000000"},
{1<<((32<<(^uint(0)>>63))-1) - 1, "9223372036854775807"},
{uint(1<<(32<<(^uint(0)>>63)) - 1), "-1"},
{math.MaxInt8, "127"},
{math.MaxUint8, "255"},
{math.MaxInt16, "32767"},
{math.MaxUint16, "65535"},
{math.MaxInt32, "2147483647"},
{math.MaxUint32, "4294967295"},
{math.MaxInt64, "9223372036854775807"},
{uint64(math.MaxUint64), "18446744073709551615"},
{"abc", "abc"},
{[]byte("abc"), "abc"},
}
for _, test := range tests {
if toString(test.in) != test.out {
t.Fatalf("want: %s, got: %s", test.out, toString(test.in))
}
}
}

Loading…
Cancel
Save