Merge pull request #5731 from Colstuwjx/fix-log-test

Fix logger test case, add SetSkipLine func.
This commit is contained in:
Wenkai Yin 2018-09-06 08:17:17 +08:00 committed by GitHub
commit 5427c0064c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -25,6 +25,11 @@ var (
message = "message" message = "message"
) )
// contains reports whether the string is contained in the log.
func contains(t *testing.T, str string, lvl string, line, msg string) bool {
return strings.Contains(str, lvl) && strings.Contains(str, line) && strings.Contains(str, msg)
}
func TestSetx(t *testing.T) { func TestSetx(t *testing.T) {
logger := New(nil, nil, WarningLevel) logger := New(nil, nil, WarningLevel)
logger.SetOutput(os.Stdout) logger.SetOutput(os.Stdout)
@ -52,7 +57,7 @@ func TestDebug(t *testing.T) {
Debug(message) Debug(message)
str := buf.String() str := buf.String()
if len(str) != 0 { if str != "" {
t.Errorf("unexpected message: %s != %s", str, "") t.Errorf("unexpected message: %s != %s", str, "")
} }
} }
@ -64,80 +69,116 @@ func TestDebugf(t *testing.T) {
Debugf("%s", message) Debugf("%s", message)
str := buf.String() str := buf.String()
if len(str) != 0 { if str != "" {
t.Errorf("unexpected message: %s != %s", str, "") t.Errorf("unexpected message: %s != %s", str, "")
} }
} }
func TestInfo(t *testing.T) { func TestInfo(t *testing.T) {
var (
expectedLevel = InfoLevel.string()
expectLine = ""
expectMsg = "message"
)
buf := enter() buf := enter()
defer exit() defer exit()
Info(message) Info(message)
str := buf.String() str := buf.String()
if strings.HasSuffix(str, "[INFO] message") { if !contains(t, str, expectedLevel, expectLine, expectMsg) {
t.Errorf("unexpected message: %s != %s", str, "") t.Errorf("unexpected message: %s, expected level: %s, expected line: %s, expected message: %s", str, expectedLevel, expectLine, expectMsg)
} }
} }
func TestInfof(t *testing.T) { func TestInfof(t *testing.T) {
var (
expectedLevel = InfoLevel.string()
expectLine = ""
expectMsg = "message"
)
buf := enter() buf := enter()
defer exit() defer exit()
Infof("%s", message) Infof("%s", message)
str := buf.String() str := buf.String()
if strings.HasSuffix(str, "[INFO] message") { if !contains(t, str, expectedLevel, expectLine, expectMsg) {
t.Errorf("unexpected message: %s != %s", str, "") t.Errorf("unexpected message: %s, expected level: %s, expected line: %s, expected message: %s", str, expectedLevel, expectLine, expectMsg)
} }
} }
func TestWarning(t *testing.T) { func TestWarning(t *testing.T) {
var (
expectedLevel = WarningLevel.string()
expectLine = ""
expectMsg = "message"
)
buf := enter() buf := enter()
defer exit() defer exit()
Warning(message) Warning(message)
str := buf.String() str := buf.String()
if strings.HasSuffix(str, "[WARNING] message") { if !contains(t, str, expectedLevel, expectLine, expectMsg) {
t.Errorf("unexpected message: %s != %s", str, "") t.Errorf("unexpected message: %s, expected level: %s, expected line: %s, expected message: %s", str, expectedLevel, expectLine, expectMsg)
} }
} }
func TestWarningf(t *testing.T) { func TestWarningf(t *testing.T) {
var (
expectedLevel = WarningLevel.string()
expectLine = ""
expectMsg = "message"
)
buf := enter() buf := enter()
defer exit() defer exit()
Warningf("%s", message) Warningf("%s", message)
str := buf.String() str := buf.String()
if strings.HasSuffix(str, "[WARNING] message") { if !contains(t, str, expectedLevel, expectLine, expectMsg) {
t.Errorf("unexpected message: %s != %s", str, "") t.Errorf("unexpected message: %s, expected level: %s, expected line: %s, expected message: %s", str, expectedLevel, expectLine, expectMsg)
} }
} }
func TestError(t *testing.T) { func TestError(t *testing.T) {
var (
expectedLevel = ErrorLevel.string()
expectLine = "logger_test.go:159"
expectMsg = "message"
)
buf := enter() buf := enter()
defer exit() defer exit()
Error(message) Error(message)
str := buf.String() str := buf.String()
if strings.HasSuffix(str, "[ERROR] message") { if !contains(t, str, expectedLevel, expectLine, expectMsg) {
t.Errorf("unexpected message: %s != %s", str, "") t.Errorf("unexpected message: %s, expected level: %s, expected line: %s, expected message: %s", str, expectedLevel, expectLine, expectMsg)
} }
} }
func TestErrorf(t *testing.T) { func TestErrorf(t *testing.T) {
var (
expectedLevel = ErrorLevel.string()
expectLine = "logger_test.go:177"
expectMsg = "message"
)
buf := enter() buf := enter()
defer exit() defer exit()
Errorf("%s", message) Errorf("%s", message)
str := buf.String() str := buf.String()
if strings.HasSuffix(str, "[ERROR] message") { if !contains(t, str, expectedLevel, expectLine, expectMsg) {
t.Errorf("unexpected message: %s != %s", str, "") t.Errorf("unexpected message: %s, expected level: %s, expected line: %s, expected message: %s", str, expectedLevel, expectLine, expectMsg)
} }
} }