From 3300cf5a4e4422e66796a65c8669a802c513cd39 Mon Sep 17 00:00:00 2001 From: hlts2 Date: Mon, 15 Jun 2020 14:08:11 +0900 Subject: [PATCH 01/29] feat: add params pacakge test Signed-off-by: hlts2 --- internal/params/option_test.go | 742 +++++++++----------------- internal/params/params_test.go | 317 +++++++---- internal/params/test_datas/config.yml | 0 3 files changed, 437 insertions(+), 622 deletions(-) create mode 100644 internal/params/test_datas/config.yml diff --git a/internal/params/option_test.go b/internal/params/option_test.go index 03f3b809f6..86d7deb35f 100644 --- a/internal/params/option_test.go +++ b/internal/params/option_test.go @@ -18,88 +18,64 @@ package params import ( + "reflect" "testing" + "github.com/vdaas/vald/internal/errors" "go.uber.org/goleak" ) func TestWithConfigFilePathKeys(t *testing.T) { - type T = interface{} + type T = parser type args struct { keys []string } type want struct { obj *T - // Uncomment this line if the option returns an error, otherwise delete it - // err error } type test struct { - name string - args args - want want - // Use the first line if the option returns an error. otherwise use the second line - // checkFunc func(want, *T, error) error - // checkFunc func(want, *T) error + name string + args args + want want + checkFunc func(want, *T) error beforeFunc func(args) afterFunc func(args) } - // Uncomment this block if the option returns an error, otherwise delete it - /* - defaultCheckFunc := func(w want, obj *T, err error) error { - if !errors.Is(err, w.err) { - return errors.Errorf("got error = %v, want %v", err, w.err) - } - if !reflect.DeepEqual(obj, w.obj) { - return errors.Errorf("got = %v, want %v", obj, w.obj) - } - return nil - } - */ - - // Uncomment this block if the option do not returns an error, otherwise delete it - /* - defaultCheckFunc := func(w want, obj *T) error { - if !reflect.DeepEqual(obj, w.obj) { - return errors.Errorf("got = %v, want %v", obj, w.c) - } - return nil - } - */ + 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{ - // TODO test cases - /* - { - name: "test_case_1", - args: args { - keys: nil, - }, - want: want { - obj: new(T), - }, - }, - */ - - // TODO test cases - /* - func() test { - return test { - name: "test_case_2", - args: args { - keys: nil, - }, - want: want { - obj: new(T), - }, - } - }(), - */ + { + name: "set success when keys is `key1`", + args: args{ + keys: []string{ + "key1", + }, + }, + want: want{ + obj: &T{ + filePath: struct { + keys []string + defaultPath string + description string + }{ + keys: []string{ + "key1", + }, + }, + }, + }, + }, } 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(test.args) } @@ -107,112 +83,66 @@ func TestWithConfigFilePathKeys(t *testing.T) { defer test.afterFunc(test.args) } - // Uncomment this block if the option returns an error, otherwise delete it - /* - if test.checkFunc == nil { - test.checkFunc = defaultCheckFunc - } - - got := WithConfigFilePathKeys(test.args.keys...) - obj := new(T) - if err := test.checkFunc(test.want, obj, got(obj)); err != nil { - tt.Errorf("error = %v", err) - } - */ - - // Uncomment this block if the option returns an error, otherwise delete it - /* - if test.checkFunc == nil { - test.checkFunc = defaultCheckFunc - } - got := WithConfigFilePathKeys(test.args.keys...) - obj := new(T) - got(obj) - if err := test.checkFunc(tt.want, obj); err != nil { - tt.Errorf("error = %v", err) - } - */ + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + got := WithConfigFilePathKeys(test.args.keys...) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } }) } } func TestWithConfigFilePathDefault(t *testing.T) { - type T = interface{} + type T = parser type args struct { path string } type want struct { obj *T - // Uncomment this line if the option returns an error, otherwise delete it - // err error } type test struct { - name string - args args - want want - // Use the first line if the option returns an error. otherwise use the second line - // checkFunc func(want, *T, error) error - // checkFunc func(want, *T) error + name string + args args + want want + checkFunc func(want, *T) error beforeFunc func(args) afterFunc func(args) } - // Uncomment this block if the option returns an error, otherwise delete it - /* - defaultCheckFunc := func(w want, obj *T, err error) error { - if !errors.Is(err, w.err) { - return errors.Errorf("got error = %v, want %v", err, w.err) - } - if !reflect.DeepEqual(obj, w.obj) { - return errors.Errorf("got = %v, want %v", obj, w.obj) - } - return nil - } - */ - - // Uncomment this block if the option do not returns an error, otherwise delete it - /* - defaultCheckFunc := func(w want, obj *T) error { - if !reflect.DeepEqual(obj, w.obj) { - return errors.Errorf("got = %v, want %v", obj, w.c) - } - return nil - } - */ + 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{ - // TODO test cases - /* - { - name: "test_case_1", - args: args { - path: "", - }, - want: want { - obj: new(T), - }, - }, - */ - - // TODO test cases - /* - func() test { - return test { - name: "test_case_2", - args: args { - path: "", - }, - want: want { - obj: new(T), - }, - } - }(), - */ + { + name: "set success when path is `path`", + args: args{ + path: "path", + }, + want: want{ + obj: &T{ + filePath: struct { + keys []string + defaultPath string + description string + }{ + defaultPath: "path", + }, + }, + }, + }, } 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(test.args) } @@ -220,112 +150,66 @@ func TestWithConfigFilePathDefault(t *testing.T) { defer test.afterFunc(test.args) } - // Uncomment this block if the option returns an error, otherwise delete it - /* - if test.checkFunc == nil { - test.checkFunc = defaultCheckFunc - } - - got := WithConfigFilePathDefault(test.args.path) - obj := new(T) - if err := test.checkFunc(test.want, obj, got(obj)); err != nil { - tt.Errorf("error = %v", err) - } - */ - - // Uncomment this block if the option returns an error, otherwise delete it - /* - if test.checkFunc == nil { - test.checkFunc = defaultCheckFunc - } - got := WithConfigFilePathDefault(test.args.path) - obj := new(T) - got(obj) - if err := test.checkFunc(tt.want, obj); err != nil { - tt.Errorf("error = %v", err) - } - */ + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + got := WithConfigFilePathDefault(test.args.path) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } }) } } func TestWithConfigFileDescription(t *testing.T) { - type T = interface{} + type T = parser type args struct { desc string } type want struct { obj *T - // Uncomment this line if the option returns an error, otherwise delete it - // err error } type test struct { - name string - args args - want want - // Use the first line if the option returns an error. otherwise use the second line - // checkFunc func(want, *T, error) error - // checkFunc func(want, *T) error + name string + args args + want want + checkFunc func(want, *T) error beforeFunc func(args) afterFunc func(args) } - // Uncomment this block if the option returns an error, otherwise delete it - /* - defaultCheckFunc := func(w want, obj *T, err error) error { - if !errors.Is(err, w.err) { - return errors.Errorf("got error = %v, want %v", err, w.err) - } - if !reflect.DeepEqual(obj, w.obj) { - return errors.Errorf("got = %v, want %v", obj, w.obj) - } - return nil - } - */ - - // Uncomment this block if the option do not returns an error, otherwise delete it - /* - defaultCheckFunc := func(w want, obj *T) error { - if !reflect.DeepEqual(obj, w.obj) { - return errors.Errorf("got = %v, want %v", obj, w.c) - } - return nil - } - */ + 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{ - // TODO test cases - /* - { - name: "test_case_1", - args: args { - desc: "", - }, - want: want { - obj: new(T), - }, - }, - */ - - // TODO test cases - /* - func() test { - return test { - name: "test_case_2", - args: args { - desc: "", - }, - want: want { - obj: new(T), - }, - } - }(), - */ + { + name: "set success when desc is `desc`", + args: args{ + desc: "desc", + }, + want: want{ + obj: &T{ + filePath: struct { + keys []string + defaultPath string + description string + }{ + description: "desc", + }, + }, + }, + }, } 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(test.args) } @@ -333,112 +217,70 @@ func TestWithConfigFileDescription(t *testing.T) { defer test.afterFunc(test.args) } - // Uncomment this block if the option returns an error, otherwise delete it - /* - if test.checkFunc == nil { - test.checkFunc = defaultCheckFunc - } - - got := WithConfigFileDescription(test.args.desc) - obj := new(T) - if err := test.checkFunc(test.want, obj, got(obj)); err != nil { - tt.Errorf("error = %v", err) - } - */ - - // Uncomment this block if the option returns an error, otherwise delete it - /* - if test.checkFunc == nil { - test.checkFunc = defaultCheckFunc - } - got := WithConfigFileDescription(test.args.desc) - obj := new(T) - got(obj) - if err := test.checkFunc(tt.want, obj); err != nil { - tt.Errorf("error = %v", err) - } - */ + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + got := WithConfigFileDescription(test.args.desc) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } }) } } func TestWithVersionKeys(t *testing.T) { - type T = interface{} + type T = parser type args struct { keys []string } type want struct { obj *T - // Uncomment this line if the option returns an error, otherwise delete it - // err error } type test struct { - name string - args args - want want - // Use the first line if the option returns an error. otherwise use the second line - // checkFunc func(want, *T, error) error - // checkFunc func(want, *T) error + name string + args args + want want + checkFunc func(want, *T) error beforeFunc func(args) afterFunc func(args) } - // Uncomment this block if the option returns an error, otherwise delete it - /* - defaultCheckFunc := func(w want, obj *T, err error) error { - if !errors.Is(err, w.err) { - return errors.Errorf("got error = %v, want %v", err, w.err) - } - if !reflect.DeepEqual(obj, w.obj) { - return errors.Errorf("got = %v, want %v", obj, w.obj) - } - return nil - } - */ - - // Uncomment this block if the option do not returns an error, otherwise delete it - /* - defaultCheckFunc := func(w want, obj *T) error { - if !reflect.DeepEqual(obj, w.obj) { - return errors.Errorf("got = %v, want %v", obj, w.c) - } - return nil - } - */ + 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{ - // TODO test cases - /* - { - name: "test_case_1", - args: args { - keys: nil, - }, - want: want { - obj: new(T), - }, - }, - */ - - // TODO test cases - /* - func() test { - return test { - name: "test_case_2", - args: args { - keys: nil, - }, - want: want { - obj: new(T), - }, - } - }(), - */ + { + name: "set success when keys is `key1`", + args: args{ + keys: []string{ + "key1", + }, + }, + want: want{ + obj: &T{ + version: struct { + keys []string + defaultFlag bool + description string + }{ + keys: []string{ + "key1", + }, + }, + }, + }, + }, } 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(test.args) } @@ -446,112 +288,66 @@ func TestWithVersionKeys(t *testing.T) { defer test.afterFunc(test.args) } - // Uncomment this block if the option returns an error, otherwise delete it - /* - if test.checkFunc == nil { - test.checkFunc = defaultCheckFunc - } - - got := WithVersionKeys(test.args.keys...) - obj := new(T) - if err := test.checkFunc(test.want, obj, got(obj)); err != nil { - tt.Errorf("error = %v", err) - } - */ - - // Uncomment this block if the option returns an error, otherwise delete it - /* - if test.checkFunc == nil { - test.checkFunc = defaultCheckFunc - } - got := WithVersionKeys(test.args.keys...) - obj := new(T) - got(obj) - if err := test.checkFunc(tt.want, obj); err != nil { - tt.Errorf("error = %v", err) - } - */ + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + got := WithVersionKeys(test.args.keys...) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } }) } } func TestWithVersionFlagDefault(t *testing.T) { - type T = interface{} + type T = parser type args struct { flag bool } type want struct { obj *T - // Uncomment this line if the option returns an error, otherwise delete it - // err error } type test struct { - name string - args args - want want - // Use the first line if the option returns an error. otherwise use the second line - // checkFunc func(want, *T, error) error - // checkFunc func(want, *T) error + name string + args args + want want + checkFunc func(want, *T) error beforeFunc func(args) afterFunc func(args) } - // Uncomment this block if the option returns an error, otherwise delete it - /* - defaultCheckFunc := func(w want, obj *T, err error) error { - if !errors.Is(err, w.err) { - return errors.Errorf("got error = %v, want %v", err, w.err) - } - if !reflect.DeepEqual(obj, w.obj) { - return errors.Errorf("got = %v, want %v", obj, w.obj) - } - return nil - } - */ - - // Uncomment this block if the option do not returns an error, otherwise delete it - /* - defaultCheckFunc := func(w want, obj *T) error { - if !reflect.DeepEqual(obj, w.obj) { - return errors.Errorf("got = %v, want %v", obj, w.c) - } - return nil - } - */ + 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{ - // TODO test cases - /* - { - name: "test_case_1", - args: args { - flag: false, - }, - want: want { - obj: new(T), - }, - }, - */ - - // TODO test cases - /* - func() test { - return test { - name: "test_case_2", - args: args { - flag: false, - }, - want: want { - obj: new(T), - }, - } - }(), - */ + { + name: "set success when flag is true", + args: args{ + flag: true, + }, + want: want{ + obj: &T{ + version: struct { + keys []string + defaultFlag bool + description string + }{ + defaultFlag: true, + }, + }, + }, + }, } 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(test.args) } @@ -559,112 +355,66 @@ func TestWithVersionFlagDefault(t *testing.T) { defer test.afterFunc(test.args) } - // Uncomment this block if the option returns an error, otherwise delete it - /* - if test.checkFunc == nil { - test.checkFunc = defaultCheckFunc - } - - got := WithVersionFlagDefault(test.args.flag) - obj := new(T) - if err := test.checkFunc(test.want, obj, got(obj)); err != nil { - tt.Errorf("error = %v", err) - } - */ - - // Uncomment this block if the option returns an error, otherwise delete it - /* - if test.checkFunc == nil { - test.checkFunc = defaultCheckFunc - } - got := WithVersionFlagDefault(test.args.flag) - obj := new(T) - got(obj) - if err := test.checkFunc(tt.want, obj); err != nil { - tt.Errorf("error = %v", err) - } - */ + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + got := WithVersionFlagDefault(test.args.flag) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } }) } } func TestWithVersionDescription(t *testing.T) { - type T = interface{} + type T = parser type args struct { desc string } type want struct { obj *T - // Uncomment this line if the option returns an error, otherwise delete it - // err error } type test struct { - name string - args args - want want - // Use the first line if the option returns an error. otherwise use the second line - // checkFunc func(want, *T, error) error - // checkFunc func(want, *T) error + name string + args args + want want + checkFunc func(want, *T) error beforeFunc func(args) afterFunc func(args) } - // Uncomment this block if the option returns an error, otherwise delete it - /* - defaultCheckFunc := func(w want, obj *T, err error) error { - if !errors.Is(err, w.err) { - return errors.Errorf("got error = %v, want %v", err, w.err) - } - if !reflect.DeepEqual(obj, w.obj) { - return errors.Errorf("got = %v, want %v", obj, w.obj) - } - return nil - } - */ - - // Uncomment this block if the option do not returns an error, otherwise delete it - /* - defaultCheckFunc := func(w want, obj *T) error { - if !reflect.DeepEqual(obj, w.obj) { - return errors.Errorf("got = %v, want %v", obj, w.c) - } - return nil - } - */ + 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{ - // TODO test cases - /* - { - name: "test_case_1", - args: args { - desc: "", - }, - want: want { - obj: new(T), - }, - }, - */ - - // TODO test cases - /* - func() test { - return test { - name: "test_case_2", - args: args { - desc: "", - }, - want: want { - obj: new(T), - }, - } - }(), - */ + { + name: "set success when desc is true", + args: args{ + desc: "desc", + }, + want: want{ + obj: &T{ + version: struct { + keys []string + defaultFlag bool + description string + }{ + description: "desc", + }, + }, + }, + }, } 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(test.args) } @@ -672,31 +422,15 @@ func TestWithVersionDescription(t *testing.T) { defer test.afterFunc(test.args) } - // Uncomment this block if the option returns an error, otherwise delete it - /* - if test.checkFunc == nil { - test.checkFunc = defaultCheckFunc - } - - got := WithVersionDescription(test.args.desc) - obj := new(T) - if err := test.checkFunc(test.want, obj, got(obj)); err != nil { - tt.Errorf("error = %v", err) - } - */ - - // Uncomment this block if the option returns an error, otherwise delete it - /* - if test.checkFunc == nil { - test.checkFunc = defaultCheckFunc - } - got := WithVersionDescription(test.args.desc) - obj := new(T) - got(obj) - if err := test.checkFunc(tt.want, obj); err != nil { - tt.Errorf("error = %v", err) - } - */ + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + got := WithVersionDescription(test.args.desc) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } }) } } diff --git a/internal/params/params_test.go b/internal/params/params_test.go index 50b3e4ba94..d1132c4a8c 100644 --- a/internal/params/params_test.go +++ b/internal/params/params_test.go @@ -18,7 +18,10 @@ package params import ( + stderrs "errors" + "os" "reflect" + "syscall" "testing" "github.com/vdaas/vald/internal/errors" @@ -48,36 +51,87 @@ func TestNew(t *testing.T) { return nil } tests := []test{ - // TODO test cases - /* - { - name: "test_case_1", - args: args { - opts: nil, - }, - want: want{}, - checkFunc: defaultCheckFunc, - }, - */ + { + name: "returns *parser when opts is nil", + want: want{ + want: &parser{ + filePath: struct { + keys []string + defaultPath string + description string + }{ + keys: []string{ + "f", + "file", + "c", + "config", + }, + defaultPath: "/etc/server/config.yaml", + description: "config file path", + }, + version: struct { + keys []string + defaultFlag bool + description string + }{ + keys: []string{ + "v", + "ver", + "version", + }, + defaultFlag: false, + description: "show server version", + }, + }, + }, + }, - // TODO test cases - /* - func() test { - return test { - name: "test_case_2", - args: args { - opts: nil, - }, - want: want{}, - checkFunc: defaultCheckFunc, - } - }(), - */ + { + name: "returns *parser when opts is not nil", + args: args{ + opts: []Option{ + WithConfigFilePathKeys("t", "test"), + }, + }, + want: want{ + want: &parser{ + filePath: struct { + keys []string + defaultPath string + description string + }{ + keys: []string{ + "f", + "file", + "c", + "config", + "t", + "test", + }, + defaultPath: "/etc/server/config.yaml", + description: "config file path", + }, + version: struct { + keys []string + defaultFlag bool + description string + }{ + keys: []string{ + "v", + "ver", + "version", + }, + defaultFlag: false, + description: "show server version", + }, + }, + }, + }, } 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(test.args) } @@ -92,7 +146,6 @@ func TestNew(t *testing.T) { if err := test.checkFunc(test.want, got); err != nil { tt.Errorf("error = %v", err) } - }) } } @@ -136,38 +189,109 @@ func Test_parser_Parse(t *testing.T) { return nil } tests := []test{ - // TODO test cases - /* - { - name: "test_case_1", - fields: fields { - filePath: struct{keys []string; defaultPath string; description string}{}, - version: struct{keys []string; defaultFlag bool; description string}{}, - }, - want: want{}, - checkFunc: defaultCheckFunc, - }, - */ + { + name: "returns (d, false, nil) when parse succeeds", + fields: fields{ + filePath: struct { + keys []string + defaultPath string + description string + }{ + keys: []string{ + "path", "p", + }, + defaultPath: "./test_datas/config.yml", + description: "sets file path", + }, + version: struct { + keys []string + defaultFlag bool + description string + }{ + keys: []string{ + "version", "v", + }, + defaultFlag: true, + description: "show version", + }, + }, + beforeFunc: func() { + os.Args = []string{ + "test", "--path=./test_datas/config.yml", "--version=false", + } + }, + afterFunc: func() { os.Args = nil }, + want: want{ + want: &data{ + configFilePath: "./test_datas/config.yml", + showVersion: false, + }, + }, + }, + + { + name: "returns (nil, true, nil) When parse fails but the help option is set", + beforeFunc: func() { + os.Args = []string{ + "test", "--help", + } + }, + afterFunc: func() { os.Args = nil }, + want: want{ + want1: true, + }, + }, + + { + name: "returns (nil, true, nil) When parse fails but the help option is not set", + beforeFunc: func() { + os.Args = []string{ + "test", "--name", + } + }, + afterFunc: func() { os.Args = nil }, + want: want{ + want1: false, + err: errors.ErrArgumentParseFailed(stderrs.New("flag provided but not defined: -name")), + }, + }, - // TODO test cases - /* - func() test { - return test { - name: "test_case_2", - fields: fields { - filePath: struct{keys []string; defaultPath string; description string}{}, - version: struct{keys []string; defaultFlag bool; description string}{}, - }, - want: want{}, - checkFunc: defaultCheckFunc, - } - }(), - */ + { + name: "returns (nil, true, error) When the configFilePath option is set but file dose not exist", + fields: fields{ + filePath: struct { + keys []string + defaultPath string + description string + }{ + keys: []string{ + "path", "p", + }, + description: "sets file path", + }, + }, + beforeFunc: func() { + os.Args = []string{ + "test", "--path=config.yml", + } + }, + afterFunc: func() { os.Args = nil }, + want: want{ + want1: true, + err: &os.PathError{ + Op: "stat", + Path: "config.yml", + Err: syscall.Errno(0x2), + }, + }, + }, } + type h = syscall.Errno + 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() } @@ -186,7 +310,6 @@ func Test_parser_Parse(t *testing.T) { if err := test.checkFunc(test.want, got, got1, err); err != nil { tt.Errorf("error = %v", err) } - }) } } @@ -194,7 +317,6 @@ func Test_parser_Parse(t *testing.T) { func Test_data_ConfigFilePath(t *testing.T) { type fields struct { configFilePath string - showVersion bool } type want struct { want string @@ -214,38 +336,20 @@ func Test_data_ConfigFilePath(t *testing.T) { return nil } tests := []test{ - // TODO test cases - /* - { - name: "test_case_1", - fields: fields { - configFilePath: "", - showVersion: false, - }, - want: want{}, - checkFunc: defaultCheckFunc, - }, - */ - - // TODO test cases - /* - func() test { - return test { - name: "test_case_2", - fields: fields { - configFilePath: "", - showVersion: false, - }, - want: want{}, - checkFunc: defaultCheckFunc, - } - }(), - */ + { + name: "returns `./path` when d.configFilePath is `./path`", + fields: fields{ + configFilePath: "./path", + }, + want: want{ + want: "./path", + }, + }, } 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() } @@ -257,22 +361,19 @@ func Test_data_ConfigFilePath(t *testing.T) { } d := &data{ configFilePath: test.fields.configFilePath, - showVersion: test.fields.showVersion, } got := d.ConfigFilePath() if err := test.checkFunc(test.want, got); err != nil { tt.Errorf("error = %v", err) } - }) } } func Test_data_ShowVersion(t *testing.T) { type fields struct { - configFilePath string - showVersion bool + showVersion bool } type want struct { want bool @@ -292,38 +393,20 @@ func Test_data_ShowVersion(t *testing.T) { return nil } tests := []test{ - // TODO test cases - /* - { - name: "test_case_1", - fields: fields { - configFilePath: "", - showVersion: false, - }, - want: want{}, - checkFunc: defaultCheckFunc, - }, - */ - - // TODO test cases - /* - func() test { - return test { - name: "test_case_2", - fields: fields { - configFilePath: "", - showVersion: false, - }, - want: want{}, - checkFunc: defaultCheckFunc, - } - }(), - */ + { + name: "returns true when d.showVersion is true", + fields: fields{ + showVersion: true, + }, + want: want{ + want: true, + }, + }, } 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() } @@ -334,15 +417,13 @@ func Test_data_ShowVersion(t *testing.T) { test.checkFunc = defaultCheckFunc } d := &data{ - configFilePath: test.fields.configFilePath, - showVersion: test.fields.showVersion, + showVersion: test.fields.showVersion, } got := d.ShowVersion() if err := test.checkFunc(test.want, got); err != nil { tt.Errorf("error = %v", err) } - }) } } diff --git a/internal/params/test_datas/config.yml b/internal/params/test_datas/config.yml new file mode 100644 index 0000000000..e69de29bb2 From 4166c2c0abb8b8fcfbd31bc171815a9e5473dd4f Mon Sep 17 00:00:00 2001 From: hlts2 Date: Mon, 15 Jun 2020 14:36:37 +0900 Subject: [PATCH 02/29] feat: add comment for the godoc Signed-off-by: hlts2 --- internal/params/option.go | 6 ++++++ internal/params/params.go | 5 +++++ internal/params/params_test.go | 6 +++--- internal/params/test_datas/config.yml | 0 4 files changed, 14 insertions(+), 3 deletions(-) delete mode 100644 internal/params/test_datas/config.yml diff --git a/internal/params/option.go b/internal/params/option.go index dad65c7d62..c3bd8a9787 100644 --- a/internal/params/option.go +++ b/internal/params/option.go @@ -30,36 +30,42 @@ var ( } ) +// WithConfigFilePathKeys returns Option that sets filePath.keys. func WithConfigFilePathKeys(keys ...string) Option { return func(p *parser) { p.filePath.keys = append(p.filePath.keys, keys...) } } +// WithConfigFilePathDefault returns Option that sets filePath.defaultPath. func WithConfigFilePathDefault(path string) Option { return func(p *parser) { p.filePath.defaultPath = path } } +// WithConfigFileDescription returns Option that sets filePath.description. func WithConfigFileDescription(desc string) Option { return func(p *parser) { p.filePath.description = desc } } +// WithVersionKeys returns Option that sets version.keys. func WithVersionKeys(keys ...string) Option { return func(p *parser) { p.version.keys = append(p.version.keys, keys...) } } +// WithVersionFlagDefault returns Option that sets version.defaultFlag. func WithVersionFlagDefault(flag bool) Option { return func(p *parser) { p.version.defaultFlag = flag } } +// WithVersionDescription returns Option that sets version.description. func WithVersionDescription(desc string) Option { return func(p *parser) { p.version.description = desc diff --git a/internal/params/params.go b/internal/params/params.go index d568e7927f..34816eeb88 100644 --- a/internal/params/params.go +++ b/internal/params/params.go @@ -25,6 +25,7 @@ import ( "github.com/vdaas/vald/internal/errors" ) +// Data is an interface to get the configuration path and flag. type Data interface { ConfigFilePath() string ShowVersion() bool @@ -48,6 +49,7 @@ type parser struct { } } +// New returns parser object. func New(opts ...Option) *parser { p := new(parser) for _, opt := range append(defaultOpts, opts...) { @@ -56,6 +58,7 @@ func New(opts ...Option) *parser { return p } +// Parse parses command-line argument and returns parsed data. func (p *parser) Parse() (Data, bool, error) { f := flag.NewFlagSet(filepath.Base(os.Args[0]), flag.ContinueOnError) @@ -94,10 +97,12 @@ func (p *parser) Parse() (Data, bool, error) { return d, false, nil } +// ConfigFilePath returns configFilePath. func (d *data) ConfigFilePath() string { return d.configFilePath } +// ShowVersion returns showVersion. func (d *data) ShowVersion() bool { return d.showVersion } diff --git a/internal/params/params_test.go b/internal/params/params_test.go index d1132c4a8c..64b25d5b33 100644 --- a/internal/params/params_test.go +++ b/internal/params/params_test.go @@ -200,7 +200,7 @@ func Test_parser_Parse(t *testing.T) { keys: []string{ "path", "p", }, - defaultPath: "./test_datas/config.yml", + defaultPath: "./params.go", description: "sets file path", }, version: struct { @@ -217,13 +217,13 @@ func Test_parser_Parse(t *testing.T) { }, beforeFunc: func() { os.Args = []string{ - "test", "--path=./test_datas/config.yml", "--version=false", + "test", "--path=./params.go", "--version=false", } }, afterFunc: func() { os.Args = nil }, want: want{ want: &data{ - configFilePath: "./test_datas/config.yml", + configFilePath: "./params.go", showVersion: false, }, }, diff --git a/internal/params/test_datas/config.yml b/internal/params/test_datas/config.yml deleted file mode 100644 index e69de29bb2..0000000000 From b44eb3c616683d0b29ec3f7549544abe22a63cfb Mon Sep 17 00:00:00 2001 From: hlts2 Date: Mon, 15 Jun 2020 19:05:54 +0900 Subject: [PATCH 03/29] fix: apply suggestion Signed-off-by: hlts2 --- internal/params/params.go | 1 + internal/params/params_test.go | 4 +--- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/internal/params/params.go b/internal/params/params.go index 34816eeb88..56d2af6f5d 100644 --- a/internal/params/params.go +++ b/internal/params/params.go @@ -59,6 +59,7 @@ func New(opts ...Option) *parser { } // Parse parses command-line argument and returns parsed data. +// If parse fails, returns an error but when there is help option, returns nil and true. func (p *parser) Parse() (Data, bool, error) { f := flag.NewFlagSet(filepath.Base(os.Args[0]), flag.ContinueOnError) diff --git a/internal/params/params_test.go b/internal/params/params_test.go index 64b25d5b33..ee54b96e15 100644 --- a/internal/params/params_test.go +++ b/internal/params/params_test.go @@ -190,7 +190,7 @@ func Test_parser_Parse(t *testing.T) { } tests := []test{ { - name: "returns (d, false, nil) when parse succeeds", + name: "returns (d, false, nil) when parse succeed", fields: fields{ filePath: struct { keys []string @@ -287,8 +287,6 @@ func Test_parser_Parse(t *testing.T) { }, } - type h = syscall.Errno - for _, test := range tests { t.Run(test.name, func(tt *testing.T) { defer goleak.VerifyNone(tt) From e1e144653a022fa59d47b696e6691c61d9d6fd34 Mon Sep 17 00:00:00 2001 From: hlts2 Date: Tue, 16 Jun 2020 11:18:51 +0900 Subject: [PATCH 04/29] fix: apply suggestion Signed-off-by: hlts2 --- internal/params/params.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/internal/params/params.go b/internal/params/params.go index 56d2af6f5d..d58776c227 100644 --- a/internal/params/params.go +++ b/internal/params/params.go @@ -58,8 +58,7 @@ func New(opts ...Option) *parser { return p } -// Parse parses command-line argument and returns parsed data. -// If parse fails, returns an error but when there is help option, returns nil and true. +// Parse parses command-line argument and returns parsed data and whether there is a help option or not and error. func (p *parser) Parse() (Data, bool, error) { f := flag.NewFlagSet(filepath.Base(os.Args[0]), flag.ContinueOnError) From 207ee494d63e5c49730c968cb2761a36a3f1d4f2 Mon Sep 17 00:00:00 2001 From: hlts2 Date: Tue, 16 Jun 2020 11:55:48 +0900 Subject: [PATCH 05/29] fix: added document about unused variables Signed-off-by: hlts2 --- docs/contributing/coding-style.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index 2591662eb9..bca3345699 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -264,6 +264,32 @@ func (s *something) SetSignedTok(st string) { } ``` +### Unused Variables + +It is an error to declare a variable without using it. +while a variable that is initialized but not used is at least a wasted computation and perhaps indicative of a larger bug. +So please delete unused variables. + +The following is an example of an unused variables that does not cause an error.
+Please delete any variables that you don't need, as they can create bugs. + +```go +type server struct { + addr string + port int +} + +srv := &server { + addr: "192.168.33.10:1234", + // port <- unused variables +} + +if err := srv.Run(); err != nil { + log.Fatal(err) +} + +``` + ### Error handling All errors should define in [internal/errors package](https://github.com/vdaas/vald/blob/master/internal/errors). All errors should be start with `Err` prefix, and all errors should be handle if possible. From d392959aec7fd2980459c751d035975f9139b8a3 Mon Sep 17 00:00:00 2001 From: hlts2 Date: Tue, 16 Jun 2020 16:51:15 +0900 Subject: [PATCH 06/29] fix: apply suggestion Signed-off-by: hlts2 --- docs/contributing/coding-style.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index bca3345699..f5a1f0f9b2 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -629,3 +629,27 @@ We do not suggest to modify the generated code other than the `tests` variable, } // generated test code ``` + +1. Unused Variables + + Unused variable may increase the complexity of the source code, it may confuse the developer hence introduce a new bug. + So please delete the unused variable. + + Generally the unused variable should be reported during compilation, but in some case the compiler may not report an error. This is an example of the unused variable declaration that does not cause an compilation error.
+ + ```go + type server struct { + addr string + port int + } + + srv := &server { + addr: "192.168.33.10:1234", + // port <- unused variables + } + + if err := srv.Run(); err != nil { + log.Fatal(err) + } + + ``` \ No newline at end of file From 35b8165b477a31bc58fadd518ec31f9e368e768e Mon Sep 17 00:00:00 2001 From: hlts2 Date: Tue, 16 Jun 2020 16:56:32 +0900 Subject: [PATCH 07/29] fix: apply suggestion Signed-off-by: hlts2 --- docs/contributing/coding-style.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index f5a1f0f9b2..302d999730 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -632,10 +632,10 @@ We do not suggest to modify the generated code other than the `tests` variable, 1. Unused Variables - Unused variable may increase the complexity of the source code, it may confuse the developer hence introduce a new bug. + An unused variable may increase the complexity of the source code, it may confuse the developer hence introduce a new bug. So please delete the unused variable. - Generally the unused variable should be reported during compilation, but in some case the compiler may not report an error. This is an example of the unused variable declaration that does not cause an compilation error.
+ Generally, the unused variable should be reported during compilation, but in some cases, the compiler may not report an error. This is an example of the unused variable declaration that does not cause a compilation error.
```go type server struct { From 6320312e07dfeca0d939dd535f16973f306c9e2b Mon Sep 17 00:00:00 2001 From: hlts2 Date: Tue, 16 Jun 2020 17:09:12 +0900 Subject: [PATCH 08/29] fix: apply suggestion Signed-off-by: hlts2 --- docs/contributing/coding-style.md | 48 +++++++++++++++---------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index 302d999730..3ffa09855c 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -555,6 +555,30 @@ After the command above executed, the file `*target*_test.go` will be generated The test code generated follows the table-driven test format. You can implement your test code under the `tests` variable generated following the table-driven test format. +### Unused Variables + +An unused variable may increase the complexity of the source code, it may confuse the developer hence introduce a new bug. +So please delete the unused variable. + +Generally, the unused variable should be reported during compilation, but in some cases, the compiler may not report an error. This is an example of the unused variable declaration that does not cause a compilation error.
+ +```go +type server struct { + addr string + port int +} + +srv := &server { + addr: "192.168.33.10:1234", + // port <- unused variables +} + +if err := srv.Run(); err != nil { + log.Fatal(err) +} + +``` + ### Customize test case We do not suggest to modify the generated code other than the `tests` variable, but in some cases, you may need to modify the generated code to meet your requirement, for example: @@ -628,28 +652,4 @@ We do not suggest to modify the generated code other than the `tests` variable, test.beforeFunc(test.args) } // generated test code - ``` - -1. Unused Variables - - An unused variable may increase the complexity of the source code, it may confuse the developer hence introduce a new bug. - So please delete the unused variable. - - Generally, the unused variable should be reported during compilation, but in some cases, the compiler may not report an error. This is an example of the unused variable declaration that does not cause a compilation error.
- - ```go - type server struct { - addr string - port int - } - - srv := &server { - addr: "192.168.33.10:1234", - // port <- unused variables - } - - if err := srv.Run(); err != nil { - log.Fatal(err) - } - ``` \ No newline at end of file From 7f9427333b19347c90fff2cf4ae18396202ca306 Mon Sep 17 00:00:00 2001 From: hlts2 Date: Tue, 16 Jun 2020 17:54:22 +0900 Subject: [PATCH 09/29] fix: apply suggestion Signed-off-by: hlts2 --- docs/contributing/coding-style.md | 48 ++++++++++++------------------- 1 file changed, 18 insertions(+), 30 deletions(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index 3ffa09855c..76f6c5ec83 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -266,12 +266,10 @@ func (s *something) SetSignedTok(st string) { ### Unused Variables -It is an error to declare a variable without using it. -while a variable that is initialized but not used is at least a wasted computation and perhaps indicative of a larger bug. -So please delete unused variables. +An unused variable may increase the complexity of the source code, it may confuse the developer hence introduce a new bug. +So please delete the unused variable. -The following is an example of an unused variables that does not cause an error.
-Please delete any variables that you don't need, as they can create bugs. +Generally, the unused variable should be reported during compilation, but in some cases, the compiler may not report an error. This is an example of the unused variable declaration that does not cause a compilation error.
```go type server struct { @@ -555,30 +553,6 @@ After the command above executed, the file `*target*_test.go` will be generated The test code generated follows the table-driven test format. You can implement your test code under the `tests` variable generated following the table-driven test format. -### Unused Variables - -An unused variable may increase the complexity of the source code, it may confuse the developer hence introduce a new bug. -So please delete the unused variable. - -Generally, the unused variable should be reported during compilation, but in some cases, the compiler may not report an error. This is an example of the unused variable declaration that does not cause a compilation error.
- -```go -type server struct { - addr string - port int -} - -srv := &server { - addr: "192.168.33.10:1234", - // port <- unused variables -} - -if err := srv.Run(); err != nil { - log.Fatal(err) -} - -``` - ### Customize test case We do not suggest to modify the generated code other than the `tests` variable, but in some cases, you may need to modify the generated code to meet your requirement, for example: @@ -652,4 +626,18 @@ We do not suggest to modify the generated code other than the `tests` variable, test.beforeFunc(test.args) } // generated test code - ``` \ No newline at end of file + ``` + +1. Unused fields + + By default, the template provides `fields` structure to initialize object of the test target. But in some cases, not all `fields` are needed. So please delete unnecessary fields. + + If the test case needs only `addr` of `fields`, please delete other fields. + + ```go + type fields struct { + addr string + // port int <- please delete this fields + } + ``` + \ No newline at end of file From d677d82605bfd35fed40f667bc906b3d1e2b8cf6 Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Tue, 16 Jun 2020 18:01:08 +0900 Subject: [PATCH 10/29] Update docs/contributing/coding-style.md Co-authored-by: Kevin Diu --- docs/contributing/coding-style.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index 76f6c5ec83..dea290e1dc 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -630,7 +630,9 @@ We do not suggest to modify the generated code other than the `tests` variable, 1. Unused fields - By default, the template provides `fields` structure to initialize object of the test target. But in some cases, not all `fields` are needed. So please delete unnecessary fields. + By default, the template provides `fields` structure to initialize object of the test target. + + But in some cases, not all `fields` are needed, so please delete the unnecessary fields. If the test case needs only `addr` of `fields`, please delete other fields. @@ -640,4 +642,4 @@ We do not suggest to modify the generated code other than the `tests` variable, // port int <- please delete this fields } ``` - \ No newline at end of file + From e0e0beaab36bff3b3453bbdbd7eedc008fff51d4 Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Tue, 16 Jun 2020 18:32:20 +0900 Subject: [PATCH 11/29] Update docs/contributing/coding-style.md Co-authored-by: Kevin Diu --- docs/contributing/coding-style.md | 36 ++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index dea290e1dc..8e03905be7 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -634,7 +634,41 @@ We do not suggest to modify the generated code other than the `tests` variable, But in some cases, not all `fields` are needed, so please delete the unnecessary fields. - If the test case needs only `addr` of `fields`, please delete other fields. + For example, the following struct and the corresponding function: + + ```golang + type server struct { + addr string + port int + } + func (s *server) Addr() string { + return s.addr + } + ``` + + And the generated test code is: + ```golang + func Test_server_Addr(t *testing.T) { + type fields struct { + addr string + port int + } + type want struct { + // generated test code + ``` + + Since the `port` variable is not used in this test case, you can delete the `port` definition in the test case. + + ```golang + func Test_server_Addr(t *testing.T) { + type fields struct { + addr string + // port int <-- this line should be deleted + } + type want struct { + // generated test code + ``` + ```go type fields struct { From 7834ea1592dc2d8159d84bd0a793b6423eb1e2d1 Mon Sep 17 00:00:00 2001 From: hlts2 Date: Tue, 16 Jun 2020 18:38:54 +0900 Subject: [PATCH 12/29] fix: apply suggestion Signed-off-by: hlts2 --- docs/contributing/coding-style.md | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index 8e03905be7..36514ca69f 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -667,13 +667,4 @@ We do not suggest to modify the generated code other than the `tests` variable, } type want struct { // generated test code - ``` - - - ```go - type fields struct { - addr string - // port int <- please delete this fields - } - ``` - + ``` \ No newline at end of file From 31c67627692b296a5dc9f792de9b9a740040aa67 Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Tue, 16 Jun 2020 18:53:03 +0900 Subject: [PATCH 13/29] Update docs/contributing/coding-style.md Co-authored-by: Kevin Diu --- docs/contributing/coding-style.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index 36514ca69f..f5ff8920d9 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -665,6 +665,6 @@ We do not suggest to modify the generated code other than the `tests` variable, addr string // port int <-- this line should be deleted } - type want struct { + type want struct { // generated test code - ``` \ No newline at end of file + ``` From 10435c84ba3f1cc48520f54db41b0f274f7d5050 Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Tue, 16 Jun 2020 18:53:17 +0900 Subject: [PATCH 14/29] Update docs/contributing/coding-style.md Co-authored-by: Kevin Diu --- docs/contributing/coding-style.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index f5ff8920d9..4901174592 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -666,5 +666,5 @@ We do not suggest to modify the generated code other than the `tests` variable, // port int <-- this line should be deleted } type want struct { - // generated test code + // generated test code ``` From 2fbab43337fa4585b91a7fa7926cb2d44ba3b923 Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Tue, 16 Jun 2020 18:53:32 +0900 Subject: [PATCH 15/29] Update docs/contributing/coding-style.md Co-authored-by: Kevin Diu --- docs/contributing/coding-style.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index 4901174592..ccf1d1809e 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -643,7 +643,7 @@ We do not suggest to modify the generated code other than the `tests` variable, } func (s *server) Addr() string { return s.addr - } + } ``` And the generated test code is: From 3f1a47bdbb1b1f4176971c7ae5ab9ac5a1ea2ddf Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Tue, 16 Jun 2020 18:53:42 +0900 Subject: [PATCH 16/29] Update docs/contributing/coding-style.md Co-authored-by: Kevin Diu --- docs/contributing/coding-style.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index ccf1d1809e..1f1d896daf 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -653,7 +653,7 @@ We do not suggest to modify the generated code other than the `tests` variable, addr string port int } - type want struct { + type want struct { // generated test code ``` From a310c8278ec176e011947f128f7277c2e96bfa4b Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Tue, 16 Jun 2020 18:53:52 +0900 Subject: [PATCH 17/29] Update docs/contributing/coding-style.md Co-authored-by: Kevin Diu --- docs/contributing/coding-style.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index 1f1d896daf..3ba966c7c6 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -652,7 +652,7 @@ We do not suggest to modify the generated code other than the `tests` variable, type fields struct { addr string port int - } + } type want struct { // generated test code ``` From fb09a11ced24da3d9fd392068b22a0818e8cf6db Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Tue, 16 Jun 2020 18:54:03 +0900 Subject: [PATCH 18/29] Update docs/contributing/coding-style.md Co-authored-by: Kevin Diu --- docs/contributing/coding-style.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index 3ba966c7c6..ae86b4b2df 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -654,7 +654,7 @@ We do not suggest to modify the generated code other than the `tests` variable, port int } type want struct { - // generated test code + // generated test code ``` Since the `port` variable is not used in this test case, you can delete the `port` definition in the test case. From 9432e33c546a0d8d324d6b4fde54bb719da1e721 Mon Sep 17 00:00:00 2001 From: hlts2 Date: Tue, 16 Jun 2020 19:06:07 +0900 Subject: [PATCH 19/29] fix: apply suggestion Signed-off-by: hlts2 --- docs/contributing/coding-style.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index ae86b4b2df..bbea3ef8d2 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -652,9 +652,9 @@ We do not suggest to modify the generated code other than the `tests` variable, type fields struct { addr string port int - } - type want struct { - // generated test code + } + type want struct { + // generated test code ``` Since the `port` variable is not used in this test case, you can delete the `port` definition in the test case. @@ -664,7 +664,7 @@ We do not suggest to modify the generated code other than the `tests` variable, type fields struct { addr string // port int <-- this line should be deleted - } - type want struct { - // generated test code + } + type want struct { + // generated test code ``` From 6a7c131d6509be6ea657f921219091693044dc34 Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Wed, 17 Jun 2020 13:36:03 +0900 Subject: [PATCH 20/29] Update docs/contributing/coding-style.md Co-authored-by: Kiichiro YUKAWA --- docs/contributing/coding-style.md | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index bbea3ef8d2..c1ba0ef07e 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -658,7 +658,6 @@ We do not suggest to modify the generated code other than the `tests` variable, ``` Since the `port` variable is not used in this test case, you can delete the `port` definition in the test case. - ```golang func Test_server_Addr(t *testing.T) { type fields struct { From d6bf1f6a0a5892d9658c4bed1bba2da100978ce9 Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Wed, 17 Jun 2020 13:36:34 +0900 Subject: [PATCH 21/29] Update docs/contributing/coding-style.md Co-authored-by: Kiichiro YUKAWA --- docs/contributing/coding-style.md | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index c1ba0ef07e..7255f7c54f 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -285,7 +285,6 @@ srv := &server { if err := srv.Run(); err != nil { log.Fatal(err) } - ``` ### Error handling From e242e9682c43c4cf480d7ea44b97d7b15656c10f Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Wed, 17 Jun 2020 13:36:49 +0900 Subject: [PATCH 22/29] Update docs/contributing/coding-style.md Co-authored-by: Kiichiro YUKAWA --- docs/contributing/coding-style.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index 7255f7c54f..a51e648949 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -271,7 +271,7 @@ So please delete the unused variable. Generally, the unused variable should be reported during compilation, but in some cases, the compiler may not report an error. This is an example of the unused variable declaration that does not cause a compilation error.
-```go +```golang type server struct { addr string port int From 9c27a655026f934f3a81e0544d806167131e24e7 Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Wed, 17 Jun 2020 13:36:57 +0900 Subject: [PATCH 23/29] Update docs/contributing/coding-style.md Co-authored-by: Kiichiro YUKAWA --- docs/contributing/coding-style.md | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index a51e648949..1b869b4690 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -630,7 +630,6 @@ We do not suggest to modify the generated code other than the `tests` variable, 1. Unused fields By default, the template provides `fields` structure to initialize object of the test target. - But in some cases, not all `fields` are needed, so please delete the unnecessary fields. For example, the following struct and the corresponding function: From 78146672f3171d5c7e46e110083c69a1d48efa7e Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Wed, 17 Jun 2020 13:37:07 +0900 Subject: [PATCH 24/29] Update docs/contributing/coding-style.md Co-authored-by: Kiichiro YUKAWA --- docs/contributing/coding-style.md | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index 1b869b4690..10c56a8d66 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -631,7 +631,6 @@ We do not suggest to modify the generated code other than the `tests` variable, By default, the template provides `fields` structure to initialize object of the test target. But in some cases, not all `fields` are needed, so please delete the unnecessary fields. - For example, the following struct and the corresponding function: ```golang From c72dad765b2298b987bddf40b7e2ce3f36c62939 Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Wed, 17 Jun 2020 13:37:16 +0900 Subject: [PATCH 25/29] Update docs/contributing/coding-style.md Co-authored-by: Kiichiro YUKAWA --- docs/contributing/coding-style.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index 10c56a8d66..b9392d5ec1 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -269,7 +269,7 @@ func (s *something) SetSignedTok(st string) { An unused variable may increase the complexity of the source code, it may confuse the developer hence introduce a new bug. So please delete the unused variable. -Generally, the unused variable should be reported during compilation, but in some cases, the compiler may not report an error. This is an example of the unused variable declaration that does not cause a compilation error.
+Generally, the unused variable should be reported during compilation, but in some cases, the compiler may not report an error. This is an example of the unused variable declaration that does not cause a compilation error. ```golang type server struct { From d2eed9a7c5715b302be8cb2bf729996dee69e15c Mon Sep 17 00:00:00 2001 From: hlts2 Date: Wed, 17 Jun 2020 14:13:29 +0900 Subject: [PATCH 26/29] fix: apply suggestion Signed-off-by: hlts2 --- docs/contributing/coding-style.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index b9392d5ec1..23318a06c9 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -272,14 +272,17 @@ So please delete the unused variable. Generally, the unused variable should be reported during compilation, but in some cases, the compiler may not report an error. This is an example of the unused variable declaration that does not cause a compilation error. ```golang +// In this case, this example are not using `port` field, but dose not cause a compilation error. +// So please delete `port` field of `server`. + type server struct { addr string port int } +// The port number is included in `addr`, so the `port` field of `server` is not used. srv := &server { addr: "192.168.33.10:1234", - // port <- unused variables } if err := srv.Run(); err != nil { From dc81753463447202904a7f8cb0ca7c5539e37c93 Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Wed, 17 Jun 2020 14:57:13 +0900 Subject: [PATCH 27/29] Update docs/contributing/coding-style.md Co-authored-by: Kiichiro YUKAWA --- docs/contributing/coding-style.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index 23318a06c9..ebeaeb39e0 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -269,7 +269,8 @@ func (s *something) SetSignedTok(st string) { An unused variable may increase the complexity of the source code, it may confuse the developer hence introduce a new bug. So please delete the unused variable. -Generally, the unused variable should be reported during compilation, but in some cases, the compiler may not report an error. This is an example of the unused variable declaration that does not cause a compilation error. +Generally, the unused variable should be reported during compilation, but in some cases, the compiler may not report an error. +This is an example of the unused variable declaration that does not cause a compilation error. ```golang // In this case, this example are not using `port` field, but dose not cause a compilation error. From 6a24dafa169b1f6c669ffb5752fc75abe00e4caa Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Wed, 17 Jun 2020 14:57:37 +0900 Subject: [PATCH 28/29] Update docs/contributing/coding-style.md Co-authored-by: Kiichiro YUKAWA --- docs/contributing/coding-style.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index ebeaeb39e0..f05929224f 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -281,7 +281,7 @@ type server struct { port int } -// The port number is included in `addr`, so the `port` field of `server` is not used. +// The `port` field of `server` is not used. srv := &server { addr: "192.168.33.10:1234", } From 9257230fb4ba412661d36f8ed669cb9257e48fd6 Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Wed, 17 Jun 2020 14:57:51 +0900 Subject: [PATCH 29/29] Update docs/contributing/coding-style.md Co-authored-by: Kiichiro YUKAWA --- docs/contributing/coding-style.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index f05929224f..f5418b43b2 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -278,7 +278,7 @@ This is an example of the unused variable declaration that does not cause a comp type server struct { addr string - port int + port int // you have to delete this field. } // The `port` field of `server` is not used.