test: add log test (#1576)

* test: add log test

* fix

* fix lint
pull/1580/head
海雨 3 years ago committed by GitHub
parent eec45a3d0a
commit 7cd9503b95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      log/helper_test.go
  2. 95
      log/level_test.go
  3. 5
      log/log_test.go
  4. 4
      log/std_test.go
  5. 23
      log/value_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) {

@ -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)
}
})
}
}

@ -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)
}

@ -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)
}

@ -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)
}
}

Loading…
Cancel
Save