From b10e067fe924be61d9e4bba85b14b0c5b2e7af34 Mon Sep 17 00:00:00 2001 From: 180909 <734461790@qq.com> Date: Tue, 27 Sep 2022 14:23:12 +0800 Subject: [PATCH] test(log): add toString unit test (#2373) * test(log): add toString unit test * fix lint * annotation * fix build&test --- contrib/log/aliyun/aliyun.go | 2 +- contrib/log/aliyun/aliyun_test.go | 32 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/contrib/log/aliyun/aliyun.go b/contrib/log/aliyun/aliyun.go index 390cb571e..a5e57ca6b 100644 --- a/contrib/log/aliyun/aliyun.go +++ b/contrib/log/aliyun/aliyun.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 { diff --git a/contrib/log/aliyun/aliyun_test.go b/contrib/log/aliyun/aliyun_test.go index d7acf6bbd..a5e1f0806 100644 --- a/contrib/log/aliyun/aliyun_test.go +++ b/contrib/log/aliyun/aliyun_test.go @@ -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)) + } + } +}