From 7cd9503b95babaaee5c7812eb18a953c0f1a3835 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B5=B7=E9=9B=A8?= <99347745@qq.com> Date: Thu, 21 Oct 2021 17:39:33 +0800 Subject: [PATCH] test: add log test (#1576) * test: add log test * fix * fix lint --- log/helper_test.go | 7 ++++ log/level_test.go | 95 ++++++++++++++++++++++++++++++++++++++++++++++ log/log_test.go | 5 +++ log/std_test.go | 4 ++ log/value_test.go | 23 ++++++++++- 5 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 log/level_test.go diff --git a/log/helper_test.go b/log/helper_test.go index 21d8e6972..a9c1a68be 100644 --- a/log/helper_test.go +++ b/log/helper_test.go @@ -15,6 +15,10 @@ func TestHelper(t *testing.T) { log.Debug("test debug") log.Debugf("test %s", "debug") log.Debugw("log", "test debug") + + log.Warn("test warn") + log.Warnf("test %s", "warn") + log.Warnw("log", "test warn") } func TestHelperWithMsgKey(t *testing.T) { @@ -28,8 +32,11 @@ func TestHelperLevel(t *testing.T) { log := NewHelper(DefaultLogger) log.Debug("test debug") log.Info("test info") + log.Infof("test %s", "info") log.Warn("test warn") log.Error("test error") + log.Errorf("test %s", "error") + log.Errorw("log", "test error") } func BenchmarkHelperPrint(b *testing.B) { diff --git a/log/level_test.go b/log/level_test.go new file mode 100644 index 000000000..d36882912 --- /dev/null +++ b/log/level_test.go @@ -0,0 +1,95 @@ +package log + +import "testing" + +func TestLevel_String(t *testing.T) { + tests := []struct { + name string + l Level + want string + }{ + { + name: "DEBUG", + l: LevelDebug, + want: "DEBUG", + }, + { + name: "INFO", + l: LevelInfo, + want: "INFO", + }, + { + name: "WARN", + l: LevelWarn, + want: "WARN", + }, + { + name: "ERROR", + l: LevelError, + want: "ERROR", + }, + { + name: "FATAL", + l: LevelFatal, + want: "FATAL", + }, + { + name: "other", + l: 10, + want: "", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := tt.l.String(); got != tt.want { + t.Errorf("String() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestParseLevel(t *testing.T) { + tests := []struct { + name string + s string + want Level + }{ + { + name: "DEBUG", + want: LevelDebug, + s: "DEBUG", + }, + { + name: "INFO", + want: LevelInfo, + s: "INFO", + }, + { + name: "WARN", + want: LevelWarn, + s: "WARN", + }, + { + name: "ERROR", + want: LevelError, + s: "ERROR", + }, + { + name: "FATAL", + want: LevelFatal, + s: "FATAL", + }, + { + name: "other", + want: LevelInfo, + s: "other", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := ParseLevel(tt.s); got != tt.want { + t.Errorf("ParseLevel() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/log/log_test.go b/log/log_test.go index 27a25ad6f..dea00f4d6 100644 --- a/log/log_test.go +++ b/log/log_test.go @@ -1,6 +1,7 @@ package log import ( + "context" "os" "testing" ) @@ -18,3 +19,7 @@ func TestWrapper(t *testing.T) { l := With(MultiLogger(out, err), "ts", DefaultTimestamp, "caller", DefaultCaller) _ = l.Log(LevelInfo, "msg", "test") } + +func TestWithContext(t *testing.T) { + WithContext(context.Background(), nil) +} diff --git a/log/std_test.go b/log/std_test.go index 0f11fe2f9..e9da7730a 100644 --- a/log/std_test.go +++ b/log/std_test.go @@ -10,4 +10,8 @@ func TestStdLogger(t *testing.T) { _ = logger.Log(LevelInfo, "msg", "test info") _ = logger.Log(LevelInfo, "msg", "test warn") _ = logger.Log(LevelInfo, "msg", "test error") + _ = logger.Log(LevelDebug, "singular") + + logger2 := DefaultLogger + _ = logger2.Log(LevelDebug) } diff --git a/log/value_test.go b/log/value_test.go index adc3ed9b9..fd9167a69 100644 --- a/log/value_test.go +++ b/log/value_test.go @@ -1,9 +1,30 @@ package log -import "testing" +import ( + "context" + "testing" +) func TestValue(t *testing.T) { logger := DefaultLogger logger = With(logger, "ts", DefaultTimestamp, "caller", DefaultCaller) _ = logger.Log(LevelInfo, "msg", "helloworld") + + logger = DefaultLogger + logger = With(logger) + _ = logger.Log(LevelDebug, "msg", "helloworld") + + var v1 interface{} + got := Value(context.Background(), v1) + if got != v1 { + t.Errorf("Value() = %v, want %v", got, v1) + } + var v2 Valuer = func(ctx context.Context) interface{} { + return 3 + } + got = Value(context.Background(), v2) + res := got.(int) + if res != 3 { + t.Errorf("Value() = %v, want %v", res, 3) + } }