carbon/calendar_unit_test.go

245 lines
4.6 KiB
Go
Raw Normal View History

2024-01-22 17:08:38 +08:00
package carbon
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestCarbon_Lunar(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("xxx", PRC),
want: "",
},
{
2024-04-09 20:12:49 +08:00
name: "case2",
carbon: Parse("2024-01-18", PRC),
want: "2023-12-08 00:00:00",
},
{
2024-04-09 20:12:49 +08:00
name: "case3",
carbon: Parse("2024-01-21", PRC),
want: "2023-12-11 00:00:00",
},
{
2024-04-09 20:12:49 +08:00
name: "case4",
carbon: Parse("2024-01-24", PRC),
want: "2023-12-14 00:00:00",
},
}
2024-04-09 20:12:49 +08:00
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.Lunar().String(), "Lunar()")
})
}
2024-01-22 17:08:38 +08:00
}
func TestCreateFromLunar(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: CreateFromLunar(2023, 12, 11, 0, 0, 0, false),
want: "2024-01-21 00:00:00",
},
{
2024-04-09 20:12:49 +08:00
name: "case2",
carbon: CreateFromLunar(2023, 12, 8, 0, 0, 0, false),
want: "2024-01-18 00:00:00",
},
{
2024-04-09 20:12:49 +08:00
name: "case3",
carbon: CreateFromLunar(2023, 12, 14, 12, 0, 0, false),
want: "2024-01-24 12:00:00",
},
}
2024-04-09 20:12:49 +08:00
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.ToDateTimeString(), "CreateFromLunar()")
})
}
2024-01-22 17:08:38 +08:00
}
func TestCarbon_Julian(t *testing.T) {
tests := []struct {
2024-04-09 20:12:49 +08:00
name string
carbon Carbon
wantJD float64
wantMJD float64
}{
{
2024-04-09 20:12:49 +08:00
name: "case1",
carbon: Parse("xxx"),
wantJD: 0,
wantMJD: 0,
},
{
2024-04-09 20:12:49 +08:00
name: "case2",
carbon: Parse("2024-01-24 12:00:00"),
wantJD: 2460334,
wantMJD: 60333.5,
},
{
2024-04-09 20:12:49 +08:00
name: "case3",
carbon: CreateFromJulian(2460334),
wantJD: 2460334,
wantMJD: 60333.5,
},
{
2024-04-09 20:12:49 +08:00
name: "case4",
carbon: CreateFromJulian(60333.5),
wantJD: 2460334,
wantMJD: 60333.5,
},
}
2024-04-09 20:12:49 +08:00
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.wantJD, tt.carbon.Julian().JD(), "JD()")
assert.Equalf(t, tt.wantMJD, tt.carbon.Julian().MJD(), "MJD()")
})
}
}
2024-02-01 15:23:18 +08:00
func TestCarbon_CreateFromJulian(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: CreateFromJulian(2460334),
want: "2024-01-24 12:00:00",
},
{
2024-04-09 20:12:49 +08:00
name: "case2",
carbon: CreateFromJulian(60333.5),
want: "2024-01-24 12:00:00",
},
}
2024-04-09 20:12:49 +08:00
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.ToDateTimeString(), "CreateFromJulian()")
})
}
2024-01-22 17:08:38 +08:00
}
2024-02-01 11:21:52 +08:00
func TestCarbon_Persian(t *testing.T) {
tests := []struct {
2024-04-09 20:12:49 +08:00
name string
carbon Carbon
want string
2024-02-01 11:21:52 +08:00
}{
{
2024-04-09 20:12:49 +08:00
name: "case1",
carbon: Parse("xxx", PRC),
want: "",
2024-02-01 11:21:52 +08:00
},
{
2024-04-09 20:12:49 +08:00
name: "case2",
carbon: Parse("1800-01-01 00:00:00", PRC),
want: "1178-10-11 00:00:00",
2024-02-01 11:21:52 +08:00
},
{
2024-04-09 20:12:49 +08:00
name: "case3",
carbon: Parse("2020-08-05 13:14:15", PRC),
want: "1399-05-15 13:14:15",
2024-02-01 11:21:52 +08:00
},
{
2024-04-09 20:12:49 +08:00
name: "case4",
carbon: Parse("2024-01-01 00:00:00", PRC),
want: "1402-10-11 00:00:00",
2024-02-01 11:21:52 +08:00
},
}
2024-04-09 20:12:49 +08:00
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.Persian().String(), "Persian()")
2024-02-01 11:21:52 +08:00
})
}
}
2024-02-01 15:23:18 +08:00
func TestCarbon_CreateFromPersian(t *testing.T) {
2024-02-01 11:21:52 +08:00
tests := []struct {
2024-04-09 20:12:49 +08:00
name string
carbon Carbon
want string
2024-02-01 11:21:52 +08:00
}{
{
2024-04-09 20:12:49 +08:00
name: "case1",
carbon: CreateFromPersian(1178, 10, 11, 0, 0, 0),
want: "1800-01-01 00:00:00",
2024-02-01 11:21:52 +08:00
},
{
2024-04-09 20:12:49 +08:00
name: "case2",
carbon: CreateFromPersian(1402, 10, 11, 0, 0, 0),
want: "2024-01-01 00:00:00",
2024-02-01 11:21:52 +08:00
},
{
2024-04-09 20:12:49 +08:00
name: "case3",
carbon: CreateFromPersian(1403, 5, 15, 12, 0, 0),
want: "2024-08-05 12:00:00",
2024-02-01 11:21:52 +08:00
},
}
2024-04-09 20:12:49 +08:00
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.ToDateTimeString(), "CreateFromPersian()")
2024-02-01 11:21:52 +08:00
})
}
2024-01-22 17:08:38 +08:00
}
2024-10-11 23:38:12 +08:00
2024-11-25 16:14:41 +08:00
// https://github.com/dromara/carbon/issues/246
2024-10-11 23:38:12 +08:00
func TestCarbon_Issue246(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want string
}{
{
name: "case1",
carbon: Parse("2024-09-21 00:00:00", PRC),
want: "2024-08-19 00:00:00",
},
{
name: "case2",
carbon: Parse("2024-09-21 23:50:00", PRC),
want: "2024-08-19 23:50:00",
},
{
name: "case3",
carbon: Parse("2024-09-21 23:54:00", PRC),
want: "2024-08-19 23:54:00",
},
{
name: "case4",
carbon: Parse("2024-09-21 23:55:00", PRC),
want: "2024-08-19 23:55:00",
},
{
name: "case5",
carbon: Parse("2024-09-21 23:59:00", PRC),
want: "2024-08-19 23:59:00",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.Lunar().String(), "Lunar()")
})
}
}