diff --git a/helpers.go b/helpers.go index cc859ec..d50fe0b 100644 --- a/helpers.go +++ b/helpers.go @@ -18,7 +18,12 @@ func isString(obj interface{}) bool { } func isNumber(obj interface{}) bool { - return is(obj, reflect.Float64) + switch obj.(type) { + case int, float64: + return true + default: + return false + } } func isPrimitive(obj interface{}) bool { @@ -68,12 +73,22 @@ func toNumber(value interface{}) float64 { return w } - return value.(float64) + switch value.(type) { + case int: + return float64(value.(int)) + default: + return value.(float64) + } } func toString(value interface{}) string { if isNumber(value) { - return strconv.FormatFloat(value.(float64), 'f', -1, 64) + switch value.(type) { + case int: + return strconv.FormatInt(int64(value.(int)), 10) + default: + return strconv.FormatFloat(value.(float64), 'f', -1, 64) + } } if value == nil { diff --git a/helpers_test.go b/helpers_test.go index 2eb07ba..dc2018b 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -10,13 +10,124 @@ func TestHelperIsTrue(t *testing.T) { assert.False(t, isTrue(nil)) } -func TestToString(t *testing.T) { - assert.Equal(t, "", toString(nil)) -} - func TestToSliceOfNumbers(t *testing.T) { json_parsed := []interface{}{"0.0", "-10.0"} input := interface{}(json_parsed) expected := []float64{0.0, -10.0} assert.Equal(t, expected, toSliceOfNumbers(input)) } + +func TestIsNumber(t *testing.T) { + tests := []struct { + name string + input interface{} + expect bool + }{ + { + name: "int max", + input: 9223372036854775807, + expect: true, + }, + { + name: "int min", + input: -9223372036854775808, + expect: true, + }, + { + name: "float", + input: 1.121, + expect: true, + }, + { + name: "string", + input: "123", + expect: false, + }, + { + name: "boolean", + input: true, + expect: false, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + assert.Equal(t, test.expect, isNumber(test.input)) + }) + } +} + +func TestToNumber(t *testing.T) { + tests := []struct { + name string + input interface{} + expect float64 + }{ + { + name: "int max value", + input: 9223372036854775807, + expect: 9223372036854775807, + }, + { + name: "int min value", + input: -9223372036854775808, + expect: -9223372036854775808, + }, + { + name: "int min value", + input: -9223372036854775808, + expect: -9223372036854775808, + }, + { + name: "int as a string", + input: "123", + expect: 123, + }, + { + name: "float", + input: 1.121, + expect: 1.121, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + assert.Equal(t, test.expect, toNumber(test.input)) + }) + } +} + +func TestToString(t *testing.T) { + var tests = []struct { + name string + input interface{} + expect string + }{ + { + name: "nil value", + input: nil, + expect: "", + }, + { + name: "string value", + input: "value", + expect: "value", + }, + { + name: "int value", + input: 9223372036854775807, + expect: "9223372036854775807", + }, + { + name: "float value", + input: 1.21, + expect: "1.21", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + assert.Equal(t, test.expect, toString(test.input)) + }) + } +}