From 807cd4a71d8f21fed8cb43b789b02622b649319e Mon Sep 17 00:00:00 2001 From: hlts2 Date: Mon, 11 May 2020 14:55:31 +0900 Subject: [PATCH] fix: failing tests and modified to match template Signed-off-by: hlts2 --- internal/log/log_test.go | 980 ++++++++++++++++++++---------------- internal/log/option_test.go | 367 +++++++++----- 2 files changed, 787 insertions(+), 560 deletions(-) diff --git a/internal/log/log_test.go b/internal/log/log_test.go index 4f04329e13..ec10dc6347 100644 --- a/internal/log/log_test.go +++ b/internal/log/log_test.go @@ -13,6 +13,7 @@ // See the License for the specific language governing permissions and // limitations under the License. // + package log import ( @@ -21,134 +22,215 @@ import ( "testing" "github.com/vdaas/vald/internal/errors" - "github.com/vdaas/vald/internal/log/format" "github.com/vdaas/vald/internal/log/glg" "github.com/vdaas/vald/internal/log/level" - logger "github.com/vdaas/vald/internal/log/logger" + "github.com/vdaas/vald/internal/log/logger" "github.com/vdaas/vald/internal/log/mock" - "go.uber.org/goleak" ) +var ( + goleakIgnoreOptions = []goleak.Option{ + goleak.IgnoreTopFunction("github.com/kpango/fastime.(*Fastime).StartTimerD.func1"), + } +) + func TestInit(t *testing.T) { + type args struct { + opts []Option + } + type want struct { + l Logger + } type test struct { - name string - opts []Option - checkFunc func(Logger) error + name string + args args + want want + checkFunc func(want, Logger) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got Logger) error { + if !reflect.DeepEqual(got, l) { + return errors.Errorf("got = %v, want %v", got, w.l) + } + return nil } - tests := []test{ func() test { - l := glg.New() return test{ - name: "set l object when option is not empty", - opts: []Option{ - WithLogger(l), + name: "initialize success when option is nil", + want: want{ + glg.New( + glg.WithLevel(level.DEBUG.String()), + ), }, - checkFunc: func(got Logger) error { - if !reflect.DeepEqual(got, l) { - return errors.Errorf("not equals. want: %v, but got: %v", got, l) - } - return nil + beforeFunc: func(args) { + once = sync.Once{} }, } }(), func() test { return test{ - name: "set l object when option is empty", - opts: []Option{}, - checkFunc: func(got Logger) error { - if got == nil { - return errors.New("l is nil") - } - return nil + name: "initialize success when option is not nil", + args: args{ + opts: []Option{ + WithLevel(level.FATAL.String()), + }, + }, + want: want{ + glg.New( + glg.WithLevel(level.FATAL.String()), + ), + }, + beforeFunc: func(args) { + once = sync.Once{} }, } }(), } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - defer func() { - l = nil - once = sync.Once{} - }() - - Init(tt.opts...) + for _, test := range tests { + t.Run(test.name, func(tt *testing.T) { + defer goleak.VerifyNone(tt, goleakIgnoreOptions...) + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } - if err := tt.checkFunc(l); err != nil { - t.Error(err) + Init(test.args.opts...) + if err := test.checkFunc(test.want, l); err != nil { + tt.Errorf("error = %v", err) } }) } } -func TestGetLogger(t *testing.T) { - type test struct { - name string - o *option +func Test_getLogger(t *testing.T) { + type args struct { + o *option + } + type want struct { want Logger } - + type test struct { + name string + args args + want want + checkFunc func(want, Logger) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got Logger) error { + if !reflect.DeepEqual(got, w.want) { + return errors.Errorf("got = %v, want %v", got, w.want) + } + return nil + } tests := []test{ { - name: "returns glg object when l type is GLG", - o: &option{ - logType: logger.GLG, - level: level.DEBUG, - format: format.JSON, + name: "returns glg object when *option.logType is GLG", + args: args{ + o: &option{ + logType: logger.GLG, + }, + }, + want: want{ + want: glg.New( + glg.WithLevel(level.Unknown.String()), + ), }, - want: glg.New( - glg.WithLevel(level.DEBUG.String()), - glg.WithFormat(format.JSON.String()), - ), }, - func() test { - l := glg.New() + { + name: "returns glg object when *option is empty", + args: args{ + o: new(option), + }, + want: want{ + want: glg.New( + glg.WithLevel(level.Unknown.String()), + ), + }, + }, + } - return test{ - name: "returns l when l type is Unknown", - o: &option{ - logType: logger.Unknown, - logger: l, - }, - want: l, + for _, test := range tests { + t.Run(test.name, func(tt *testing.T) { + defer goleak.VerifyNone(tt, goleakIgnoreOptions...) + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc } - }(), - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got := getLogger(tt.o) - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("not equals. want: %v, but got: %v", tt.want, got) + got := getLogger(test.args.o) + if err := test.checkFunc(test.want, got); err != nil { + tt.Errorf("error = %v", err) } }) } } func TestBold(t *testing.T) { - type test struct { - name string - str string + type args struct { + str string + } + type want struct { want string } - + type test struct { + name string + args args + want want + checkFunc func(want, string) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got string) error { + if !reflect.DeepEqual(got, w.want) { + return errors.Errorf("got = %v, want %v", got, w.want) + } + return nil + } tests := []test{ { - name: "returns concat string with bash sequence", - str: "Vald", - want: "\033[1mVald\033[22m", + name: "returns concat string with bash sequence when str is `Vald`", + args: args{ + str: "Vald", + }, + want: want{ + want: "\033[1mVald\033[22m", + }, }, } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got := Bold(tt.str) - if tt.want != got { - t.Errorf("not equals. want: %v, got: %v", tt.want, got) + for _, test := range tests { + t.Run(test.name, func(tt *testing.T) { + defer goleak.VerifyNone(tt, goleakIgnoreOptions...) + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := Bold(test.args.str) + if err := test.checkFunc(test.want, got); err != nil { + tt.Errorf("error = %v", err) } }) } @@ -158,43 +240,48 @@ func TestDebug(t *testing.T) { type args struct { vals []interface{} } - - type global struct { - l Logger + type want struct { + vals []interface{} } - type test struct { - name string - args args - global global - checkFunc func() error + name string + args args + want want + checkFunc func(want) error + beforeFunc func(args) + afterFunc func(args) } - tests := []test{ func() test { - var want []interface{} + var got []interface{} - l := &mock.Logger{ + ml := &mock.Logger{ DebugFunc: func(vals ...interface{}) { - want = vals + got = vals }, } - vals := []interface{}{ - "vald", + w := want{ + vals: []interface{}{ + "vald", + }, } return test{ name: "output success", args: args{ - vals: vals, + vals: w.vals, }, - global: global{ - l: l, + want: w, + beforeFunc: func(args) { + l = ml }, - checkFunc: func() error { - if !reflect.DeepEqual(vals, want) { - return errors.Errorf("not equals. want: %v, got: %v", want, vals) + afterFunc: func(args) { + l = nil + }, + checkFunc: func(w want) error { + if !reflect.DeepEqual(got, w.vals) { + return errors.Errorf("got = %v, want %v", got, w.vals) } return nil }, @@ -202,13 +289,19 @@ func TestDebug(t *testing.T) { }(), } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - l = tt.global.l + for _, test := range tests { + t.Run(test.name, func(tt *testing.T) { + defer goleak.VerifyNone(tt, goleakIgnoreOptions...) + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } - Debug(tt.args.vals...) - if err := tt.checkFunc(); err != nil { - t.Error(err) + Debug(test.args.vals...) + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) } }) } @@ -219,53 +312,57 @@ func TestDebugf(t *testing.T) { format string vals []interface{} } - - type global struct { - l Logger + type want struct { + format string + vals []interface{} } - type test struct { - name string - args args - global global - checkFunc func() error + name string + args args + want want + checkFunc func(want) error + beforeFunc func(args) + afterFunc func(args) } - tests := []test{ func() test { var ( - wantFormat string - wantVals []interface{} + gotFormat string + gotVals []interface{} ) - l := &mock.Logger{ + ml := &mock.Logger{ DebugfFunc: func(format string, vals ...interface{}) { - wantFormat = format - wantVals = vals + gotFormat, gotVals = format, vals }, } - format := "%v" - vals := []interface{}{ - "vald", + w := want{ + format: "format", + vals: []interface{}{ + "vald", + }, } return test{ name: "output success", args: args{ - format: format, - vals: vals, + format: w.format, + vals: w.vals, + }, + want: w, + beforeFunc: func(args) { + l = ml }, - global: global{ - l: l, + afterFunc: func(args) { + l = nil }, - checkFunc: func() error { - if !reflect.DeepEqual(format, wantFormat) { - return errors.Errorf("vals is not equals. want: %v, got: %v", wantFormat, format) + checkFunc: func(w want) error { + if !reflect.DeepEqual(gotFormat, w.format) { + return errors.Errorf("format got = %v, want %v", gotFormat, w.format) } - - if !reflect.DeepEqual(vals, wantVals) { - return errors.Errorf("vals is not equals. want: %v, got: %v", wantVals, vals) + if !reflect.DeepEqual(gotVals, w.vals) { + return errors.Errorf("format got = %v, want %v", gotVals, w.vals) } return nil }, @@ -273,13 +370,19 @@ func TestDebugf(t *testing.T) { }(), } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - l = tt.global.l + for _, test := range tests { + t.Run(test.name, func(tt *testing.T) { + defer goleak.VerifyNone(tt, goleakIgnoreOptions...) + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } - Debugf(tt.args.format, tt.args.vals...) - if err := tt.checkFunc(); err != nil { - t.Error(err) + Debugf(test.args.format, test.args.vals...) + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) } }) } @@ -289,43 +392,48 @@ func TestInfo(t *testing.T) { type args struct { vals []interface{} } - - type global struct { - l Logger + type want struct { + vals []interface{} } - type test struct { - name string - args args - global global - checkFunc func() error + name string + args args + want want + checkFunc func(want) error + beforeFunc func(args) + afterFunc func(args) } - tests := []test{ func() test { - var want []interface{} + var got []interface{} - l := &mock.Logger{ + ml := &mock.Logger{ InfoFunc: func(vals ...interface{}) { - want = vals + got = vals }, } - vals := []interface{}{ - "vald", + w := want{ + vals: []interface{}{ + "vald", + }, } return test{ name: "output success", args: args{ - vals: vals, + vals: w.vals, + }, + want: w, + beforeFunc: func(args) { + l = ml }, - global: global{ - l: l, + afterFunc: func(args) { + l = nil }, - checkFunc: func() error { - if !reflect.DeepEqual(vals, want) { - return errors.Errorf("not equals. want: %v, got: %v", want, vals) + checkFunc: func(want) error { + if !reflect.DeepEqual(got, w.vals) { + return errors.Errorf("got = %v, want %v", got, w.vals) } return nil }, @@ -333,13 +441,19 @@ func TestInfo(t *testing.T) { }(), } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - l = tt.global.l + for _, test := range tests { + t.Run(test.name, func(tt *testing.T) { + defer goleak.VerifyNone(tt, goleakIgnoreOptions...) + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } - Info(tt.args.vals...) - if err := tt.checkFunc(); err != nil { - t.Error(err) + Info(test.args.vals...) + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) } }) } @@ -350,53 +464,57 @@ func TestInfof(t *testing.T) { format string vals []interface{} } - - type global struct { - l Logger + type want struct { + format string + vals []interface{} } - type test struct { - name string - args args - global global - checkFunc func() error + name string + args args + want want + checkFunc func(want) error + beforeFunc func(args) + afterFunc func(args) } - tests := []test{ func() test { var ( - wantFormat string - wantVals []interface{} + gotFormat string + gotVals []interface{} ) - l := &mock.Logger{ + ml := &mock.Logger{ InfofFunc: func(format string, vals ...interface{}) { - wantFormat = format - wantVals = vals + gotFormat, gotVals = format, vals }, } - format := "%v" - vals := []interface{}{ - "vald", + w := want{ + format: "format", + vals: []interface{}{ + "vald", + }, } return test{ name: "output success", args: args{ - format: format, - vals: vals, + format: w.format, + vals: w.vals, + }, + want: w, + beforeFunc: func(args) { + l = ml }, - global: global{ - l: l, + afterFunc: func(args) { + l = nil }, - checkFunc: func() error { - if !reflect.DeepEqual(format, wantFormat) { - return errors.Errorf("vals is not equals. want: %v, got: %v", wantFormat, format) + checkFunc: func(w want) error { + if !reflect.DeepEqual(gotFormat, w.format) { + return errors.Errorf("format got = %v, want %v", gotFormat, w.format) } - - if !reflect.DeepEqual(vals, wantVals) { - return errors.Errorf("vals is not equals. want: %v, got: %v", wantVals, vals) + if !reflect.DeepEqual(gotVals, w.vals) { + return errors.Errorf("format got = %v, want %v", gotVals, w.vals) } return nil }, @@ -404,13 +522,19 @@ func TestInfof(t *testing.T) { }(), } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - l = tt.global.l + for _, test := range tests { + t.Run(test.name, func(tt *testing.T) { + defer goleak.VerifyNone(tt, goleakIgnoreOptions...) + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } - Infof(tt.args.format, tt.args.vals...) - if err := tt.checkFunc(); err != nil { - t.Error(err) + Infof(test.args.format, test.args.vals...) + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) } }) } @@ -420,43 +544,48 @@ func TestWarn(t *testing.T) { type args struct { vals []interface{} } - - type global struct { - l Logger + type want struct { + vals []interface{} } - type test struct { - name string - args args - global global - checkFunc func() error + name string + args args + want want + checkFunc func(want) error + beforeFunc func(args) + afterFunc func(args) } - tests := []test{ func() test { - var want []interface{} + var got []interface{} - l := &mock.Logger{ + ml := &mock.Logger{ WarnFunc: func(vals ...interface{}) { - want = vals + got = vals }, } - vals := []interface{}{ - "vald", + w := want{ + vals: []interface{}{ + "vald", + }, } return test{ name: "output success", args: args{ - vals: vals, + vals: w.vals, }, - global: global{ - l: l, + want: w, + beforeFunc: func(args) { + l = ml }, - checkFunc: func() error { - if !reflect.DeepEqual(vals, want) { - return errors.Errorf("not equals. want: %v, got: %v", want, vals) + afterFunc: func(args) { + l = nil + }, + checkFunc: func(want) error { + if !reflect.DeepEqual(got, w.vals) { + return errors.Errorf("got = %v, want %v", got, w.vals) } return nil }, @@ -464,13 +593,19 @@ func TestWarn(t *testing.T) { }(), } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - l = tt.global.l + for _, test := range tests { + t.Run(test.name, func(tt *testing.T) { + defer goleak.VerifyNone(tt, goleakIgnoreOptions...) + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } - Warn(tt.args.vals...) - if err := tt.checkFunc(); err != nil { - t.Error(err) + Warn(test.args.vals...) + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) } }) } @@ -481,53 +616,57 @@ func TestWarnf(t *testing.T) { format string vals []interface{} } - - type global struct { - l Logger + type want struct { + format string + vals []interface{} } - type test struct { - name string - args args - global global - checkFunc func() error + name string + args args + want want + checkFunc func(want) error + beforeFunc func(args) + afterFunc func(args) } - tests := []test{ func() test { var ( - wantFormat string - wantVals []interface{} + gotFormat string + gotVals []interface{} ) - l := &mock.Logger{ + ml := &mock.Logger{ WarnfFunc: func(format string, vals ...interface{}) { - wantFormat = format - wantVals = vals + gotFormat, gotVals = format, vals }, } - format := "%v" - vals := []interface{}{ - "vald", + w := want{ + format: "format", + vals: []interface{}{ + "vald", + }, } return test{ name: "output success", args: args{ - format: format, - vals: vals, + format: w.format, + vals: w.vals, + }, + want: w, + beforeFunc: func(args) { + l = ml }, - global: global{ - l: l, + afterFunc: func(args) { + l = nil }, - checkFunc: func() error { - if !reflect.DeepEqual(format, wantFormat) { - return errors.Errorf("vals is not equals. want: %v, got: %v", wantFormat, format) + checkFunc: func(w want) error { + if !reflect.DeepEqual(gotFormat, w.format) { + return errors.Errorf("format got = %v, want %v", gotFormat, w.format) } - - if !reflect.DeepEqual(vals, wantVals) { - return errors.Errorf("vals is not equals. want: %v, got: %v", wantVals, vals) + if !reflect.DeepEqual(gotVals, w.vals) { + return errors.Errorf("format got = %v, want %v", gotVals, w.vals) } return nil }, @@ -535,13 +674,19 @@ func TestWarnf(t *testing.T) { }(), } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - l = tt.global.l + for _, test := range tests { + t.Run(test.name, func(tt *testing.T) { + defer goleak.VerifyNone(tt, goleakIgnoreOptions...) + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } - Warnf(tt.args.format, tt.args.vals...) - if err := tt.checkFunc(); err != nil { - t.Error(err) + Warnf(test.args.format, test.args.vals...) + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) } }) } @@ -551,43 +696,48 @@ func TestError(t *testing.T) { type args struct { vals []interface{} } - - type global struct { - l Logger + type want struct { + vals []interface{} } - type test struct { - name string - args args - global global - checkFunc func() error + name string + args args + want want + checkFunc func(want) error + beforeFunc func(args) + afterFunc func(args) } - tests := []test{ func() test { - var want []interface{} + var got []interface{} - l := &mock.Logger{ + ml := &mock.Logger{ ErrorFunc: func(vals ...interface{}) { - want = vals + got = vals }, } - vals := []interface{}{ - "vald", + w := want{ + vals: []interface{}{ + "vald", + }, } return test{ name: "output success", args: args{ - vals: vals, + vals: w.vals, + }, + want: w, + beforeFunc: func(args) { + l = ml }, - global: global{ - l: l, + afterFunc: func(args) { + l = nil }, - checkFunc: func() error { - if !reflect.DeepEqual(vals, want) { - return errors.Errorf("not equals. want: %v, got: %v", want, vals) + checkFunc: func(w want) error { + if !reflect.DeepEqual(got, w.vals) { + return errors.Errorf("got = %v, want %v", got, w.vals) } return nil }, @@ -595,13 +745,19 @@ func TestError(t *testing.T) { }(), } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - l = tt.global.l + for _, test := range tests { + t.Run(test.name, func(tt *testing.T) { + defer goleak.VerifyNone(tt, goleakIgnoreOptions...) + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } - Error(tt.args.vals...) - if err := tt.checkFunc(); err != nil { - t.Error(err) + Error(test.args.vals...) + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) } }) } @@ -612,53 +768,57 @@ func TestErrorf(t *testing.T) { format string vals []interface{} } - - type global struct { - l Logger + type want struct { + format string + vals []interface{} } - type test struct { - name string - args args - global global - checkFunc func() error + name string + args args + want want + checkFunc func(want) error + beforeFunc func(args) + afterFunc func(args) } - tests := []test{ func() test { var ( - wantFormat string - wantVals []interface{} + gotFormat string + gotVals []interface{} ) - l := &mock.Logger{ + ml := &mock.Logger{ ErrorfFunc: func(format string, vals ...interface{}) { - wantFormat = format - wantVals = vals + gotFormat, gotVals = format, vals }, } - format := "fmt" - vals := []interface{}{ - "vald", + w := want{ + format: "format", + vals: []interface{}{ + "vald", + }, } return test{ name: "output success", args: args{ - format: format, - vals: vals, + format: w.format, + vals: w.vals, + }, + want: w, + beforeFunc: func(args) { + l = ml }, - global: global{ - l: l, + afterFunc: func(args) { + l = nil }, - checkFunc: func() error { - if !reflect.DeepEqual(format, wantFormat) { - return errors.Errorf("vals is not equals. want: %v, got: %v", wantFormat, format) + checkFunc: func(w want) error { + if !reflect.DeepEqual(gotFormat, w.format) { + return errors.Errorf("format got = %v, want %v", gotFormat, w.format) } - - if !reflect.DeepEqual(vals, wantVals) { - return errors.Errorf("vals is not equals. want: %v, got: %v", wantVals, vals) + if !reflect.DeepEqual(gotVals, w.vals) { + return errors.Errorf("format got = %v, want %v", gotVals, w.vals) } return nil }, @@ -666,13 +826,19 @@ func TestErrorf(t *testing.T) { }(), } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - l = tt.global.l + for _, test := range tests { + t.Run(test.name, func(tt *testing.T) { + defer goleak.VerifyNone(tt, goleakIgnoreOptions...) + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } - Errorf(tt.args.format, tt.args.vals...) - if err := tt.checkFunc(); err != nil { - t.Error(err) + Errorf(test.args.format, test.args.vals...) + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) } }) } @@ -682,43 +848,48 @@ func TestFatal(t *testing.T) { type args struct { vals []interface{} } - - type global struct { - l Logger + type want struct { + vals []interface{} } - type test struct { - name string - args args - global global - checkFunc func() error + name string + args args + want want + checkFunc func(want) error + beforeFunc func(args) + afterFunc func(args) } - tests := []test{ func() test { - var want []interface{} + var got []interface{} - l := &mock.Logger{ + ml := &mock.Logger{ FatalFunc: func(vals ...interface{}) { - want = vals + got = vals }, } - vals := []interface{}{ - "vald", + w := want{ + vals: []interface{}{ + "vald", + }, } return test{ name: "output success", args: args{ - vals: vals, + vals: w.vals, }, - global: global{ - l: l, + want: w, + beforeFunc: func(args) { + l = ml }, - checkFunc: func() error { - if !reflect.DeepEqual(vals, want) { - return errors.Errorf("not equals. want: %v, got: %v", want, vals) + afterFunc: func(args) { + l = nil + }, + checkFunc: func(w want) error { + if !reflect.DeepEqual(got, w.vals) { + return errors.Errorf("got = %v, want %v", got, w.vals) } return nil }, @@ -726,13 +897,19 @@ func TestFatal(t *testing.T) { }(), } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - l = tt.global.l + for _, test := range tests { + t.Run(test.name, func(tt *testing.T) { + defer goleak.VerifyNone(tt, goleakIgnoreOptions...) + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } - Fatal(tt.args.vals...) - if err := tt.checkFunc(); err != nil { - t.Error(err) + Fatal(test.args.vals...) + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) } }) } @@ -743,53 +920,57 @@ func TestFatalf(t *testing.T) { format string vals []interface{} } - - type global struct { - l Logger + type want struct { + format string + vals []interface{} } - type test struct { - name string - args args - global global - checkFunc func() error + name string + args args + want want + checkFunc func(want) error + beforeFunc func(args) + afterFunc func(args) } - tests := []test{ func() test { var ( - wantFormat string - wantVals []interface{} + gotFormat string + gotVals []interface{} ) - l := &mock.Logger{ + ml := &mock.Logger{ FatalfFunc: func(format string, vals ...interface{}) { - wantFormat = format - wantVals = vals + gotFormat, gotVals = format, vals }, } - format := "%v" - vals := []interface{}{ - "vald", + w := want{ + format: "format", + vals: []interface{}{ + "vald", + }, } return test{ name: "output success", args: args{ - format: format, - vals: vals, + format: w.format, + vals: w.vals, }, - global: global{ - l: l, + want: w, + beforeFunc: func(args) { + l = ml }, - checkFunc: func() error { - if !reflect.DeepEqual(format, wantFormat) { - return errors.Errorf("vals is not equals. want: %v, got: %v", wantFormat, format) + afterFunc: func(args) { + l = nil + }, + checkFunc: func(w want) error { + if !reflect.DeepEqual(gotFormat, w.format) { + return errors.Errorf("format got = %v, want %v", gotFormat, w.format) } - - if !reflect.DeepEqual(vals, wantVals) { - return errors.Errorf("vals is not equals. want: %v, got: %v", wantVals, vals) + if !reflect.DeepEqual(gotVals, w.vals) { + return errors.Errorf("format got = %v, want %v", gotVals, w.vals) } return nil }, @@ -797,85 +978,20 @@ func TestFatalf(t *testing.T) { }(), } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - l = tt.global.l - - Fatalf(tt.args.format, tt.args.vals...) - if err := tt.checkFunc(); err != nil { - t.Error(err) - } - }) - } -} - -func Test_getLogger(t *testing.T) { - type args struct { - o *option - } - type want struct { - want Logger - } - type test struct { - name string - args args - want want - checkFunc func(want, Logger) error - beforeFunc func(args) - afterFunc func(args) - } - defaultCheckFunc := func(w want, got Logger) error { - if !reflect.DeepEqual(got, w.want) { - return errors.Errorf("got = %v, want %v", got, w.want) - } - return nil - } - tests := []test{ - // TODO test cases - /* - { - name: "test_case_1", - args: args { - o: option{}, - }, - want: want{}, - checkFunc: defaultCheckFunc, - }, - */ - - // TODO test cases - /* - func() test { - return test { - name: "test_case_2", - args: args { - o: option{}, - }, - want: want{}, - checkFunc: defaultCheckFunc, - } - }(), - */ - } - for _, test := range tests { t.Run(test.name, func(tt *testing.T) { - defer goleak.VerifyNone(t) + defer goleak.VerifyNone(tt, goleakIgnoreOptions...) if test.beforeFunc != nil { test.beforeFunc(test.args) } if test.afterFunc != nil { defer test.afterFunc(test.args) } - if test.checkFunc == nil { - test.checkFunc = defaultCheckFunc - } - got := getLogger(test.args.o) - if err := test.checkFunc(test.want, got); err != nil { + Fatalf(test.args.format, test.args.vals...) + if err := test.checkFunc(test.want); err != nil { tt.Errorf("error = %v", err) } - }) } } diff --git a/internal/log/option_test.go b/internal/log/option_test.go index 584f0775c7..b4b6d87d54 100644 --- a/internal/log/option_test.go +++ b/internal/log/option_test.go @@ -22,208 +22,319 @@ import ( "github.com/vdaas/vald/internal/errors" "github.com/vdaas/vald/internal/log/format" "github.com/vdaas/vald/internal/log/level" - logger "github.com/vdaas/vald/internal/log/logger" + "github.com/vdaas/vald/internal/log/logger" "github.com/vdaas/vald/internal/log/mock" + "go.uber.org/goleak" ) func TestWithLogger(t *testing.T) { + type T = option + type args struct { + logger Logger + } + type want struct { + obj *T + } type test struct { - name string - l Logger - checkFunc func(Option) error + name string + args args + want want + checkFunc func(want, *T) error + beforeFunc func(args) + afterFunc func(args) + } + + defaultCheckFunc := func(w want, obj *T) error { + if !reflect.DeepEqual(obj, w.obj) { + return errors.Errorf("got = %v, want %v", obj, w.obj) + } + return nil } tests := []test{ func() test { - l := new(mock.Logger) + ml := new(mock.Logger) return test{ name: "set success when l is not nil", - l: l, - checkFunc: func(opt Option) error { - option := new(option) - opt(option) - - if !reflect.DeepEqual(option.logger, l) { - return errors.New("invalid params was set") - } - - return nil + args: args{ + logger: ml, + }, + want: want{ + obj: &T{ + logger: ml, + }, }, } }(), func() test { - l := new(mock.Logger) - return test{ - name: "returns nothing when l is nil", - l: nil, - checkFunc: func(opt Option) error { - option := &option{ - logger: l, - } - opt(option) - - if !reflect.DeepEqual(option.logger, l) { - return errors.New("invalid params was set") - } - - return nil + name: "set nothing when l is nil", + want: want{ + obj: new(T), }, } }(), } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - opt := WithLogger(tt.l) - if err := tt.checkFunc(opt); err != nil { - t.Error(err) + for _, test := range tests { + t.Run(test.name, func(tt *testing.T) { + defer goleak.VerifyNone(tt, goleakIgnoreOptions...) + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + got := WithLogger(test.args.logger) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) } }) } } func TestWithLoggerType(t *testing.T) { + type T = option + type args struct { + str string + } + type want struct { + obj *T + } type test struct { - name string - str string - checkFunc func(Option) error + name string + args args + want want + checkFunc func(want, *T) error + beforeFunc func(args) + afterFunc func(args) + } + + defaultCheckFunc := func(w want, obj *T) error { + if !reflect.DeepEqual(obj, w.obj) { + return errors.Errorf("got = %v, want %v", obj, w.obj) + } + return nil } tests := []test{ { - name: "set success when str is not empty", - str: logger.GLG.String(), - checkFunc: func(opt Option) error { - option := new(option) - opt(option) - - if option.logType != logger.GLG { - return errors.New("invalid params was set") - } - return nil + name: "set success when str is `glg`", + args: args{ + str: logger.GLG.String(), + }, + want: want{ + obj: &T{ + logType: logger.GLG, + }, }, }, { - name: "returns nothing when str is empty", - checkFunc: func(opt Option) error { - option := &option{ - logType: logger.ZAP, - } - opt(option) - - if option.logType != logger.ZAP { - return errors.New("invalid params was set") - } - return nil + name: "set nothing when str is empty", + want: want{ + obj: new(T), + }, + }, + + { + name: "set nothing when str is invalid", + args: args{ + str: "invalid", + }, + want: want{ + obj: new(T), }, }, } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - opt := WithLoggerType(tt.str) - if err := tt.checkFunc(opt); err != nil { - t.Error(err) + for _, test := range tests { + t.Run(test.name, func(tt *testing.T) { + defer goleak.VerifyNone(tt, goleakIgnoreOptions...) + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + got := WithLoggerType(test.args.str) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) } }) } } func TestWithLevel(t *testing.T) { + type T = option + type args struct { + str string + } + type want struct { + obj *T + } type test struct { - name string - str string - checkFunc func(Option) error + name string + args args + want want + checkFunc func(want, *T) error + beforeFunc func(args) + afterFunc func(args) + } + + defaultCheckFunc := func(w want, obj *T) error { + if !reflect.DeepEqual(obj, w.obj) { + return errors.Errorf("got = %v, want %v", obj, w.obj) + } + return nil } tests := []test{ { - name: "set success when str is not empty", - str: level.DEBUG.String(), - checkFunc: func(opt Option) error { - option := new(option) - opt(option) - - if option.level != level.DEBUG { - return errors.New("invalid params was set") - } - return nil + name: "set success when str is `DEBUG`", + args: args{ + str: level.DEBUG.String(), + }, + want: want{ + obj: &T{ + level: level.DEBUG, + }, }, }, { - name: "returns nothing when str is empty", - checkFunc: func(opt Option) error { - option := &option{ - level: level.ERROR, - } - opt(option) - - if option.level != level.ERROR { - return errors.New("invalid params was set") - } - return nil + name: "set nothing when str is empty", + want: want{ + obj: new(T), + }, + }, + + { + name: "set nothing when str is invalid", + args: args{ + str: "invalid", + }, + want: want{ + obj: new(T), }, }, } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - opt := WithLevel(tt.str) - if err := tt.checkFunc(opt); err != nil { - t.Error(err) + for _, test := range tests { + t.Run(test.name, func(tt *testing.T) { + defer goleak.VerifyNone(tt, goleakIgnoreOptions...) + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + got := WithLevel(test.args.str) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) } }) } } func TestWithFormat(t *testing.T) { + type T = option + type args struct { + str string + } + type want struct { + obj *T + } type test struct { - name string - str string - checkFunc func(Option) error + name string + args args + want want + checkFunc func(want, *T) error + beforeFunc func(args) + afterFunc func(args) + } + + defaultCheckFunc := func(w want, obj *T) error { + if !reflect.DeepEqual(obj, w.obj) { + return errors.Errorf("got = %v, want %v", obj, w.obj) + } + return nil } tests := []test{ - { - name: "set success when str is not empty", - str: format.JSON.String(), - checkFunc: func(opt Option) error { - option := new(option) - opt(option) - - if option.format != format.JSON { - return errors.New("invalid params was set") - } - return nil - }, - }, + func() test { + return test{ + name: "set success when str is json", + args: args{ + str: format.JSON.String(), + }, + want: want{ + obj: &T{ + format: format.JSON, + }, + }, + } + }(), - { - name: "returns nothing when str is empty", - checkFunc: func(opt Option) error { - option := &option{ - format: format.JSON, - } - opt(option) - - if option.format != format.JSON { - return errors.New("invalid params was set") - } - return nil - }, - }, + func() test { + return test{ + name: "set nothing when str is empty", + want: want{ + obj: new(T), + }, + } + }(), + + func() test { + return test{ + name: "set nothing when str is invalid", + args: args{ + str: "invalid", + }, + want: want{ + obj: new(T), + }, + } + }(), } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - opt := WithFormat(tt.str) - if err := tt.checkFunc(opt); err != nil { - t.Error(err) + for _, test := range tests { + t.Run(test.name, func(tt *testing.T) { + defer goleak.VerifyNone(tt, goleakIgnoreOptions...) + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + got := WithFormat(test.args.str) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) } }) }