gf/util/gconv/gconv_z_unit_string_test.go

66 lines
2.1 KiB
Go
Raw Normal View History

// Copyright 2018 gf Author(https://github.com/gogf/gf). 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 (
2019-06-19 09:06:52 +08:00
"testing"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/test/gtest"
"github.com/gogf/gf/util/gconv"
)
type stringStruct1 struct {
2019-06-19 09:06:52 +08:00
Name string
}
type stringStruct2 struct {
2019-06-19 09:06:52 +08:00
Name string
}
func (s *stringStruct1) String() string {
2019-06-19 09:06:52 +08:00
return s.Name
}
func Test_String(t *testing.T) {
2020-03-19 22:56:12 +08:00
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")
2020-03-19 22:56:12 +08:00
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")
2020-03-19 22:56:12 +08:00
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")
2020-03-19 22:56:12 +08:00
t.AssertEQ(gconv.String(true), "true")
t.AssertEQ(gconv.String(false), "false")
2020-03-19 22:56:12 +08:00
t.AssertEQ(gconv.String([]byte("bytes")), "bytes")
2020-03-19 22:56:12 +08:00
t.AssertEQ(gconv.String(stringStruct1{"john"}), `{"Name":"john"}`)
t.AssertEQ(gconv.String(&stringStruct1{"john"}), "john")
2020-03-19 22:56:12 +08:00
t.AssertEQ(gconv.String(stringStruct2{"john"}), `{"Name":"john"}`)
t.AssertEQ(gconv.String(&stringStruct2{"john"}), `{"Name":"john"}`)
2019-06-19 09:06:52 +08:00
})
}