1 Star 0 Fork 63

jangocheng/carbon

forked from dromara/carbon 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
extremum_unit_test.go 1.82 KB
一键复制 编辑 原始数据 按行查看 历史
gouguoyin 提交于 2024-01-12 10:57 . v2.3.5
package carbon
import (
"github.com/stretchr/testify/assert"
"strconv"
"testing"
)
func TestCarbon_Closest(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input1 string
input2 string
input3 string
expected string
}{
{"", "2023-03-28", "2023-04-16", "2023-03-28"},
{"2023-04-01", "", "2023-04-16", "2023-04-16"},
{"2023-04-01", "2023-03-28", "", "2023-03-28"},
{"2023-04-01", "", "", ""},
{"2023-04-01", "2023-03-28", "2023-03-28", "2023-03-28"},
{"2023-04-01", "2023-03-28", "2023-04-16", "2023-03-28"},
}
for index, test := range tests {
c := Parse(test.input1).Closest(Parse(test.input2), Parse(test.input3))
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_Farthest(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input1 string
input2 string
input3 string
expected string
}{
{"", "2023-03-28", "2023-04-16", "2023-04-16"},
{"2023-04-01", "", "2023-04-16", "2023-04-16"},
{"2023-04-01", "2023-03-28", "", "2023-03-28"},
{"2023-04-01", "", "", ""},
{"2023-04-01", "2023-03-28", "2023-03-28", "2023-03-28"},
{"2023-04-01", "2023-03-28", "2023-04-16", "2023-04-16"},
{"2023-04-01", "2023-04-05", "2023-04-02", "2023-04-05"},
}
for index, test := range tests {
c := Parse(test.input1).Farthest(Parse(test.input2), Parse(test.input3))
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_Max(t *testing.T) {
now := Now()
max := Max(now.SubDay(), now, now.AddDay())
assert.Equal(t, now.AddDay().Timestamp(), max.Timestamp())
}
func TestCarbon_Min(t *testing.T) {
now := Now()
min := Min(now, now.SubDay(), now.AddDay())
assert.Equal(t, now.SubDay().Timestamp(), min.Timestamp())
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/jangocheng/carbon.git
git@gitee.com:jangocheng/carbon.git
jangocheng
carbon
carbon
master

搜索帮助