add ut cases for package gtime (#2303)

* add ut cases for package gtime

* add ut cases for package gtime

* add ut cases for package gtime
This commit is contained in:
黄骞 2022-11-17 19:44:20 +08:00 committed by GitHub
parent d2113b4d23
commit 7cd415b1df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 105 additions and 0 deletions

View File

@ -92,6 +92,15 @@ func Test_Format(t *testing.T) {
} }
}) })
gtest.C(t, func(t *gtest.T) {
var ti *gtime.Time = nil
t.Assert(ti.Format("Y-m-d h:i:s"), "")
t.Assert(ti.FormatNew("Y-m-d h:i:s"), nil)
t.Assert(ti.FormatTo("Y-m-d h:i:s"), nil)
t.Assert(ti.Layout("Y-m-d h:i:s"), "")
t.Assert(ti.LayoutNew("Y-m-d h:i:s"), nil)
t.Assert(ti.LayoutTo("Y-m-d h:i:s"), nil)
})
} }
func Test_Format_ZeroString(t *testing.T) { func Test_Format_ZeroString(t *testing.T) {

View File

@ -18,10 +18,19 @@ import (
func Test_SetTimeZone(t *testing.T) { func Test_SetTimeZone(t *testing.T) {
gtest.C(t, func(t *gtest.T) { gtest.C(t, func(t *gtest.T) {
t.Assert(gtime.SetTimeZone("Asia/Shanghai"), nil) t.Assert(gtime.SetTimeZone("Asia/Shanghai"), nil)
t.AssertNE(gtime.SetTimeZone("testzone"), nil)
// t.Assert(time.Local.String(), "Asia/Shanghai") // t.Assert(time.Local.String(), "Asia/Shanghai")
}) })
} }
func Test_TimestampStr(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.AssertGT(len(gtime.TimestampMilliStr()), 0)
t.AssertGT(len(gtime.TimestampMicroStr()), 0)
t.AssertGT(len(gtime.TimestampNanoStr()), 0)
})
}
func Test_Nanosecond(t *testing.T) { func Test_Nanosecond(t *testing.T) {
gtest.C(t, func(t *gtest.T) { gtest.C(t, func(t *gtest.T) {
nanos := gtime.TimestampNano() nanos := gtime.TimestampNano()
@ -69,6 +78,14 @@ func Test_Datetime(t *testing.T) {
} }
t.Assert(datetime, timeTemp.Time.Format("2006-01-02 15:04:05")) t.Assert(datetime, timeTemp.Time.Format("2006-01-02 15:04:05"))
}) })
gtest.C(t, func(t *gtest.T) {
timeTemp, err := gtime.StrToTime("")
t.Assert(err, nil)
t.AssertLT(timeTemp.Unix(), 0)
timeTemp, err = gtime.StrToTime("2006-01")
t.AssertNE(err, nil)
t.Assert(timeTemp, nil)
})
} }
func Test_ISO8601(t *testing.T) { func Test_ISO8601(t *testing.T) {

View File

@ -8,6 +8,7 @@ package gtime_test
import ( import (
"fmt" "fmt"
"github.com/gogf/gf/v2/internal/json"
"testing" "testing"
"time" "time"
@ -48,6 +49,27 @@ func Test_New(t *testing.T) {
timeTemp := gtime.New("2021-2-9 08:01:21") timeTemp := gtime.New("2021-2-9 08:01:21")
t.Assert(timeTemp.Format("Y-m-d H:i:s"), "2021-02-09 08:01:21") t.Assert(timeTemp.Format("Y-m-d H:i:s"), "2021-02-09 08:01:21")
t.Assert(timeTemp.Time.Format("2006-01-02 15:04:05"), "2021-02-09 08:01:21") t.Assert(timeTemp.Time.Format("2006-01-02 15:04:05"), "2021-02-09 08:01:21")
timeTemp = gtime.New("2021-02-09 08:01:21", []byte("Y-m-d H:i:s"))
t.Assert(timeTemp.Format("Y-m-d H:i:s"), "2021-02-09 08:01:21")
t.Assert(timeTemp.Time.Format("2006-01-02 15:04:05"), "2021-02-09 08:01:21")
timeTemp = gtime.New([]byte("2021-02-09 08:01:21"))
t.Assert(timeTemp.Format("Y-m-d H:i:s"), "2021-02-09 08:01:21")
t.Assert(timeTemp.Time.Format("2006-01-02 15:04:05"), "2021-02-09 08:01:21")
timeTemp = gtime.New([]byte("2021-02-09 08:01:21"), "Y-m-d H:i:s")
t.Assert(timeTemp.Format("Y-m-d H:i:s"), "2021-02-09 08:01:21")
t.Assert(timeTemp.Time.Format("2006-01-02 15:04:05"), "2021-02-09 08:01:21")
timeTemp = gtime.New([]byte("2021-02-09 08:01:21"), []byte("Y-m-d H:i:s"))
t.Assert(timeTemp.Format("Y-m-d H:i:s"), "2021-02-09 08:01:21")
t.Assert(timeTemp.Time.Format("2006-01-02 15:04:05"), "2021-02-09 08:01:21")
})
//
gtest.C(t, func(t *gtest.T) {
t.Assert(gtime.New(gtime.Time{}), nil)
t.Assert(gtime.New(&gtime.Time{}), nil)
}) })
} }
@ -124,6 +146,8 @@ func Test_NewFromTimeStamp(t *testing.T) {
t.Assert(timeTemp.Format("Y-m-d H:i:s"), "2019-04-05 18:24:06") t.Assert(timeTemp.Format("Y-m-d H:i:s"), "2019-04-05 18:24:06")
timeTemp1 := gtime.NewFromTimeStamp(0) timeTemp1 := gtime.NewFromTimeStamp(0)
t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "0001-01-01 00:00:00") t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "0001-01-01 00:00:00")
timeTemp2 := gtime.NewFromTimeStamp(155445984)
t.Assert(timeTemp2.Format("Y-m-d H:i:s"), "1974-12-05 11:26:24")
}) })
} }
@ -134,6 +158,50 @@ func Test_Time_Second(t *testing.T) {
}) })
} }
func Test_Time_IsZero(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var ti *gtime.Time = nil
t.Assert(ti.IsZero(), true)
})
}
func Test_Time_AddStr(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
gt := gtime.New("2018-08-08 08:08:08")
gt1, err := gt.AddStr("10T")
t.Assert(gt1, nil)
t.AssertNE(err, nil)
})
}
func Test_Time_Equal(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var t1 *gtime.Time = nil
var t2 = gtime.New()
t.Assert(t1.Equal(t2), false)
t.Assert(t1.Equal(t1), true)
t.Assert(t2.Equal(t1), false)
})
}
func Test_Time_After(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var t1 *gtime.Time = nil
var t2 = gtime.New()
t.Assert(t1.After(t2), false)
t.Assert(t2.After(t1), true)
})
}
func Test_Time_Sub(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var t1 *gtime.Time = nil
var t2 = gtime.New()
t.Assert(t1.Sub(t2), time.Duration(0))
t.Assert(t2.Sub(t1), time.Duration(0))
})
}
func Test_Time_Nanosecond(t *testing.T) { func Test_Time_Nanosecond(t *testing.T) {
gtest.C(t, func(t *gtest.T) { gtest.C(t, func(t *gtest.T) {
timeTemp := gtime.Now() timeTemp := gtime.Now()
@ -468,4 +536,15 @@ func Test_DeepCopy(t *testing.T) {
u2 := gutil.Copy(u1).(*User) u2 := gutil.Copy(u1).(*User)
t.Assert(u1, u2) t.Assert(u1, u2)
}) })
gtest.C(t, func(t *gtest.T) {
var t1 *gtime.Time = nil
t.Assert(t1.DeepCopy(), nil)
})
}
func Test_UnmarshalJSON(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var t1 gtime.Time
t.AssertNE(json.Unmarshal([]byte("{}"), &t1), nil)
})
} }