github.com/sandwich-go/boost@v1.3.29/xconv/int_test.go (about) 1 package xconv 2 3 import ( 4 . "github.com/smartystreets/goconvey/convey" 5 "testing" 6 ) 7 8 type testInt struct { 9 f1 int64 10 f2 uint64 11 } 12 13 func (b testInt) Int64() int64 { return b.f1 } 14 func (b testInt) Uint64() uint64 { return b.f2 } 15 16 func TestInt(t *testing.T) { 17 Convey(`test int`, t, func() { 18 for _, v := range []struct { 19 i interface{} 20 is int 21 }{ 22 {nil, 0}, {3, 3}, {uint(3), 3}, {"3", 3}, {float32(3), 3}, {float64(3), 3}, 23 {int8(3), 3}, {int16(3), 3}, {int32(3), 3}, {int64(3), 3}, 24 {uint8(3), 3}, {uint16(3), 3}, {uint32(3), 3}, {uint64(3), 3}, 25 {testInt{f1: 3, f2: 4}, 3}, 26 } { 27 So(Int(v.i), ShouldEqual, v.is) 28 } 29 }) 30 31 Convey(`test int8`, t, func() { 32 for _, v := range []struct { 33 i interface{} 34 is int8 35 }{ 36 {nil, 0}, {3, 3}, {uint(3), 3}, {"3", 3}, {float32(3), 3}, {float64(3), 3}, 37 {int8(3), 3}, {int16(3), 3}, {int32(3), 3}, {int64(3), 3}, 38 {uint8(3), 3}, {uint16(3), 3}, {uint32(3), 3}, {uint64(3), 3}, 39 {testInt{f1: 3, f2: 4}, 3}, 40 } { 41 So(Int8(v.i), ShouldEqual, v.is) 42 } 43 }) 44 45 Convey(`test int16`, t, func() { 46 for _, v := range []struct { 47 i interface{} 48 is int16 49 }{ 50 {nil, 0}, {3, 3}, {uint(3), 3}, {"3", 3}, {float32(3), 3}, {float64(3), 3}, 51 {int8(3), 3}, {int16(3), 3}, {int32(3), 3}, {int64(3), 3}, 52 {uint8(3), 3}, {uint16(3), 3}, {uint32(3), 3}, {uint64(3), 3}, 53 {testInt{f1: 3, f2: 4}, 3}, 54 } { 55 So(Int16(v.i), ShouldEqual, v.is) 56 } 57 }) 58 59 Convey(`test int32`, t, func() { 60 for _, v := range []struct { 61 i interface{} 62 is int32 63 }{ 64 {nil, 0}, {3, 3}, {uint(3), 3}, {"3", 3}, {float32(3), 3}, {float64(3), 3}, 65 {int8(3), 3}, {int16(3), 3}, {int32(3), 3}, {int64(3), 3}, 66 {uint8(3), 3}, {uint16(3), 3}, {uint32(3), 3}, {uint64(3), 3}, 67 {testInt{f1: 3, f2: 4}, 3}, 68 } { 69 So(Int32(v.i), ShouldEqual, v.is) 70 } 71 }) 72 73 Convey(`test int64`, t, func() { 74 for _, v := range []struct { 75 i interface{} 76 is int64 77 }{ 78 {nil, 0}, {3, 3}, {uint(3), 3}, {"3", 3}, {float32(3), 3}, {float64(3), 3}, 79 {int8(3), 3}, {int16(3), 3}, {int32(3), 3}, {int64(3), 3}, 80 {uint8(3), 3}, {uint16(3), 3}, {uint32(3), 3}, {uint64(3), 3}, 81 {testInt{f1: 3, f2: 4}, 3}, 82 } { 83 So(Int64(v.i), ShouldEqual, v.is) 84 } 85 }) 86 }