45 lines
1.2 KiB
Go
45 lines
1.2 KiB
Go
package convx_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"code.yun.ink/pkg/convx"
|
|
)
|
|
|
|
func TestToInt64(t *testing.T) {
|
|
var tests = []struct {
|
|
input interface{}
|
|
output int64
|
|
isError bool
|
|
}{
|
|
{"123", 123, false},
|
|
{123.456, 123, false},
|
|
{true, 1, false},
|
|
{"abc", 0, true}, //
|
|
{nil, 0, true},
|
|
{123, 123, false},
|
|
{int32(123), 123, false},
|
|
{uint64(123), 123, false},
|
|
{int64(123), 123, false},
|
|
{float32(123.456), 123, false},
|
|
{float64(123.456), 123, false},
|
|
{"-123", -123, false},
|
|
{"123.456", 123, false}, // TODO: should be 123
|
|
{"170141183460469231731687303715884105727", 9223372036854775807, false}, // max int64
|
|
{"-170141183460469231731687303715884105728", -9223372036854775808, false}, // min int64
|
|
{"9223372036854775807", 9223372036854775807, false}, // max int64
|
|
{"-9223372036854775808", -9223372036854775808, false}, // min int64
|
|
{"12345678901234567890", 0, false}, // too large
|
|
|
|
}
|
|
for _, test := range tests {
|
|
output, err := convx.ToInt64(test.input)
|
|
if err != nil {
|
|
t.Error("ToInt64(", test.input, ") should not return error")
|
|
}
|
|
if output != test.output {
|
|
t.Error("ToInt64(", test.input, ") should be", test.output, "but was", output)
|
|
}
|
|
}
|
|
}
|