Skip to content

Commit

Permalink
added testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
jack.jin committed Feb 7, 2023
1 parent fb6fcd2 commit 4423ae4
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 10 deletions.
20 changes: 10 additions & 10 deletions altsrc/map_input_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -152,15 +152,15 @@ 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)
}
return otherValue, nil
}
nestedGenericValue, exists := nestedVal(name, fsm.valueMap)
if exists {
otherValue, isType := castToInt64(otherGenericValue)
otherValue, isType := CastToInt64(otherGenericValue)
if !isType {
return 0, incorrectTypeForFlagError(name, "int64", nestedGenericValue)
}
Expand All @@ -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 {
Expand Down Expand Up @@ -209,15 +209,15 @@ 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)
}
return otherValue, nil
}
nestedGenericValue, exists := nestedVal(name, fsm.valueMap)
if exists {
otherValue, isType := castToUint(nestedGenericValue)
otherValue, isType := CastToUint(nestedGenericValue)
if !isType {
return 0, incorrectTypeForFlagError(name, "uint", nestedGenericValue)
}
Expand All @@ -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 {
Expand Down Expand Up @@ -263,15 +263,15 @@ 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)
}
return otherValue, nil
}
nestedGenericValue, exists := nestedVal(name, fsm.valueMap)
if exists {
otherValue, isType := castToUint64(nestedGenericValue)
otherValue, isType := CastToUint64(nestedGenericValue)
if !isType {
return 0, incorrectTypeForFlagError(name, "uint64", nestedGenericValue)
}
Expand Down Expand Up @@ -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)
}
Expand Down
53 changes: 53 additions & 0 deletions altsrc/yaml_file_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"log"
"os"
"reflect"
"testing"
"time"

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}
}

0 comments on commit 4423ae4

Please sign in to comment.