Skip to content

Commit

Permalink
Add more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
onrik committed Nov 16, 2022
1 parent de9f70e commit 7239e9c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
4 changes: 2 additions & 2 deletions default.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ func setDefaultValue(v reflect.Value) error {
if isInt(v.Field(i)) {
value, err := strconv.ParseInt(defaultValue, 10, 64)
if err != nil {
return fmt.Errorf("%s is invalid value for int", defaultValue)
return fmt.Errorf("%s is invalid value for %s", defaultValue, v.Field(i).Type().Kind())
}
v.Field(i).SetInt(value)
continue
}
if isUint(v.Field(i)) {
value, err := strconv.ParseUint(defaultValue, 10, 64)
if err != nil {
return fmt.Errorf("%s is invalid value for int", defaultValue)
return fmt.Errorf("%s is invalid value for %s", defaultValue, v.Field(i).Type().Kind())
}
v.Field(i).SetUint(value)
continue
Expand Down
49 changes: 49 additions & 0 deletions default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,46 @@ func TestFillDefaultValues(t *testing.T) {
require.Equal(t, 5432, config.DB.Port)
require.Equal(t, 2*time.Second, config.DB.Timeout)
require.Equal(t, true, config.DB.Debug)

// Test empty
config2 := &struct {
Count int `yaconf:"default"`
}{}
err = fillDefaultValues(&config2)
require.Nil(t, err)

// Test invalid int
config3 := &struct {
Count int `yaconf:"default=yes"`
}{}
err = fillDefaultValues(&config3)
require.NotNil(t, err)
require.Equal(t, "yes is invalid value for int", err.Error())

// Test invalid uint
config4 := &struct {
Count uint `yaconf:"default=a"`
}{}
err = fillDefaultValues(&config4)
require.NotNil(t, err)
require.Equal(t, "a is invalid value for uint", err.Error())

// Test invalid duration
config5 := &struct {
Timeout time.Duration `yaconf:"default=22"`
}{}
err = fillDefaultValues(&config5)
require.NotNil(t, err)
require.Equal(t, "22 is invalid value for time.Duration", err.Error())

// Test invalid bool
config6 := &struct {
Debug bool `yaconf:"default=22"`
}{}
err = fillDefaultValues(&config6)
require.NotNil(t, err)
require.Equal(t, "22 is invalid value for bool", err.Error())

}

func TestSetDefaultValue(t *testing.T) {
Expand All @@ -47,6 +87,15 @@ func TestSetDefaultValue(t *testing.T) {
require.Nil(t, err)
require.Empty(t, config.private)
require.Equal(t, "localhost", config.DB.Host)

// Test empty

// Test invalid bool
config2 := struct {
Debug bool `yaconf:"default=22"`
}{}
err = setDefaultValue(reflect.ValueOf(config2))
require.NotNil(t, err)
}

func TestIsDuration(t *testing.T) {
Expand Down
20 changes: 20 additions & 0 deletions yaconf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,27 @@ func TestRead(t *testing.T) {
err = Read(file.Name(), &config2)
require.NotNil(t, err)

// Test invalid unmarshal
config3 := struct {
LogLevel int `yaml:"log_level"`
}{}
err = Read(file.Name(), &config3)
require.NotNil(t, err)

// Test invalid default valud
config4 := struct {
Pid int `yaml:"pid" yaconf:"default=yes"`
}{}
err = Read(file.Name(), &config4)
require.NotNil(t, err)
require.Equal(t, "yes is invalid value for int", err.Error())

// Test with custom validator
err = Read(file.Name(), &testConfig3{})
require.NotNil(t, err)
require.Equal(t, "invalid", err.Error())

// Test invalid file
err = Read("/tmp/yaconf/test.yml", &testConfig3{})
require.NotNil(t, err)
}

0 comments on commit 7239e9c

Please sign in to comment.