From 4e2a280ad1bfe0812ea003d14ce46ec8b0196082 Mon Sep 17 00:00:00 2001 From: Joakim Olsson Date: Wed, 27 Jan 2021 09:07:03 +0100 Subject: [PATCH] chore: change implementation to be able to handle WithFields etc. --- mocks.go | 97 +++---------- mocks_test.go | 391 +------------------------------------------------- 2 files changed, 22 insertions(+), 466 deletions(-) diff --git a/mocks.go b/mocks.go index 4fb5af1..64e6758 100644 --- a/mocks.go +++ b/mocks.go @@ -24,14 +24,30 @@ import ( "github.com/apex/log" "github.com/sanity-io/litter" "testing" - "time" ) -// Mock implements `log.Interface` for use in unit-testing +// Mock has a Logger for use in unit-testing type Mock struct { + Logger log.Interface Logged []string } +// HandleLog stores the logged entries to be able to check them later +func (m *Mock) HandleLog(entry *log.Entry) error { + m.Logged = append(m.Logged, fmt.Sprintf("%s: %s", entry.Level.String(), entry.Message)) + return nil +} + +var _ log.Handler = &Mock{} + +func New() *Mock { + mock := &Mock{ + Logger: log.Log, + } + log.SetHandler(mock) + return mock +} + // Check verifies that the application has logged the expected strings func (m *Mock) Check(t testing.TB, wantLogged []string) { t.Helper() @@ -43,80 +59,3 @@ func (m *Mock) Check(t testing.TB, wantLogged []string) { } } } - -// Debug implements log.Interface Debug -func (m *Mock) Debug(s string) { - m.Logged = append(m.Logged, fmt.Sprintf("DEBUG: %s", s)) -} - -// Info implements log.Interface Info -func (m *Mock) Info(s string) { - m.Logged = append(m.Logged, fmt.Sprintf("INFO: %s", s)) -} - -// Warn implements log.Interface Warn -func (m *Mock) Warn(s string) { - m.Logged = append(m.Logged, fmt.Sprintf("WARN: %s", s)) -} - -// Error implements log.Interface Error -func (m *Mock) Error(s string) { - m.Logged = append(m.Logged, fmt.Sprintf("ERROR: %s", s)) -} - -// Fatal implements log.Interface Fatal -func (m *Mock) Fatal(s string) { - m.Logged = append(m.Logged, fmt.Sprintf("FATAL: %s", s)) -} - -// Debugf implements log.Interface Debugf -func (m *Mock) Debugf(s string, i ...interface{}) { - m.Logged = append(m.Logged, fmt.Sprintf("DEBUG: %s", fmt.Sprintf(s, i...))) -} - -// Infof implements log.Interface Infof -func (m *Mock) Infof(s string, i ...interface{}) { - m.Logged = append(m.Logged, fmt.Sprintf("INFO: %s", fmt.Sprintf(s, i...))) -} - -// Warnf implements log.Interface Warnf -func (m *Mock) Warnf(s string, i ...interface{}) { - m.Logged = append(m.Logged, fmt.Sprintf("WARN: %s", fmt.Sprintf(s, i...))) -} - -// Errorf implements log.Interface Errorf -func (m *Mock) Errorf(s string, i ...interface{}) { - m.Logged = append(m.Logged, fmt.Sprintf("ERROR: %s", fmt.Sprintf(s, i...))) -} - -// Fatalf implements log.Interface Fatalf -func (m *Mock) Fatalf(s string, i ...interface{}) { - m.Logged = append(m.Logged, fmt.Sprintf("FATAL: %s", fmt.Sprintf(s, i...))) -} - -// Trace implements log.Interface Trace -func (m *Mock) Trace(string) *log.Entry { - return nil -} - -// WithFields implements log.Interface WithFields -func (m *Mock) WithFields(log.Fielder) *log.Entry { - return nil -} - -// WithField implements log.Interface WithField -func (m *Mock) WithField(string, interface{}) *log.Entry { - return nil -} - -// WithDuration implements log.Interface WithDuration -func (m *Mock) WithDuration(time.Duration) *log.Entry { - return nil -} - -// WithError implements log.Interface WithError -func (m *Mock) WithError(error) *log.Entry { - return nil -} - -var _ log.Interface = &Mock{} diff --git a/mocks_test.go b/mocks_test.go index 0383fcb..c4c27e7 100644 --- a/mocks_test.go +++ b/mocks_test.go @@ -21,10 +21,8 @@ package apex import ( "fmt" - "github.com/apex/log" "reflect" "testing" - "time" ) func TestMock_Check(t *testing.T) { @@ -32,7 +30,6 @@ func TestMock_Check(t *testing.T) { Logged []string } type args struct { - t *testing.T wantLogged []string } tests := []struct { @@ -72,390 +69,10 @@ func TestMock_Check(t *testing.T) { } } -func TestMock_Debug(t *testing.T) { - type args struct { - s string - } - tests := []struct { - name string - args args - want []string - }{ - { - name: "debug", - args: args{s: "something"}, - want: []string{"DEBUG: something"}, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - m := &Mock{} - m.Debug(tt.args.s) - m.Check(t, tt.want) - }) - } -} - -func TestMock_Debugf(t *testing.T) { - type args struct { - s string - i []interface{} - } - tests := []struct { - name string - args args - want []string - }{ - { - name: "debugf", - args: args{ - s: "%s - %d", - i: []interface{}{"a", 5}, - }, - want: []string{"DEBUG: a - 5"}, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - m := &Mock{} - m.Debugf(tt.args.s, tt.args.i...) - m.Check(t, tt.want) - }) - } -} - -func TestMock_Error(t *testing.T) { - type args struct { - s string - } - tests := []struct { - name string - args args - want []string - }{ - { - name: "error", - args: args{s: "something"}, - want: []string{"ERROR: something"}, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - m := &Mock{} - m.Error(tt.args.s) - m.Check(t, tt.want) - }) - } -} - -func TestMock_Errorf(t *testing.T) { - type args struct { - s string - i []interface{} - } - tests := []struct { - name string - args args - want []string - }{ - { - name: "errorf", - args: args{ - s: "%s - %d", - i: []interface{}{"a", 5}, - }, - want: []string{"ERROR: a - 5"}, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - m := &Mock{} - m.Errorf(tt.args.s, tt.args.i...) - m.Check(t, tt.want) - }) - } -} - -func TestMock_Fatal(t *testing.T) { - type args struct { - s string - } - tests := []struct { - name string - args args - want []string - }{ - { - name: "fatal", - args: args{s: "something"}, - want: []string{"FATAL: something"}, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - m := &Mock{} - m.Fatal(tt.args.s) - m.Check(t, tt.want) - }) - } -} - -func TestMock_Fatalf(t *testing.T) { - type args struct { - s string - i []interface{} - } - tests := []struct { - name string - args args - want []string - }{ - { - name: "fatalf", - args: args{ - s: "%s - %d", - i: []interface{}{"a", 5}, - }, - want: []string{"FATAL: a - 5"}, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - m := &Mock{} - m.Fatalf(tt.args.s, tt.args.i...) - m.Check(t, tt.want) - }) - } -} - -func TestMock_Info(t *testing.T) { - type args struct { - s string - } - tests := []struct { - name string - args args - want []string - }{ - { - name: "info", - args: args{s: "something"}, - want: []string{"INFO: something"}, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - m := &Mock{} - m.Info(tt.args.s) - m.Check(t, tt.want) - }) - } -} - -func TestMock_Infof(t *testing.T) { - type args struct { - s string - i []interface{} - } - tests := []struct { - name string - args args - want []string - }{ - { - name: "infof", - args: args{ - s: "%s - %d", - i: []interface{}{"a", 5}, - }, - want: []string{"INFO: a - 5"}, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - m := &Mock{} - m.Infof(tt.args.s, tt.args.i...) - m.Check(t, tt.want) - }) - } -} - -func TestMock_Trace(t *testing.T) { - type args struct { - in0 string - } - tests := []struct { - name string - args args - want *log.Entry - }{ - { - name: "always return nil", - args: args{}, - want: nil, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - m := &Mock{} - if got := m.Trace(tt.args.in0); !reflect.DeepEqual(got, tt.want) { - t.Errorf("Trace() = %v, want %v", got, tt.want) - } - }) - } -} - -func TestMock_Warn(t *testing.T) { - type args struct { - s string - } - tests := []struct { - name string - args args - want []string - }{ - { - name: "warn", - args: args{s: "something"}, - want: []string{"WARN: something"}, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - m := &Mock{} - m.Warn(tt.args.s) - m.Check(t, tt.want) - }) - } -} - -func TestMock_Warnf(t *testing.T) { - type args struct { - s string - i []interface{} - } - tests := []struct { - name string - args args - want []string - }{ - { - name: "warnf", - args: args{ - s: "%s - %d", - i: []interface{}{"a", 5}, - }, - want: []string{"WARN: a - 5"}, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - m := &Mock{} - m.Warnf(tt.args.s, tt.args.i...) - m.Check(t, tt.want) - }) - } -} - -func TestMock_WithDuration(t *testing.T) { - type args struct { - in0 time.Duration - } - tests := []struct { - name string - args args - want *log.Entry - }{ - { - name: "always return nil", - args: args{}, - want: nil, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - m := &Mock{} - if got := m.WithDuration(tt.args.in0); !reflect.DeepEqual(got, tt.want) { - t.Errorf("WithDuration() = %v, want %v", got, tt.want) - } - }) - } -} - -func TestMock_WithError(t *testing.T) { - type args struct { - in0 error - } - tests := []struct { - name string - args args - want *log.Entry - }{ - { - name: "always return nil", - args: args{}, - want: nil, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - m := &Mock{} - if got := m.WithError(tt.args.in0); !reflect.DeepEqual(got, tt.want) { - t.Errorf("WithError() = %v, want %v", got, tt.want) - } - }) - } -} - -func TestMock_WithField(t *testing.T) { - type args struct { - in0 string - in1 interface{} - } - tests := []struct { - name string - args args - want *log.Entry - }{ - { - name: "always return nil", - args: args{}, - want: nil, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - m := &Mock{} - if got := m.WithField(tt.args.in0, tt.args.in1); !reflect.DeepEqual(got, tt.want) { - t.Errorf("WithField() = %v, want %v", got, tt.want) - } - }) - } -} - -func TestMock_WithFields(t *testing.T) { - type args struct { - in0 log.Fielder - } - tests := []struct { - name string - args args - want *log.Entry - }{ - { - name: "always return nil", - args: args{}, - want: nil, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - m := &Mock{} - if got := m.WithFields(tt.args.in0); !reflect.DeepEqual(got, tt.want) { - t.Errorf("WithFields() = %v, want %v", got, tt.want) - } - }) - } +func TestNew(t *testing.T) { + logger := New() + logger.Logger.Info("logging") + logger.Check(t, []string{"info: logging"}) } type MockT struct {