github.com/gogf/gf/v2@v2.7.4/util/gconv/gconv_z_unit_convert_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  	"time"
    12  
    13  	"github.com/gogf/gf/v2/os/gtime"
    14  	"github.com/gogf/gf/v2/test/gtest"
    15  	"github.com/gogf/gf/v2/util/gconv"
    16  )
    17  
    18  func TestConvert(t *testing.T) {
    19  	gtest.C(t, func(t *gtest.T) {
    20  		t.AssertEQ(gconv.Convert(true, "bool"), true)
    21  		t.AssertEQ(gconv.Convert(false, "bool"), false)
    22  
    23  		t.Assert(gconv.Convert(int(0), "int"), int(0))
    24  		t.Assert(gconv.Convert(int8(0), "int8"), int8(0))
    25  		t.Assert(gconv.Convert(int16(0), "int16"), int16(0))
    26  		t.Assert(gconv.Convert(int32(0), "int32"), int32(0))
    27  		t.Assert(gconv.Convert(int64(1), "int64"), int64(1))
    28  
    29  		t.Assert(gconv.Convert(uint(0), "uint"), uint(0))
    30  		t.Assert(gconv.Convert(uint8(0), "uint8"), uint8(0))
    31  		t.Assert(gconv.Convert(uint16(0), "uint16"), uint16(0))
    32  		t.Assert(gconv.Convert(uint32(0), "uint32"), uint32(0))
    33  		t.Assert(gconv.Convert(uint64(0), "uint64"), uint64(0))
    34  
    35  		t.Assert(gconv.Convert(float32(0), "float32"), float32(0))
    36  		t.Assert(gconv.Convert(float64(0), "float64"), float64(0))
    37  
    38  		t.AssertEQ(gconv.Convert([]int{1, 2}, "[]int"), []int{1, 2})
    39  		t.AssertEQ(gconv.Convert([]int8{1, 2}, "[]int8}"), []int8{1, 2})
    40  		t.AssertEQ(gconv.Convert([]int16{1, 2}, "[]int16"), []int16{1, 2})
    41  		t.AssertEQ(gconv.Convert([]int32{1, 2}, "[]int32"), []int32{1, 2})
    42  		t.AssertEQ(gconv.Convert([]int64{1, 2}, "[]int64"), []int64{1, 2})
    43  		t.AssertEQ(gconv.Convert([]uint{1, 2}, "[]uint"), []uint{1, 2})
    44  		t.AssertEQ(gconv.Convert([]uint8{1, 2}, "[]uint8}"), []uint8{1, 2})
    45  		t.AssertEQ(gconv.Convert([]uint16{1, 2}, "[]uint16"), []uint16{1, 2})
    46  		t.AssertEQ(gconv.Convert([]uint32{1, 2}, "[]uint32"), []uint32{1, 2})
    47  		t.AssertEQ(gconv.Convert([]uint64{1, 2}, "[]uint64"), []uint64{1, 2})
    48  		t.AssertEQ(gconv.Convert([]float32{1, 2}, "[]float32"), []float32{1, 2})
    49  		t.AssertEQ(gconv.Convert([]float64{1, 2}, "[]float64"), []float64{1, 2})
    50  		t.AssertEQ(gconv.Convert([]string{"1", "2"}, "[]string"), []string{"1", "2"})
    51  		t.AssertEQ(gconv.Convert([]byte{}, "[]byte"), []uint8{})
    52  
    53  		var anyTest interface{} = nil
    54  		t.AssertEQ(gconv.Convert(anyTest, "string"), "")
    55  		t.AssertEQ(gconv.Convert("1", "string"), "1")
    56  
    57  		t.AssertEQ(gconv.Convert("1989-01-02", "Time", "Y-m-d"),
    58  			gconv.Time("1989-01-02", "Y-m-d"))
    59  
    60  		t.AssertEQ(gconv.Convert(1989, "Time"),
    61  			gconv.Time("1970-01-01 08:33:09 +0800 CST"))
    62  		t.AssertEQ(gconv.Convert(1989, "gtime.Time"),
    63  			*gconv.GTime("1970-01-01 08:33:09 +0800 CST"))
    64  		t.AssertEQ(gconv.Convert(1989, "*gtime.Time"),
    65  			gconv.GTime(1989))
    66  		t.AssertEQ(gconv.Convert(1989, "Duration"),
    67  			time.Duration(int64(1989)))
    68  		t.AssertEQ(gconv.Convert("1989", "Duration"),
    69  			time.Duration(int64(1989)))
    70  		t.AssertEQ(gconv.Convert("1989", ""),
    71  			"1989")
    72  
    73  		// TODO gconv.Convert(gtime.Now(), "gtime.Time", 1) = {{0001-01-01 00:00:00 +0000 UTC}}
    74  		t.AssertEQ(gconv.Convert(gtime.Now(), "gtime.Time", 1), *gtime.New())
    75  		t.AssertEQ(gconv.Convert(gtime.Now(), "*gtime.Time", 1), gtime.New())
    76  		t.AssertEQ(gconv.Convert(gtime.Now(), "GTime", 1), *gtime.New())
    77  
    78  		var boolValue bool = true
    79  		t.Assert(gconv.Convert(boolValue, "*bool"), true)
    80  		t.Assert(gconv.Convert(&boolValue, "*bool"), true)
    81  
    82  		var intNum int = 1
    83  		t.Assert(gconv.Convert(intNum, "*int"), int(1))
    84  		t.Assert(gconv.Convert(&intNum, "*int"), int(1))
    85  		var int8Num int8 = 1
    86  		t.Assert(gconv.Convert(int8Num, "*int8"), int(1))
    87  		t.Assert(gconv.Convert(&int8Num, "*int8"), int(1))
    88  		var int16Num int16 = 1
    89  		t.Assert(gconv.Convert(int16Num, "*int16"), int(1))
    90  		t.Assert(gconv.Convert(&int16Num, "*int16"), int(1))
    91  		var int32Num int32 = 1
    92  		t.Assert(gconv.Convert(int32Num, "*int32"), int(1))
    93  		t.Assert(gconv.Convert(&int32Num, "*int32"), int(1))
    94  		var int64Num int64 = 1
    95  		t.Assert(gconv.Convert(int64Num, "*int64"), int(1))
    96  		t.Assert(gconv.Convert(&int64Num, "*int64"), int(1))
    97  
    98  		var uintNum uint = 1
    99  		t.Assert(gconv.Convert(&uintNum, "*uint"), int(1))
   100  		var uint8Num uint8 = 1
   101  		t.Assert(gconv.Convert(uint8Num, "*uint8"), int(1))
   102  		t.Assert(gconv.Convert(&uint8Num, "*uint8"), int(1))
   103  		var uint16Num uint16 = 1
   104  		t.Assert(gconv.Convert(uint16Num, "*uint16"), int(1))
   105  		t.Assert(gconv.Convert(&uint16Num, "*uint16"), int(1))
   106  		var uint32Num uint32 = 1
   107  		t.Assert(gconv.Convert(uint32Num, "*uint32"), int(1))
   108  		t.Assert(gconv.Convert(&uint32Num, "*uint32"), int(1))
   109  		var uint64Num uint64 = 1
   110  		t.Assert(gconv.Convert(uint64Num, "*uint64"), int(1))
   111  		t.Assert(gconv.Convert(&uint64Num, "*uint64"), int(1))
   112  
   113  		var float32Num float32 = 1.1
   114  		t.Assert(gconv.Convert(float32Num, "*float32"), float32(1.1))
   115  		t.Assert(gconv.Convert(&float32Num, "*float32"), float32(1.1))
   116  
   117  		var float64Num float64 = 1.1
   118  		t.Assert(gconv.Convert(float64Num, "*float64"), float64(1.1))
   119  		t.Assert(gconv.Convert(&float64Num, "*float64"), float64(1.1))
   120  
   121  		var stringValue string = "1"
   122  		t.Assert(gconv.Convert(stringValue, "*string"), "1")
   123  		t.Assert(gconv.Convert(&stringValue, "*string"), "1")
   124  
   125  		var durationValue time.Duration = 1989
   126  		var expectDurationValue = time.Duration(int64(1989))
   127  		t.AssertEQ(gconv.Convert(&durationValue, "*time.Duration"),
   128  			&expectDurationValue)
   129  		t.AssertEQ(gconv.Convert(durationValue, "*time.Duration"),
   130  			&expectDurationValue)
   131  
   132  		var mapStrInt = map[string]int{"k1": 1}
   133  		var mapStrStr = map[string]string{"k1": "1"}
   134  		var mapStrAny = map[string]interface{}{"k1": 1}
   135  		t.AssertEQ(gconv.Convert(mapStrInt, "map[string]string"), mapStrStr)
   136  		t.AssertEQ(gconv.Convert(mapStrInt, "map[string]interface{}"), mapStrAny)
   137  	})
   138  }