1 Star 0 Fork 70

vdebug/carbon

forked from dromara/carbon 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
calendar.lunar_test.go 17.77 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
package carbon
import (
"fmt"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
)
func TestLunar_Animal(t *testing.T) {
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
output string // 期望输出值
}{
{1, "", ""},
{2, "0", ""},
{3, "0000-00-00", ""},
{4, "00:00:00", ""},
{5, "0000-00-00 00:00:00", ""},
{6, "2020-05-01", "鼠"},
{7, "2020-08-05", "鼠"},
{8, "2021-07-07", "牛"},
{9, "2010-08-05", "虎"},
{10, "2011-08-05", "兔"},
{11, "2012-08-05", "龙"},
{12, "2013-08-05", "蛇"},
{13, "2014-08-05", "马"},
{14, "2015-08-05", "羊"},
{15, "2016-08-05", "猴"},
{16, "2017-08-05", "鸡"},
{17, "2018-08-05", "狗"},
{18, "2019-08-05", "猪"},
{19, "2020-04-23", "鼠"},
{20, "2020-08-05", "鼠"},
{21, "2021-05-12", "牛"},
{22, "2021-08-05", "牛"},
}
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().Animal(), test.output, "Current test id is "+strconv.Itoa(test.id))
}
}
func TestLunar_Festival(t *testing.T) {
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
output string // 期望输出值
}{
{1, "", ""},
{2, "0", ""},
{3, "0000-00-00", ""},
{4, "00:00:00", ""},
{5, "0000-00-00 00:00:00", ""},
{6, "2020-04-23", ""},
{7, "2021-02-12", "春节"},
{8, "2021-02-26", "元宵节"},
{9, "2021-05-12", ""},
{10, "2021-06-14", "端午节"},
{11, "2021-08-14", "七夕节"},
{12, "2021-08-22", "中元节"},
{13, "2021-09-21", "中秋节"},
{14, "2021-10-14", "重阳节"},
{15, "2021-10-14", "重阳节"},
{16, "2021-11-05", "寒衣节"},
{17, "2021-11-19", "下元节"},
{18, "2022-01-10", "腊八节"},
{19, "2022-01-25", "小年"},
}
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().Festival(), test.output, "Current test id is "+strconv.Itoa(test.id))
}
}
func TestLunar_Year(t *testing.T) {
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
output int // 期望输出值
}{
{1, "", 0},
{2, "0", 0},
{3, "0000-00-00", 0},
{4, "00:00:00", 0},
{5, "0000-00-00 00:00:00", 0},
{6, "2020-04-23", 2020},
{7, "2020-05-01", 2020},
{8, "2020-08-05", 2020},
{9, "2021-01-01", 2020},
{10, "2021-05-12", 2021},
{11, "2021-07-07", 2021},
}
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().Year(), test.output, "Current test id is "+strconv.Itoa(test.id))
}
}
func TestLunar_Month(t *testing.T) {
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
output int // 期望输出值
}{
{1, "", 0},
{2, "0", 0},
{3, "0000-00-00", 0},
{4, "00:00:00", 0},
{5, "0000-00-00 00:00:00", 0},
{6, "2020-04-23", 4},
{7, "2020-05-01", 4},
{8, "2020-08-05", 6},
{9, "2021-07-07", 5},
}
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().Month(), test.output, "Current test id is "+strconv.Itoa(test.id))
}
}
func TestLunar_Day(t *testing.T) {
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
output int // 期望输出值
}{
{1, "", 0},
{2, "0", 0},
{3, "0000-00-00", 0},
{4, "00:00:00", 0},
{5, "0000-00-00 00:00:00", 0},
{6, "2020-04-23", 1},
{7, "2020-05-01", 9},
{8, "2020-08-05", 16},
{9, "2021-07-07", 28},
}
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().Day(), test.output, "Current test id is "+strconv.Itoa(test.id))
}
}
func TestLunar_LeapMonth(t *testing.T) {
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
output int // 期望输出值
}{
{1, "", 0},
{2, "0", 0},
{3, "0000-00-00", 0},
{4, "00:00:00", 0},
{5, "0000-00-00 00:00:00", 0},
{6, "2020-04-23", 4},
{7, "2020-05-01", 4},
{8, "2020-08-05", 4},
{9, "2021-07-07", 0},
}
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().LeapMonth(), test.output, "Current test id is "+strconv.Itoa(test.id))
}
}
func TestLunar_ToYearString(t *testing.T) {
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
output string // 期望输出值
}{
{1, "", ""},
{2, "0", ""},
{3, "0000-00-00", ""},
{4, "00:00:00", ""},
{5, "0000-00-00 00:00:00", ""},
{6, "2020-04-23", "二零二零"},
{7, "2020-05-01", "二零二零"},
{8, "2020-08-05", "二零二零"},
{9, "2021-01-01", "二零二零"},
{10, "2021-05-12", "二零二一"},
{11, "2021-07-07", "二零二一"},
}
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().ToYearString(), test.output, "Current test id is "+strconv.Itoa(test.id))
}
}
func TestLunar_ToMonthString(t *testing.T) {
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
output string // 期望输出值
}{
{1, "", ""},
{2, "0", ""},
{3, "0000-00-00", ""},
{4, "00:00:00", ""},
{5, "0000-00-00 00:00:00", ""},
{6, "2020-01-01", "腊"},
{7, "2020-02-01", "正"},
{8, "2020-03-01", "二"},
{9, "2020-04-01", "三"},
{10, "2020-04-23", "四"},
{11, "2020-05-01", "四"},
{12, "2020-06-01", "四"},
{13, "2020-07-01", "五"},
{14, "2020-07-07", "五"},
{15, "2020-08-01", "六"},
{16, "2020-09-01", "七"},
{17, "2020-10-01", "八"},
{18, "2020-11-01", "九"},
{19, "2020-12-01", "十"},
{20, "2021-01-01", "十一"},
{21, "2021-02-01", "腊"},
{22, "2021-05-12", "四"},
}
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().ToMonthString(), test.output, "Current test id is "+strconv.Itoa(test.id))
}
}
func TestLunar_ToDayString(t *testing.T) {
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
output string // 期望输出值
}{
{1, "", ""},
{2, "0", ""},
{3, "0000-00-00", ""},
{4, "00:00:00", ""},
{5, "0000-00-00 00:00:00", ""},
{1, "2020-01-01", "初七"},
{2, "2020-02-01", "初八"},
{3, "2020-03-01", "初八"},
{4, "2020-04-01", "初九"},
{5, "2020-04-23", "初一"},
{6, "2020-05-01", "初九"},
{7, "2020-06-01", "初十"},
{8, "2020-07-01", "十一"},
{9, "2020-08-01", "十二"},
{10, "2020-09-01", "十四"},
{11, "2020-10-01", "十五"},
{12, "2020-11-01", "十六"},
{13, "2020-12-01", "十七"},
{14, "2021-01-03", "二十"},
{15, "2021-04-11", "三十"},
}
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().ToDayString(), test.output, "Current test id is "+strconv.Itoa(test.id))
}
}
func TestLunar_String(t *testing.T) {
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
output string // 期望输出值
}{
{1, "", ""},
{2, "0", ""},
{3, "0000-00-00", ""},
{4, "00:00:00", ""},
{5, "0000-00-00 00:00:00", ""},
{6, "2020-01-01", "二零一九年腊月初七"},
{7, "2020-02-01", "二零二零年正月初八"},
{8, "2020-03-01", "二零二零年二月初八"},
{9, "2020-04-01", "二零二零年三月初九"},
{10, "2020-04-23", "二零二零年四月初一"},
{11, "2020-05-01", "二零二零年四月初九"},
{12, "2020-06-01", "二零二零年四月初十"},
{13, "2020-07-01", "二零二零年五月十一"},
{14, "2020-08-01", "二零二零年六月十二"},
{15, "2020-09-01", "二零二零年七月十四"},
{16, "2020-10-01", "二零二零年八月十五"},
{17, "2020-11-01", "二零二零年九月十六"},
{18, "2020-12-01", "二零二零年十月十七"},
}
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(fmt.Sprintf("%s", c.Lunar()), test.output, "Current test id is "+strconv.Itoa(test.id))
}
}
func TestLunar_ToString(t *testing.T) {
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
output string // 期望输出值
}{
{1, "", ""},
{2, "0", ""},
{3, "0000-00-00", ""},
{4, "00:00:00", ""},
{5, "0000-00-00 00:00:00", ""},
{6, "2020-01-01", "二零一九年腊月初七"},
{7, "2020-02-01", "二零二零年正月初八"},
{8, "2020-03-01", "二零二零年二月初八"},
{9, "2020-04-01", "二零二零年三月初九"},
{10, "2020-04-23", "二零二零年四月初一"},
{11, "2020-05-01", "二零二零年四月初九"},
{12, "2020-06-01", "二零二零年四月初十"},
{13, "2020-07-01", "二零二零年五月十一"},
{14, "2020-08-01", "二零二零年六月十二"},
{15, "2020-09-01", "二零二零年七月十四"},
{16, "2020-10-01", "二零二零年八月十五"},
{17, "2020-11-01", "二零二零年九月十六"},
{18, "2020-12-01", "二零二零年十月十七"},
}
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().ToString(), test.output, "Current test id is "+strconv.Itoa(test.id))
}
}
func TestLunar_IsLeapYear(t *testing.T) {
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出值
}{
{1, "", false},
{2, "0", false},
{3, "0000-00-00", false},
{4, "00:00:00", false},
{5, "0000-00-00 00:00:00", false},
{6, "2020-04-23", true},
{7, "2020-05-01", true},
{8, "2020-08-05", true},
{9, "2021-01-01", true},
{10, "2021-07-07", false},
}
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().IsLeapYear(), test.output, "Current test id is "+strconv.Itoa(test.id))
}
}
func TestLunar_IsLeapMonth(t *testing.T) {
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出值
}{
{1, "", false},
{2, "0", false},
{3, "0000-00-00", false},
{4, "00:00:00", false},
{5, "0000-00-00 00:00:00", false},
{6, "2020-04-23", true},
{7, "2020-05-01", true},
{8, "2020-08-05", false},
{9, "2021-01-01", false},
{10, "2021-07-07", false},
}
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().IsLeapMonth(), test.output, "Current test id is "+strconv.Itoa(test.id))
}
}
func TestLunar_IsRatYear(t *testing.T) {
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出值
}{
{1, "", false},
{2, "0", false},
{3, "0000-00-00", false},
{4, "00:00:00", false},
{5, "0000-00-00 00:00:00", false},
{6, "2020-05-01", true},
{7, "2020-08-05", true},
{8, "2021-01-01", true},
{9, "2021-07-07", false},
}
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().IsRatYear(), test.output, "Current test id is "+strconv.Itoa(test.id))
}
}
func TestLunar_IsOxYear(t *testing.T) {
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出值
}{
{1, "", false},
{2, "0", false},
{3, "0000-00-00", false},
{4, "00:00:00", false},
{5, "0000-00-00 00:00:00", false},
{6, "2020-05-01", false},
{7, "2020-08-05", false},
{8, "2021-01-01", false},
{9, "2021-07-07", true},
}
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().IsOxYear(), test.output, "Current test id is "+strconv.Itoa(test.id))
}
}
func TestLunar_IsTigerYear(t *testing.T) {
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出值
}{
{1, "", false},
{2, "0", false},
{3, "0000-00-00", false},
{4, "00:00:00", false},
{5, "0000-00-00 00:00:00", false},
{6, "2020-05-01", false},
{7, "2020-08-05", false},
{8, "2021-01-01", false},
{9, "2021-07-07", false},
{10, "2022-08-05", true},
}
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().IsTigerYear(), test.output, "Current test id is "+strconv.Itoa(test.id))
}
}
func TestLunar_IsRabbitYear(t *testing.T) {
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出值
}{
{1, "", false},
{2, "0", false},
{3, "0000-00-00", false},
{4, "00:00:00", false},
{5, "0000-00-00 00:00:00", false},
{6, "2020-05-01", false},
{7, "2020-08-05", false},
{8, "2021-01-01", false},
{9, "2021-07-07", false},
{10, "2023-08-05", true},
}
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().IsRabbitYear(), test.output, "Current test id is "+strconv.Itoa(test.id))
}
}
func TestLunar_IsDragonYear(t *testing.T) {
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出值
}{
{1, "", false},
{2, "0", false},
{3, "0000-00-00", false},
{4, "00:00:00", false},
{5, "0000-00-00 00:00:00", false},
{6, "2020-05-01", false},
{7, "2020-08-05", false},
{8, "2021-01-01", false},
{9, "2021-07-07", false},
{10, "2024-08-05", true},
}
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().IsDragonYear(), test.output, "Current test id is "+strconv.Itoa(test.id))
}
}
func TestLunar_IsSnakeYear(t *testing.T) {
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出值
}{
{1, "", false},
{2, "0", false},
{3, "0000-00-00", false},
{4, "00:00:00", false},
{5, "0000-00-00 00:00:00", false},
{6, "2020-05-01", false},
{7, "2020-08-05", false},
{8, "2021-01-01", false},
{9, "2021-07-07", false},
{10, "2025-08-05", true},
}
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().IsSnakeYear(), test.output, "Current test id is "+strconv.Itoa(test.id))
}
}
func TestLunar_IsHorseYear(t *testing.T) {
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出值
}{
{1, "", false},
{2, "0", false},
{3, "0000-00-00", false},
{4, "00:00:00", false},
{5, "0000-00-00 00:00:00", false},
{6, "2020-05-01", false},
{7, "2020-08-05", false},
{8, "2021-01-01", false},
{9, "2021-07-07", false},
{10, "2026-08-05", true},
}
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().IsHorseYear(), test.output, "Current test id is "+strconv.Itoa(test.id))
}
}
func TestLunar_IsGoatYear(t *testing.T) {
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出值
}{
{1, "", false},
{2, "0", false},
{3, "0000-00-00", false},
{4, "00:00:00", false},
{5, "0000-00-00 00:00:00", false},
{6, "2020-05-01", false},
{7, "2020-08-05", false},
{8, "2021-01-01", false},
{9, "2021-07-07", false},
{10, "2027-08-05", true},
}
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().IsGoatYear(), test.output, "Current test id is "+strconv.Itoa(test.id))
}
}
func TestLunar_IsMonkeyYear(t *testing.T) {
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出值
}{
{1, "", false},
{2, "0", false},
{3, "0000-00-00", false},
{4, "00:00:00", false},
{5, "0000-00-00 00:00:00", false},
{6, "2020-05-01", false},
{7, "2020-08-05", false},
{8, "2021-01-01", false},
{9, "2021-07-07", false},
{10, "2028-08-05", true},
}
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().IsMonkeyYear(), test.output, "Current test id is "+strconv.Itoa(test.id))
}
}
func TestLunar_IsRoosterYear(t *testing.T) {
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出值
}{
{1, "", false},
{2, "0", false},
{3, "0000-00-00", false},
{4, "00:00:00", false},
{5, "0000-00-00 00:00:00", false},
{6, "2020-05-01", false},
{7, "2020-08-05", false},
{8, "2021-01-01", false},
{9, "2021-07-07", false},
{10, "2029-08-05", true},
}
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().IsRoosterYear(), test.output, "Current test id is "+strconv.Itoa(test.id))
}
}
func TestLunar_IsDogYear(t *testing.T) {
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出值
}{
{1, "", false},
{2, "0", false},
{3, "0000-00-00", false},
{4, "00:00:00", false},
{5, "0000-00-00 00:00:00", false},
{6, "2020-05-01", false},
{7, "2020-08-05", false},
{8, "2021-01-01", false},
{9, "2021-07-07", false},
{10, "2030-08-05", true},
}
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().IsDogYear(), test.output, "Current test id is "+strconv.Itoa(test.id))
}
}
func TestLunar_IsPigYear(t *testing.T) {
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出值
}{
{1, "", false},
{2, "0", false},
{3, "0000-00-00", false},
{4, "00:00:00", false},
{5, "0000-00-00 00:00:00", false},
{6, "2020-05-01", false},
{7, "2020-08-05", false},
{8, "2021-01-01", false},
{9, "2021-07-07", false},
{10, "2031-08-05", true},
}
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().IsPigYear(), test.output, "Current test id is "+strconv.Itoa(test.id))
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/vdebug/carbon.git
git@gitee.com:vdebug/carbon.git
vdebug
carbon
carbon
master

搜索帮助