github.com/wangyougui/gf/v2@v2.6.5/util/gconv/gconv_z_unit_basic_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/wangyougui/gf. 6 7 package gconv_test 8 9 import ( 10 "testing" 11 12 "github.com/wangyougui/gf/v2/test/gtest" 13 "github.com/wangyougui/gf/v2/util/gconv" 14 ) 15 16 func Test_Basic(t *testing.T) { 17 gtest.C(t, func(t *gtest.T) { 18 f32 := float32(123.456) 19 i64 := int64(1552578474888) 20 t.AssertEQ(gconv.Int(f32), int(123)) 21 t.AssertEQ(gconv.Int8(f32), int8(123)) 22 t.AssertEQ(gconv.Int16(f32), int16(123)) 23 t.AssertEQ(gconv.Int32(f32), int32(123)) 24 t.AssertEQ(gconv.Int64(f32), int64(123)) 25 t.AssertEQ(gconv.Int64(f32), int64(123)) 26 t.AssertEQ(gconv.Uint(f32), uint(123)) 27 t.AssertEQ(gconv.Uint8(f32), uint8(123)) 28 t.AssertEQ(gconv.Uint16(f32), uint16(123)) 29 t.AssertEQ(gconv.Uint32(f32), uint32(123)) 30 t.AssertEQ(gconv.Uint64(f32), uint64(123)) 31 t.AssertEQ(gconv.Float32(f32), float32(123.456)) 32 t.AssertEQ(gconv.Float64(i64), float64(i64)) 33 t.AssertEQ(gconv.Bool(f32), true) 34 t.AssertEQ(gconv.String(f32), "123.456") 35 t.AssertEQ(gconv.String(i64), "1552578474888") 36 }) 37 38 gtest.C(t, func(t *gtest.T) { 39 s := "-0xFF" 40 t.Assert(gconv.Int(s), int64(-0xFF)) 41 }) 42 } 43 44 func Test_Duration(t *testing.T) { 45 gtest.C(t, func(t *gtest.T) { 46 d := gconv.Duration("1s") 47 t.Assert(d.String(), "1s") 48 t.Assert(d.Nanoseconds(), 1000000000) 49 }) 50 } 51 52 func Test_ConvertWithRefer(t *testing.T) { 53 gtest.C(t, func(t *gtest.T) { 54 t.AssertEQ(gconv.ConvertWithRefer("1", 100), 1) 55 t.AssertEQ(gconv.ConvertWithRefer("1.01", 1.111), 1.01) 56 t.AssertEQ(gconv.ConvertWithRefer("1.01", "1.111"), "1.01") 57 t.AssertEQ(gconv.ConvertWithRefer("1.01", false), true) 58 t.AssertNE(gconv.ConvertWithRefer("1.01", false), false) 59 }) 60 }