From 36e587db935afdb7eb93cc4a9bc675eb8783753c Mon Sep 17 00:00:00 2001 From: hlts2 Date: Mon, 11 May 2020 15:53:35 +0900 Subject: [PATCH] fix: to match the test template Signed-off-by: hlts2 --- internal/log/format/format_test.go | 191 +++++++++++++++-------------- 1 file changed, 98 insertions(+), 93 deletions(-) diff --git a/internal/log/format/format_test.go b/internal/log/format/format_test.go index 661a887b85..75ace4e04a 100644 --- a/internal/log/format/format_test.go +++ b/internal/log/format/format_test.go @@ -23,153 +23,160 @@ import ( "go.uber.org/goleak" ) -func TestString(t *testing.T) { +func TestFormat_String(t *testing.T) { + type want struct { + want string + } type test struct { - name string - format Format - want string + name string + f Format + want want + checkFunc func(want, string) error + beforeFunc func() + afterFunc func() + } + 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 raw", - format: RAW, - want: "raw", + name: "returns raw when f is RAW", + f: RAW, + want: want{ + want: "raw", + }, }, { - name: "returns json", - format: JSON, - want: "json", + name: "returns json when f is JSON", + f: JSON, + want: want{ + want: "json", + }, }, { - name: "returns ltsv", - format: LTSV, - want: "ltsv", + name: "returns ltsv when f is LTSV", + f: LTSV, + want: want{ + want: "ltsv", + }, }, { - name: "returns unknown", - format: Format(100), - want: "unknown", + name: "returns unknown when f is 100", + f: Format(100), + want: want{ + want: "unknown", + }, }, } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got := tt.format.String() - if got != tt.want { - t.Errorf("not equals. want: %v, but got: %v", tt.want, got) + for _, test := range tests { + t.Run(test.name, func(tt *testing.T) { + defer goleak.VerifyNone(tt) + if test.beforeFunc != nil { + test.beforeFunc() + } + if test.afterFunc != nil { + defer test.afterFunc() + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := test.f.String() + if err := test.checkFunc(test.want, got); err != nil { + tt.Errorf("error = %v", err) } + }) } } func TestAtof(t *testing.T) { - type test struct { - name string - str string + type want struct { want Format } + type test struct { + name string + str string + want want + checkFunc func(want, Format) error + beforeFunc func() + afterFunc func() + } + defaultCheckFunc := func(w want, got Format) error { + if !reflect.DeepEqual(got, w.want) { + return errors.Errorf("got = %v, want %v", got, w.want) + } + return nil + } tests := []test{ { - name: "returns RAW when str is raw", + name: "returns RAW when str is `raw`", str: "raw", - want: RAW, + want: want{ + want: RAW, + }, }, { - name: "returns RAW when str is RAw", + name: "returns RAW when str is `RAw`", str: "RAw", - want: RAW, + want: want{ + want: RAW, + }, }, { - name: "returns JSON when str is json", + name: "returns JSON when str is `json`", str: "json", - want: JSON, + want: want{ + want: JSON, + }, }, { - name: "returns JSON when str is JSOn", + name: "returns JSON when str is `JSOn`", str: "JSOn", - want: JSON, + want: want{ + want: JSON, + }, }, { - name: "returns LTSV when str is ltsv", + name: "returns LTSV when str is `ltsv`", str: "ltsv", - want: LTSV, + want: want{ + want: LTSV, + }, }, { - name: "returns LTSV when str is LTSv", + name: "returns LTSV when str is `LTSv`", str: "LTSv", - want: LTSV, + want: want{ + LTSV, + }, }, { - name: "returns Unknown when str is Vald", + name: "returns Unknown when str is `Vald`", str: "Vald", - want: Unknown, + want: want{ + want: Unknown, + }, }, } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got := Atof(tt.str) - if got != tt.want { - t.Errorf("not equals. want: %v, but got: %v", tt.want, got) - } - }) - } -} - -func TestFormat_String(t *testing.T) { - type want struct { - want string - } - type test struct { - name string - f Format - want want - checkFunc func(want, string) error - beforeFunc func() - afterFunc func() - } - 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{ - // TODO test cases - /* - { - name: "test_case_1", - want: want{}, - checkFunc: defaultCheckFunc, - }, - */ - - // TODO test cases - /* - func() test { - return test { - name: "test_case_2", - want: want{}, - checkFunc: defaultCheckFunc, - } - }(), - */ - } - for _, test := range tests { t.Run(test.name, func(tt *testing.T) { - defer goleak.VerifyNone(t) + defer goleak.VerifyNone(tt) if test.beforeFunc != nil { test.beforeFunc() } @@ -179,12 +186,10 @@ func TestFormat_String(t *testing.T) { if test.checkFunc == nil { test.checkFunc = defaultCheckFunc } - - got := test.f.String() + got := Atof(test.str) if err := test.checkFunc(test.want, got); err != nil { tt.Errorf("error = %v", err) } - }) } }