test coverage

This commit is contained in:
Liang Ding 2014-12-21 11:06:10 +08:00
parent a9326e876b
commit 98f6e2fbe7
2 changed files with 96 additions and 18 deletions

View File

@ -35,7 +35,8 @@ import (
// Logging level. // Logging level.
const ( const (
Trace = iota Off = iota
Trace
Debug Debug
Info Info
Warn Warn
@ -78,6 +79,8 @@ func getLevel(level string) int {
level = strings.ToLower(level) level = strings.ToLower(level)
switch level { switch level {
case "off":
return Off
case "trace": case "trace":
return Trace return Trace
case "debug": case "debug":
@ -98,6 +101,21 @@ func (l *Logger) SetLevel(level string) {
l.level = getLevel(level) l.level = getLevel(level)
} }
// IsTraceEnabled determines whether the trace level is enabled.
func (l *Logger) IsTraceEnabled() bool {
return l.level <= Trace
}
// IsDebugEnabled determines whether the debug level is enabled.
func (l *Logger) IsDebugEnabled() bool {
return l.level <= Debug
}
// IsWarnEnabled determines whether the debug level is enabled.
func (l *Logger) IsWarnEnabled() bool {
return l.level <= Warn
}
// Trace prints trace level message. // Trace prints trace level message.
func (l *Logger) Trace(v ...interface{}) { func (l *Logger) Trace(v ...interface{}) {
if Trace < l.level { if Trace < l.level {
@ -118,21 +136,6 @@ func (l *Logger) Tracef(format string, v ...interface{}) {
l.logger.Output(2, fmt.Sprintf(format, v...)) l.logger.Output(2, fmt.Sprintf(format, v...))
} }
// IsTraceEnabled determines whether the trace level is enabled.
func (l *Logger) IsTraceEnabled() bool {
return l.level <= Trace
}
// IsDebugEnabled determines whether the debug level is enabled.
func (l *Logger) IsDebugEnabled() bool {
return l.level <= Debug
}
// IsWarnEnabled determines whether the debug level is enabled.
func (l *Logger) IsWarnEnabled() bool {
return l.level <= Warn
}
// Debug prints debug level message. // Debug prints debug level message.
func (l *Logger) Debug(v ...interface{}) { func (l *Logger) Debug(v ...interface{}) {
if Debug < l.level { if Debug < l.level {

View File

@ -26,40 +26,105 @@ func TestSetLevel(t *testing.T) {
} }
func TestTrace(t *testing.T) { func TestTrace(t *testing.T) {
logger.SetLevel("trace")
logger.Trace("trace")
logger.SetLevel("off")
logger.Trace("trace") logger.Trace("trace")
} }
func TestTracef(t *testing.T) { func TestTracef(t *testing.T) {
logger.SetLevel("trace")
logger.Tracef("tracef")
logger.SetLevel("off")
logger.Tracef("tracef") logger.Tracef("tracef")
} }
func TestDebug(t *testing.T) {
logger.SetLevel("debug")
logger.Debug("debug")
logger.SetLevel("off")
logger.Debug("debug")
}
func TestDebugf(t *testing.T) {
logger.SetLevel("debug")
logger.Debugf("debugf")
logger.SetLevel("off")
logger.Debug("debug")
}
func TestInfo(t *testing.T) { func TestInfo(t *testing.T) {
logger.SetLevel("info")
logger.Info("info")
logger.SetLevel("off")
logger.Info("info") logger.Info("info")
} }
func TestInfof(t *testing.T) { func TestInfof(t *testing.T) {
logger.SetLevel("info")
logger.Infof("infof")
logger.SetLevel("off")
logger.Infof("infof") logger.Infof("infof")
} }
func TestWarn(t *testing.T) { func TestWarn(t *testing.T) {
logger.SetLevel("warn")
logger.Warn("warn")
logger.SetLevel("off")
logger.Warn("warn") logger.Warn("warn")
} }
func TestWarnf(t *testing.T) { func TestWarnf(t *testing.T) {
logger.SetLevel("warn")
logger.Warnf("warnf")
logger.SetLevel("off")
logger.Warnf("warnf") logger.Warnf("warnf")
} }
func TestError(t *testing.T) { func TestError(t *testing.T) {
logger.SetLevel("error")
logger.Error("error")
logger.SetLevel("off")
logger.Error("error") logger.Error("error")
} }
func TestErrorf(t *testing.T) { func TestErrorf(t *testing.T) {
logger.SetLevel("error")
logger.Errorf("errorf")
logger.SetLevel("off")
logger.Errorf("errorf") logger.Errorf("errorf")
} }
func TestGetLevel(t *testing.T) { func TestGetLevel(t *testing.T) {
l := getLevel("info") if getLevel("trace") != Trace {
logger.Info(l) t.FailNow()
return
}
if getLevel("debug") != Debug {
t.FailNow()
return
}
if getLevel("info") != Info {
t.FailNow()
return
}
if getLevel("warn") != Warn {
t.FailNow()
return
}
if getLevel("error") != Error {
t.FailNow()
return
}
} }
func TestLoggerSetLevel(t *testing.T) { func TestLoggerSetLevel(t *testing.T) {
@ -97,3 +162,13 @@ func TestIsDebugEnabled(t *testing.T) {
return return
} }
} }
func TestIsWarnEnabled(t *testing.T) {
logger.SetLevel("warn")
if !logger.IsWarnEnabled() {
t.FailNow()
return
}
}