From 160f3c252099cad695d456f8eff9473d90159e43 Mon Sep 17 00:00:00 2001 From: shockerli Date: Tue, 9 Mar 2021 17:32:38 +0800 Subject: [PATCH] add int/int8/int16/int32 --- cvt.go | 52 +++++++ cvt_test.go | 372 ++++++++++++++++++++++++++++++++++++++++++++++++++ cvte.go | 60 +++++++-- cvte_test.go | 375 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 851 insertions(+), 8 deletions(-) diff --git a/cvt.go b/cvt.go index 4014bc2..7e758e9 100644 --- a/cvt.go +++ b/cvt.go @@ -90,3 +90,55 @@ func Int64(v interface{}, def ...int64) int64 { return 0 } + +// Int32 convert an interface to a int32 type, with default value +func Int32(v interface{}, def ...int32) int32 { + if v, err := Int32E(v); err == nil { + return v + } + + if len(def) > 0 { + return def[0] + } + + return 0 +} + +// Int16 convert an interface to a int16 type, with default value +func Int16(v interface{}, def ...int16) int16 { + if v, err := Int16E(v); err == nil { + return v + } + + if len(def) > 0 { + return def[0] + } + + return 0 +} + +// Int8 convert an interface to a int8 type, with default value +func Int8(v interface{}, def ...int8) int8 { + if v, err := Int8E(v); err == nil { + return v + } + + if len(def) > 0 { + return def[0] + } + + return 0 +} + +// Int convert an interface to a int type, with default value +func Int(v interface{}, def ...int) int { + if v, err := IntE(v); err == nil { + return v + } + + if len(def) > 0 { + return def[0] + } + + return 0 +} diff --git a/cvt_test.go b/cvt_test.go index c694e5c..d1d5d12 100644 --- a/cvt_test.go +++ b/cvt_test.go @@ -679,3 +679,375 @@ func TestInt64_BaseLine(t *testing.T) { assert.Equal(t, tt.expect, v, msg) } } + +func TestInt32_HasDefault(t *testing.T) { + tests := []struct { + input interface{} + def int32 + expect int32 + }{ + // supported value, def is not used, def != expect + {int(8), 1, 8}, + {int8(8), 1, 8}, + {int16(8), 1, 8}, + {int32(8), 1, 8}, + {int64(8), 1, 8}, + {uint(8), 1, 8}, + {uint8(8), 1, 8}, + {uint16(8), 1, 8}, + {uint32(8), 1, 8}, + {uint64(8), 1, 8}, + {float32(8.31), 1, 8}, + {float64(8.31), 1, 8}, + {"8", 2, 8}, + {"8.00", 2, 8}, + {"8.01", 2, 8}, + {int(-8), 1, -8}, + {int8(-8), 1, -8}, + {int16(-8), 1, -8}, + {int32(-8), 1, -8}, + {int64(-8), 1, -8}, + {float32(-8.31), 1, -8}, + {float64(-8.31), 1, -8}, + {"-8", 1, -8}, + {"-8.01", 1, -8}, + {true, 2, 1}, + {false, 2, 0}, + {nil, 2, 0}, + {aliasTypeInt_0, 2, 0}, + {&aliasTypeInt_0, 2, 0}, + {aliasTypeInt_1, 2, 1}, + {&aliasTypeInt_1, 2, 1}, + {aliasTypeString_0, 2, 0}, + {&aliasTypeString_0, 2, 0}, + {aliasTypeString_1, 2, 1}, + {&aliasTypeString_1, 2, 1}, + {aliasTypeString_8d15, 2, 8}, + {&aliasTypeString_8d15, 2, 8}, + {aliasTypeString_8d15_minus, 1, -8}, + {&aliasTypeString_8d15_minus, 1, -8}, + + // unsupported value, def == expect + {"10a", 1, 1}, + {"a10a", 1, 1}, + {"8.01a", 1, 1}, + {"8.01 ", 1, 1}, + {"hello", 1, 1}, + {testing.T{}, 1, 1}, + {&testing.T{}, 1, 1}, + {[]int{}, 1, 1}, + {[]string{}, 1, 1}, + {[...]string{}, 1, 1}, + {map[int]string{}, 1, 1}, + } + + for i, tt := range tests { + msg := fmt.Sprintf("i = %d, input[%+v], def[%+v], expect[%+v]", i, tt.input, tt.def, tt.expect) + + v := cvt.Int32(tt.input, tt.def) + assert.Equal(t, tt.expect, v, msg) + } +} + +func TestInt32_BaseLine(t *testing.T) { + tests := []struct { + input interface{} + expect int32 + }{ + {testing.T{}, 0}, + {&testing.T{}, 0}, + {[]int{}, 0}, + {[]int{1, 2, 3}, 0}, + {[]string{}, 0}, + {[]string{"a", "b", "c"}, 0}, + {[...]string{}, 0}, + {map[int]string{}, 0}, + {"4873546382743564386435354655456575456754356765546554643456", 0}, + } + + for i, tt := range tests { + msg := fmt.Sprintf("i = %d, input[%+v], expect[%+v]", i, tt.input, tt.expect) + + v := cvt.Int32(tt.input) + assert.Equal(t, tt.expect, v, msg) + } +} + +func TestInt16_HasDefault(t *testing.T) { + tests := []struct { + input interface{} + def int16 + expect int16 + }{ + // supported value, def is not used, def != expect + {int(8), 1, 8}, + {int8(8), 1, 8}, + {int16(8), 1, 8}, + {int32(8), 1, 8}, + {int64(8), 1, 8}, + {uint(8), 1, 8}, + {uint8(8), 1, 8}, + {uint16(8), 1, 8}, + {uint32(8), 1, 8}, + {uint64(8), 1, 8}, + {float32(8.31), 1, 8}, + {float64(8.31), 1, 8}, + {"8", 2, 8}, + {"8.00", 2, 8}, + {"8.01", 2, 8}, + {int(-8), 1, -8}, + {int8(-8), 1, -8}, + {int16(-8), 1, -8}, + {int32(-8), 1, -8}, + {int64(-8), 1, -8}, + {float32(-8.31), 1, -8}, + {float64(-8.31), 1, -8}, + {"-8", 1, -8}, + {"-8.01", 1, -8}, + {true, 2, 1}, + {false, 2, 0}, + {nil, 2, 0}, + {aliasTypeInt_0, 2, 0}, + {&aliasTypeInt_0, 2, 0}, + {aliasTypeInt_1, 2, 1}, + {&aliasTypeInt_1, 2, 1}, + {aliasTypeString_0, 2, 0}, + {&aliasTypeString_0, 2, 0}, + {aliasTypeString_1, 2, 1}, + {&aliasTypeString_1, 2, 1}, + {aliasTypeString_8d15, 2, 8}, + {&aliasTypeString_8d15, 2, 8}, + {aliasTypeString_8d15_minus, 1, -8}, + {&aliasTypeString_8d15_minus, 1, -8}, + + // unsupported value, def == expect + {"10a", 1, 1}, + {"a10a", 1, 1}, + {"8.01a", 1, 1}, + {"8.01 ", 1, 1}, + {"hello", 1, 1}, + {testing.T{}, 1, 1}, + {&testing.T{}, 1, 1}, + {[]int{}, 1, 1}, + {[]string{}, 1, 1}, + {[...]string{}, 1, 1}, + {map[int]string{}, 1, 1}, + } + + for i, tt := range tests { + msg := fmt.Sprintf("i = %d, input[%+v], def[%+v], expect[%+v]", i, tt.input, tt.def, tt.expect) + + v := cvt.Int16(tt.input, tt.def) + assert.Equal(t, tt.expect, v, msg) + } +} + +func TestInt16_BaseLine(t *testing.T) { + tests := []struct { + input interface{} + expect int16 + }{ + {testing.T{}, 0}, + {&testing.T{}, 0}, + {[]int{}, 0}, + {[]int{1, 2, 3}, 0}, + {[]string{}, 0}, + {[]string{"a", "b", "c"}, 0}, + {[...]string{}, 0}, + {map[int]string{}, 0}, + {"4873546382743564386435354655456575456754356765546554643456", 0}, + } + + for i, tt := range tests { + msg := fmt.Sprintf("i = %d, input[%+v], expect[%+v]", i, tt.input, tt.expect) + + v := cvt.Int16(tt.input) + assert.Equal(t, tt.expect, v, msg) + } +} + +func TestInt8_HasDefault(t *testing.T) { + tests := []struct { + input interface{} + def int8 + expect int8 + }{ + // supported value, def is not used, def != expect + {int(8), 1, 8}, + {int8(8), 1, 8}, + {int16(8), 1, 8}, + {int32(8), 1, 8}, + {int64(8), 1, 8}, + {uint(8), 1, 8}, + {uint8(8), 1, 8}, + {uint16(8), 1, 8}, + {uint32(8), 1, 8}, + {uint64(8), 1, 8}, + {float32(8.31), 1, 8}, + {float64(8.31), 1, 8}, + {"8", 2, 8}, + {"8.00", 2, 8}, + {"8.01", 2, 8}, + {int(-8), 1, -8}, + {int8(-8), 1, -8}, + {int16(-8), 1, -8}, + {int32(-8), 1, -8}, + {int64(-8), 1, -8}, + {float32(-8.31), 1, -8}, + {float64(-8.31), 1, -8}, + {"-8", 1, -8}, + {"-8.01", 1, -8}, + {true, 2, 1}, + {false, 2, 0}, + {nil, 2, 0}, + {aliasTypeInt_0, 2, 0}, + {&aliasTypeInt_0, 2, 0}, + {aliasTypeInt_1, 2, 1}, + {&aliasTypeInt_1, 2, 1}, + {aliasTypeString_0, 2, 0}, + {&aliasTypeString_0, 2, 0}, + {aliasTypeString_1, 2, 1}, + {&aliasTypeString_1, 2, 1}, + {aliasTypeString_8d15, 2, 8}, + {&aliasTypeString_8d15, 2, 8}, + {aliasTypeString_8d15_minus, 1, -8}, + {&aliasTypeString_8d15_minus, 1, -8}, + + // unsupported value, def == expect + {"10a", 1, 1}, + {"a10a", 1, 1}, + {"8.01a", 1, 1}, + {"8.01 ", 1, 1}, + {"hello", 1, 1}, + {testing.T{}, 1, 1}, + {&testing.T{}, 1, 1}, + {[]int{}, 1, 1}, + {[]string{}, 1, 1}, + {[...]string{}, 1, 1}, + {map[int]string{}, 1, 1}, + } + + for i, tt := range tests { + msg := fmt.Sprintf("i = %d, input[%+v], def[%+v], expect[%+v]", i, tt.input, tt.def, tt.expect) + + v := cvt.Int8(tt.input, tt.def) + assert.Equal(t, tt.expect, v, msg) + } +} + +func TestInt8_BaseLine(t *testing.T) { + tests := []struct { + input interface{} + expect int8 + }{ + {testing.T{}, 0}, + {&testing.T{}, 0}, + {[]int{}, 0}, + {[]int{1, 2, 3}, 0}, + {[]string{}, 0}, + {[]string{"a", "b", "c"}, 0}, + {[...]string{}, 0}, + {map[int]string{}, 0}, + {"4873546382743564386435354655456575456754356765546554643456", 0}, + } + + for i, tt := range tests { + msg := fmt.Sprintf("i = %d, input[%+v], expect[%+v]", i, tt.input, tt.expect) + + v := cvt.Int8(tt.input) + assert.Equal(t, tt.expect, v, msg) + } +} + +func TestInt_HasDefault(t *testing.T) { + tests := []struct { + input interface{} + def int + expect int + }{ + // supported value, def is not used, def != expect + {int(8), 1, 8}, + {int8(8), 1, 8}, + {int16(8), 1, 8}, + {int32(8), 1, 8}, + {int64(8), 1, 8}, + {uint(8), 1, 8}, + {uint8(8), 1, 8}, + {uint16(8), 1, 8}, + {uint32(8), 1, 8}, + {uint64(8), 1, 8}, + {float32(8.31), 1, 8}, + {float64(8.31), 1, 8}, + {"8", 2, 8}, + {"8.00", 2, 8}, + {"8.01", 2, 8}, + {int(-8), 1, -8}, + {int8(-8), 1, -8}, + {int16(-8), 1, -8}, + {int32(-8), 1, -8}, + {int64(-8), 1, -8}, + {float32(-8.31), 1, -8}, + {float64(-8.31), 1, -8}, + {"-8", 1, -8}, + {"-8.01", 1, -8}, + {true, 2, 1}, + {false, 2, 0}, + {nil, 2, 0}, + {aliasTypeInt_0, 2, 0}, + {&aliasTypeInt_0, 2, 0}, + {aliasTypeInt_1, 2, 1}, + {&aliasTypeInt_1, 2, 1}, + {aliasTypeString_0, 2, 0}, + {&aliasTypeString_0, 2, 0}, + {aliasTypeString_1, 2, 1}, + {&aliasTypeString_1, 2, 1}, + {aliasTypeString_8d15, 2, 8}, + {&aliasTypeString_8d15, 2, 8}, + {aliasTypeString_8d15_minus, 1, -8}, + {&aliasTypeString_8d15_minus, 1, -8}, + + // unsupported value, def == expect + {"10a", 1, 1}, + {"a10a", 1, 1}, + {"8.01a", 1, 1}, + {"8.01 ", 1, 1}, + {"hello", 1, 1}, + {testing.T{}, 1, 1}, + {&testing.T{}, 1, 1}, + {[]int{}, 1, 1}, + {[]string{}, 1, 1}, + {[...]string{}, 1, 1}, + {map[int]string{}, 1, 1}, + } + + for i, tt := range tests { + msg := fmt.Sprintf("i = %d, input[%+v], def[%+v], expect[%+v]", i, tt.input, tt.def, tt.expect) + + v := cvt.Int(tt.input, tt.def) + assert.Equal(t, tt.expect, v, msg) + } +} + +func TestInt_BaseLine(t *testing.T) { + tests := []struct { + input interface{} + expect int + }{ + {testing.T{}, 0}, + {&testing.T{}, 0}, + {[]int{}, 0}, + {[]int{1, 2, 3}, 0}, + {[]string{}, 0}, + {[]string{"a", "b", "c"}, 0}, + {[...]string{}, 0}, + {map[int]string{}, 0}, + {"4873546382743564386435354655456575456754356765546554643456", 0}, + } + + for i, tt := range tests { + msg := fmt.Sprintf("i = %d, input[%+v], expect[%+v]", i, tt.input, tt.expect) + + v := cvt.Int(tt.input) + assert.Equal(t, tt.expect, v, msg) + } +} diff --git a/cvte.go b/cvte.go index f4a29d8..c5ba24c 100644 --- a/cvte.go +++ b/cvte.go @@ -67,9 +67,6 @@ func Uint64E(val interface{}) (uint64, error) { if e := catch("uint64", val, e); e != nil { return 0, e } - if v > math.MaxUint64 { - return 0, fmt.Errorf(formatOutOfLimit, newErr(val, "uint64"), uint64(math.MaxUint64)) - } return v, nil } @@ -168,15 +165,62 @@ func Int64E(val interface{}) (int64, error) { if e := catch("int64", val, e); e != nil { return 0, e } - if strconv.IntSize == 64 && v > math.MaxInt64 { - return 0, fmt.Errorf(formatOutOfLimit, newErr(val, "int64"), math.MaxInt64) - } else if strconv.IntSize == 32 && v > math.MaxInt32 { - return 0, fmt.Errorf(formatOutOfLimit, newErr(val, "int64"), math.MaxInt32) - } return v, nil } +// Int32E convert an interface to a int32 type +func Int32E(val interface{}) (int32, error) { + v, e := convInt64(val) + if e := catch("int32", val, e); e != nil { + return 0, e + } + if v > math.MaxInt32 { + return 0, fmt.Errorf(formatOutOfLimit, newErr(val, "int32"), int32(math.MaxInt32)) + } + + return int32(v), nil +} + +// Int16E convert an interface to a int16 type +func Int16E(val interface{}) (int16, error) { + v, e := convInt64(val) + if e := catch("int16", val, e); e != nil { + return 0, e + } + if v > math.MaxInt16 { + return 0, fmt.Errorf(formatOutOfLimit, newErr(val, "int16"), int16(math.MaxInt16)) + } + + return int16(v), nil +} + +// Int8E convert an interface to a int8 type +func Int8E(val interface{}) (int8, error) { + v, e := convInt64(val) + if e := catch("int8", val, e); e != nil { + return 0, e + } + if v > math.MaxInt8 { + return 0, fmt.Errorf(formatOutOfLimit, newErr(val, "int8"), int8(math.MaxInt8)) + } + + return int8(v), nil +} + +// IntE convert an interface to a int type +func IntE(val interface{}) (int, error) { + v, e := convInt64(val) + if e := catch("int", val, e); e != nil { + return 0, e + } + if strconv.IntSize == 32 && v > math.MaxInt32 { + return 0, fmt.Errorf(formatOutOfLimit, newErr(val, "int"), int32(math.MaxInt32)) + } + + return int(v), nil +} + func convInt64(val interface{}) (int64, error) { v, _, rv := Indirect(val) diff --git a/cvte_test.go b/cvte_test.go index b250e53..7b43716 100644 --- a/cvte_test.go +++ b/cvte_test.go @@ -654,3 +654,378 @@ func TestInt64E(t *testing.T) { assert.Equal(t, tt.expect, v, msg) } } + +func TestInt32E(t *testing.T) { + tests := []struct { + input interface{} + expect int32 + isErr bool + }{ + {int(8), 8, false}, + {int8(8), 8, false}, + {int16(8), 8, false}, + {int32(8), 8, false}, + {int64(8), 8, false}, + {uint(8), 8, false}, + {uint8(8), 8, false}, + {uint16(8), 8, false}, + {uint32(8), 8, false}, + {uint64(8), 8, false}, + {float32(8.31), 8, false}, + {float64(8.31), 8, false}, + {true, 1, false}, + {false, 0, false}, + {int(-8), -8, false}, + {int8(-8), -8, false}, + {int16(-8), -8, false}, + {int32(-8), -8, false}, + {int64(-8), -8, false}, + {float32(-8.31), -8, false}, + {float64(-8.31), -8, false}, + {"-8", -8, false}, + {"-8.01", -8, false}, + {"8", 8, false}, + {"8.00", 8, false}, + {"8.01", 8, false}, + {[]byte("-8"), -8, false}, + {[]byte("-8.01"), -8, false}, + {[]byte("8"), 8, false}, + {[]byte("8.00"), 8, false}, + {[]byte("8.01"), 8, false}, + {math.MaxInt32, int32(math.MaxInt32), false}, + {nil, 0, false}, + {aliasTypeInt_0, 0, false}, + {&aliasTypeInt_0, 0, false}, + {aliasTypeInt_1, 1, false}, + {&aliasTypeInt_1, 1, false}, + {aliasTypeString_0, 0, false}, + {&aliasTypeString_0, 0, false}, + {aliasTypeString_1, 1, false}, + {&aliasTypeString_1, 1, false}, + {aliasTypeString_8d15, 8, false}, + {&aliasTypeString_8d15, 8, false}, + {aliasTypeString_8d15_minus, -8, false}, + {&aliasTypeString_8d15_minus, -8, false}, + {aliasTypeBool_true, 1, false}, + {&aliasTypeBool_true, 1, false}, + {aliasTypeBool_false, 0, false}, + {&aliasTypeBool_false, 0, false}, + + // errors + {"10a", 0, true}, + {"a10a", 0, true}, + {"8.01a", 0, true}, + {"8.01 ", 0, true}, + {"4873546382743564386435354655456575456754356765546554643456", 0, true}, + {float64(4873546382743564386435354655456575456754356765546554643456), 0, true}, + {uint64(math.MaxUint64), 0, true}, + {int64(math.MaxInt64), 0, true}, + {uint32(math.MaxUint32), 0, true}, + {"hello", 0, true}, + {testing.T{}, 0, true}, + {&testing.T{}, 0, true}, + {[]int{}, 0, true}, + {[]string{}, 0, true}, + {[...]string{}, 0, true}, + {map[int]string{}, 0, true}, + } + + for i, tt := range tests { + msg := fmt.Sprintf("i = %d, input[%+v], expect[%v], isErr[%v]", i, tt.input, tt.expect, tt.isErr) + + v, err := cvt.Int32E(tt.input) + if tt.isErr { + assert.Error(t, err, msg) + continue + } + + assert.NoError(t, err, msg) + assert.Equal(t, tt.expect, v, msg) + + // Non-E test with no default value: + v = cvt.Int32(tt.input) + assert.Equal(t, tt.expect, v, msg) + } +} + +func TestInt16E(t *testing.T) { + tests := []struct { + input interface{} + expect int16 + isErr bool + }{ + {int(8), 8, false}, + {int8(8), 8, false}, + {int16(8), 8, false}, + {int32(8), 8, false}, + {int64(8), 8, false}, + {uint(8), 8, false}, + {uint8(8), 8, false}, + {uint16(8), 8, false}, + {uint32(8), 8, false}, + {uint64(8), 8, false}, + {float32(8.31), 8, false}, + {float64(8.31), 8, false}, + {true, 1, false}, + {false, 0, false}, + {int(-8), -8, false}, + {int8(-8), -8, false}, + {int16(-8), -8, false}, + {int32(-8), -8, false}, + {int64(-8), -8, false}, + {float32(-8.31), -8, false}, + {float64(-8.31), -8, false}, + {"-8", -8, false}, + {"-8.01", -8, false}, + {"8", 8, false}, + {"8.00", 8, false}, + {"8.01", 8, false}, + {[]byte("-8"), -8, false}, + {[]byte("-8.01"), -8, false}, + {[]byte("8"), 8, false}, + {[]byte("8.00"), 8, false}, + {[]byte("8.01"), 8, false}, + {math.MaxInt16, int16(math.MaxInt16), false}, + {nil, 0, false}, + {aliasTypeInt_0, 0, false}, + {&aliasTypeInt_0, 0, false}, + {aliasTypeInt_1, 1, false}, + {&aliasTypeInt_1, 1, false}, + {aliasTypeString_0, 0, false}, + {&aliasTypeString_0, 0, false}, + {aliasTypeString_1, 1, false}, + {&aliasTypeString_1, 1, false}, + {aliasTypeString_8d15, 8, false}, + {&aliasTypeString_8d15, 8, false}, + {aliasTypeString_8d15_minus, -8, false}, + {&aliasTypeString_8d15_minus, -8, false}, + {aliasTypeBool_true, 1, false}, + {&aliasTypeBool_true, 1, false}, + {aliasTypeBool_false, 0, false}, + {&aliasTypeBool_false, 0, false}, + + // errors + {"10a", 0, true}, + {"a10a", 0, true}, + {"8.01a", 0, true}, + {"8.01 ", 0, true}, + {"4873546382743564386435354655456575456754356765546554643456", 0, true}, + {float64(4873546382743564386435354655456575456754356765546554643456), 0, true}, + {uint64(math.MaxUint64), 0, true}, + {uint32(math.MaxUint32), 0, true}, + {int64(math.MaxInt64), 0, true}, + {int32(math.MaxInt32), 0, true}, + {uint16(math.MaxUint16), 0, true}, + {"hello", 0, true}, + {testing.T{}, 0, true}, + {&testing.T{}, 0, true}, + {[]int{}, 0, true}, + {[]string{}, 0, true}, + {[...]string{}, 0, true}, + {map[int]string{}, 0, true}, + } + + for i, tt := range tests { + msg := fmt.Sprintf("i = %d, input[%+v], expect[%v], isErr[%v]", i, tt.input, tt.expect, tt.isErr) + + v, err := cvt.Int16E(tt.input) + if tt.isErr { + assert.Error(t, err, msg) + continue + } + + assert.NoError(t, err, msg) + assert.Equal(t, tt.expect, v, msg) + + // Non-E test with no default value: + v = cvt.Int16(tt.input) + assert.Equal(t, tt.expect, v, msg) + } +} + +func TestInt8E(t *testing.T) { + tests := []struct { + input interface{} + expect int8 + isErr bool + }{ + {int(8), 8, false}, + {int8(8), 8, false}, + {int16(8), 8, false}, + {int32(8), 8, false}, + {int64(8), 8, false}, + {uint(8), 8, false}, + {uint8(8), 8, false}, + {uint16(8), 8, false}, + {uint32(8), 8, false}, + {uint64(8), 8, false}, + {float32(8.31), 8, false}, + {float64(8.31), 8, false}, + {true, 1, false}, + {false, 0, false}, + {int(-8), -8, false}, + {int8(-8), -8, false}, + {int16(-8), -8, false}, + {int32(-8), -8, false}, + {int64(-8), -8, false}, + {float32(-8.31), -8, false}, + {float64(-8.31), -8, false}, + {"-8", -8, false}, + {"-8.01", -8, false}, + {"8", 8, false}, + {"8.00", 8, false}, + {"8.01", 8, false}, + {[]byte("-8"), -8, false}, + {[]byte("-8.01"), -8, false}, + {[]byte("8"), 8, false}, + {[]byte("8.00"), 8, false}, + {[]byte("8.01"), 8, false}, + {int8(math.MaxInt8), math.MaxInt8, false}, + {nil, 0, false}, + {aliasTypeInt_0, 0, false}, + {&aliasTypeInt_0, 0, false}, + {aliasTypeInt_1, 1, false}, + {&aliasTypeInt_1, 1, false}, + {aliasTypeString_0, 0, false}, + {&aliasTypeString_0, 0, false}, + {aliasTypeString_1, 1, false}, + {&aliasTypeString_1, 1, false}, + {aliasTypeString_8d15, 8, false}, + {&aliasTypeString_8d15, 8, false}, + {aliasTypeString_8d15_minus, -8, false}, + {&aliasTypeString_8d15_minus, -8, false}, + {aliasTypeBool_true, 1, false}, + {&aliasTypeBool_true, 1, false}, + {aliasTypeBool_false, 0, false}, + {&aliasTypeBool_false, 0, false}, + + // errors + {"10a", 0, true}, + {"a10a", 0, true}, + {"8.01a", 0, true}, + {"8.01 ", 0, true}, + {"4873546382743564386435354655456575456754356765546554643456", 0, true}, + {float64(4873546382743564386435354655456575456754356765546554643456), 0, true}, + {uint64(math.MaxUint64), 0, true}, + {uint32(math.MaxUint32), 0, true}, + {int64(math.MaxInt64), 0, true}, + {int32(math.MaxInt32), 0, true}, + {int16(math.MaxInt16), 0, true}, + {uint8(math.MaxUint8), 0, true}, + {"hello", 0, true}, + {testing.T{}, 0, true}, + {&testing.T{}, 0, true}, + {[]int{}, 0, true}, + {[]string{}, 0, true}, + {[...]string{}, 0, true}, + {map[int]string{}, 0, true}, + } + + for i, tt := range tests { + msg := fmt.Sprintf("i = %d, input[%+v], expect[%v], isErr[%v]", i, tt.input, tt.expect, tt.isErr) + + v, err := cvt.Int8E(tt.input) + if tt.isErr { + assert.Error(t, err, msg) + continue + } + + assert.NoError(t, err, msg) + assert.Equal(t, tt.expect, v, msg) + + // Non-E test with no default value: + v = cvt.Int8(tt.input) + assert.Equal(t, tt.expect, v, msg) + } +} + +func TestIntE(t *testing.T) { + tests := []struct { + input interface{} + expect int + isErr bool + }{ + {int(8), 8, false}, + {int8(8), 8, false}, + {int16(8), 8, false}, + {int32(8), 8, false}, + {int64(8), 8, false}, + {uint(8), 8, false}, + {uint8(8), 8, false}, + {uint16(8), 8, false}, + {uint32(8), 8, false}, + {uint64(8), 8, false}, + {float32(8.31), 8, false}, + {float64(8.31), 8, false}, + {true, 1, false}, + {false, 0, false}, + {int(-8), -8, false}, + {int8(-8), -8, false}, + {int16(-8), -8, false}, + {int32(-8), -8, false}, + {int64(-8), -8, false}, + {float32(-8.31), -8, false}, + {float64(-8.31), -8, false}, + {"-8", -8, false}, + {"-8.01", -8, false}, + {"8", 8, false}, + {"8.00", 8, false}, + {"8.01", 8, false}, + {[]byte("-8"), -8, false}, + {[]byte("-8.01"), -8, false}, + {[]byte("8"), 8, false}, + {[]byte("8.00"), 8, false}, + {[]byte("8.01"), 8, false}, + {int(math.MaxInt32), int(math.MaxInt32), false}, + {nil, 0, false}, + {aliasTypeInt_0, 0, false}, + {&aliasTypeInt_0, 0, false}, + {aliasTypeInt_1, 1, false}, + {&aliasTypeInt_1, 1, false}, + {aliasTypeString_0, 0, false}, + {&aliasTypeString_0, 0, false}, + {aliasTypeString_1, 1, false}, + {&aliasTypeString_1, 1, false}, + {aliasTypeString_8d15, 8, false}, + {&aliasTypeString_8d15, 8, false}, + {aliasTypeString_8d15_minus, -8, false}, + {&aliasTypeString_8d15_minus, -8, false}, + {aliasTypeBool_true, 1, false}, + {&aliasTypeBool_true, 1, false}, + {aliasTypeBool_false, 0, false}, + {&aliasTypeBool_false, 0, false}, + + // errors + {"10a", 0, true}, + {"a10a", 0, true}, + {"8.01a", 0, true}, + {"8.01 ", 0, true}, + {"4873546382743564386435354655456575456754356765546554643456", 0, true}, + {float64(4873546382743564386435354655456575456754356765546554643456), 0, true}, + {uint64(math.MaxUint64), 0, true}, + {"hello", 0, true}, + {testing.T{}, 0, true}, + {&testing.T{}, 0, true}, + {[]int{}, 0, true}, + {[]string{}, 0, true}, + {[...]string{}, 0, true}, + {map[int]string{}, 0, true}, + } + + for i, tt := range tests { + msg := fmt.Sprintf("i = %d, input[%+v], expect[%v], isErr[%v]", i, tt.input, tt.expect, tt.isErr) + + v, err := cvt.IntE(tt.input) + if tt.isErr { + assert.Error(t, err, msg) + continue + } + + assert.NoError(t, err, msg) + assert.Equal(t, tt.expect, v, msg) + + // Non-E test with no default value: + v = cvt.Int(tt.input) + assert.Equal(t, tt.expect, v, msg) + } +}