gf/util/gconv/gconv_z_unit_string_test.go
黄骞 9f12673631
add ut cases for package gconv (#2272)
* improve gconv.go code coverage

* improve gconv_convert.go code coverage

* improve gconv_float.go code coverage

* improve gconv_map.go code coverage

* improve gconv_maps.go code coverage

* improve gconv_maptomap.go code coverage

* improve gconv_maptomaps.go code coverage
2022-11-07 17:53:51 +08:00

72 lines
2.3 KiB
Go

// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gconv_test
import (
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/util/gconv"
"testing"
"time"
)
type stringStruct1 struct {
Name string
}
type stringStruct2 struct {
Name string
}
func (s *stringStruct1) String() string {
return s.Name
}
func Test_String(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.AssertEQ(gconv.String(int(123)), "123")
t.AssertEQ(gconv.String(int(-123)), "-123")
t.AssertEQ(gconv.String(int8(123)), "123")
t.AssertEQ(gconv.String(int8(-123)), "-123")
t.AssertEQ(gconv.String(int16(123)), "123")
t.AssertEQ(gconv.String(int16(-123)), "-123")
t.AssertEQ(gconv.String(int32(123)), "123")
t.AssertEQ(gconv.String(int32(-123)), "-123")
t.AssertEQ(gconv.String(int64(123)), "123")
t.AssertEQ(gconv.String(int64(-123)), "-123")
t.AssertEQ(gconv.String(int64(1552578474888)), "1552578474888")
t.AssertEQ(gconv.String(int64(-1552578474888)), "-1552578474888")
t.AssertEQ(gconv.String(uint(123)), "123")
t.AssertEQ(gconv.String(uint8(123)), "123")
t.AssertEQ(gconv.String(uint16(123)), "123")
t.AssertEQ(gconv.String(uint32(123)), "123")
t.AssertEQ(gconv.String(uint64(155257847488898765)), "155257847488898765")
t.AssertEQ(gconv.String(float32(123.456)), "123.456")
t.AssertEQ(gconv.String(float32(-123.456)), "-123.456")
t.AssertEQ(gconv.String(float64(1552578474888.456)), "1552578474888.456")
t.AssertEQ(gconv.String(float64(-1552578474888.456)), "-1552578474888.456")
t.AssertEQ(gconv.String(true), "true")
t.AssertEQ(gconv.String(false), "false")
t.AssertEQ(gconv.String([]byte("bytes")), "bytes")
t.AssertEQ(gconv.String(stringStruct1{"john"}), `{"Name":"john"}`)
t.AssertEQ(gconv.String(&stringStruct1{"john"}), "john")
t.AssertEQ(gconv.String(stringStruct2{"john"}), `{"Name":"john"}`)
t.AssertEQ(gconv.String(&stringStruct2{"john"}), `{"Name":"john"}`)
var nilTime *time.Time = nil
t.AssertEQ(gconv.String(nilTime), "")
var nilGTime *gtime.Time = nil
t.AssertEQ(gconv.String(nilGTime), "")
})
}