carbon/carbon_test.go

90 lines
2.0 KiB
Go
Raw Normal View History

2020-09-08 12:18:07 +08:00
package carbon
import (
"testing"
"time"
)
2020-10-10 16:11:22 +08:00
func TestCarbon_Now(t *testing.T) {
expected := time.Now().Format(DateTimeFormat)
output := Now().Time.Format(DateTimeFormat)
if expected != output {
t.Fatalf("Expected %s, but got %s", expected, output)
}
2020-10-22 09:34:10 +08:00
output = SetTimezone(PRC).Now().Time.Format(DateTimeFormat)
2020-10-10 16:11:22 +08:00
if expected != output {
t.Fatalf("Expected %s, but got %s", expected, output)
}
}
func TestCarbon_Yesterday(t *testing.T) {
expected := time.Now().AddDate(0, 0, -1).Format(DateTimeFormat)
output := Yesterday().Time.Format(DateTimeFormat)
if expected != output {
t.Fatalf("Expected %s, but got %s", expected, output)
}
output = SetTimezone(PRC).Parse("2020-08-05").Yesterday().Time.Format(DateFormat)
2020-10-10 16:11:22 +08:00
if "2020-08-04" != output {
2020-10-10 16:11:22 +08:00
t.Fatalf("Expected %s, but got %s", expected, output)
}
}
func TestCarbon_Tomorrow(t *testing.T) {
expected := time.Now().AddDate(0, 0, 1).Format(DateTimeFormat)
output := Tomorrow().Time.Format(DateTimeFormat)
if expected != output {
2021-02-18 14:32:31 +08:00
t.Errorf("Expected %s, but got %s", expected, output)
2020-10-10 16:11:22 +08:00
}
output = SetTimezone(PRC).Parse("2020-08-05").Tomorrow().Time.Format(DateFormat)
2020-10-10 16:11:22 +08:00
if "2020-08-06" != output {
2021-02-18 14:32:31 +08:00
t.Errorf("Expected %s, but got %s", expected, output)
2020-10-01 00:22:11 +08:00
}
}
2021-02-23 09:32:55 +08:00
func TestCarbon_Time2Carbon(t *testing.T) {
Tests := []struct {
time time.Time // // 输入参数
output string // 期望输出值
}{
{time.Now(), time.Now().Format(DateTimeFormat)},
}
for _, v := range Tests {
output := Time2Carbon(v.time).ToDateTimeString()
if output != v.output {
t.Errorf("Expected %s, but got %s", v.output, output)
}
}
}
func TestCarbon_Carbon2Time(t *testing.T) {
Tests := []struct {
input Carbon // 输入值
output string // 期望输出值
}{
{Parse("2020-08-05"), "2020-08-05"},
}
for _, v := range Tests {
output := v.input.Carbon2Time().Format("2006-01-02")
if output != v.output {
t.Errorf("Input %s, expected %s, but got %s\n", v.input.ToDateString(), v.output, output)
}
}
}