github.com/gogf/gf/v2@v2.7.4/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  	"reflect"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/gogf/gf/v2/container/gvar"
    15  	"github.com/gogf/gf/v2/os/gtime"
    16  	"github.com/gogf/gf/v2/test/gtest"
    17  	"github.com/gogf/gf/v2/util/gconv"
    18  )
    19  
    20  var stringTests = []struct {
    21  	value  interface{}
    22  	expect string
    23  }{
    24  	{true, "true"},
    25  	{false, "false"},
    26  
    27  	{int(0), "0"},
    28  	{int(123), "123"},
    29  	{int8(123), "123"},
    30  	{int16(123), "123"},
    31  	{int32(123), "123"},
    32  	{int64(123), "123"},
    33  
    34  	{uint(0), "0"},
    35  	{uint(123), "123"},
    36  	{uint8(123), "123"},
    37  	{uint16(123), "123"},
    38  	{uint32(123), "123"},
    39  	{uint64(123), "123"},
    40  
    41  	{uintptr(0), "0"},
    42  	{uintptr(123), "123"},
    43  
    44  	{rune(0), "0"},
    45  	{rune(49), "49"},
    46  
    47  	{float32(123), "123"},
    48  	{float64(123.456), "123.456"},
    49  
    50  	{[]byte(""), ""},
    51  
    52  	{"", ""},
    53  	{"true", "true"},
    54  	{"false", "false"},
    55  	{"Neptune", "Neptune"},
    56  
    57  	{complex(1, 2), "(1+2i)"},
    58  	{complex(123.456, 789.123), "(123.456+789.123i)"},
    59  
    60  	{[3]int{1, 2, 3}, "[1,2,3]"},
    61  	{[]int{1, 2, 3}, "[1,2,3]"},
    62  
    63  	{map[int]int{1: 1}, `{"1":1}`},
    64  	{map[string]string{"Earth": "太平洋"}, `{"Earth":"太平洋"}`},
    65  
    66  	{struct{}{}, "{}"},
    67  	{nil, ""},
    68  	{(*string)(nil), ""},
    69  
    70  	{gvar.New(123), "123"},
    71  	{gvar.New(123.456), "123.456"},
    72  
    73  	{goTime, "1911-10-10 00:00:00 +0000 UTC"},
    74  	{&goTime, "1911-10-10 00:00:00 +0000 UTC"},
    75  	// TODO The String method of gtime not equals to time.Time
    76  	{gfTime, "1911-10-10 00:00:00"},
    77  	{&gfTime, "1911-10-10 00:00:00"},
    78  	//{gfTime, "1911-10-10 00:00:00 +0000 UTC"},
    79  	//{&gfTime, "1911-10-10 00:00:00 +0000 UTC"},
    80  }
    81  
    82  var (
    83  	goTime = time.Date(
    84  		1911, 10, 10, 0, 0, 0, 0, time.UTC,
    85  	)
    86  	gfTime = gtime.NewFromTime(goTime)
    87  )
    88  
    89  func TestString(t *testing.T) {
    90  	gtest.C(t, func(t *gtest.T) {
    91  		for _, test := range stringTests {
    92  			t.AssertEQ(gconv.String(test.value), test.expect)
    93  		}
    94  	})
    95  
    96  	gtest.C(t, func(t *gtest.T) {
    97  		t.AssertEQ(gconv.Strings(nil), nil)
    98  	})
    99  }
   100  
   101  func TestStrings(t *testing.T) {
   102  	gtest.C(t, func(t *gtest.T) {
   103  		for _, test := range stringTests {
   104  			if test.value == nil {
   105  				t.AssertNil(gconv.Strings(test.value))
   106  				continue
   107  			}
   108  
   109  			var (
   110  				sliceType = reflect.SliceOf(reflect.TypeOf(test.value))
   111  				strings   = reflect.MakeSlice(sliceType, 0, 0)
   112  				expects   = []string{
   113  					test.expect, test.expect,
   114  				}
   115  			)
   116  			strings = reflect.Append(strings, reflect.ValueOf(test.value))
   117  			strings = reflect.Append(strings, reflect.ValueOf(test.value))
   118  
   119  			t.AssertEQ(gconv.Strings(strings.Interface()), expects)
   120  			t.AssertEQ(gconv.SliceStr(strings.Interface()), expects)
   121  		}
   122  	})
   123  
   124  	// Test for special types.
   125  	gtest.C(t, func(t *gtest.T) {
   126  		// []int8 json
   127  		t.AssertEQ(gconv.Strings([]uint8(`{"Name":"Earth"}"`)),
   128  			[]string{"123", "34", "78", "97", "109", "101", "34", "58", "34", "69", "97", "114", "116", "104", "34", "125", "34"})
   129  	})
   130  }