// Copyright (c) 2020 Unbound Software Development Svenska AB // // Permission is hereby granted, free of charge, to any person obtaining a copy of // this software and associated documentation files (the "Software"), to deal in // the Software without restriction, including without limitation the rights to // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of // the Software, and to permit persons to whom the Software is furnished to do so, // subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. package apex import ( "fmt" "github.com/apex/log" "reflect" "testing" "time" ) func TestMock_Check(t *testing.T) { type fields struct { Logged []string } type args struct { t *testing.T wantLogged []string } tests := []struct { name string fields fields args args want []string }{ { name: "same", fields: fields{Logged: []string{"same"}}, args: args{wantLogged: []string{"same"}}, want: nil, }, { name: "different", fields: fields{Logged: []string{"same"}}, args: args{wantLogged: []string{"different"}}, want: []string{`Logger() got []string{ "same", }, want []string{ "different", }`}, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { m := &Mock{ Logged: tt.fields.Logged, } temp := &MockT{} m.Check(temp, tt.args.wantLogged) if !reflect.DeepEqual(temp.errors, tt.want) { t.Errorf("Check() got %+v, want %+v", temp.errors, tt.want) } }) } } 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) } }) } } type MockT struct { testing.T errors []string } func (m *MockT) Errorf(format string, args ...interface{}) { m.errors = append(m.errors, fmt.Sprintf(format, args...)) }