github.com/avicd/go-utilx@v0.1.0/refx/as_test.go (about) 1 package refx 2 3 import ( 4 "github.com/stretchr/testify/assert" 5 "testing" 6 ) 7 8 type testAsSt struct { 9 Str string 10 } 11 12 func TestAs(t *testing.T) { 13 var p *testAsSt 14 p1 := testAsSt{Str: "1"} 15 // Bool 16 assert.Equal(t, false, AsBool(p)) 17 assert.Equal(t, true, AsBool(p1)) 18 assert.Equal(t, true, AsBool(1)) 19 assert.Equal(t, false, AsBool(0)) 20 21 // Integer 22 assert.Equal(t, 10, AsInt(10.322)) 23 assert.Equal(t, 10, AsInt(int64(10))) 24 assert.Equal(t, int8(23), AsInt8(23.21)) 25 assert.Equal(t, int16(12), AsInt16(12)) 26 assert.Equal(t, int32(15), AsInt32(15.12)) 27 assert.Equal(t, int64(32), AsInt64(32.1)) 28 29 // Uint 30 assert.Equal(t, uint(10), AsUint(10.322)) 31 assert.Equal(t, uint8(10), AsUint8(int64(10))) 32 assert.Equal(t, uint8(23), AsUint8(23.21)) 33 assert.Equal(t, uint16(12), AsUint16(12)) 34 assert.Equal(t, uint32(15), AsUint32(15.12)) 35 assert.Equal(t, uint64(32), AsUint64(32.1)) 36 37 // Float 38 assert.Equal(t, float32(32.0), AsFloat32(32)) 39 assert.Equal(t, float32(16.0), AsFloat32(16)) 40 assert.Equal(t, 21.0, AsFloat64(21)) 41 assert.Equal(t, 12.0, AsFloat64(float32(12.0))) 42 43 // String 44 assert.Equal(t, "true", AsString(true)) 45 assert.Equal(t, "false", AsString(false)) 46 assert.Equal(t, "10.1", AsString(10.1)) 47 assert.Equal(t, "---", AsString("---")) 48 assert.Equal(t, "0", AsString(0)) 49 assert.Equal(t, "", AsString(nil)) 50 assert.Equal(t, "", AsString(p)) 51 }