From 4081869c49b224ee8a666be4e2d66b2c8b66460f Mon Sep 17 00:00:00 2001 From: wlynxg Date: Fri, 1 Sep 2023 17:45:22 +0800 Subject: [PATCH] test: add unit tests --- util/gconv/gconv_z_unit_converter_test.go | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/util/gconv/gconv_z_unit_converter_test.go b/util/gconv/gconv_z_unit_converter_test.go index c477391d767..c839f48b9e4 100644 --- a/util/gconv/gconv_z_unit_converter_test.go +++ b/util/gconv/gconv_z_unit_converter_test.go @@ -8,7 +8,9 @@ package gconv_test import ( "testing" + "time" + "github.com/gogf/gf/v2/os/gtime" "github.com/gogf/gf/v2/test/gtest" "github.com/gogf/gf/v2/util/gconv" ) @@ -43,6 +45,12 @@ func TestConverter_Struct(t *testing.T) { ValTa tB } + type tEE struct { + Val1 time.Time `json:"val1"` + Val2 *time.Time `json:"val2"` + Val3 *time.Time `json:"val3"` + } + gtest.C(t, func(t *gtest.T) { a := &tA{ Val: 1, @@ -174,6 +182,23 @@ func TestConverter_Struct(t *testing.T) { t.Assert(dd.ValTa.Val1, 234) t.Assert(dd.ValTa.Val2, "abc") }) + + // fix: https://github.com/gogf/gf/issues/2665 + gtest.C(t, func(t *gtest.T) { + aa := &tEE{} + + var tmp = map[string]any{ + "val1": "2023-04-15 19:10:00 +0800 CST", + "val2": "2023-04-15 19:10:00 +0800 CST", + "val3": "2006-01-02T15:04:05Z07:00", + } + err := gconv.Struct(tmp, aa) + t.AssertNil(err) + t.AssertNE(aa, nil) + t.Assert(aa.Val1.Local(), gtime.New("2023-04-15 19:10:00 +0800 CST").Local().Time) + t.Assert(aa.Val2.Local(), gtime.New("2023-04-15 19:10:00 +0800 CST").Local().Time) + t.Assert(aa.Val3.Local(), gtime.New("2006-01-02T15:04:05Z07:00").Local().Time) + }) } func TestConverter_CustomBasicType_ToStruct(t *testing.T) {