Update tests

remotes/r/develop
Guoqiang Chen 7 years ago
parent da474fcc77
commit 690752443e

@ -1,6 +1,7 @@
package log package log
import ( import (
"strings"
"testing" "testing"
) )
@ -23,6 +24,11 @@ func TestLevelToString(t *testing.T) {
if got != tt.wantOut { if got != tt.wantOut {
t.Errorf("%v.String() output = %v, want %v", tt.level, got, tt.wantOut) t.Errorf("%v.String() output = %v, want %v", tt.level, got, tt.wantOut)
} }
gotColor := tt.level.ColorString()
if !strings.Contains(gotColor, tt.wantOut) {
t.Errorf("%v.ColorString() output = %v, want %v", tt.level, gotColor, tt.wantOut)
}
} }
} }

@ -13,6 +13,11 @@ func IsInfoEnabled() bool {
return Default.IsInfoEnabled() return Default.IsInfoEnabled()
} }
// Indicate whether output info message
func IsPrintEnabled() bool {
return Default.IsPrintEnabled()
}
// Indicate whether output warning message // Indicate whether output warning message
func IsWarnEnabled() bool { func IsWarnEnabled() bool {
return Default.IsWarnEnabled() return Default.IsWarnEnabled()
@ -23,6 +28,11 @@ func IsErrorEnabled() bool {
return Default.IsErrorEnabled() return Default.IsErrorEnabled()
} }
// Indicate whether output panic message
func IsPanicEnabled() bool {
return Default.IsPanicEnabled()
}
// Indicate whether output fatal message // Indicate whether output fatal message
func IsFatalEnabled() bool { func IsFatalEnabled() bool {
return Default.IsFatalEnabled() return Default.IsFatalEnabled()

@ -32,6 +32,10 @@ func (l *Logger) IsInfoEnabled() bool {
return l.Level >= INFO return l.Level >= INFO
} }
func (l *Logger) IsPrintEnabled() bool {
return l.Level > OFF
}
func (l *Logger) IsWarnEnabled() bool { func (l *Logger) IsWarnEnabled() bool {
return l.Level >= WARN return l.Level >= WARN
} }
@ -40,12 +44,16 @@ func (l *Logger) IsErrorEnabled() bool {
return l.Level >= ERROR return l.Level >= ERROR
} }
func (l *Logger) IsPanicEnabled() bool {
return l.Level >= PANIC
}
func (l *Logger) IsFatalEnabled() bool { func (l *Logger) IsFatalEnabled() bool {
return l.Level >= FATAL return l.Level >= FATAL
} }
func (l *Logger) IsDisabled() bool { func (l *Logger) IsDisabled() bool {
return l.Level >= OFF return l.Level <= OFF
} }
func (l *Logger) Debug(obj ...interface{}) { func (l *Logger) Debug(obj ...interface{}) {

@ -28,25 +28,27 @@ func init() {
func TestLogOnLevel(t *testing.T) { func TestLogOnLevel(t *testing.T) {
type fns struct { type fns struct {
fn0 func() bool
fn1 func(...interface{}) fn1 func(...interface{})
fn2 func(...interface{}) fn2 func(...interface{})
fn3 func(string, ...interface{}) fn3 func(string, ...interface{})
ispanic bool ispanic bool
isexit bool isexit bool
} }
debugFns := fns{Debug, Debugln, Debugf, false, false} debugFns := fns{IsDebugEnabled, Debug, Debugln, Debugf, false, false}
infoFns := fns{Info, Infoln, Infof, false, false} infoFns := fns{IsInfoEnabled, Info, Infoln, Infof, false, false}
printFns := fns{Print, Println, Printf, false, false} printFns := fns{IsPrintEnabled, Print, Println, Printf, false, false}
warnFns := fns{Warn, Warnln, Warnf, false, false} warnFns := fns{IsWarnEnabled, Warn, Warnln, Warnf, false, false}
errorFns := fns{Error, Errorln, Errorf, false, false} errorFns := fns{IsErrorEnabled, Error, Errorln, Errorf, false, false}
panicFns := fns{Panic, Panicln, Panicf, true, false} panicFns := fns{IsPanicEnabled, Panic, Panicln, Panicf, true, false}
fatalFns := fns{Fatal, Fatalln, Fatalf, false, true} fatalFns := fns{IsFatalEnabled, Fatal, Fatalln, Fatalf, false, true}
tests := []struct { tests := []struct {
level Level level Level
fns fns fns fns
hits int hits int
}{ }{
// 0
{level: DEBUG, fns: debugFns, hits: 1}, {level: DEBUG, fns: debugFns, hits: 1},
{level: DEBUG, fns: infoFns, hits: 1}, {level: DEBUG, fns: infoFns, hits: 1},
{level: DEBUG, fns: printFns, hits: 1}, {level: DEBUG, fns: printFns, hits: 1},
@ -54,7 +56,7 @@ func TestLogOnLevel(t *testing.T) {
{level: DEBUG, fns: errorFns, hits: 1}, {level: DEBUG, fns: errorFns, hits: 1},
{level: DEBUG, fns: panicFns, hits: 1}, {level: DEBUG, fns: panicFns, hits: 1},
{level: DEBUG, fns: fatalFns, hits: 1}, {level: DEBUG, fns: fatalFns, hits: 1},
// 7
{level: INFO, fns: debugFns, hits: 0}, {level: INFO, fns: debugFns, hits: 0},
{level: INFO, fns: infoFns, hits: 1}, {level: INFO, fns: infoFns, hits: 1},
{level: INFO, fns: printFns, hits: 1}, {level: INFO, fns: printFns, hits: 1},
@ -62,7 +64,7 @@ func TestLogOnLevel(t *testing.T) {
{level: INFO, fns: errorFns, hits: 1}, {level: INFO, fns: errorFns, hits: 1},
{level: INFO, fns: panicFns, hits: 1}, {level: INFO, fns: panicFns, hits: 1},
{level: INFO, fns: fatalFns, hits: 1}, {level: INFO, fns: fatalFns, hits: 1},
// 14
{level: WARN, fns: debugFns, hits: 0}, {level: WARN, fns: debugFns, hits: 0},
{level: WARN, fns: infoFns, hits: 0}, {level: WARN, fns: infoFns, hits: 0},
{level: WARN, fns: printFns, hits: 1}, {level: WARN, fns: printFns, hits: 1},
@ -70,7 +72,7 @@ func TestLogOnLevel(t *testing.T) {
{level: WARN, fns: errorFns, hits: 1}, {level: WARN, fns: errorFns, hits: 1},
{level: WARN, fns: panicFns, hits: 1}, {level: WARN, fns: panicFns, hits: 1},
{level: WARN, fns: fatalFns, hits: 1}, {level: WARN, fns: fatalFns, hits: 1},
// 21
{level: ERROR, fns: debugFns, hits: 0}, {level: ERROR, fns: debugFns, hits: 0},
{level: ERROR, fns: infoFns, hits: 0}, {level: ERROR, fns: infoFns, hits: 0},
{level: ERROR, fns: printFns, hits: 1}, {level: ERROR, fns: printFns, hits: 1},
@ -78,7 +80,7 @@ func TestLogOnLevel(t *testing.T) {
{level: ERROR, fns: errorFns, hits: 1}, {level: ERROR, fns: errorFns, hits: 1},
{level: ERROR, fns: panicFns, hits: 1}, {level: ERROR, fns: panicFns, hits: 1},
{level: ERROR, fns: fatalFns, hits: 1}, {level: ERROR, fns: fatalFns, hits: 1},
// 28
{level: PANIC, fns: debugFns, hits: 0}, {level: PANIC, fns: debugFns, hits: 0},
{level: PANIC, fns: infoFns, hits: 0}, {level: PANIC, fns: infoFns, hits: 0},
{level: PANIC, fns: printFns, hits: 1}, {level: PANIC, fns: printFns, hits: 1},
@ -86,7 +88,7 @@ func TestLogOnLevel(t *testing.T) {
{level: PANIC, fns: errorFns, hits: 0}, {level: PANIC, fns: errorFns, hits: 0},
{level: PANIC, fns: panicFns, hits: 1}, {level: PANIC, fns: panicFns, hits: 1},
{level: PANIC, fns: fatalFns, hits: 1}, {level: PANIC, fns: fatalFns, hits: 1},
// 35
{level: FATAL, fns: debugFns, hits: 0}, {level: FATAL, fns: debugFns, hits: 0},
{level: FATAL, fns: infoFns, hits: 0}, {level: FATAL, fns: infoFns, hits: 0},
{level: FATAL, fns: printFns, hits: 1}, {level: FATAL, fns: printFns, hits: 1},
@ -94,7 +96,7 @@ func TestLogOnLevel(t *testing.T) {
{level: FATAL, fns: errorFns, hits: 0}, {level: FATAL, fns: errorFns, hits: 0},
{level: FATAL, fns: panicFns, hits: 0}, {level: FATAL, fns: panicFns, hits: 0},
{level: FATAL, fns: fatalFns, hits: 1}, {level: FATAL, fns: fatalFns, hits: 1},
// 42
{level: OFF, fns: debugFns, hits: 0}, {level: OFF, fns: debugFns, hits: 0},
{level: OFF, fns: infoFns, hits: 0}, {level: OFF, fns: infoFns, hits: 0},
{level: OFF, fns: printFns, hits: 0}, {level: OFF, fns: printFns, hits: 0},
@ -129,6 +131,17 @@ func TestLogOnLevel(t *testing.T) {
tt.fns.fn2("message") tt.fns.fn2("message")
tt.fns.fn3("message") tt.fns.fn3("message")
// check is enabled
if tt.fns.fn0() {
if logHits == 0 {
t.Errorf("Case %d, IsEnabled, but no log hits", i)
}
} else {
if logHits != 0 {
t.Errorf("Case %d, not IsEnabled, but found log hits=%v", i, logHits)
}
}
// check log hits // check log hits
if logHits != tt.hits*3 { if logHits != tt.hits*3 {
t.Errorf("Case %d, fn hits on level %v, got %v, want %v", i, tt.level, logHits, tt.hits) t.Errorf("Case %d, fn hits on level %v, got %v, want %v", i, tt.level, logHits, tt.hits)

Loading…
Cancel
Save