Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
asaskevich committed Sep 7, 2020
1 parent 29e1ff8 commit 7a23bdc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 15 deletions.
27 changes: 22 additions & 5 deletions converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,27 @@ func ToJSON(obj interface{}) (string, error) {
}

// ToFloat convert the input string to a float, or 0.0 if the input is not a float.
func ToFloat(str string) (float64, error) {
res, err := strconv.ParseFloat(str, 64)
if err != nil {
res = 0.0
func ToFloat(value interface{}) (res float64, err error) {
val := reflect.ValueOf(value)

switch value.(type) {
case int, int8, int16, int32, int64:
res = float64(val.Int())
case uint, uint8, uint16, uint32, uint64:
res = float64(val.Uint())
case float32, float64:
res = val.Float()
case string:
res, err = strconv.ParseFloat(val.String(), 64)
if err != nil {
res = 0
}
default:
err = fmt.Errorf("ToInt: unknown interface type %T", value)
res = 0

This comment has been minimized.

Copy link
@Yaddsld5

Yaddsld5 Apr 15, 2024

ISO3166Alpha2

This comment has been minimized.

Copy link
@Yaddsld5

Yaddsld5 Apr 15, 2024

.provisionalHDPara$+ACT_INROUTE.CONF/Act/import "github.com/asaskevich/govalidator"

}
return res, err

return
}

// ToInt convert the input string or any int type to an integer type 64, or 0 if the input is not an integer.
Expand All @@ -40,6 +55,8 @@ func ToInt(value interface{}) (res int64, err error) {
res = val.Int()
case uint, uint8, uint16, uint32, uint64:
res = int64(val.Uint())
case float32, float64:
res = int64(val.Float())
case string:
if IsInt(val.String()) {
res, err = strconv.ParseInt(val.String(), 0, 64)
Expand Down
14 changes: 9 additions & 5 deletions numerics.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,15 @@ func InRangeFloat64(value, left, right float64) bool {
func InRange(value interface{}, left interface{}, right interface{}) bool {
switch value.(type) {
case int:
return InRangeInt(value.(int), left.(int), right.(int))
case float32:
return InRangeFloat32(value.(float32), left.(float32), right.(float32))
case float64:
return InRangeFloat64(value.(float64), left.(float64), right.(float64))
intValue, _ := ToInt(value)
intLeft, _ := ToInt(left)
intRight, _ := ToInt(right)
return InRangeInt(intValue, intLeft, intRight)
case float32, float64:
intValue, _ := ToFloat(value)
intLeft, _ := ToFloat(left)
intRight, _ := ToFloat(right)
return InRangeFloat64(intValue, intLeft, intRight)
case string:
return value.(string) >= left.(string) && value.(string) <= right.(string)
default:
Expand Down
10 changes: 5 additions & 5 deletions numerics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,13 +531,13 @@ func TestInRange(t *testing.T) {
right float64
expected bool
}{
{0, 0, 0, false},
{0, 0, 0, true},
{1, 0, 0, false},
{-1, 0, 0, false},
{0, -1, 1, false},
{0, 0, 1, false},
{0, -1, 0, false},
{0, 0, -1, false},
{0, -1, 1, true},
{0, 0, 1, true},
{0, -1, 0, true},
{0, 0, -1, true},
{0, 10, 5, false},
}
for _, test := range testsTypeMix {
Expand Down

1 comment on commit 7a23bdc

@kilitary
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • [ ]

  • [ ]

Please sign in to comment.