carbon/boundary_unit_test.go

848 lines
15 KiB
Go
Raw Normal View History

2021-02-18 14:32:31 +08:00
package carbon
import (
"testing"
"github.com/stretchr/testify/assert"
2021-02-18 14:32:31 +08:00
)
func TestCarbon_StartOfCentury(t *testing.T) {
tests := []struct {
2024-04-09 20:12:49 +08:00
name string
carbon Carbon
want string
}{
2024-04-09 20:12:49 +08:00
{
name: "case1",
carbon: Parse(""),
want: "",
},
{
name: "case2",
carbon: Parse("xxx"),
want: "",
},
{
name: "case3",
carbon: Parse("2020-01-01 00:00:00"),
want: "2000-01-01 00:00:00",
},
{
name: "case4",
carbon: Parse("2020-01-31 23:59:59"),
want: "2000-01-01 00:00:00",
},
{
name: "case5",
carbon: Parse("2020-02-01 13:14:15"),
want: "2000-01-01 00:00:00",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.StartOfCentury().ToDateTimeString(), "StartOfCentury()")
})
}
}
func TestCarbon_EndOfCentury(t *testing.T) {
tests := []struct {
2024-04-09 20:12:49 +08:00
name string
carbon Carbon
want string
}{
2024-04-09 20:12:49 +08:00
{
name: "case1",
carbon: Parse(""),
want: "",
},
{
name: "case2",
carbon: Parse("xxx"),
want: "",
},
{
name: "case3",
carbon: Parse("2020-01-01 00:00:00"),
want: "2099-12-31 23:59:59",
},
{
name: "case4",
carbon: Parse("2020-01-31 23:59:59"),
want: "2099-12-31 23:59:59",
},
{
name: "case5",
carbon: Parse("2020-02-01 13:14:15"),
want: "2099-12-31 23:59:59",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.EndOfCentury().ToDateTimeString(), "EndOfCentury()")
})
}
}
func TestCarbon_StartOfDecade(t *testing.T) {
tests := []struct {
2024-04-09 20:12:49 +08:00
name string
carbon Carbon
want string
}{
2024-04-09 20:12:49 +08:00
{
name: "case1",
carbon: Parse(""),
want: "",
},
{
name: "case2",
carbon: Parse("xxx"),
want: "",
},
{
name: "case3",
carbon: Parse("2020-01-31 23:59:59"),
want: "2020-01-01 00:00:00",
},
{
name: "case4",
carbon: Parse("2021-01-01 00:00:00"),
want: "2020-01-01 00:00:00",
},
{
name: "case5",
carbon: Parse("2029-01-31 23:59:59"),
want: "2020-01-01 00:00:00",
},
{
name: "case6",
carbon: Parse("2020-02-01 13:14:15"),
want: "2020-01-01 00:00:00",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.StartOfDecade().ToDateTimeString(), "StartOfDecade()")
})
}
}
func TestCarbon_EndOfDecade(t *testing.T) {
tests := []struct {
2024-04-09 20:12:49 +08:00
name string
carbon Carbon
want string
}{
2024-04-09 20:12:49 +08:00
{
name: "case1",
carbon: Parse(""),
want: "",
},
{
name: "case2",
carbon: Parse("xxx"),
want: "",
},
{
name: "case3",
carbon: Parse("2020-01-31 23:59:59"),
want: "2029-12-31 23:59:59",
},
{
name: "case4",
carbon: Parse("2021-01-01 00:00:00"),
want: "2029-12-31 23:59:59",
},
{
name: "case5",
carbon: Parse("2029-01-31 23:59:59"),
want: "2029-12-31 23:59:59",
},
{
name: "case6",
carbon: Parse("2020-02-01 13:14:15"),
want: "2029-12-31 23:59:59",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.EndOfDecade().ToDateTimeString(), "EndOfDecade()")
})
}
}
2021-02-18 14:32:31 +08:00
func TestCarbon_StartOfYear(t *testing.T) {
tests := []struct {
2024-04-09 20:12:49 +08:00
name string
carbon Carbon
want string
2021-02-18 14:32:31 +08:00
}{
2024-04-09 20:12:49 +08:00
{
name: "case1",
carbon: Parse(""),
want: "",
},
{
name: "case2",
carbon: Parse("xxx"),
want: "",
},
{
name: "case3",
carbon: Parse("2020-01-01 00:00:00"),
want: "2020-01-01 00:00:00",
},
{
name: "case4",
carbon: Parse("2020-01-31 23:59:59"),
want: "2020-01-01 00:00:00",
},
{
name: "case5",
carbon: Parse("2020-02-01 13:14:15"),
want: "2020-01-01 00:00:00",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.StartOfYear().ToDateTimeString(), "StartOfYear()")
})
2021-02-18 14:32:31 +08:00
}
}
func TestCarbon_EndOfYear(t *testing.T) {
tests := []struct {
2024-04-09 20:12:49 +08:00
name string
carbon Carbon
want string
2021-02-18 14:32:31 +08:00
}{
2024-04-09 20:12:49 +08:00
{
name: "case1",
carbon: Parse(""),
want: "",
},
{
name: "case2",
carbon: Parse("xxx"),
want: "",
},
{
name: "case3",
carbon: Parse("2020-01-01 00:00:00"),
want: "2020-12-31 23:59:59",
},
{
name: "case4",
carbon: Parse("2020-01-31 23:59:59"),
want: "2020-12-31 23:59:59",
},
{
name: "case5",
carbon: Parse("2020-02-01 13:14:15"),
want: "2020-12-31 23:59:59",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.EndOfYear().ToDateTimeString(), "EndOfYear()")
})
2021-02-18 14:32:31 +08:00
}
}
func TestCarbon_StartOfQuarter(t *testing.T) {
tests := []struct {
2024-04-09 20:12:49 +08:00
name string
carbon Carbon
want string
}{
2024-04-09 20:12:49 +08:00
{
name: "case1",
carbon: Parse(""),
want: "",
},
{
name: "case2",
carbon: Parse("xxx"),
want: "",
},
{
name: "case3",
carbon: Parse("2020-01-15 00:00:00"),
want: "2020-01-01 00:00:00",
},
{
name: "case4",
carbon: Parse("2020-02-15 00:00:00"),
want: "2020-01-01 00:00:00",
},
{
name: "case5",
carbon: Parse("2020-03-15 00:00:00"),
want: "2020-01-01 00:00:00",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.StartOfQuarter().ToDateTimeString(), "StartOfQuarter()")
})
}
}
func TestCarbon_EndOfQuarter(t *testing.T) {
tests := []struct {
2024-04-09 20:12:49 +08:00
name string
carbon Carbon
want string
}{
2024-04-09 20:12:49 +08:00
{
name: "case1",
carbon: Parse(""),
want: "",
},
{
name: "case2",
carbon: Parse("xxx"),
want: "",
},
{
name: "case3",
carbon: Parse("2020-01-15 00:00:00"),
want: "2020-03-31 23:59:59",
},
{
name: "case4",
carbon: Parse("2020-02-15 00:00:00"),
want: "2020-03-31 23:59:59",
},
{
name: "case5",
carbon: Parse("2020-03-15 00:00:00"),
want: "2020-03-31 23:59:59",
},
{
name: "case6",
carbon: Parse("2020-04-15 23:59:59"),
want: "2020-06-30 23:59:59",
},
{
name: "case7",
carbon: Parse("2020-05-15 23:59:59"),
want: "2020-06-30 23:59:59",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.EndOfQuarter().ToDateTimeString(), "EndOfQuarter()")
})
}
}
2021-02-18 14:32:31 +08:00
func TestCarbon_StartOfMonth(t *testing.T) {
tests := []struct {
2024-04-09 20:12:49 +08:00
name string
carbon Carbon
want string
2021-02-18 14:32:31 +08:00
}{
2024-04-09 20:12:49 +08:00
{
name: "case1",
carbon: Parse(""),
want: "",
},
{
name: "case2",
carbon: Parse("xxx"),
want: "",
},
{
name: "case3",
carbon: Parse("2020-01-15 00:00:00"),
want: "2020-01-01 00:00:00",
},
{
name: "case4",
carbon: Parse("2020-02-15 00:00:00"),
want: "2020-02-01 00:00:00",
},
{
name: "case5",
carbon: Parse("2020-03-15 00:00:00"),
want: "2020-03-01 00:00:00",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.StartOfMonth().ToDateTimeString(), "StartOfMonth()")
})
2021-02-18 14:32:31 +08:00
}
}
func TestCarbon_EndOfMonth(t *testing.T) {
tests := []struct {
2024-04-09 20:12:49 +08:00
name string
carbon Carbon
want string
2021-02-18 14:32:31 +08:00
}{
2024-04-09 20:12:49 +08:00
{
name: "case1",
carbon: Parse(""),
want: "",
},
{
name: "case2",
carbon: Parse("xxx"),
want: "",
},
{
name: "case3",
carbon: Parse("2020-01-15 00:00:00"),
want: "2020-01-31 23:59:59",
},
{
name: "case4",
carbon: Parse("2020-02-15 00:00:00"),
want: "2020-02-29 23:59:59",
},
{
name: "case5",
carbon: Parse("2020-03-15 00:00:00"),
want: "2020-03-31 23:59:59",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.EndOfMonth().ToDateTimeString(), "EndOfMonth()")
})
2021-02-18 14:32:31 +08:00
}
}
func TestCarbon_StartOfWeek(t *testing.T) {
tests := []struct {
2024-04-09 20:12:49 +08:00
name string
carbon Carbon
want string
2021-02-18 14:32:31 +08:00
}{
2024-04-09 20:12:49 +08:00
{
name: "case1",
carbon: Parse(""),
want: "",
},
{
name: "case2",
carbon: Parse("xxx"),
want: "",
},
{
name: "case3",
carbon: Parse("2021-06-13"),
want: "2021-06-13 00:00:00",
},
{
name: "case4",
carbon: Parse("2021-06-14"),
want: "2021-06-13 00:00:00",
},
{
name: "case5",
carbon: Parse("2021-06-18"),
want: "2021-06-13 00:00:00",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.StartOfWeek().ToDateTimeString(), "StartOfWeek()")
})
2021-02-18 14:32:31 +08:00
}
}
func TestCarbon_EndOfWeek(t *testing.T) {
tests := []struct {
2024-04-09 20:12:49 +08:00
name string
carbon Carbon
want string
2021-02-18 14:32:31 +08:00
}{
2024-04-09 20:12:49 +08:00
{
name: "case1",
carbon: Parse(""),
want: "",
},
{
name: "case2",
carbon: Parse("xxx"),
want: "",
},
{
name: "case3",
carbon: Parse("2021-06-13"),
want: "2021-06-19 23:59:59",
},
{
name: "case4",
carbon: Parse("2021-06-14"),
want: "2021-06-19 23:59:59",
},
{
name: "case5",
carbon: Parse("2021-06-18"),
want: "2021-06-19 23:59:59",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.EndOfWeek().ToDateTimeString(), "EndOfWeek()")
})
2021-02-18 14:32:31 +08:00
}
}
func TestCarbon_StartOfDay(t *testing.T) {
tests := []struct {
2024-04-09 20:12:49 +08:00
name string
carbon Carbon
want string
2021-02-18 14:32:31 +08:00
}{
2024-04-09 20:12:49 +08:00
{
name: "case1",
carbon: Parse(""),
want: "",
},
{
name: "case2",
carbon: Parse("xxx"),
want: "",
},
{
name: "case3",
carbon: Parse("2020-01-15 00:00:00"),
want: "2020-01-15 00:00:00",
},
{
name: "case4",
carbon: Parse("2020-02-15 00:00:00"),
want: "2020-02-15 00:00:00",
},
{
name: "case5",
carbon: Parse("2020-03-15 00:00:00"),
want: "2020-03-15 00:00:00",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.StartOfDay().ToDateTimeString(), "StartOfDay()")
})
2021-02-18 14:32:31 +08:00
}
}
func TestCarbon_EndOfDay(t *testing.T) {
tests := []struct {
2024-04-09 20:12:49 +08:00
name string
carbon Carbon
want string
2021-02-18 14:32:31 +08:00
}{
2024-04-09 20:12:49 +08:00
{
name: "case1",
carbon: Parse(""),
want: "",
},
{
name: "case2",
carbon: Parse("xxx"),
want: "",
},
{
name: "case3",
carbon: Parse("2020-01-15 00:00:00"),
want: "2020-01-15 23:59:59",
},
{
name: "case4",
carbon: Parse("2020-02-15 00:00:00"),
want: "2020-02-15 23:59:59",
},
{
name: "case5",
carbon: Parse("2020-03-15 00:00:00"),
want: "2020-03-15 23:59:59",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.EndOfDay().ToDateTimeString(), "EndOfDay()")
})
2021-02-18 14:32:31 +08:00
}
}
func TestCarbon_StartOfHour(t *testing.T) {
tests := []struct {
2024-04-09 20:12:49 +08:00
name string
carbon Carbon
want string
2021-02-18 14:32:31 +08:00
}{
2024-04-09 20:12:49 +08:00
{
name: "case1",
carbon: Parse(""),
want: "",
},
{
name: "case2",
carbon: Parse("xxx"),
want: "",
},
{
name: "case3",
carbon: Parse("2020-01-15 00:00:00"),
want: "2020-01-15 00:00:00",
},
{
name: "case4",
carbon: Parse("2020-02-15 00:00:00"),
want: "2020-02-15 00:00:00",
},
{
name: "case5",
carbon: Parse("2020-03-15 00:00:00"),
want: "2020-03-15 00:00:00",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.StartOfHour().ToDateTimeString(), "StartOfHour()")
})
2021-02-18 14:32:31 +08:00
}
}
func TestCarbon_EndOfHour(t *testing.T) {
tests := []struct {
2024-04-09 20:12:49 +08:00
name string
carbon Carbon
want string
2021-02-18 14:32:31 +08:00
}{
2024-04-09 20:12:49 +08:00
{
name: "case1",
carbon: Parse(""),
want: "",
},
{
name: "case2",
carbon: Parse("xxx"),
want: "",
},
{
name: "case3",
carbon: Parse(""),
want: "",
},
{
name: "case4",
carbon: Parse("xxx"),
want: "",
},
{
name: "case5",
carbon: Parse("2020-01-15 00:00:00"),
want: "2020-01-15 00:59:59",
},
{
name: "case6",
carbon: Parse("2020-02-15 00:00:00"),
want: "2020-02-15 00:59:59",
},
{
name: "case7",
carbon: Parse("2020-03-15 00:00:00"),
want: "2020-03-15 00:59:59",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.EndOfHour().ToDateTimeString(), "EndOfHour()")
})
2021-02-18 14:32:31 +08:00
}
}
func TestCarbon_StartOfMinute(t *testing.T) {
tests := []struct {
2024-04-09 20:12:49 +08:00
name string
carbon Carbon
want string
2021-02-18 14:32:31 +08:00
}{
2024-04-09 20:12:49 +08:00
{
name: "case1",
carbon: Parse(""),
want: "",
},
{
name: "case2",
carbon: Parse("xxx"),
want: "",
},
{
name: "case3",
carbon: Parse(""),
want: "",
},
{
name: "case4",
carbon: Parse("xxx"),
want: "",
},
{
name: "case5",
carbon: Parse("2020-01-15 00:00:00"),
want: "2020-01-15 00:00:00",
},
{
name: "case6",
carbon: Parse("2020-02-15 00:00:00"),
want: "2020-02-15 00:00:00",
},
{
name: "case7",
carbon: Parse("2020-03-15 00:00:00"),
want: "2020-03-15 00:00:00",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.StartOfMinute().ToDateTimeString(), "StartOfMinute()")
})
2021-02-18 14:32:31 +08:00
}
}
func TestCarbon_EndOfMinute(t *testing.T) {
tests := []struct {
2024-04-09 20:12:49 +08:00
name string
carbon Carbon
want string
2021-02-18 14:32:31 +08:00
}{
2024-04-09 20:12:49 +08:00
{
name: "case1",
carbon: Parse(""),
want: "",
},
{
name: "case2",
carbon: Parse("xxx"),
want: "",
},
{
name: "case3",
carbon: Parse("2020-01-15 00:00:00"),
want: "2020-01-15 00:00:59",
},
{
name: "case4",
carbon: Parse("2020-02-15 00:00:00"),
want: "2020-02-15 00:00:59",
},
{
name: "case5",
carbon: Parse("2020-03-15 00:00:00"),
want: "2020-03-15 00:00:59",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.EndOfMinute().ToDateTimeString(), "EndOfMinute()")
})
2021-02-18 14:32:31 +08:00
}
}
func TestCarbon_StartOfSecond(t *testing.T) {
tests := []struct {
2024-04-09 20:12:49 +08:00
name string
carbon Carbon
want string
2021-02-18 14:32:31 +08:00
}{
2024-04-09 20:12:49 +08:00
{
name: "case1",
carbon: Parse(""),
want: "",
},
{
name: "case2",
carbon: Parse("xxx"),
want: "",
},
{
name: "case3",
carbon: Parse("2020-01-15 00:00:00.999999999", PRC),
want: "2020-01-15 00:00:00 +0800 CST",
},
{
name: "case4",
carbon: Parse("2020-02-15 00:00:00.999999999", PRC),
want: "2020-02-15 00:00:00 +0800 CST",
},
{
name: "case5",
carbon: Parse("2020-03-15 00:00:00.999999999", PRC),
want: "2020-03-15 00:00:00 +0800 CST",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.StartOfSecond().ToString(), "StartOfSecond()")
})
2021-02-18 14:32:31 +08:00
}
}
func TestCarbon_EndOfSecond(t *testing.T) {
tests := []struct {
2024-04-09 20:12:49 +08:00
name string
carbon Carbon
want string
2021-02-18 14:32:31 +08:00
}{
2024-04-09 20:12:49 +08:00
{
name: "case1",
carbon: Parse(""),
want: "",
},
{
name: "case2",
carbon: Parse("xxx"),
want: "",
},
{
name: "case3",
carbon: Parse("2020-01-15 00:00:00.123", PRC),
want: "2020-01-15 00:00:00.999999999 +0800 CST",
},
{
name: "case4",
carbon: Parse("2020-02-15 00:00:00.123", PRC),
want: "2020-02-15 00:00:00.999999999 +0800 CST",
},
{
name: "case5",
carbon: Parse("2020-03-15 00:00:00.123", PRC),
want: "2020-03-15 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.EndOfSecond().ToString(), "EndOfSecond()")
})
2021-02-18 14:32:31 +08:00
}
}