mirror of https://github.com/fluffle/goirc
My mock is clunky for external use, but works well for internal testing. Switch to gomock.
This commit is contained in:
parent
b1037b2603
commit
aa12177399
|
@ -7,16 +7,16 @@ import (
|
||||||
// Note: the below is deliberately PLACED AT THE TOP OF THIS FILE because
|
// 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!
|
// it is fragile. It ensures the right file:line is logged. Sorry!
|
||||||
func TestLogCorrectLineNumbers(t *testing.T) {
|
func TestLogCorrectLineNumbers(t *testing.T) {
|
||||||
l, m := NewMock(t)
|
l, m := newMock(t)
|
||||||
l.Log(Error, "Error!")
|
l.Log(Error, "Error!")
|
||||||
// This breaks the mock encapsulation a little, but meh.
|
// 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" {
|
if s := string(m.m[Error].written); s[20:] != "logging_test.go:11: ERROR Error!\n" {
|
||||||
t.Errorf("Error incorrectly logged (check line numbers!)")
|
t.Errorf("Error incorrectly logged (check line numbers!)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStandardLogging(t *testing.T) {
|
func TestStandardLogging(t *testing.T) {
|
||||||
l, m := NewMock(t)
|
l, m := newMock(t)
|
||||||
l.SetLogLevel(Error)
|
l.SetLogLevel(Error)
|
||||||
|
|
||||||
l.Log(4, "Nothing should be logged yet")
|
l.Log(4, "Nothing should be logged yet")
|
||||||
|
@ -36,7 +36,7 @@ func TestStandardLogging(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAllLoggingLevels(t *testing.T) {
|
func TestAllLoggingLevels(t *testing.T) {
|
||||||
l, m := NewMock(t)
|
l, m := newMock(t)
|
||||||
|
|
||||||
l.Log(4, "Log to level 4.")
|
l.Log(4, "Log to level 4.")
|
||||||
m.ExpectAt(4, "Log to level 4.")
|
m.ExpectAt(4, "Log to level 4.")
|
|
@ -0,0 +1,93 @@
|
||||||
|
// Automatically generated by MockGen. DO NOT EDIT!
|
||||||
|
// Source: logging.go
|
||||||
|
|
||||||
|
package logging
|
||||||
|
|
||||||
|
import (
|
||||||
|
gomock "gomock.googlecode.com/hg/gomock"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Mock of Logger interface
|
||||||
|
type MockLogger struct {
|
||||||
|
ctrl *gomock.Controller
|
||||||
|
recorder *_MockLoggerRecorder
|
||||||
|
}
|
||||||
|
|
||||||
|
// Recorder for MockLogger (not exported)
|
||||||
|
type _MockLoggerRecorder struct {
|
||||||
|
mock *MockLogger
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewMockLogger(ctrl *gomock.Controller) *MockLogger {
|
||||||
|
mock := &MockLogger{ctrl: ctrl}
|
||||||
|
mock.recorder = &_MockLoggerRecorder{mock}
|
||||||
|
return mock
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockLogger) EXPECT() *_MockLoggerRecorder {
|
||||||
|
return m.recorder
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockLogger) Log(_param0 LogLevel, _param1 string, _param2 ...interface{}) {
|
||||||
|
m.ctrl.Call(m, "Log", _param0, _param1, _param2)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mr *_MockLoggerRecorder) Log(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call {
|
||||||
|
return mr.mock.ctrl.RecordCall(mr.mock, "Log", arg0, arg1, arg2)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockLogger) Debug(_param0 string, _param1 ...interface{}) {
|
||||||
|
m.ctrl.Call(m, "Debug", _param0, _param1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mr *_MockLoggerRecorder) Debug(arg0 interface{}, arg1 ...interface{}) *gomock.Call {
|
||||||
|
return mr.mock.ctrl.RecordCall(mr.mock, "Debug", arg0, arg1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockLogger) Info(_param0 string, _param1 ...interface{}) {
|
||||||
|
m.ctrl.Call(m, "Info", _param0, _param1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mr *_MockLoggerRecorder) Info(arg0 interface{}, arg1 ...interface{}) *gomock.Call {
|
||||||
|
return mr.mock.ctrl.RecordCall(mr.mock, "Info", arg0, arg1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockLogger) Warn(_param0 string, _param1 ...interface{}) {
|
||||||
|
m.ctrl.Call(m, "Warn", _param0, _param1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mr *_MockLoggerRecorder) Warn(arg0 interface{}, arg1 ...interface{}) *gomock.Call {
|
||||||
|
return mr.mock.ctrl.RecordCall(mr.mock, "Warn", arg0, arg1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockLogger) Error(_param0 string, _param1 ...interface{}) {
|
||||||
|
m.ctrl.Call(m, "Error", _param0, _param1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mr *_MockLoggerRecorder) Error(arg0 interface{}, arg1 ...interface{}) *gomock.Call {
|
||||||
|
return mr.mock.ctrl.RecordCall(mr.mock, "Error", arg0, arg1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockLogger) Fatal(_param0 string, _param1 ...interface{}) {
|
||||||
|
m.ctrl.Call(m, "Fatal", _param0, _param1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mr *_MockLoggerRecorder) Fatal(arg0 interface{}, arg1 ...interface{}) *gomock.Call {
|
||||||
|
return mr.mock.ctrl.RecordCall(mr.mock, "Fatal", arg0, arg1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockLogger) SetLogLevel(_param0 LogLevel) {
|
||||||
|
m.ctrl.Call(m, "SetLogLevel", _param0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mr *_MockLoggerRecorder) SetLogLevel(arg0 interface{}) *gomock.Call {
|
||||||
|
return mr.mock.ctrl.RecordCall(mr.mock, "SetLogLevel", arg0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockLogger) SetOnly(_param0 bool) {
|
||||||
|
m.ctrl.Call(m, "SetOnly", _param0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mr *_MockLoggerRecorder) SetOnly(arg0 interface{}) *gomock.Call {
|
||||||
|
return mr.mock.ctrl.RecordCall(mr.mock, "SetOnly", arg0)
|
||||||
|
}
|
|
@ -6,8 +6,6 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
// These are provided to ease testing code that uses the logging pkg
|
|
||||||
|
|
||||||
// TODO(fluffle): Assumes at most one logging line will be written
|
// TODO(fluffle): Assumes at most one logging line will be written
|
||||||
// between calls to Expect*. Change to be Expect(exp []string)?
|
// between calls to Expect*. Change to be Expect(exp []string)?
|
||||||
|
|
||||||
|
@ -40,14 +38,16 @@ func (w *mockWriter) reset() {
|
||||||
w.written = w.written[:0]
|
w.written = w.written[:0]
|
||||||
}
|
}
|
||||||
|
|
||||||
type WriterMap struct {
|
type writerMap struct {
|
||||||
t *testing.T
|
t *testing.T
|
||||||
m map[LogLevel]*mockWriter
|
m map[LogLevel]*mockWriter
|
||||||
}
|
}
|
||||||
|
|
||||||
// This doesn't create a mock Logger but a Logger that writes to mock outputs
|
// This doesn't create a mock Logger but a Logger that writes to mock outputs
|
||||||
func NewMock(t *testing.T) (*logger, *WriterMap) {
|
// for testing purposes. Use the gomock-generated mock_logging package for
|
||||||
wMap := &WriterMap{
|
// external testing code that needs to mock out a logger.
|
||||||
|
func newMock(t *testing.T) (*logger, *writerMap) {
|
||||||
|
wMap := &writerMap{
|
||||||
t: t,
|
t: t,
|
||||||
m: map[LogLevel]*mockWriter{
|
m: map[LogLevel]*mockWriter{
|
||||||
Debug: &mockWriter{make([]byte, 0)},
|
Debug: &mockWriter{make([]byte, 0)},
|
||||||
|
@ -66,7 +66,7 @@ func NewMock(t *testing.T) (*logger, *WriterMap) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// When you expect something to be logged but don't care so much what level at.
|
// When you expect something to be logged but don't care so much what level at.
|
||||||
func (wm *WriterMap) Expect(exp string) {
|
func (wm *writerMap) Expect(exp string) {
|
||||||
found := false
|
found := false
|
||||||
for lv, w := range wm.m {
|
for lv, w := range wm.m {
|
||||||
if s := w.getLine(); s != "" && !found {
|
if s := w.getLine(); s != "" && !found {
|
||||||
|
@ -91,7 +91,7 @@ func (wm *WriterMap) Expect(exp string) {
|
||||||
|
|
||||||
|
|
||||||
// When you expect nothing to be logged
|
// When you expect nothing to be logged
|
||||||
func (wm *WriterMap) ExpectNothing() {
|
func (wm *writerMap) ExpectNothing() {
|
||||||
for lv, w := range wm.m {
|
for lv, w := range wm.m {
|
||||||
if s := w.getLine(); s != "" {
|
if s := w.getLine(); s != "" {
|
||||||
wm.t.Errorf("Unexpected log message at level %s:",
|
wm.t.Errorf("Unexpected log message at level %s:",
|
||||||
|
@ -103,7 +103,7 @@ func (wm *WriterMap) ExpectNothing() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// When you expect something to be logged at a specific level.
|
// When you expect something to be logged at a specific level.
|
||||||
func (wm *WriterMap) ExpectAt(lv LogLevel, exp string) {
|
func (wm *writerMap) ExpectAt(lv LogLevel, exp string) {
|
||||||
var w *mockWriter
|
var w *mockWriter
|
||||||
if _, ok := wm.m[lv]; !ok {
|
if _, ok := wm.m[lv]; !ok {
|
||||||
w = wm.m[Debug]
|
w = wm.m[Debug]
|
Loading…
Reference in New Issue