1 Star 0 Fork 66

ajunlonglive/carbon

forked from dromara/carbon 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
comparer_test.go 27.34 KB
一键复制 编辑 原始数据 按行查看 历史
gouguoyin 提交于 2021-08-10 11:01 . unified unit test format
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
package carbon
import (
"strconv"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCarbon_IsZero(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected bool // 期望值
}{
{"", true},
{"0", true},
{"0000-00-00", true},
{"00:00:00", true},
{"0000-00-00 00:00:00", true},
{"2020-08-05", false},
}
for index, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(test.expected, c.IsZero(), "Current test index is "+strconv.Itoa(index))
}
}
func TestError_IsZero(t *testing.T) {
c := Parse("2020-13-50")
assert.True(t, c.IsZero(), "It should catch an exception in IsZero()")
}
func TestCarbon_IsInvalid(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected bool // 期望值
}{
{"", true},
{"0", true},
{"0000-00-00", true},
{"00:00:00", true},
{"0000-00-00 00:00:00", true},
{"2020-08-05", false},
}
for index, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(test.expected, c.IsInvalid(), "Current test index is "+strconv.Itoa(index))
}
}
func TestError_IsInvalid(t *testing.T) {
c := SetTimezone("xxx")
assert.True(t, c.IsInvalid(), "It should catch an exception in IsInvalid()")
}
func TestCarbon_IsNow(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input Carbon // 输入值
expected bool // 期望值
}{
{Parse(""), false},
{Parse("0"), false},
{Parse("0000-00-00"), false},
{Parse("00:00:00"), false},
{Parse("0000-00-00 00:00:00"), false},
{Tomorrow(), false},
{Now(), true},
{Yesterday(), false},
}
for index, test := range tests {
c := test.input
assert.Nil(c.Error)
assert.Equal(test.expected, c.IsNow(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_IsFuture(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input Carbon // 输入值
expected bool // 期望值
}{
{Parse(""), false},
{Parse("0"), false},
{Parse("0000-00-00"), false},
{Parse("00:00:00"), false},
{Parse("0000-00-00 00:00:00"), false},
{Tomorrow(), true},
{Now(), false},
{Yesterday(), false},
}
for index, test := range tests {
c := test.input
assert.Nil(c.Error)
assert.Equal(test.expected, c.IsFuture(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_IsPast(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input Carbon // 输入值
expected bool // 期望值
}{
{Parse(""), false},
{Parse("0"), false},
{Parse("0000-00-00"), false},
{Parse("00:00:00"), false},
{Parse("0000-00-00 00:00:00"), false},
{Tomorrow(), false},
{Now(), false},
{Yesterday(), true},
}
for index, test := range tests {
c := test.input
assert.Nil(c.Error)
assert.Equal(test.expected, c.IsPast(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_IsLeapYear(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected bool // 期望值
}{
{"", false},
{"0", false},
{"0000-00-00", false},
{"00:00:00", false},
{"0000-00-00 00:00:00", false},
{"2015-01-01", false},
{"2016-01-01", true},
{"2017-01-01", false},
{"2018-01-01", false},
{"2019-01-01", false},
{"2020-01-01", true},
{"2021-01-01", false},
}
for index, test := range tests {
c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error)
assert.Equal(test.expected, c.IsLeapYear(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_IsLongYear(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected bool // 期望值
}{
{"", false},
{"0", false},
{"0000-00-00", false},
{"00:00:00", false},
{"0000-00-00 00:00:00", false},
{"2015-01-01", true},
{"2016-01-01", false},
{"2017-01-01", false},
{"2018-01-01", false},
{"2019-01-01", false},
{"2020-01-01", true},
{"2021-01-01", false},
}
for index, test := range tests {
c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error)
assert.Equal(test.expected, c.IsLongYear(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_IsJanuary(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected bool // 期望值
}{
{"", false},
{"0", false},
{"0000-00-00", false},
{"00:00:00", false},
{"0000-00-00 00:00:00", false},
{"2020-01-01", true},
{"2020-02-01", false},
{"2020-03-01", false},
{"2020-04-01", false},
{"2020-05-01", false},
{"2020-06-01", false},
{"2020-07-01", false},
{"2020-08-01", false},
{"2020-09-01", false},
{"2020-10-01", false},
{"2020-11-01", false},
{"2020-12-01", false},
}
for index, test := range tests {
c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error)
assert.Equal(test.expected, c.IsJanuary(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_IsFebruary(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected bool // 期望值
}{
{"", false},
{"0", false},
{"0000-00-00", false},
{"00:00:00", false},
{"0000-00-00 00:00:00", false},
{"2020-01-01", false},
{"2020-02-01", true},
{"2020-03-01", false},
{"2020-04-01", false},
{"2020-05-01", false},
{"2020-06-01", false},
{"2020-07-01", false},
{"2020-08-01", false},
{"2020-09-01", false},
{"2020-10-01", false},
{"2020-11-01", false},
{"2020-12-01", false},
}
for index, test := range tests {
c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error)
assert.Equal(test.expected, c.IsFebruary(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_IsMarch(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected bool // 期望值
}{
{"", false},
{"0", false},
{"0000-00-00", false},
{"00:00:00", false},
{"0000-00-00 00:00:00", false},
{"2020-01-01", false},
{"2020-02-01", false},
{"2020-03-01", true},
{"2020-04-01", false},
{"2020-05-01", false},
{"2020-06-01", false},
{"2020-07-01", false},
{"2020-08-01", false},
{"2020-09-01", false},
{"2020-10-01", false},
{"2020-11-01", false},
{"2020-12-01", false},
}
for index, test := range tests {
c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error)
assert.Equal(test.expected, c.IsMarch(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_IsApril(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected bool // 期望值
}{
{"", false},
{"0", false},
{"0000-00-00", false},
{"00:00:00", false},
{"0000-00-00 00:00:00", false},
{"2020-01-01", false},
{"2020-02-01", false},
{"2020-03-01", false},
{"2020-04-01", true},
{"2020-05-01", false},
{"2020-06-01", false},
{"2020-07-01", false},
{"2020-08-01", false},
{"2020-09-01", false},
{"2020-10-01", false},
{"2020-11-01", false},
{"2020-12-01", false},
}
for index, test := range tests {
c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error)
assert.Equal(test.expected, c.IsApril(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_IsMay(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected bool // 期望值
}{
{"", false},
{"0", false},
{"0000-00-00", false},
{"00:00:00", false},
{"0000-00-00 00:00:00", false},
{"2020-01-01", false},
{"2020-02-01", false},
{"2020-03-01", false},
{"2020-04-01", false},
{"2020-05-01", true},
{"2020-06-01", false},
{"2020-07-01", false},
{"2020-08-01", false},
{"2020-09-01", false},
{"2020-10-01", false},
{"2020-11-01", false},
{"2020-12-01", false},
}
for index, test := range tests {
c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error)
assert.Equal(test.expected, c.IsMay(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_IsJune(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected bool // 期望值
}{
{"", false},
{"0", false},
{"0000-00-00", false},
{"00:00:00", false},
{"0000-00-00 00:00:00", false},
{"2020-01-01", false},
{"2020-02-01", false},
{"2020-03-01", false},
{"2020-04-01", false},
{"2020-05-01", false},
{"2020-06-01", true},
{"2020-07-01", false},
{"2020-08-01", false},
{"2020-09-01", false},
{"2020-10-01", false},
{"2020-11-01", false},
{"2020-12-01", false},
}
for index, test := range tests {
c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error)
assert.Equal(test.expected, c.IsJune(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_IsJuly(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected bool // 期望值
}{
{"", false},
{"0", false},
{"0000-00-00", false},
{"00:00:00", false},
{"0000-00-00 00:00:00", false},
{"2020-01-01", false},
{"2020-02-01", false},
{"2020-03-01", false},
{"2020-04-01", false},
{"2020-05-01", false},
{"2020-06-01", false},
{"2020-07-01", true},
{"2020-08-01", false},
{"2020-09-01", false},
{"2020-10-01", false},
{"2020-11-01", false},
{"2020-12-01", false},
}
for index, test := range tests {
c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error)
assert.Equal(test.expected, c.IsJuly(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_IsAugust(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected bool // 期望值
}{
{"", false},
{"0", false},
{"0000-00-00", false},
{"00:00:00", false},
{"0000-00-00 00:00:00", false},
{"2020-01-01", false},
{"2020-02-01", false},
{"2020-03-01", false},
{"2020-04-01", false},
{"2020-05-01", false},
{"2020-06-01", false},
{"2020-07-01", false},
{"2020-08-01", true},
{"2020-09-01", false},
{"2020-10-01", false},
{"2020-11-01", false},
{"2020-12-01", false},
}
for index, test := range tests {
c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error)
assert.Equal(test.expected, c.IsAugust(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_IsSeptember(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected bool // 期望值
}{
{"", false},
{"0", false},
{"0000-00-00", false},
{"00:00:00", false},
{"0000-00-00 00:00:00", false},
{"2020-01-01", false},
{"2020-02-01", false},
{"2020-03-01", false},
{"2020-04-01", false},
{"2020-05-01", false},
{"2020-06-01", false},
{"2020-07-01", false},
{"2020-08-01", false},
{"2020-09-01", true},
{"2020-10-01", false},
{"2020-11-01", false},
{"2020-12-01", false},
}
for index, test := range tests {
c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error)
assert.Equal(test.expected, c.IsSeptember(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_IsOctober(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected bool // 期望值
}{
{"", false},
{"0", false},
{"0000-00-00", false},
{"00:00:00", false},
{"0000-00-00 00:00:00", false},
{"2020-01-01", false},
{"2020-02-01", false},
{"2020-03-01", false},
{"2020-04-01", false},
{"2020-05-01", false},
{"2020-06-01", false},
{"2020-07-01", false},
{"2020-08-01", false},
{"2020-09-01", false},
{"2020-10-01", true},
{"2020-11-01", false},
{"2020-12-01", false},
}
for index, test := range tests {
c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error)
assert.Equal(test.expected, c.IsOctober(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_IsNovember(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected bool // 期望值
}{
{"", false},
{"0", false},
{"0000-00-00", false},
{"00:00:00", false},
{"0000-00-00 00:00:00", false},
{"2020-01-01", false},
{"2020-02-01", false},
{"2020-03-01", false},
{"2020-04-01", false},
{"2020-05-01", false},
{"2020-06-01", false},
{"2020-07-01", false},
{"2020-08-01", false},
{"2020-09-01", false},
{"2020-10-01", false},
{"2020-11-01", true},
{"2020-12-01", false},
}
for index, test := range tests {
c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error)
assert.Equal(test.expected, c.IsNovember(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_IsDecember(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected bool // 期望值
}{
{"", false},
{"0", false},
{"0000-00-00", false},
{"00:00:00", false},
{"0000-00-00 00:00:00", false},
{"2020-01-01", false},
{"2020-02-01", false},
{"2020-03-01", false},
{"2020-04-01", false},
{"2020-05-01", false},
{"2020-06-01", false},
{"2020-07-01", false},
{"2020-08-01", false},
{"2020-09-01", false},
{"2020-10-01", false},
{"2020-11-01", false},
{"2020-12-01", true},
}
for index, test := range tests {
c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error)
assert.Equal(test.expected, c.IsDecember(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_IsMonday(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected bool // 期望值
}{
{"", false},
{"0", false},
{"0000-00-00", false},
{"00:00:00", false},
{"0000-00-00 00:00:00", false},
{"2020-10-05", true},
{"2020-10-06", false},
{"2020-10-07", false},
{"2020-10-08", false},
{"2020-10-09", false},
{"2020-10-10", false},
{"2020-10-11", false},
}
for index, test := range tests {
c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error)
assert.Equal(test.expected, c.IsMonday(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_IsTuesday(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected bool // 期望值
}{
{"", false},
{"0", false},
{"0000-00-00", false},
{"00:00:00", false},
{"0000-00-00 00:00:00", false},
{"2020-10-05", false},
{"2020-10-06", true},
{"2020-10-07", false},
{"2020-10-08", false},
{"2020-10-09", false},
{"2020-10-10", false},
{"2020-10-11", false},
}
for index, test := range tests {
c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error)
assert.Equal(test.expected, c.IsTuesday(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_IsWednesday(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected bool // 期望值
}{
{"", false},
{"0", false},
{"0000-00-00", false},
{"00:00:00", false},
{"0000-00-00 00:00:00", false},
{"2020-10-05", false},
{"2020-10-06", false},
{"2020-10-07", true},
{"2020-10-08", false},
{"2020-10-09", false},
{"2020-10-10", false},
{"2020-10-11", false},
}
for index, test := range tests {
c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error)
assert.Equal(test.expected, c.IsWednesday(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_IsThursday(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected bool // 期望值
}{
{"", false},
{"0", false},
{"0000-00-00", false},
{"00:00:00", false},
{"0000-00-00 00:00:00", false},
{"2020-10-05", false},
{"2020-10-06", false},
{"2020-10-07", false},
{"2020-10-08", true},
{"2020-10-09", false},
{"2020-10-10", false},
{"2020-10-11", false},
}
for index, test := range tests {
c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error)
assert.Equal(test.expected, c.IsThursday(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_IsFriday(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected bool // 期望值
}{
{"", false},
{"0", false},
{"0000-00-00", false},
{"00:00:00", false},
{"0000-00-00 00:00:00", false},
{"2020-10-05", false},
{"2020-10-06", false},
{"2020-10-07", false},
{"2020-10-08", false},
{"2020-10-09", true},
{"2020-10-10", false},
{"2020-10-11", false},
}
for index, test := range tests {
c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error)
assert.Equal(test.expected, c.IsFriday(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_IsSaturday(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected bool // 期望值
}{
{"", false},
{"0", false},
{"0000-00-00", false},
{"00:00:00", false},
{"0000-00-00 00:00:00", false},
{"2020-10-05", false},
{"2020-10-06", false},
{"2020-10-07", false},
{"2020-10-08", false},
{"2020-10-09", false},
{"2020-10-10", true},
{"2020-10-11", false},
}
for index, test := range tests {
c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error)
assert.Equal(test.expected, c.IsSaturday(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_IsSunday(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected bool // 期望值
}{
{"", false},
{"0", false},
{"0000-00-00", false},
{"00:00:00", false},
{"0000-00-00 00:00:00", false},
{"2020-10-05", false},
{"2020-10-06", false},
{"2020-10-07", false},
{"2020-10-08", false},
{"2020-10-09", false},
{"2020-10-10", false},
{"2020-10-11", true},
}
for index, test := range tests {
c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error)
assert.Equal(test.expected, c.IsSunday(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_IsWeekday(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected bool // 期望值
}{
{"", false},
{"0", false},
{"0000-00-00", false},
{"00:00:00", false},
{"0000-00-00 00:00:00", false},
{"2020-10-05", true},
{"2020-10-06", true},
{"2020-10-07", true},
{"2020-10-08", true},
{"2020-10-09", true},
{"2020-10-10", false},
{"2020-10-11", false},
}
for index, test := range tests {
c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error)
assert.Equal(test.expected, c.IsWeekday(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_IsWeekend(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected bool // 期望值
}{
{"", false},
{"0", false},
{"0000-00-00", false},
{"00:00:00", false},
{"0000-00-00 00:00:00", false},
{"2020-10-05", false},
{"2020-10-06", false},
{"2020-10-07", false},
{"2020-10-08", false},
{"2020-10-09", false},
{"2020-10-10", true},
{"2020-10-11", true},
}
for index, test := range tests {
c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error)
assert.Equal(test.expected, c.IsWeekend(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_IsYesterday(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input Carbon // 输入值
expected bool // 期望值
}{
{NewCarbon(), false},
{Now(), false},
{Yesterday(), true},
{Tomorrow(), false},
}
for index, test := range tests {
c := test.input
assert.Nil(c.Error)
assert.Equal(test.expected, c.IsYesterday(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_IsToday(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input Carbon // 输入值
expected bool // 期望值
}{
{NewCarbon(), false},
{Now(), true},
{Yesterday(), false},
{Tomorrow(), false},
}
for index, test := range tests {
c := test.input
assert.Nil(c.Error)
assert.Equal(test.expected, c.IsToday(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_IsTomorrow(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input Carbon // 输入值
expected bool // 期望值
}{
{NewCarbon(), false},
{Now(), false},
{Yesterday(), false},
{Tomorrow(), true},
}
for index, test := range tests {
c := test.input
assert.Nil(c.Error)
assert.Equal(test.expected, c.IsTomorrow(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_Compare(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
param1 string // 输入参数1
param2 string // 输入参数2
expected bool // 期望值
}{
{"2020-08-05", ">", "2020-08-04", true},
{"2020-08-05", "<", "2020-08-04", false},
{"2020-08-05", "<", "2020-08-06", true},
{"2020-08-05", ">", "2020-08-06", false},
{"2020-08-05", "=", "2020-08-05", true},
{"2020-08-05", ">=", "2020-08-05", true},
{"2020-08-05", "<=", "2020-08-05", true},
{"2020-08-05", "!=", "2020-08-05", false},
{"2020-08-05", "<>", "2020-08-05", false},
{"2020-08-05", "!=", "2020-08-04", true},
{"2020-08-05", "<>", "2020-08-04", true},
{"2020-08-05", "+", "2020-08-04", false},
}
for index, test := range tests {
c1, c2 := Parse(test.input), Parse(test.param2)
assert.Nil(c1.Error)
assert.Nil(c2.Error)
assert.Equal(test.expected, c1.Compare(test.param1, c2), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_Gt(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
param string // 参数值
expected bool // 期望值
}{
{"2020-08-05", "2020-08-05", false},
{"2020-08-05", "2020-08-04", true},
{"2020-08-05", "2020-08-06", false},
}
for index, test := range tests {
c1, c2 := Parse(test.input), Parse(test.param)
assert.Nil(c1.Error)
assert.Nil(c2.Error)
assert.Equal(test.expected, c1.Gt(c2), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_Lt(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
param string // 参数值
expected bool // 期望值
}{
{"2020-08-05", "2020-08-05", false},
{"2020-08-05", "2020-08-04", false},
{"2020-08-05", "2020-08-06", true},
}
for index, test := range tests {
c1, c2 := Parse(test.input), Parse(test.param)
assert.Nil(c1.Error)
assert.Nil(c2.Error)
assert.Equal(test.expected, c1.Lt(c2), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_Eq(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
param string // 参数值
expected bool // 期望值
}{
{"2020-08-05", "2020-08-05", true},
{"2020-08-05", "2020-08-04", false},
{"2020-08-05", "2020-08-06", false},
}
for index, test := range tests {
c1, c2 := Parse(test.input), Parse(test.param)
assert.Nil(c1.Error)
assert.Nil(c2.Error)
assert.Equal(test.expected, c1.Eq(c2), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_Ne(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
param string // 参数值
expected bool // 期望值
}{
{"2020-08-05", "2020-08-05", false},
{"2020-08-05", "2020-08-04", true},
{"2020-08-05", "2020-08-06", true},
}
for index, test := range tests {
c1, c2 := Parse(test.input), Parse(test.param)
assert.Nil(c1.Error)
assert.Nil(c2.Error)
assert.Equal(test.expected, c1.Ne(c2), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_Gte(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
param string // 参数值
expected bool // 期望值
}{
{"2020-08-05", "2020-08-05", true},
{"2020-08-05", "2020-08-04", true},
{"2020-08-05", "2020-08-06", false},
}
for index, test := range tests {
c1, c2 := Parse(test.input), Parse(test.param)
assert.Nil(c1.Error)
assert.Nil(c2.Error)
assert.Equal(test.expected, c1.Gte(c2), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_Lte(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
param string // 参数值
expected bool // 期望值
}{
{"2020-08-05", "2020-08-05", true},
{"2020-08-05", "2020-08-04", false},
{"2020-08-05", "2020-08-06", true},
}
for index, test := range tests {
c1, c2 := Parse(test.input), Parse(test.param)
assert.Nil(c1.Error)
assert.Nil(c2.Error)
assert.Equal(test.expected, c1.Lte(c2), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_Between(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
param1 string // 输入参数1
param2 string // 输入参数2
expected bool // 期望值
}{
{"2020-08-05 13:14:15", "2020-08-05 13:14:15", "2020-08-05 13:14:15", false},
{"2020-08-05 13:14:15", "2020-08-05 13:14:15", "2020-08-06 13:14:15", false},
{"2020-08-05 13:14:15", "2020-08-04 13:14:15", "2020-08-05 13:14:15", false},
{"2020-08-05 13:14:15", "2020-08-04 13:14:15", "2020-08-06 13:14:15", true},
}
for index, test := range tests {
c1, c2, c3 := Parse(test.input), Parse(test.param1), Parse(test.param2)
assert.Nil(c1.Error)
assert.Nil(c2.Error)
assert.Nil(c3.Error)
assert.Equal(test.expected, c1.Between(c2, c3), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_BetweenIncludedStart(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
param1 string // 输入参数1
param2 string // 输入参数2
expected bool // 期望值
}{
{"2020-08-05 13:14:15", "2020-08-05 13:14:15", "2020-08-05 13:14:15", false},
{"2020-08-05 13:14:15", "2020-08-05 13:14:15", "2020-08-06 13:14:15", true},
{"2020-08-05 13:14:15", "2020-08-04 13:14:15", "2020-08-05 13:14:15", false},
{"2020-08-05 13:14:15", "2020-08-04 13:14:15", "2020-08-06 13:14:15", true},
}
for index, test := range tests {
c1, c2, c3 := Parse(test.input), Parse(test.param1), Parse(test.param2)
assert.Nil(c1.Error)
assert.Nil(c2.Error)
assert.Nil(c3.Error)
assert.Equal(test.expected, c1.BetweenIncludedStart(c2, c3), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_BetweenIncludedEnd(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
param1 string // 输入参数1
param2 string // 输入参数2
expected bool // 期望值
}{
{"2020-08-05 13:14:15", "2020-08-05 13:14:15", "2020-08-05 13:14:15", false},
{"2020-08-05 13:14:15", "2020-08-05 13:14:15", "2020-08-06 13:14:15", false},
{"2020-08-05 13:14:15", "2020-08-04 13:14:15", "2020-08-05 13:14:15", true},
{"2020-08-05 13:14:15", "2020-08-04 13:14:15", "2020-08-06 13:14:15", true},
}
for index, test := range tests {
c1, c2, c3 := Parse(test.input), Parse(test.param1), Parse(test.param2)
assert.Nil(c1.Error)
assert.Nil(c2.Error)
assert.Nil(c3.Error)
assert.Equal(test.expected, c1.BetweenIncludedEnd(c2, c3), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_BetweenIncludedBoth(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
param1 string // 输入参数1
param2 string // 输入参数2
expected bool // 期望值
}{
{"2020-08-05 13:14:15", "2020-08-05 13:14:15", "2020-08-05 13:14:15", true},
{"2020-08-05 13:14:15", "2020-08-05 13:14:15", "2020-08-06 13:14:15", true},
{"2020-08-05 13:14:15", "2020-08-04 13:14:15", "2020-08-05 13:14:15", true},
{"2020-08-05 13:14:15", "2020-08-04 13:14:15", "2020-08-06 13:14:15", true},
{"2020-08-05 13:14:15", "2020-08-06 13:14:15", "2020-08-06 13:14:15", false},
}
for index, test := range tests {
c1, c2, c3 := Parse(test.input), Parse(test.param1), Parse(test.param2)
assert.Nil(c1.Error)
assert.Nil(c2.Error)
assert.Nil(c3.Error)
assert.Equal(test.expected, c1.BetweenIncludedBoth(c2, c3), "Current test index is "+strconv.Itoa(index))
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/ajunlonglive/carbon.git
git@gitee.com:ajunlonglive/carbon.git
ajunlonglive
carbon
carbon
v2.0.1

搜索帮助

0d507c66 1850385 C8b1a773 1850385