From aa12177399bea6c90d0332e94be1831d28656967 Mon Sep 17 00:00:00 2001 From: Alex Bramley Date: Sat, 5 Nov 2011 09:17:32 +0000 Subject: [PATCH] My mock is clunky for external use, but works well for internal testing. Switch to gomock. --- logging/{log.go => logging.go} | 0 logging/{log_test.go => logging_test.go} | 8 +- logging/mock_logging.go | 93 ++++++++++++++++++++++++ logging/{mock.go => mock_test.go} | 16 ++-- 4 files changed, 105 insertions(+), 12 deletions(-) rename logging/{log.go => logging.go} (100%) rename logging/{log_test.go => logging_test.go} (89%) create mode 100644 logging/mock_logging.go rename logging/{mock.go => mock_test.go} (88%) diff --git a/logging/log.go b/logging/logging.go similarity index 100% rename from logging/log.go rename to logging/logging.go diff --git a/logging/log_test.go b/logging/logging_test.go similarity index 89% rename from logging/log_test.go rename to logging/logging_test.go index c02e3ad..886d06a 100644 --- a/logging/log_test.go +++ b/logging/logging_test.go @@ -7,16 +7,16 @@ 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(t) + l, m := newMock(t) l.Log(Error, "Error!") // 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!)") } } func TestStandardLogging(t *testing.T) { - l, m := NewMock(t) + l, m := newMock(t) l.SetLogLevel(Error) l.Log(4, "Nothing should be logged yet") @@ -36,7 +36,7 @@ func TestStandardLogging(t *testing.T) { } func TestAllLoggingLevels(t *testing.T) { - l, m := NewMock(t) + l, m := newMock(t) l.Log(4, "Log to level 4.") m.ExpectAt(4, "Log to level 4.") diff --git a/logging/mock_logging.go b/logging/mock_logging.go new file mode 100644 index 0000000..1f5da56 --- /dev/null +++ b/logging/mock_logging.go @@ -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) +} diff --git a/logging/mock.go b/logging/mock_test.go similarity index 88% rename from logging/mock.go rename to logging/mock_test.go index 9e95304..418b996 100644 --- a/logging/mock.go +++ b/logging/mock_test.go @@ -6,8 +6,6 @@ import ( "testing" ) -// These are provided to ease testing code that uses the logging pkg - // TODO(fluffle): Assumes at most one logging line will be written // between calls to Expect*. Change to be Expect(exp []string)? @@ -40,14 +38,16 @@ func (w *mockWriter) reset() { w.written = w.written[:0] } -type WriterMap struct { +type writerMap struct { t *testing.T m map[LogLevel]*mockWriter } // This doesn't create a mock Logger but a Logger that writes to mock outputs -func NewMock(t *testing.T) (*logger, *WriterMap) { - wMap := &WriterMap{ +// for testing purposes. Use the gomock-generated mock_logging package for +// external testing code that needs to mock out a logger. +func newMock(t *testing.T) (*logger, *writerMap) { + wMap := &writerMap{ t: t, m: map[LogLevel]*mockWriter{ 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. -func (wm *WriterMap) Expect(exp string) { +func (wm *writerMap) Expect(exp string) { found := false for lv, w := range wm.m { if s := w.getLine(); s != "" && !found { @@ -91,7 +91,7 @@ func (wm *WriterMap) Expect(exp string) { // When you expect nothing to be logged -func (wm *WriterMap) ExpectNothing() { +func (wm *writerMap) ExpectNothing() { for lv, w := range wm.m { if s := w.getLine(); 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. -func (wm *WriterMap) ExpectAt(lv LogLevel, exp string) { +func (wm *writerMap) ExpectAt(lv LogLevel, exp string) { var w *mockWriter if _, ok := wm.m[lv]; !ok { w = wm.m[Debug]