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}, // string {123.456, 123, false}, // float64 {true, 1, false}, // bool {"abc", 0, true}, // string {nil, 0, true}, // nil {123, 123, false}, // int {int32(123), 123, false}, // int32 {uint64(123), 123, false}, // uint64 {int64(123), 123, false}, // int64 {float32(123.456), 123, false}, // float32 {float64(123.456), 123, false}, // float64 {"-123", -123, false}, // string {"123.456", 123, false}, // string {"170141183460469231731687303715884105727", 9223372036854775807, false}, // max int64 {"-170141183460469231731687303715884105728", -9223372036854775808, false}, // min int64 {"9223372036854775807", 9223372036854775807, false}, // max int64 {"-9223372036854775808", -9223372036854775808, false}, // min int64 {"12345678901234567890", 9223372036854775807, false}, // too large } for idx, test := range tests { output, err := convx.ToInt64(test.input) if test.isError && err == nil { t.Error(idx, ": ToInt64(", test.input, ") should return error") continue } else if !test.isError && err != nil { t.Error(idx, ": ToInt64(", test.input, ") should not return error", err.Error()) continue } if output != test.output { t.Error("ToInt64(", test.input, ") should be", test.output, "but was", output) } } }