|
|
@ -5,56 +5,141 @@ import (
|
|
|
|
"testing"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var hits int = 0
|
|
|
|
var (
|
|
|
|
|
|
|
|
logHits int = 0
|
|
|
|
|
|
|
|
exitHits int = 0
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type hitsFormatter struct{}
|
|
|
|
type hitsFormatter struct{}
|
|
|
|
|
|
|
|
|
|
|
|
func (f *hitsFormatter) Format(level Level, msg string, logger *Logger) []byte {
|
|
|
|
func (f *hitsFormatter) Format(level Level, msg string, logger *Logger) []byte {
|
|
|
|
hits++
|
|
|
|
logHits++
|
|
|
|
return []byte(msg)
|
|
|
|
return []byte(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var l = &Logger{
|
|
|
|
func init() {
|
|
|
|
Level: DEBUG,
|
|
|
|
Default.Formatter = new(hitsFormatter)
|
|
|
|
Formatter: new(hitsFormatter),
|
|
|
|
Default.Out = new(bytes.Buffer)
|
|
|
|
Out: new(bytes.Buffer),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func reset() {
|
|
|
|
Exit = func(code int) {
|
|
|
|
l.Level = DEBUG
|
|
|
|
exitHits++
|
|
|
|
hits = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestLogOnLevel(t *testing.T) {
|
|
|
|
func TestLogOnLevel(t *testing.T) {
|
|
|
|
|
|
|
|
type fns struct {
|
|
|
|
|
|
|
|
fn1 func(...interface{})
|
|
|
|
|
|
|
|
fn2 func(...interface{})
|
|
|
|
|
|
|
|
fn3 func(string, ...interface{})
|
|
|
|
|
|
|
|
ispanic bool
|
|
|
|
|
|
|
|
isexit bool
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
debugFns := fns{Debug, Debugln, Debugf, false, false}
|
|
|
|
|
|
|
|
infoFns := fns{Info, Infoln, Infof, false, false}
|
|
|
|
|
|
|
|
printFns := fns{Print, Println, Printf, false, false}
|
|
|
|
|
|
|
|
warnFns := fns{Warn, Warnln, Warnf, false, false}
|
|
|
|
|
|
|
|
errorFns := fns{Error, Errorln, Errorf, false, false}
|
|
|
|
|
|
|
|
panicFns := fns{Panic, Panicln, Panicf, true, false}
|
|
|
|
|
|
|
|
fatalFns := fns{Fatal, Fatalln, Fatalf, false, true}
|
|
|
|
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
tests := []struct {
|
|
|
|
level Level
|
|
|
|
level Level
|
|
|
|
fn func(...interface{})
|
|
|
|
fns fns
|
|
|
|
hits int
|
|
|
|
hits int
|
|
|
|
}{
|
|
|
|
}{
|
|
|
|
{level: DEBUG, fn: l.Debug, hits: 1},
|
|
|
|
{level: DEBUG, fns: debugFns, hits: 1},
|
|
|
|
{level: DEBUG, fn: l.Info, hits: 1},
|
|
|
|
{level: DEBUG, fns: infoFns, hits: 1},
|
|
|
|
{level: INFO, fn: l.Debug, hits: 0},
|
|
|
|
{level: DEBUG, fns: printFns, hits: 1},
|
|
|
|
{level: INFO, fn: l.Info, hits: 1},
|
|
|
|
{level: DEBUG, fns: warnFns, hits: 1},
|
|
|
|
{level: WARN, fn: l.Debugln, hits: 0},
|
|
|
|
{level: DEBUG, fns: errorFns, hits: 1},
|
|
|
|
{level: WARN, fn: l.Infoln, hits: 0},
|
|
|
|
{level: DEBUG, fns: panicFns, hits: 1},
|
|
|
|
{level: WARN, fn: l.Println, hits: 1},
|
|
|
|
{level: DEBUG, fns: fatalFns, hits: 1},
|
|
|
|
{level: WARN, fn: l.Errorln, hits: 1},
|
|
|
|
|
|
|
|
{level: ERROR, fn: l.Debug, hits: 0},
|
|
|
|
{level: INFO, fns: debugFns, hits: 0},
|
|
|
|
{level: ERROR, fn: l.Print, hits: 1},
|
|
|
|
{level: INFO, fns: infoFns, hits: 1},
|
|
|
|
{level: ERROR, fn: l.Error, hits: 1},
|
|
|
|
{level: INFO, fns: printFns, hits: 1},
|
|
|
|
{level: OFF, fn: l.Debugln, hits: 0},
|
|
|
|
{level: INFO, fns: warnFns, hits: 1},
|
|
|
|
{level: OFF, fn: l.Println, hits: 0},
|
|
|
|
{level: INFO, fns: errorFns, hits: 1},
|
|
|
|
{level: OFF, fn: l.Errorln, hits: 0},
|
|
|
|
{level: INFO, fns: panicFns, hits: 1},
|
|
|
|
|
|
|
|
{level: INFO, fns: fatalFns, hits: 1},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{level: WARN, fns: debugFns, hits: 0},
|
|
|
|
|
|
|
|
{level: WARN, fns: infoFns, hits: 0},
|
|
|
|
|
|
|
|
{level: WARN, fns: printFns, hits: 1},
|
|
|
|
|
|
|
|
{level: WARN, fns: warnFns, hits: 1},
|
|
|
|
|
|
|
|
{level: WARN, fns: errorFns, hits: 1},
|
|
|
|
|
|
|
|
{level: WARN, fns: panicFns, hits: 1},
|
|
|
|
|
|
|
|
{level: WARN, fns: fatalFns, hits: 1},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{level: ERROR, fns: debugFns, hits: 0},
|
|
|
|
|
|
|
|
{level: ERROR, fns: infoFns, hits: 0},
|
|
|
|
|
|
|
|
{level: ERROR, fns: printFns, hits: 1},
|
|
|
|
|
|
|
|
{level: ERROR, fns: warnFns, hits: 0},
|
|
|
|
|
|
|
|
{level: ERROR, fns: errorFns, hits: 1},
|
|
|
|
|
|
|
|
{level: ERROR, fns: panicFns, hits: 1},
|
|
|
|
|
|
|
|
{level: ERROR, fns: fatalFns, hits: 1},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{level: PANIC, fns: debugFns, hits: 0},
|
|
|
|
|
|
|
|
{level: PANIC, fns: infoFns, hits: 0},
|
|
|
|
|
|
|
|
{level: PANIC, fns: printFns, hits: 1},
|
|
|
|
|
|
|
|
{level: PANIC, fns: warnFns, hits: 0},
|
|
|
|
|
|
|
|
{level: PANIC, fns: errorFns, hits: 0},
|
|
|
|
|
|
|
|
{level: PANIC, fns: panicFns, hits: 1},
|
|
|
|
|
|
|
|
{level: PANIC, fns: fatalFns, hits: 1},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{level: FATAL, fns: debugFns, hits: 0},
|
|
|
|
|
|
|
|
{level: FATAL, fns: infoFns, hits: 0},
|
|
|
|
|
|
|
|
{level: FATAL, fns: printFns, hits: 1},
|
|
|
|
|
|
|
|
{level: FATAL, fns: warnFns, hits: 0},
|
|
|
|
|
|
|
|
{level: FATAL, fns: errorFns, hits: 0},
|
|
|
|
|
|
|
|
{level: FATAL, fns: panicFns, hits: 0},
|
|
|
|
|
|
|
|
{level: FATAL, fns: fatalFns, hits: 1},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{level: OFF, fns: debugFns, hits: 0},
|
|
|
|
|
|
|
|
{level: OFF, fns: infoFns, hits: 0},
|
|
|
|
|
|
|
|
{level: OFF, fns: printFns, hits: 0},
|
|
|
|
|
|
|
|
{level: OFF, fns: warnFns, hits: 0},
|
|
|
|
|
|
|
|
{level: OFF, fns: errorFns, hits: 0},
|
|
|
|
|
|
|
|
{level: OFF, fns: panicFns, hits: 0},
|
|
|
|
|
|
|
|
{level: OFF, fns: fatalFns, hits: 0},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
|
for i, tt := range tests {
|
|
|
|
l.Level = tt.level
|
|
|
|
func() {
|
|
|
|
hits = 0
|
|
|
|
defer func() {
|
|
|
|
|
|
|
|
// check panic hits
|
|
|
|
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
|
|
|
|
if !tt.fns.ispanic {
|
|
|
|
|
|
|
|
t.Errorf("Case %d, got panic", i)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if tt.fns.ispanic {
|
|
|
|
|
|
|
|
t.Errorf("Case %d, no panic found", i)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// reset
|
|
|
|
|
|
|
|
logHits = 0
|
|
|
|
|
|
|
|
exitHits = 0
|
|
|
|
|
|
|
|
Default.Level = tt.level
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// run
|
|
|
|
|
|
|
|
tt.fns.fn1("message")
|
|
|
|
|
|
|
|
tt.fns.fn2("message")
|
|
|
|
|
|
|
|
tt.fns.fn3("message")
|
|
|
|
|
|
|
|
|
|
|
|
tt.fn("message")
|
|
|
|
// check log hits
|
|
|
|
|
|
|
|
if logHits != tt.hits*3 {
|
|
|
|
|
|
|
|
t.Errorf("Case %d, fn hits on level %v, got %v, want %v", i, tt.level, logHits, tt.hits)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if hits != tt.hits {
|
|
|
|
// check exit hits
|
|
|
|
t.Errorf("Case %d, fn hits on level %v, got %v, want %v", i, tt.level, hits, tt.hits)
|
|
|
|
if tt.fns.isexit {
|
|
|
|
}
|
|
|
|
if exitHits != 3 {
|
|
|
|
|
|
|
|
t.Errorf("Case %d, no exits hits", i)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|