Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4e2a280ad1 |
@@ -24,14 +24,30 @@ import (
|
|||||||
"github.com/apex/log"
|
"github.com/apex/log"
|
||||||
"github.com/sanity-io/litter"
|
"github.com/sanity-io/litter"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Mock implements `log.Interface` for use in unit-testing
|
// Mock has a Logger for use in unit-testing
|
||||||
type Mock struct {
|
type Mock struct {
|
||||||
|
Logger log.Interface
|
||||||
Logged []string
|
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
|
// Check verifies that the application has logged the expected strings
|
||||||
func (m *Mock) Check(t testing.TB, wantLogged []string) {
|
func (m *Mock) Check(t testing.TB, wantLogged []string) {
|
||||||
t.Helper()
|
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{}
|
|
||||||
|
|||||||
+4
-387
@@ -21,10 +21,8 @@ package apex
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/apex/log"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMock_Check(t *testing.T) {
|
func TestMock_Check(t *testing.T) {
|
||||||
@@ -32,7 +30,6 @@ func TestMock_Check(t *testing.T) {
|
|||||||
Logged []string
|
Logged []string
|
||||||
}
|
}
|
||||||
type args struct {
|
type args struct {
|
||||||
t *testing.T
|
|
||||||
wantLogged []string
|
wantLogged []string
|
||||||
}
|
}
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
@@ -72,390 +69,10 @@ func TestMock_Check(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMock_Debug(t *testing.T) {
|
func TestNew(t *testing.T) {
|
||||||
type args struct {
|
logger := New()
|
||||||
s string
|
logger.Logger.Info("logging")
|
||||||
}
|
logger.Check(t, []string{"info: logging"})
|
||||||
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)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type MockT struct {
|
type MockT struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user