2021-02-18 14:32:31 +08:00
|
|
|
package carbon
|
|
|
|
|
|
|
|
import (
|
2021-07-19 09:33:57 +08:00
|
|
|
"strconv"
|
2021-02-18 14:32:31 +08:00
|
|
|
"testing"
|
|
|
|
|
2021-07-19 09:33:57 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
2021-02-18 14:32:31 +08:00
|
|
|
|
2021-07-19 09:33:57 +08:00
|
|
|
func TestCarbon_SetTimezone(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
2021-02-18 14:32:31 +08:00
|
|
|
|
2021-07-19 09:33:57 +08:00
|
|
|
tests := []struct {
|
|
|
|
id int // 测试id
|
|
|
|
input string // 输入值
|
|
|
|
timezone string // 输入参数
|
2021-07-23 11:07:13 +08:00
|
|
|
expected string // 期望值
|
2021-07-19 09:33:57 +08:00
|
|
|
}{
|
|
|
|
{1, "2020-08-05 13:14:15", PRC, "2020-08-05 13:14:15"},
|
|
|
|
{2, "2020-08-05", Tokyo, "2020-08-05 00:00:00"},
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
|
2021-07-19 09:33:57 +08:00
|
|
|
for _, test := range tests {
|
|
|
|
c := SetTimezone(test.timezone).Parse(test.input)
|
|
|
|
assert.Nil(c.Error)
|
2021-07-23 11:07:13 +08:00
|
|
|
assert.Equal(test.expected, c.ToDateTimeString(), "Current test id is "+strconv.Itoa(test.id))
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-23 11:07:13 +08:00
|
|
|
func TestError_SetTimezone(t *testing.T) {
|
|
|
|
timezone := "xxx"
|
|
|
|
c := SetTimezone(timezone)
|
|
|
|
assert.Equal(t, invalidTimezoneError(timezone), c.Error, "Should catch an exception in SetTimezone()")
|
|
|
|
}
|
|
|
|
|
2021-07-19 09:33:57 +08:00
|
|
|
func TestCarbon_SetLanguage(t *testing.T) {
|
2021-02-18 14:32:31 +08:00
|
|
|
lang := NewLanguage()
|
|
|
|
resources := map[string]string{
|
2021-07-19 09:33:57 +08:00
|
|
|
"seasons": "spring|summer|autumn|winter",
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
2021-07-19 09:33:57 +08:00
|
|
|
|
2021-02-18 14:32:31 +08:00
|
|
|
if lang.SetLocale("en") == nil {
|
|
|
|
lang.SetResources(resources)
|
|
|
|
}
|
|
|
|
|
2021-07-19 09:33:57 +08:00
|
|
|
assert := assert.New(t)
|
2021-02-18 14:32:31 +08:00
|
|
|
|
2021-07-19 09:33:57 +08:00
|
|
|
tests := []struct {
|
2021-07-23 11:07:13 +08:00
|
|
|
id int // 测试id
|
|
|
|
input string // 输入值
|
|
|
|
expected string // 期望值
|
2021-02-18 14:32:31 +08:00
|
|
|
}{
|
2021-07-19 09:33:57 +08:00
|
|
|
{1, "", ""},
|
|
|
|
{2, "2020-08-05", "summer"},
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
|
2021-07-19 09:33:57 +08:00
|
|
|
for _, test := range tests {
|
2021-07-23 11:07:13 +08:00
|
|
|
c := Parse(test.input).SetLanguage(lang)
|
2021-07-19 09:33:57 +08:00
|
|
|
assert.Nil(c.Error)
|
2021-07-23 11:07:13 +08:00
|
|
|
assert.Equal(test.expected, c.Season(), "Current test id is "+strconv.Itoa(test.id))
|
2021-07-19 09:33:57 +08:00
|
|
|
}
|
2021-02-18 14:32:31 +08:00
|
|
|
|
2021-07-19 09:33:57 +08:00
|
|
|
for _, test := range tests {
|
2021-07-23 11:07:13 +08:00
|
|
|
c := SetLanguage(lang).Parse(test.input)
|
2021-07-19 09:33:57 +08:00
|
|
|
assert.Nil(c.Error)
|
2021-07-23 11:07:13 +08:00
|
|
|
assert.Equal(test.expected, c.Season(), "Current test id is "+strconv.Itoa(test.id))
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCarbon_SetYear(t *testing.T) {
|
2021-07-19 09:33:57 +08:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
tests := []struct {
|
2021-07-23 11:07:13 +08:00
|
|
|
id int // 测试id
|
|
|
|
input string // 输入值
|
|
|
|
year int // 输入参数
|
|
|
|
expected string // 期望值
|
2021-02-18 14:32:31 +08:00
|
|
|
}{
|
2021-07-19 09:33:57 +08:00
|
|
|
{1, "2020-01-01", 2019, "2019-01-01"},
|
|
|
|
{2, "2020-01-31", 2019, "2019-01-31"},
|
|
|
|
{3, "2020-02-01", 2019, "2019-02-01"},
|
|
|
|
{4, "2020-02-28", 2019, "2019-02-28"},
|
|
|
|
{5, "2020-02-29", 2019, "2019-03-01"},
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
|
2021-07-19 09:33:57 +08:00
|
|
|
for _, test := range tests {
|
|
|
|
c := Parse(test.input).SetYear(test.year)
|
|
|
|
assert.Nil(c.Error)
|
2021-07-23 11:07:13 +08:00
|
|
|
assert.Equal(test.expected, c.ToDateString(), "Current test id is "+strconv.Itoa(test.id))
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-23 11:07:13 +08:00
|
|
|
func TestError_SetYear(t *testing.T) {
|
|
|
|
value, year := "2020-08-50", 2020
|
|
|
|
c := Parse(value).SetYear(year)
|
|
|
|
assert.Equal(t, invalidValueError(value), c.Error, "Should catch an exception in SetYear()")
|
|
|
|
}
|
|
|
|
|
2021-02-18 14:32:31 +08:00
|
|
|
func TestCarbon_SetMonth(t *testing.T) {
|
2021-07-19 09:33:57 +08:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
tests := []struct {
|
2021-07-23 11:07:13 +08:00
|
|
|
id int // 测试id
|
|
|
|
input string // 输入值
|
|
|
|
month int // 输入参数
|
|
|
|
expected string // 期望值
|
2021-02-18 14:32:31 +08:00
|
|
|
}{
|
2021-07-19 09:33:57 +08:00
|
|
|
{1, "2020-01-01", 2, "2020-02-01"},
|
|
|
|
{2, "2020-01-30", 2, "2020-03-01"},
|
|
|
|
{3, "2020-01-31", 2, "2020-03-02"},
|
|
|
|
{4, "2020-08-05", 2, "2020-02-05"},
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
|
2021-07-19 09:33:57 +08:00
|
|
|
for _, test := range tests {
|
|
|
|
c := Parse(test.input).SetMonth(test.month)
|
|
|
|
assert.Nil(c.Error)
|
2021-07-23 11:07:13 +08:00
|
|
|
assert.Equal(test.expected, c.ToDateString(), "Current test id is "+strconv.Itoa(test.id))
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-23 11:07:13 +08:00
|
|
|
func TestError_SetMonth(t *testing.T) {
|
|
|
|
value, month := "2020-08-50", 12
|
|
|
|
c1 := Parse(value).SetMonth(month)
|
|
|
|
assert.Equal(t, invalidValueError(value), c1.Error, "Should catch an exception in SetYear()")
|
|
|
|
}
|
|
|
|
|
2021-02-18 14:32:31 +08:00
|
|
|
func TestCarbon_SetDay(t *testing.T) {
|
2021-07-19 09:33:57 +08:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
tests := []struct {
|
2021-07-23 11:07:13 +08:00
|
|
|
id int // 测试id
|
|
|
|
input string // 输入值
|
|
|
|
day int // 输入参数
|
|
|
|
expected string // 期望值
|
2021-02-18 14:32:31 +08:00
|
|
|
}{
|
2021-07-19 09:33:57 +08:00
|
|
|
{1, "2020-01-01", 31, "2020-01-31"},
|
|
|
|
{2, "2020-02-01", 31, "2020-03-02"},
|
|
|
|
{3, "2020-02-28", 31, "2020-03-02"},
|
|
|
|
{4, "2020-02-29", 31, "2020-03-02"},
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
|
2021-07-19 09:33:57 +08:00
|
|
|
for _, test := range tests {
|
|
|
|
c := Parse(test.input).SetDay(test.day)
|
|
|
|
assert.Nil(c.Error)
|
2021-07-23 11:07:13 +08:00
|
|
|
assert.Equal(test.expected, c.ToDateString(), "Current test id is "+strconv.Itoa(test.id))
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-23 11:07:13 +08:00
|
|
|
func TestError_SetDay(t *testing.T) {
|
|
|
|
value, day := "2020-08-50", 30
|
|
|
|
c := Parse(value).SetDay(day)
|
|
|
|
assert.Equal(t, invalidValueError(value), c.Error, "Should catch an exception in SetYear()")
|
|
|
|
}
|
|
|
|
|
2021-02-18 14:32:31 +08:00
|
|
|
func TestCarbon_SetHour(t *testing.T) {
|
2021-07-19 09:33:57 +08:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
tests := []struct {
|
2021-07-23 11:07:13 +08:00
|
|
|
id int // 测试id
|
|
|
|
input string // 输入值
|
|
|
|
hour int // 输入参数
|
|
|
|
expected string // 期望值
|
2021-02-18 14:32:31 +08:00
|
|
|
}{
|
2021-07-19 09:33:57 +08:00
|
|
|
{1, "2020-08-05 13:14:15", 10, "2020-08-05 10:14:15"},
|
|
|
|
{2, "2020-08-05 13:14:15", 24, "2020-08-06 00:14:15"},
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
|
2021-07-19 09:33:57 +08:00
|
|
|
for _, test := range tests {
|
|
|
|
c := Parse(test.input).SetHour(test.hour)
|
|
|
|
assert.Nil(c.Error)
|
2021-07-23 11:07:13 +08:00
|
|
|
assert.Equal(test.expected, c.ToDateTimeString(), "Current test id is "+strconv.Itoa(test.id))
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-23 11:07:13 +08:00
|
|
|
func TestError_SetHour(t *testing.T) {
|
|
|
|
value, hour := "2020-08-50", 12
|
|
|
|
c := Parse(value).SetHour(hour)
|
|
|
|
assert.Equal(t, invalidValueError(value), c.Error, "Should catch an exception in SetYear()")
|
|
|
|
}
|
|
|
|
|
2021-02-18 14:32:31 +08:00
|
|
|
func TestCarbon_SetMinute(t *testing.T) {
|
2021-07-19 09:33:57 +08:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
tests := []struct {
|
2021-07-23 11:07:13 +08:00
|
|
|
id int // 测试id
|
|
|
|
input string // 输入值
|
|
|
|
minute int // 输入参数
|
|
|
|
expected string // 期望值
|
2021-02-18 14:32:31 +08:00
|
|
|
}{
|
2021-07-19 09:33:57 +08:00
|
|
|
{1, "2020-08-05 13:14:15", 10, "2020-08-05 13:10:15"},
|
|
|
|
{2, "2020-08-05 13:14:15", 60, "2020-08-05 14:00:15"},
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
|
2021-07-19 09:33:57 +08:00
|
|
|
for _, test := range tests {
|
|
|
|
c := Parse(test.input).SetMinute(test.minute)
|
|
|
|
assert.Nil(c.Error)
|
2021-07-23 11:07:13 +08:00
|
|
|
assert.Equal(test.expected, c.ToDateTimeString(), "Current test id is "+strconv.Itoa(test.id))
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-23 11:07:13 +08:00
|
|
|
func TestError_SetMinute(t *testing.T) {
|
|
|
|
value, minute := "2020-08-50", 30
|
|
|
|
c := Parse(value).SetMinute(minute)
|
|
|
|
assert.Equal(t, invalidValueError(value), c.Error, "Should catch an exception in SetYear()")
|
|
|
|
}
|
|
|
|
|
2021-02-18 14:32:31 +08:00
|
|
|
func TestCarbon_SetSecond(t *testing.T) {
|
2021-07-19 09:33:57 +08:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
tests := []struct {
|
2021-07-23 11:07:13 +08:00
|
|
|
id int // 测试id
|
|
|
|
input string // 输入值
|
|
|
|
second int // 输入参数
|
|
|
|
expected int // 期望值
|
2021-07-19 09:33:57 +08:00
|
|
|
}{
|
|
|
|
{1, "2020-08-05 13:14:15", 10, 10},
|
|
|
|
{2, "2020-08-05 13:14:15", 59, 59},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
c := Parse(test.input).SetSecond(test.second)
|
|
|
|
assert.Nil(c.Error)
|
2021-07-23 11:07:13 +08:00
|
|
|
assert.Equal(test.expected, c.Second(), "Current test id is "+strconv.Itoa(test.id))
|
2021-07-19 09:33:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-23 11:07:13 +08:00
|
|
|
func TestError_SetSecond(t *testing.T) {
|
|
|
|
value, second := "2020-08-50", 30
|
|
|
|
c := Parse(value).SetSecond(second)
|
|
|
|
assert.Equal(t, invalidValueError(value), c.Error, "Should catch an exception in SetYear()")
|
|
|
|
}
|
|
|
|
|
2021-07-19 09:33:57 +08:00
|
|
|
func TestCarbon_SetMillisecond(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
tests := []struct {
|
|
|
|
id int // 测试id
|
|
|
|
input string // 输入值
|
|
|
|
millisecond int // 输入参数
|
2021-07-23 11:07:13 +08:00
|
|
|
expected int // 期望值
|
2021-02-18 14:32:31 +08:00
|
|
|
}{
|
2021-07-19 09:33:57 +08:00
|
|
|
{1, "2020-08-05 13:14:15", 100, 100},
|
|
|
|
{2, "2020-08-05 13:14:15", 999, 999},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
c := Parse(test.input).SetMillisecond(test.millisecond)
|
|
|
|
assert.Nil(c.Error)
|
2021-07-23 11:07:13 +08:00
|
|
|
assert.Equal(test.expected, c.Millisecond(), "Current test id is "+strconv.Itoa(test.id))
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
2021-07-19 09:33:57 +08:00
|
|
|
}
|
|
|
|
|
2021-07-23 11:07:13 +08:00
|
|
|
func TestError_SetMillisecond(t *testing.T) {
|
|
|
|
value, millisecond := "2020-08-50", 100
|
|
|
|
c := Parse(value).SetMillisecond(millisecond)
|
|
|
|
assert.Equal(t, invalidValueError(value), c.Error, "Should catch an exception in SetYear()")
|
|
|
|
}
|
|
|
|
|
2021-07-19 09:33:57 +08:00
|
|
|
func TestCarbon_SetMicrosecond(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
2021-02-18 14:32:31 +08:00
|
|
|
|
2021-07-19 09:33:57 +08:00
|
|
|
tests := []struct {
|
|
|
|
id int // 测试id
|
|
|
|
input string // 输入值
|
|
|
|
microsecond int // 输入参数
|
2021-07-23 11:07:13 +08:00
|
|
|
expected int // 期望值
|
2021-07-19 09:33:57 +08:00
|
|
|
}{
|
|
|
|
{1, "2020-08-05 13:14:15", 100000, 100000},
|
|
|
|
{2, "2020-08-05 13:14:15", 999999, 999999},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
c := Parse(test.input).SetMicrosecond(test.microsecond)
|
|
|
|
assert.Nil(c.Error)
|
2021-07-23 11:07:13 +08:00
|
|
|
assert.Equal(test.expected, c.Microsecond(), "Current test id is "+strconv.Itoa(test.id))
|
2021-07-19 09:33:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-23 11:07:13 +08:00
|
|
|
func TestError_SetMicrosecond(t *testing.T) {
|
|
|
|
value, microsecond := "2020-08-50", 100000
|
|
|
|
c := Parse(value).SetMicrosecond(microsecond)
|
|
|
|
assert.Equal(t, invalidValueError(value), c.Error, "Should catch an exception in SetYear()")
|
|
|
|
}
|
|
|
|
|
2021-07-19 09:33:57 +08:00
|
|
|
func TestCarbon_SetNanosecond(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
id int // 测试id
|
|
|
|
input string // 输入值
|
|
|
|
nanosecond int // 输入参数
|
2021-07-23 11:07:13 +08:00
|
|
|
expected int // 期望值
|
2021-07-19 09:33:57 +08:00
|
|
|
}{
|
|
|
|
{1, "2020-08-05 13:14:15", 100000000, 100000000},
|
|
|
|
{2, "2020-08-05 13:14:15", 999999999, 999999999},
|
|
|
|
}
|
2021-02-18 14:32:31 +08:00
|
|
|
|
2021-07-19 09:33:57 +08:00
|
|
|
for _, test := range tests {
|
|
|
|
c := Parse(test.input).SetNanosecond(test.nanosecond)
|
|
|
|
assert.Nil(c.Error)
|
2021-07-23 11:07:13 +08:00
|
|
|
assert.Equal(test.expected, c.Nanosecond(), "Current test id is "+strconv.Itoa(test.id))
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
}
|
2021-07-23 11:07:13 +08:00
|
|
|
|
|
|
|
func TestError_SetNanosecond(t *testing.T) {
|
|
|
|
value, nanosecond := "2020-08-50", 100000000
|
|
|
|
c := Parse(value).SetNanosecond(nanosecond)
|
|
|
|
assert.Equal(t, invalidValueError(value), c.Error, "Should catch an exception in SetYear()")
|
|
|
|
}
|