2011-09-28 22:47:30 +00:00
|
|
|
package logging
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2011-10-22 11:06:18 +00:00
|
|
|
// Note: the below is deliberately PLACED AT THE TOP OF THIS FILE because
|
|
|
|
// it is fragile. It ensures the right file:line is logged. Sorry!
|
|
|
|
func TestLogCorrectLineNumbers(t *testing.T) {
|
2011-11-05 05:56:45 +00:00
|
|
|
l, m := NewMock(t)
|
2011-10-22 11:06:18 +00:00
|
|
|
l.Log(Error, "Error!")
|
2011-11-05 05:56:45 +00:00
|
|
|
// This breaks the mock encapsulation a little, but meh.
|
|
|
|
if s := string(m.m[Error].written); s[20:] != "log_test.go:11: ERROR Error!\n" {
|
2011-10-22 11:06:18 +00:00
|
|
|
t.Errorf("Error incorrectly logged (check line numbers!)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-22 14:43:13 +00:00
|
|
|
func TestStandardLogging(t *testing.T) {
|
2011-11-05 05:56:45 +00:00
|
|
|
l, m := NewMock(t)
|
2011-10-27 17:24:08 +00:00
|
|
|
l.SetLogLevel(Error)
|
2011-10-22 11:06:18 +00:00
|
|
|
|
|
|
|
l.Log(4, "Nothing should be logged yet")
|
2011-11-05 05:56:45 +00:00
|
|
|
m.ExpectNothing()
|
2011-10-22 11:06:18 +00:00
|
|
|
|
|
|
|
l.Log(Debug, "or yet...")
|
2011-11-05 05:56:45 +00:00
|
|
|
m.ExpectNothing()
|
2011-10-22 11:06:18 +00:00
|
|
|
|
|
|
|
l.Log(Info, "or yet...")
|
2011-11-05 05:56:45 +00:00
|
|
|
m.ExpectNothing()
|
2011-10-22 11:06:18 +00:00
|
|
|
|
|
|
|
l.Log(Warn, "or yet!")
|
2011-11-05 05:56:45 +00:00
|
|
|
m.ExpectNothing()
|
2011-10-22 11:06:18 +00:00
|
|
|
|
|
|
|
l.Log(Error, "Error!")
|
2011-11-05 05:56:45 +00:00
|
|
|
m.Expect("Error!")
|
2011-10-22 14:43:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAllLoggingLevels(t *testing.T) {
|
2011-11-05 05:56:45 +00:00
|
|
|
l, m := NewMock(t)
|
2011-10-22 14:43:13 +00:00
|
|
|
|
|
|
|
l.Log(4, "Log to level 4.")
|
2011-11-05 05:56:45 +00:00
|
|
|
m.ExpectAt(4, "Log to level 4.")
|
2011-10-22 14:43:13 +00:00
|
|
|
|
|
|
|
l.Debug("Log to debug.")
|
2011-11-05 05:56:45 +00:00
|
|
|
m.ExpectAt(Debug, "Log to debug.")
|
2011-10-22 14:43:13 +00:00
|
|
|
|
|
|
|
l.Info("Log to info.")
|
2011-11-05 05:56:45 +00:00
|
|
|
m.ExpectAt(Info, "Log to info.")
|
2011-10-22 14:43:13 +00:00
|
|
|
|
|
|
|
l.Warn("Log to warning.")
|
2011-11-05 05:56:45 +00:00
|
|
|
m.ExpectAt(Warn, "Log to warning.")
|
2011-10-22 14:43:13 +00:00
|
|
|
|
|
|
|
l.Error("Log to error.")
|
2011-11-05 05:56:45 +00:00
|
|
|
m.ExpectAt(Error, "Log to error.")
|
2011-10-22 14:43:13 +00:00
|
|
|
|
|
|
|
// recover to track the panic caused by Fatal.
|
|
|
|
defer func() { recover() }()
|
|
|
|
l.Fatal("Log to fatal.")
|
2011-11-05 05:56:45 +00:00
|
|
|
m.ExpectAt(Fatal, "Log to fatal.")
|
2011-10-22 11:06:18 +00:00
|
|
|
}
|