1 Star 0 Fork 63

golang-package/carbon

forked from dromara/carbon 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
creator_unit_test.go 13.12 KB
一键复制 编辑 原始数据 按行查看 历史
gouguoyin 提交于 2024-04-09 18:36 . 统一单元测试格式
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
package carbon
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestCarbon_CreateFromStdTime(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want string
}{
{
name: "case1",
carbon: CreateFromStdTime(time.Now()),
want: time.Now().Format(DateTimeLayout),
},
{
name: "case2",
carbon: CreateFromStdTime(time.Now(), PRC),
want: time.Now().Format(DateTimeLayout),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.ToDateTimeString(), "CreateFromStdTime()")
})
}
}
func TestCarbon_CreateFromTimestamp(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want string
}{
{
name: "case1",
carbon: Parse("xxx").CreateFromTimestamp(0),
want: "",
},
{
name: "case2",
carbon: CreateFromTimestamp(-1),
want: "1970-01-01 07:59:59 +0800 CST",
},
{
name: "case3",
carbon: CreateFromTimestamp(0),
want: "1970-01-01 08:00:00 +0800 CST",
},
{
name: "case4",
carbon: CreateFromTimestamp(1),
want: "1970-01-01 08:00:01 +0800 CST",
},
{
name: "case5",
carbon: CreateFromTimestamp(1649735755, PRC),
want: "2022-04-12 11:55:55 +0800 CST",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.ToString(), "CreateFromTimestamp()")
})
}
}
func TestCarbon_CreateFromTimestampMilli(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want string
}{
{
name: "case1",
carbon: Parse("xxx").CreateFromTimestampMilli(0),
want: "",
},
{
name: "case2",
carbon: CreateFromTimestampMilli(-1),
want: "1970-01-01 07:59:59.999 +0800 CST",
},
{
name: "case3",
carbon: CreateFromTimestampMilli(0),
want: "1970-01-01 08:00:00 +0800 CST",
},
{
name: "case4",
carbon: CreateFromTimestampMilli(1),
want: "1970-01-01 08:00:00.001 +0800 CST",
},
{
name: "case5",
carbon: CreateFromTimestampMilli(1649735755981, PRC),
want: "2022-04-12 11:55:55.981 +0800 CST",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.ToString(), "CreateFromTimestampMilli()")
})
}
}
func TestCarbon_CreateFromTimestampMicro(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want string
}{
{
name: "case1",
carbon: Parse("xxx").CreateFromTimestampMicro(0),
want: "",
},
{
name: "case2",
carbon: CreateFromTimestampMicro(-1),
want: "1970-01-01 07:59:59.999999 +0800 CST",
},
{
name: "case3",
carbon: CreateFromTimestampMicro(0),
want: "1970-01-01 08:00:00 +0800 CST",
},
{
name: "case4",
carbon: CreateFromTimestampMicro(1),
want: "1970-01-01 08:00:00.000001 +0800 CST",
},
{
name: "case5",
carbon: CreateFromTimestampMicro(1649735755981566, PRC),
want: "2022-04-12 11:55:55.981566 +0800 CST",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.ToString(), "CreateFromTimestampMicro()")
})
}
}
func TestCarbon_CreateFromTimestampNano(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want string
}{
{
name: "case1",
carbon: Parse("xxx").CreateFromTimestampNano(0),
want: "",
},
{
name: "case2",
carbon: CreateFromTimestampNano(-1),
want: "1970-01-01 07:59:59.999999999 +0800 CST",
},
{
name: "case3",
carbon: CreateFromTimestampNano(0),
want: "1970-01-01 08:00:00 +0800 CST",
},
{
name: "case4",
carbon: CreateFromTimestampNano(1, PRC),
want: "1970-01-01 08:00:00.000000001 +0800 CST",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.ToString(), "CreateFromTimestampNano()")
})
}
}
func TestCarbon_CreateFromDateTime(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want string
}{
{
name: "case1",
carbon: Parse("xxx").CreateFromDateTime(0, 0, 0, 0, 0, 0),
want: "",
},
{
name: "case2",
carbon: CreateFromDateTime(2020, 1, 1, 13, 14, 15),
want: "2020-01-01 13:14:15",
},
{
name: "case3",
carbon: CreateFromDateTime(2020, 1, 31, 13, 14, 15),
want: "2020-01-31 13:14:15",
},
{
name: "case4",
carbon: CreateFromDateTime(2020, 2, 1, 13, 14, 15, PRC),
want: "2020-02-01 13:14:15",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.ToDateTimeString(), "CreateFromDateTime()")
})
}
}
func TestCarbon_CreateFromDateTimeMilli(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want string
}{
{
name: "case1",
carbon: Parse("xxx").CreateFromDateTimeMilli(0, 0, 0, 0, 0, 0, 0),
want: "",
},
{
name: "case2",
carbon: CreateFromDateTimeMilli(2020, 1, 1, 13, 14, 15, 999),
want: "2020-01-01 13:14:15.999 +0800 CST",
},
{
name: "case3",
carbon: CreateFromDateTimeMilli(2020, 1, 31, 13, 14, 15, 999),
want: "2020-01-31 13:14:15.999 +0800 CST",
},
{
name: "case4",
carbon: CreateFromDateTimeMilli(2020, 2, 1, 13, 14, 15, 999),
want: "2020-02-01 13:14:15.999 +0800 CST",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.ToString(), "CreateFromDateTimeMilli()")
})
}
}
func TestCarbon_CreateFromDateTimeMicro(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want string
}{
{
name: "case1",
carbon: Parse("xxx").CreateFromDateTimeMicro(0, 0, 0, 0, 0, 0, 0),
want: "",
},
{
name: "case2",
carbon: CreateFromDateTimeMicro(2020, 1, 1, 13, 14, 15, 999999),
want: "2020-01-01 13:14:15.999999 +0800 CST",
},
{
name: "case3",
carbon: CreateFromDateTimeMicro(2020, 1, 31, 13, 14, 15, 999999),
want: "2020-01-31 13:14:15.999999 +0800 CST",
},
{
name: "case4",
carbon: CreateFromDateTimeMicro(2020, 2, 1, 13, 14, 15, 999999),
want: "2020-02-01 13:14:15.999999 +0800 CST",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.ToString(), "CreateFromDateTimeMicro()")
})
}
}
func TestCarbon_CreateFromDateTimeNano(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want string
}{
{
name: "case1",
carbon: Parse("xxx").CreateFromDateTimeNano(0, 0, 0, 0, 0, 0, 0),
want: "",
},
{
name: "case2",
carbon: CreateFromDateTimeNano(2020, 1, 1, 13, 14, 15, 999999999),
want: "2020-01-01 13:14:15.999999999 +0800 CST",
},
{
name: "case3",
carbon: CreateFromDateTimeNano(2020, 1, 31, 13, 14, 15, 999999999),
want: "2020-01-31 13:14:15.999999999 +0800 CST",
},
{
name: "case4",
carbon: CreateFromDateTimeNano(2020, 2, 1, 13, 14, 15, 999999999),
want: "2020-02-01 13:14:15.999999999 +0800 CST",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.ToString(), "CreateFromDateTimeNano()")
})
}
}
func TestCarbon_CreateFromDate(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want string
}{
{
name: "case1",
carbon: Parse("xxx").CreateFromDate(0, 0, 0),
want: "",
},
{
name: "case2",
carbon: CreateFromDate(2020, 1, 1),
want: "2020-01-01 00:00:00 +0800 CST",
},
{
name: "case3",
carbon: CreateFromDate(2020, 1, 31),
want: "2020-01-31 00:00:00 +0800 CST",
},
{
name: "case4",
carbon: CreateFromDate(2020, 2, 1),
want: "2020-02-01 00:00:00 +0800 CST",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.ToString(), "CreateFromDate()")
})
}
}
func TestCarbon_CreateFromDateMilli(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want string
}{
{
name: "case1",
carbon: Parse("xxx").CreateFromDateMilli(0, 0, 0, 0),
want: "",
},
{
name: "case2",
carbon: CreateFromDateMilli(2020, 1, 1, 999),
want: "2020-01-01 00:00:00.999 +0800 CST",
},
{
name: "case3",
carbon: CreateFromDateMilli(2020, 1, 31, 999),
want: "2020-01-31 00:00:00.999 +0800 CST",
},
{
name: "case4",
carbon: CreateFromDateMilli(2020, 2, 1, 999),
want: "2020-02-01 00:00:00.999 +0800 CST",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.ToString(), "CreateFromDateMilli()")
})
}
}
func TestCarbon_CreateFromDateMicro(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want string
}{
{
name: "case1",
carbon: Parse("xxx").CreateFromDateMicro(0, 0, 0, 0),
want: "",
},
{
name: "case2",
carbon: CreateFromDateMicro(2020, 1, 1, 999999),
want: "2020-01-01 00:00:00.999999 +0800 CST",
},
{
name: "case3",
carbon: CreateFromDateMicro(2020, 1, 31, 999999),
want: "2020-01-31 00:00:00.999999 +0800 CST",
},
{
name: "case4",
carbon: CreateFromDateMicro(2020, 2, 1, 999999),
want: "2020-02-01 00:00:00.999999 +0800 CST",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.ToString(), "CreateFromDateMicro()")
})
}
}
func TestCarbon_CreateFromDateNano(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want string
}{
{
name: "case1",
carbon: Parse("xxx").CreateFromDateNano(0, 0, 0, 0),
want: "",
},
{
name: "case2",
carbon: CreateFromDateNano(2020, 1, 1, 999999999),
want: "2020-01-01 00:00:00.999999999 +0800 CST",
},
{
name: "case3",
carbon: CreateFromDateNano(2020, 1, 31, 999999999),
want: "2020-01-31 00:00:00.999999999 +0800 CST",
},
{
name: "case4",
carbon: CreateFromDateNano(2020, 2, 1, 999999999),
want: "2020-02-01 00:00:00.999999999 +0800 CST",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.ToString(), "CreateFromDateNano()")
})
}
}
func TestCarbon_CreateFromTime(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want Carbon
}{
{
name: "case1",
carbon: CreateFromTime(0, 0, 0),
want: Now().SetTime(0, 0, 0),
},
{
name: "case2",
carbon: CreateFromTime(0, 0, 15),
want: Now().SetTime(0, 0, 15),
},
{
name: "case3",
carbon: CreateFromTime(0, 14, 15),
want: Now().SetTime(0, 14, 15),
},
{
name: "case4",
carbon: CreateFromTime(13, 14, 15),
want: Now().SetTime(13, 14, 15),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want.ToTimeString(), tt.carbon.ToTimeString(), "CreateFromTime()")
})
}
}
func TestCarbon_CreateFromTimeMilli(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want Carbon
}{
{
name: "case1",
carbon: CreateFromTimeMilli(0, 0, 0, 999),
want: Now().SetTimeMilli(0, 0, 0, 999),
},
{
name: "case2",
carbon: CreateFromTimeMilli(0, 0, 15, 999),
want: Now().SetTimeMilli(0, 0, 15, 999),
},
{
name: "case3",
carbon: CreateFromTimeMilli(0, 14, 15, 999),
want: Now().SetTimeMilli(0, 14, 15, 999),
},
{
name: "case4",
carbon: CreateFromTimeMilli(13, 14, 15, 999),
want: Now().SetTimeMilli(13, 14, 15, 999),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want.ToTimeMilliString(), tt.carbon.ToTimeMilliString(), "CreateFromTimeMilli()")
})
}
}
func TestCarbon_CreateFromTimeMicro(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want Carbon
}{
{
name: "case1",
carbon: CreateFromTimeMicro(0, 0, 0, 999999),
want: Now().SetTimeMicro(0, 0, 0, 999999),
},
{
name: "case2",
carbon: CreateFromTimeMicro(0, 0, 15, 999999),
want: Now().SetTimeMicro(0, 0, 15, 999999),
},
{
name: "case3",
carbon: CreateFromTimeMicro(0, 14, 15, 999999),
want: Now().SetTimeMicro(0, 14, 15, 999999),
},
{
name: "case4",
carbon: CreateFromTimeMicro(13, 14, 15, 999999),
want: Now().SetTimeMicro(13, 14, 15, 999999),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want.ToTimeMilliString(), tt.carbon.ToTimeMilliString(), "CreateFromTimeMicro()")
})
}
}
func TestCarbon_CreateFromTimeNano(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want Carbon
}{
{
name: "case1",
carbon: CreateFromTimeNano(0, 0, 0, 999999999),
want: Now().SetTimeNano(0, 0, 0, 999999999),
},
{
name: "case2",
carbon: CreateFromTimeNano(0, 0, 15, 999999999),
want: Now().SetTimeNano(0, 0, 15, 999999999),
},
{
name: "case3",
carbon: CreateFromTimeNano(0, 14, 15, 999999999),
want: Now().SetTimeNano(0, 14, 15, 999999999),
},
{
name: "case4",
carbon: CreateFromTimeNano(13, 14, 15, 999999999),
want: Now().SetTimeNano(13, 14, 15, 999999999),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want.ToTimeNanoString(), tt.carbon.ToTimeNanoString(), "CreateFromTimeMicro()")
})
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/golang-package/carbon.git
git@gitee.com:golang-package/carbon.git
golang-package
carbon
carbon
master

搜索帮助