32 Star 408 Fork 63

GVPdromara/carbon

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
extremum_unit_test.go 2.88 KB
一键复制 编辑 原始数据 按行查看 历史
gouguoyin 提交于 2024-04-09 20:12 . 统一单元测试格式
package carbon
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestCarbon_Closest(t *testing.T) {
tests := []struct {
name string
carbon1 Carbon
carbon2 Carbon
carbon3 Carbon
want string
}{
{
name: "case1",
carbon1: Parse(""),
carbon2: Parse("2023-03-28"),
carbon3: Parse("2023-04-16"),
want: "2023-03-28",
},
{
name: "case2",
carbon1: Parse("2023-04-01"),
carbon2: Parse(""),
carbon3: Parse("2023-04-16"),
want: "2023-04-16",
},
{
name: "case3",
carbon1: Parse("2023-04-01"),
carbon2: Parse("2023-03-28"),
carbon3: Parse(""),
want: "2023-03-28",
},
{
name: "case4",
carbon1: Parse("2023-04-01"),
carbon2: Parse(""),
carbon3: Parse(""),
want: "",
},
{
name: "case5",
carbon1: Parse("2023-04-01"),
carbon2: Parse("2023-03-28"),
carbon3: Parse("2023-03-28"),
want: "2023-03-28",
},
{
name: "case6",
carbon1: Parse("2023-04-01"),
carbon2: Parse("2023-03-28"),
carbon3: Parse("2023-04-16"),
want: "2023-03-28",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon1.Closest(tt.carbon2, tt.carbon3).ToDateString(), "Closest()")
})
}
}
func TestCarbon_Farthest(t *testing.T) {
tests := []struct {
name string
carbon1 Carbon
carbon2 Carbon
carbon3 Carbon
want string
}{
{
name: "case1",
carbon1: Parse(""),
carbon2: Parse("2023-03-28"),
carbon3: Parse("2023-04-16"),
want: "2023-04-16",
},
{
name: "case2",
carbon1: Parse("2023-04-01"),
carbon2: Parse(""),
carbon3: Parse("2023-04-16"),
want: "2023-04-16",
},
{
name: "case3",
carbon1: Parse("2023-04-01"),
carbon2: Parse("2023-03-28"),
carbon3: Parse(""),
want: "2023-03-28",
},
{
name: "case4",
carbon1: Parse("2023-04-01"),
carbon2: Parse(""),
carbon3: Parse(""),
want: "",
},
{
name: "case5",
carbon1: Parse("2023-04-01"),
carbon2: Parse("2023-03-28"),
carbon3: Parse("2023-03-28"),
want: "2023-03-28",
},
{
name: "case6",
carbon1: Parse("2023-04-01"),
carbon2: Parse("2023-03-28"),
carbon3: Parse("2023-03-28"),
want: "2023-03-28",
},
{
name: "case7",
carbon1: Parse("2023-04-01"),
carbon2: Parse("2023-04-05"),
carbon3: Parse("2023-04-02"),
want: "2023-04-05",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon1.Farthest(tt.carbon2, tt.carbon3).ToDateString(), "Farthest()")
})
}
}
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/dromara/carbon.git
git@gitee.com:dromara/carbon.git
dromara
carbon
carbon
master

搜索帮助