From 4423ae419ad5f1be5232beed15e5249a697e215d Mon Sep 17 00:00:00 2001 From: "jack.jin" Date: Tue, 7 Feb 2023 15:46:10 +0900 Subject: [PATCH] added testcase --- altsrc/map_input_source.go | 20 ++++++------- altsrc/yaml_file_loader_test.go | 53 +++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 10 deletions(-) diff --git a/altsrc/map_input_source.go b/altsrc/map_input_source.go index abd989f0d4..857e4febea 100644 --- a/altsrc/map_input_source.go +++ b/altsrc/map_input_source.go @@ -128,7 +128,7 @@ func (fsm *MapInputSource) Float64(name string) (float64, error) { return 0, nil } -func castToInt64(v interface{}) (int64, bool) { +func CastToInt64(v interface{}) (int64, bool) { int64Value := int64(0) isType := false if v == nil { @@ -152,7 +152,7 @@ func castToInt64(v interface{}) (int64, bool) { func (fsm *MapInputSource) Int64(name string) (int64, error) { otherGenericValue, exists := fsm.valueMap[name] if exists { - otherValue, isType := castToInt64(otherGenericValue) + otherValue, isType := CastToInt64(otherGenericValue) if !isType { return 0, incorrectTypeForFlagError(name, "int64", otherGenericValue) } @@ -160,7 +160,7 @@ func (fsm *MapInputSource) Int64(name string) (int64, error) { } nestedGenericValue, exists := nestedVal(name, fsm.valueMap) if exists { - otherValue, isType := castToInt64(otherGenericValue) + otherValue, isType := CastToInt64(otherGenericValue) if !isType { return 0, incorrectTypeForFlagError(name, "int64", nestedGenericValue) } @@ -170,7 +170,7 @@ func (fsm *MapInputSource) Int64(name string) (int64, error) { return 0, nil } -func castToUint(v interface{}) (uint, bool) { +func CastToUint(v interface{}) (uint, bool) { uintValue := uint(0) isType := false if v == nil { @@ -209,7 +209,7 @@ func castToUint(v interface{}) (uint, bool) { func (fsm *MapInputSource) Uint(name string) (uint, error) { otherGenericValue, exists := fsm.valueMap[name] if exists { - otherValue, isType := castToUint(otherGenericValue) + otherValue, isType := CastToUint(otherGenericValue) if !isType { return 0, incorrectTypeForFlagError(name, "uint", otherGenericValue) } @@ -217,7 +217,7 @@ func (fsm *MapInputSource) Uint(name string) (uint, error) { } nestedGenericValue, exists := nestedVal(name, fsm.valueMap) if exists { - otherValue, isType := castToUint(nestedGenericValue) + otherValue, isType := CastToUint(nestedGenericValue) if !isType { return 0, incorrectTypeForFlagError(name, "uint", nestedGenericValue) } @@ -227,7 +227,7 @@ func (fsm *MapInputSource) Uint(name string) (uint, error) { return 0, nil } -func castToUint64(v interface{}) (uint64, bool) { +func CastToUint64(v interface{}) (uint64, bool) { uint64Value := uint64(0) isType := false if v == nil { @@ -263,7 +263,7 @@ func castToUint64(v interface{}) (uint64, bool) { func (fsm *MapInputSource) Uint64(name string) (uint64, error) { otherGenericValue, exists := fsm.valueMap[name] if exists { - otherValue, isType := castToUint64(otherGenericValue) + otherValue, isType := CastToUint64(otherGenericValue) if !isType { return 0, incorrectTypeForFlagError(name, "uint64", otherGenericValue) } @@ -271,7 +271,7 @@ func (fsm *MapInputSource) Uint64(name string) (uint64, error) { } nestedGenericValue, exists := nestedVal(name, fsm.valueMap) if exists { - otherValue, isType := castToUint64(nestedGenericValue) + otherValue, isType := CastToUint64(nestedGenericValue) if !isType { return 0, incorrectTypeForFlagError(name, "uint64", nestedGenericValue) } @@ -378,7 +378,7 @@ func (fsm *MapInputSource) Int64Slice(name string) ([]int64, error) { var int64Slice = make([]int64, 0, len(otherValue)) for i, v := range otherValue { - int64Value, isType := castToInt64(v) + int64Value, isType := CastToInt64(v) if !isType { return nil, incorrectTypeForFlagError(fmt.Sprintf("%s[%d]", name, i), "int64", v) } diff --git a/altsrc/yaml_file_loader_test.go b/altsrc/yaml_file_loader_test.go index 89fcce19d7..3f35abac73 100644 --- a/altsrc/yaml_file_loader_test.go +++ b/altsrc/yaml_file_loader_test.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "log" "os" + "reflect" "testing" "time" @@ -89,6 +90,25 @@ func ExampleApp_Run_yamlFileLoaderDuration() { // keepalive 45s } +func TestYamlFileInt64Slice(t *testing.T) { + _ = ioutil.WriteFile("current.yaml", []byte(`top: [100, 9223372036854775808]`), 0666) + defer os.Remove("current.yaml") + + testFlag := []cli.Flag{ + &altsrc.StringFlag{StringFlag: &cli.StringFlag{Name: "conf"}}, + &altsrc.Int64SliceFlag{Int64SliceFlag: &cli.Int64SliceFlag{Name: "top", EnvVars: []string{"THE_TEST"}}}, + } + app := &cli.App{} + app.Before = altsrc.InitInputSourceWithContext(testFlag, altsrc.NewYamlSourceFromFlagFunc("conf")) + app.Action = func(c *cli.Context) error { return nil } + app.Flags = append(app.Flags, testFlag...) + + test := []string{"testApp", "--conf", "current.yaml"} + if err := app.Run(test); err == nil { + t.Error("Should return the mismatch type error") + } +} + func TestYamlFileStringSlice(t *testing.T) { _ = ioutil.WriteFile("current.yaml", []byte(`top: test: ["s1", "s2"]`), 0666) @@ -306,3 +326,36 @@ func TestYamlFileInt64(t *testing.T) { } } } + +func TestCastToUint64(t *testing.T) { + tests := []struct { + value interface{} + expect bool + }{ + {int64(100), true}, + {uint(100), true}, + } + + for _, test := range tests { + v, isType := altsrc.CastToUint64(test.value) + if isType != test.expect && reflect.TypeOf(v).Kind() != reflect.Uint64 { + t.Fatalf("expect %v, but %v", test.expect, isType) + } + } +} + +func TestCastToUint(t *testing.T) { + tests := []struct { + value interface{} + expect bool + }{ + {int64(100), true}, + } + + for _, test := range tests { + v, isType := altsrc.CastToUint(test.value) + if isType != test.expect && reflect.TypeOf(v).Kind() != reflect.Uint64 { + t.Fatalf("expect %v, but %v", test.expect, isType) + } + } +}