chore: inline log.Interface and handle empty and nil slices the same

This commit is contained in:
2022-06-17 09:53:03 +02:00
parent 18d12ed1b4
commit d5e322ca9e
2 changed files with 9 additions and 5 deletions
+7 -3
View File
@@ -31,7 +31,7 @@ import (
// Mock has a Logger for use in unit-testing // Mock has a Logger for use in unit-testing
type Mock struct { type Mock struct {
*sync.RWMutex *sync.RWMutex
Logger log.Interface log.Interface
Logged []string Logged []string
} }
@@ -48,8 +48,9 @@ var _ log.Handler = &Mock{}
// New instantiates a new Mock and sets the log.Handler to it // New instantiates a new Mock and sets the log.Handler to it
func New() *Mock { func New() *Mock {
mock := &Mock{ mock := &Mock{
RWMutex: &sync.RWMutex{}, RWMutex: &sync.RWMutex{},
Logger: log.Log, Interface: log.Log,
Logged: make([]string, 0),
} }
log.SetHandler(mock) log.SetHandler(mock)
return mock return mock
@@ -60,5 +61,8 @@ func (m *Mock) Check(t testing.TB, wantLogged []string) {
t.Helper() t.Helper()
m.RLock() m.RLock()
defer m.RUnlock() defer m.RUnlock()
if wantLogged == nil {
wantLogged = []string{}
}
assert.Equal(t, wantLogged, m.Logged) assert.Equal(t, wantLogged, m.Logged)
} }
+2 -2
View File
@@ -50,7 +50,7 @@ func TestMock_Check(t *testing.T) {
name: "different", name: "different",
fields: fields{Logged: []string{"same"}}, fields: fields{Logged: []string{"same"}},
args: args{wantLogged: []string{"different"}}, args: args{wantLogged: []string{"different"}},
want: []string{"\n\tError Trace:\tmocks.go:63\n\t \t\t\t\tmocks_test.go:63\n\tError: \tNot equal: \n\t \texpected: []string{\"different\"}\n\t \tactual : []string{\"same\"}\n\t \t\n\t \tDiff:\n\t \t--- Expected\n\t \t+++ Actual\n\t \t@@ -1,3 +1,3 @@\n\t \t ([]string) (len=1) {\n\t \t- (string) (len=9) \"different\"\n\t \t+ (string) (len=4) \"same\"\n\t \t }\n\tTest: \t\n"}, want: []string{"\n\tError Trace:\tmocks.go:67\n\t \t\t\t\tmocks_test.go:63\n\tError: \tNot equal: \n\t \texpected: []string{\"different\"}\n\t \tactual : []string{\"same\"}\n\t \t\n\t \tDiff:\n\t \t--- Expected\n\t \t+++ Actual\n\t \t@@ -1,3 +1,3 @@\n\t \t ([]string) (len=1) {\n\t \t- (string) (len=9) \"different\"\n\t \t+ (string) (len=4) \"same\"\n\t \t }\n\tTest: \t\n"},
}, },
} }
for _, tt := range tests { for _, tt := range tests {
@@ -68,7 +68,7 @@ func TestMock_Check(t *testing.T) {
func TestNew(t *testing.T) { func TestNew(t *testing.T) {
logger := New() logger := New()
logger.Logger.Info("logging") logger.Info("logging")
logger.Check(t, []string{"info: logging"}) logger.Check(t, []string{"info: logging"})
} }