github.com/gogf/gf@v1.16.9/util/gconv/gconv_z_unit_string_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/gogf/gf. 6 7 package gconv_test 8 9 import ( 10 "testing" 11 12 "github.com/gogf/gf/test/gtest" 13 "github.com/gogf/gf/util/gconv" 14 ) 15 16 type stringStruct1 struct { 17 Name string 18 } 19 20 type stringStruct2 struct { 21 Name string 22 } 23 24 func (s *stringStruct1) String() string { 25 return s.Name 26 } 27 28 func Test_String(t *testing.T) { 29 gtest.C(t, func(t *gtest.T) { 30 t.AssertEQ(gconv.String(int(123)), "123") 31 t.AssertEQ(gconv.String(int(-123)), "-123") 32 t.AssertEQ(gconv.String(int8(123)), "123") 33 t.AssertEQ(gconv.String(int8(-123)), "-123") 34 t.AssertEQ(gconv.String(int16(123)), "123") 35 t.AssertEQ(gconv.String(int16(-123)), "-123") 36 t.AssertEQ(gconv.String(int32(123)), "123") 37 t.AssertEQ(gconv.String(int32(-123)), "-123") 38 t.AssertEQ(gconv.String(int64(123)), "123") 39 t.AssertEQ(gconv.String(int64(-123)), "-123") 40 t.AssertEQ(gconv.String(int64(1552578474888)), "1552578474888") 41 t.AssertEQ(gconv.String(int64(-1552578474888)), "-1552578474888") 42 43 t.AssertEQ(gconv.String(uint(123)), "123") 44 t.AssertEQ(gconv.String(uint8(123)), "123") 45 t.AssertEQ(gconv.String(uint16(123)), "123") 46 t.AssertEQ(gconv.String(uint32(123)), "123") 47 t.AssertEQ(gconv.String(uint64(155257847488898765)), "155257847488898765") 48 49 t.AssertEQ(gconv.String(float32(123.456)), "123.456") 50 t.AssertEQ(gconv.String(float32(-123.456)), "-123.456") 51 t.AssertEQ(gconv.String(float64(1552578474888.456)), "1552578474888.456") 52 t.AssertEQ(gconv.String(float64(-1552578474888.456)), "-1552578474888.456") 53 54 t.AssertEQ(gconv.String(true), "true") 55 t.AssertEQ(gconv.String(false), "false") 56 57 t.AssertEQ(gconv.String([]byte("bytes")), "bytes") 58 59 t.AssertEQ(gconv.String(stringStruct1{"john"}), `{"Name":"john"}`) 60 t.AssertEQ(gconv.String(&stringStruct1{"john"}), "john") 61 62 t.AssertEQ(gconv.String(stringStruct2{"john"}), `{"Name":"john"}`) 63 t.AssertEQ(gconv.String(&stringStruct2{"john"}), `{"Name":"john"}`) 64 }) 65 }