1
0
Fork 0
mirror of https://github.com/fluffle/goirc synced 2025-07-12 08:11:17 +00:00

More logging yak-shaving:

* Change Mock API to match mockNetConn Expect* calls.
* Make logging.NewMock take and store *testing.T.
* Add a level-agnostic Expect().
* Make writerMap an externally accessible type.
* Adjust tests to take this shaving into account.
This commit is contained in:
Alex Bramley 2011-11-05 05:56:45 +00:00
parent cfffcf2a85
commit 588a3168ac
2 changed files with 96 additions and 55 deletions

View file

@ -7,54 +7,54 @@ import (
// 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) {
l, m := NewMock()
l, m := NewMock(t)
l.Log(Error, "Error!")
if s := string(m[Error].written); s[20:] != "log_test.go:11: ERROR Error!\n" {
// 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" {
t.Errorf("Error incorrectly logged (check line numbers!)")
}
}
func TestStandardLogging(t *testing.T) {
l, m := NewMock()
l, m := NewMock(t)
l.SetLogLevel(Error)
l.Log(4, "Nothing should be logged yet")
m.CheckNothingWritten(t)
m.ExpectNothing()
l.Log(Debug, "or yet...")
m.CheckNothingWritten(t)
m.ExpectNothing()
l.Log(Info, "or yet...")
m.CheckNothingWritten(t)
m.ExpectNothing()
l.Log(Warn, "or yet!")
m.CheckNothingWritten(t)
m.ExpectNothing()
l.Log(Error, "Error!")
m.CheckWrittenAtLevel(t, Error, "Error!")
m.Expect("Error!")
}
func TestAllLoggingLevels(t *testing.T) {
l, m := NewMock()
l.SetLogLevel(5)
l, m := NewMock(t)
l.Log(4, "Log to level 4.")
m.CheckWrittenAtLevel(t, 4, "Log to level 4.")
m.ExpectAt(4, "Log to level 4.")
l.Debug("Log to debug.")
m.CheckWrittenAtLevel(t, Debug, "Log to debug.")
m.ExpectAt(Debug, "Log to debug.")
l.Info("Log to info.")
m.CheckWrittenAtLevel(t, Info, "Log to info.")
m.ExpectAt(Info, "Log to info.")
l.Warn("Log to warning.")
m.CheckWrittenAtLevel(t, Warn, "Log to warning.")
m.ExpectAt(Warn, "Log to warning.")
l.Error("Log to error.")
m.CheckWrittenAtLevel(t, Error, "Log to error.")
m.ExpectAt(Error, "Log to error.")
// recover to track the panic caused by Fatal.
defer func() { recover() }()
l.Fatal("Log to fatal.")
m.CheckWrittenAtLevel(t, Fatal, "Log to fatal.")
m.ExpectAt(Fatal, "Log to fatal.")
}