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