1 Star 0 Fork 63

神以灵丶/carbon

forked from dromara/carbon 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
getter_unit_test.go 32.30 KB
一键复制 编辑 原始数据 按行查看 历史
gouguoyin 提交于 2024-04-09 20:12 . 统一单元测试格式
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650
package carbon
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestCarbon_StdTime(t *testing.T) {
expected := time.Now().Format(DateTimeLayout)
actual := Now().StdTime().Format(DateTimeLayout)
assert.Equal(t, expected, actual)
}
func TestCarbon_DaysInYear(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want int
}{
{
name: "case1",
carbon: Parse(""),
want: 0,
},
{
name: "case2",
carbon: Parse("2020-08-05"),
want: 366,
},
{
name: "case3",
carbon: Parse("2021-08-05"),
want: 365,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.DaysInYear(), "DaysInYear()")
})
}
}
func TestCarbon_DaysInMonth(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want int
}{
{
name: "case1",
carbon: Parse(""),
want: 0,
},
{
name: "case2",
carbon: Parse("2020-01-05"),
want: 31,
},
{
name: "case3",
carbon: Parse("2020-02-05"),
want: 29,
},
{
name: "case4",
carbon: Parse("2020-03-05"),
want: 31,
},
{
name: "case5",
carbon: Parse("2020-04-05"),
want: 30,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.DaysInMonth(), "DaysInMonth()")
})
}
}
func TestCarbon_MonthOfYear(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want int
}{
{
name: "case1",
carbon: Parse(""),
want: 0,
},
{
name: "case2",
carbon: Parse("2020-01-05"),
want: 1,
},
{
name: "case3",
carbon: Parse("2020-02-05"),
want: 2,
},
{
name: "case4",
carbon: Parse("2020-03-05"),
want: 3,
},
{
name: "case5",
carbon: Parse("2020-04-05"),
want: 4,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.MonthOfYear(), "MonthOfYear()")
})
}
}
func TestCarbon_DayOfYear(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want int
}{
{
name: "case1",
carbon: Parse(""),
want: 0,
},
{
name: "case2",
carbon: Parse("2020-01-05"),
want: 5,
},
{
name: "case3",
carbon: Parse("2020-02-05"),
want: 36,
},
{
name: "case4",
carbon: Parse("2020-03-05"),
want: 65,
},
{
name: "case5",
carbon: Parse("2020-04-05"),
want: 96,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.DayOfYear(), "DayOfYear()")
})
}
}
func TestCarbon_DayOfMonth(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want int
}{
{
name: "case1",
carbon: Parse(""),
want: 0,
},
{
name: "case2",
carbon: Parse("xxx"),
want: 0,
},
{
name: "case3",
carbon: Parse("2020-01-01"),
want: 1,
},
{
name: "case4",
carbon: Parse("2020-01-05"),
want: 5,
},
{
name: "case5",
carbon: Parse("2020-01-31"),
want: 31,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.DayOfMonth(), "DayOfMonth()")
})
}
}
func TestCarbon_DayOfWeek(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want int
}{
{
name: "case1",
carbon: Parse(""),
want: 0,
},
{
name: "case2",
carbon: Parse("xxx"),
want: 0,
},
{
name: "case3",
carbon: Parse("2020-08-03"),
want: 1,
},
{
name: "case4",
carbon: Parse("2020-08-04"),
want: 2,
},
{
name: "case5",
carbon: Parse("2020-08-05"),
want: 3,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.DayOfWeek(), "DayOfWeek()")
})
}
}
func TestCarbon_WeekOfYear(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want int
}{
{
name: "case1",
carbon: Parse(""),
want: 0,
},
{
name: "case2",
carbon: Parse("xxx"),
want: 0,
},
{
name: "case3",
carbon: Parse("2021-01-01"),
want: 53,
},
{
name: "case4",
carbon: Parse("2021-02-01"),
want: 5,
},
{
name: "case5",
carbon: Parse("2021-03-01"),
want: 9,
},
{
name: "case6",
carbon: Parse("2021-04-01"),
want: 13,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.WeekOfYear(), "WeekOfYear()")
})
}
}
func TestCarbon_WeekOfMonth(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want int
}{
{
name: "case1",
carbon: Parse(""),
want: 0,
},
{
name: "case2",
carbon: Parse("xxx"),
want: 0,
},
{
name: "case3",
carbon: Parse("2021-07-01"),
want: 1,
},
{
name: "case4",
carbon: Parse("2021-07-02"),
want: 1,
},
{
name: "case5",
carbon: Parse("2021-07-03"),
want: 1,
},
{
name: "case6",
carbon: Parse("2021-07-04"),
want: 1,
},
{
name: "case7",
carbon: Parse("2021-07-05"),
want: 2,
},
{
name: "case8",
carbon: Parse("2021-07-06"),
want: 2,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.WeekOfMonth(), "WeekOfMonth()")
})
}
}
func TestCarbon_DateTime(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want struct {
year, month, day, hour, minute, second int
}
}{
{
name: "case1",
carbon: Parse(""),
want: struct{ year, month, day, hour, minute, second int }{year: 0, month: 0, day: 0},
},
{
name: "case2",
carbon: Parse("xxx"),
want: struct{ year, month, day, hour, minute, second int }{year: 0, month: 0, day: 0},
},
{
name: "case3",
carbon: Parse("2020-01-01"),
want: struct{ year, month, day, hour, minute, second int }{year: 2020, month: 1, day: 1},
},
{
name: "case4",
carbon: Parse("2020-01-01 13:14:15.999999999"),
want: struct{ year, month, day, hour, minute, second int }{year: 2020, month: 1, day: 1, hour: 13, minute: 14, second: 15},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
year, month, day, hour, minute, second := tt.carbon.DateTime()
assert.Equalf(t, tt.want.year, year, "DateTime()")
assert.Equalf(t, tt.want.month, month, "DateTime()")
assert.Equalf(t, tt.want.day, day, "DateTime()")
assert.Equalf(t, tt.want.hour, hour, "DateTime()")
assert.Equalf(t, tt.want.minute, minute, "DateTime()")
assert.Equalf(t, tt.want.second, second, "DateTime()")
})
}
}
func TestCarbon_DateTimeMilli(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want struct {
year, month, day, hour, minute, second, millisecond int
}
}{
{
name: "case1",
carbon: Parse(""),
want: struct{ year, month, day, hour, minute, second, millisecond int }{year: 0, month: 0, day: 0, millisecond: 0},
},
{
name: "case2",
carbon: Parse("xxx"),
want: struct{ year, month, day, hour, minute, second, millisecond int }{year: 0, month: 0, day: 0, millisecond: 0},
},
{
name: "case3",
carbon: Parse("xxx"),
want: struct{ year, month, day, hour, minute, second, millisecond int }{year: 0, month: 0, day: 0, millisecond: 0},
},
{
name: "case4",
carbon: Parse("2020-01-01"),
want: struct{ year, month, day, hour, minute, second, millisecond int }{year: 2020, month: 1, day: 1, millisecond: 0},
},
{
name: "case5",
carbon: Parse("2020-01-01 13:14:15.999999999"),
want: struct{ year, month, day, hour, minute, second, millisecond int }{year: 2020, month: 1, day: 1, hour: 13, minute: 14, second: 15, millisecond: 999},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
year, month, day, hour, minute, second, millisecond := tt.carbon.DateTimeMilli()
assert.Equalf(t, tt.want.year, year, "DateTimeMilli()")
assert.Equalf(t, tt.want.month, month, "DateTimeMilli()")
assert.Equalf(t, tt.want.day, day, "DateTimeMilli()")
assert.Equalf(t, tt.want.hour, hour, "DateTimeMilli()")
assert.Equalf(t, tt.want.minute, minute, "DateTimeMilli()")
assert.Equalf(t, tt.want.second, second, "DateTimeMilli()")
assert.Equalf(t, tt.want.millisecond, millisecond, "DateTimeMilli()")
})
}
}
func TestCarbon_DateTimeMicro(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want struct {
year, month, day, hour, minute, second, microsecond int
}
}{
{
name: "case1",
carbon: Parse(""),
want: struct{ year, month, day, hour, minute, second, microsecond int }{year: 0, month: 0, day: 0, microsecond: 0},
},
{
name: "case2",
carbon: Parse("xxx"),
want: struct{ year, month, day, hour, minute, second, microsecond int }{year: 0, month: 0, day: 0, microsecond: 0},
},
{
name: "case3",
carbon: Parse("2020-01-01"),
want: struct{ year, month, day, hour, minute, second, microsecond int }{year: 2020, month: 1, day: 1, microsecond: 0},
},
{
name: "case4",
carbon: Parse("2020-01-01 13:14:15.999999999"),
want: struct{ year, month, day, hour, minute, second, microsecond int }{year: 2020, month: 1, day: 1, hour: 13, minute: 14, second: 15, microsecond: 999999},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
year, month, day, hour, minute, second, microsecond := tt.carbon.DateTimeMicro()
assert.Equalf(t, tt.want.year, year, "DateTimeMicro()")
assert.Equalf(t, tt.want.month, month, "DateTimeMicro()")
assert.Equalf(t, tt.want.day, day, "DateTimeMicro()")
assert.Equalf(t, tt.want.hour, hour, "DateTimeMicro()")
assert.Equalf(t, tt.want.minute, minute, "DateTimeMicro()")
assert.Equalf(t, tt.want.second, second, "DateTimeMicro()")
assert.Equalf(t, tt.want.microsecond, microsecond, "DateTimeMicro()")
})
}
}
func TestCarbon_DateTimeNano(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want struct {
year, month, day, hour, minute, second, nanosecond int
}
}{
{
name: "case1",
carbon: Parse(""),
want: struct{ year, month, day, hour, minute, second, nanosecond int }{year: 0, month: 0, day: 0, nanosecond: 0},
},
{
name: "case2",
carbon: Parse("xxx"),
want: struct{ year, month, day, hour, minute, second, nanosecond int }{year: 0, month: 0, day: 0, nanosecond: 0},
},
{
name: "case3",
carbon: Parse("2020-01-01"),
want: struct{ year, month, day, hour, minute, second, nanosecond int }{year: 2020, month: 1, day: 1, nanosecond: 0},
},
{
name: "case4",
carbon: Parse("2020-01-01 13:14:15.999999999"),
want: struct{ year, month, day, hour, minute, second, nanosecond int }{year: 2020, month: 1, day: 1, hour: 13, minute: 14, second: 15, nanosecond: 999999999},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
year, month, day, hour, minute, second, nanosecond := tt.carbon.DateTimeNano()
assert.Equalf(t, tt.want.year, year, "DateTimeNano()")
assert.Equalf(t, tt.want.month, month, "DateTimeNano()")
assert.Equalf(t, tt.want.day, day, "DateTimeNano()")
assert.Equalf(t, tt.want.hour, hour, "DateTimeNano()")
assert.Equalf(t, tt.want.minute, minute, "DateTimeNano()")
assert.Equalf(t, tt.want.second, second, "DateTimeNano()")
assert.Equalf(t, tt.want.nanosecond, nanosecond, "DateTimeNano()")
})
}
}
func TestCarbon_Date(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want struct {
year, month, day int
}
}{
{
name: "case1",
carbon: Parse(""),
want: struct{ year, month, day int }{year: 0, month: 0, day: 0},
},
{
name: "case2",
carbon: Parse("2020-01-01"),
want: struct{ year, month, day int }{year: 2020, month: 1, day: 1},
},
{
name: "case3",
carbon: Parse("2020-01-01 13:14:15.999999999"),
want: struct{ year, month, day int }{year: 2020, month: 1, day: 1},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
year, month, day := tt.carbon.Date()
assert.Equalf(t, tt.want.year, year, "Date()")
assert.Equalf(t, tt.want.month, month, "Date()")
assert.Equalf(t, tt.want.day, day, "Date()")
})
}
}
func TestCarbon_DateMilli(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want struct {
year, month, day, millisecond int
}
}{
{
name: "case1",
carbon: Parse(""),
want: struct{ year, month, day, millisecond int }{year: 0, month: 0, day: 0, millisecond: 0},
},
{
name: "case2",
carbon: Parse("2020-01-01"),
want: struct{ year, month, day, millisecond int }{year: 2020, month: 1, day: 1, millisecond: 0},
},
{
name: "case3",
carbon: Parse("2020-01-01 13:14:15.999999999"),
want: struct{ year, month, day, millisecond int }{year: 2020, month: 1, day: 1, millisecond: 999},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
year, month, day, millisecond := tt.carbon.DateMilli()
assert.Equalf(t, tt.want.year, year, "DateMilli()")
assert.Equalf(t, tt.want.month, month, "DateMilli()")
assert.Equalf(t, tt.want.day, day, "DateMilli()")
assert.Equalf(t, tt.want.millisecond, millisecond, "DateMilli()")
})
}
}
func TestCarbon_DateMicro(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want struct {
year, month, day, microsecond int
}
}{
{
name: "case1",
carbon: Parse(""),
want: struct{ year, month, day, microsecond int }{year: 0, month: 0, day: 0, microsecond: 0},
},
{
name: "case2",
carbon: Parse("2020-01-01"),
want: struct{ year, month, day, microsecond int }{year: 2020, month: 1, day: 1, microsecond: 0},
},
{
name: "case3",
carbon: Parse("2020-01-01 13:14:15.999999999"),
want: struct{ year, month, day, microsecond int }{year: 2020, month: 1, day: 1, microsecond: 999999},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
year, month, day, microsecond := tt.carbon.DateMicro()
assert.Equalf(t, tt.want.year, year, "DateMicro()")
assert.Equalf(t, tt.want.month, month, "DateMicro()")
assert.Equalf(t, tt.want.day, day, "DateMicro()")
assert.Equalf(t, tt.want.microsecond, microsecond, "DateMicro()")
})
}
}
func TestCarbon_DateNano(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want struct {
year, month, day, nanosecond int
}
}{
{
name: "case1",
carbon: Parse(""),
want: struct{ year, month, day, nanosecond int }{year: 0, month: 0, day: 0, nanosecond: 0},
},
{
name: "case2",
carbon: Parse("2020-01-01"),
want: struct{ year, month, day, nanosecond int }{year: 2020, month: 1, day: 1, nanosecond: 0},
},
{
name: "case3",
carbon: Parse("2020-01-01 13:14:15.999999999"),
want: struct{ year, month, day, nanosecond int }{year: 2020, month: 1, day: 1, nanosecond: 999999999},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
year, month, day, nanosecond := tt.carbon.DateNano()
assert.Equalf(t, tt.want.year, year, "DateNano()")
assert.Equalf(t, tt.want.month, month, "DateNano()")
assert.Equalf(t, tt.want.day, day, "DateNano()")
assert.Equalf(t, tt.want.nanosecond, nanosecond, "DateNano()")
})
}
}
func TestCarbon_Time(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want struct {
hour, minute, second int
}
}{
{
name: "case1",
carbon: Parse(""),
want: struct{ hour, minute, second int }{hour: 0, minute: 0, second: 0},
},
{
name: "case2",
carbon: Parse("2020-01-01"),
want: struct{ hour, minute, second int }{hour: 0, minute: 0, second: 0},
},
{
name: "case3",
carbon: Parse("2020-01-01 13:14:15.999999999"),
want: struct{ hour, minute, second int }{hour: 13, minute: 14, second: 15},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
hour, minute, second := tt.carbon.Time()
assert.Equalf(t, tt.want.hour, hour, "Time()")
assert.Equalf(t, tt.want.minute, minute, "Time()")
assert.Equalf(t, tt.want.second, second, "Time()")
})
}
}
func TestCarbon_TimeMilli(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want struct {
hour, minute, second, millisecond int
}
}{
{
name: "case1",
carbon: Parse(""),
want: struct{ hour, minute, second, millisecond int }{hour: 0, minute: 0, second: 0, millisecond: 0},
},
{
name: "case2",
carbon: Parse("2020-01-01"),
want: struct{ hour, minute, second, millisecond int }{hour: 0, minute: 0, second: 0, millisecond: 0},
},
{
name: "case3",
carbon: Parse("2020-01-01 13:14:15.999999999"),
want: struct{ hour, minute, second, millisecond int }{hour: 13, minute: 14, second: 15, millisecond: 999},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
hour, minute, second, millisecond := tt.carbon.TimeMilli()
assert.Equalf(t, tt.want.hour, hour, "TimeMilli()")
assert.Equalf(t, tt.want.minute, minute, "TimeMilli()")
assert.Equalf(t, tt.want.second, second, "TimeMilli()")
assert.Equalf(t, tt.want.millisecond, millisecond, "TimeMilli()")
})
}
}
func TestCarbon_TimeMicro(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want struct {
hour, minute, second, microsecond int
}
}{
{
name: "case1",
carbon: Parse(""),
want: struct{ hour, minute, second, microsecond int }{hour: 0, minute: 0, second: 0, microsecond: 0},
},
{
name: "case2",
carbon: Parse("2020-01-01"),
want: struct{ hour, minute, second, microsecond int }{hour: 0, minute: 0, second: 0, microsecond: 0},
},
{
name: "case3",
carbon: Parse("2020-01-01 13:14:15.999999999"),
want: struct{ hour, minute, second, microsecond int }{hour: 13, minute: 14, second: 15, microsecond: 999999},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
hour, minute, second, microsecond := tt.carbon.TimeMicro()
assert.Equalf(t, tt.want.hour, hour, "TimeMicro()")
assert.Equalf(t, tt.want.minute, minute, "TimeMicro()")
assert.Equalf(t, tt.want.second, second, "TimeMicro()")
assert.Equalf(t, tt.want.microsecond, microsecond, "TimeMicro()")
})
}
}
func TestCarbon_TimeNano(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want struct {
hour, minute, second, nanosecond int
}
}{
{
name: "case1",
carbon: Parse(""),
want: struct{ hour, minute, second, nanosecond int }{hour: 0, minute: 0, second: 0, nanosecond: 0},
},
{
name: "case2",
carbon: Parse("2020-01-01"),
want: struct{ hour, minute, second, nanosecond int }{hour: 0, minute: 0, second: 0, nanosecond: 0},
},
{
name: "case3",
carbon: Parse("2020-01-01 13:14:15.999999999"),
want: struct{ hour, minute, second, nanosecond int }{hour: 13, minute: 14, second: 15, nanosecond: 999999999},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
hour, minute, second, nanosecond := tt.carbon.TimeNano()
assert.Equalf(t, tt.want.hour, hour, "TimeNano()")
assert.Equalf(t, tt.want.minute, minute, "TimeNano()")
assert.Equalf(t, tt.want.second, second, "TimeNano()")
assert.Equalf(t, tt.want.nanosecond, nanosecond, "TimeNano()")
})
}
}
func TestCarbon_Century(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want int
}{
{
name: "case1",
carbon: Parse(""),
want: 0,
},
{
name: "case2",
carbon: Parse("1990-08-05"),
want: 20,
},
{
name: "case3",
carbon: Parse("2021-08-05"),
want: 21,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.Century(), "Century()")
})
}
}
func TestCarbon_Decade(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want int
}{
{
name: "case1",
carbon: Parse(""),
want: 0,
},
{
name: "case2",
carbon: Parse("2010-08-05"),
want: 10,
},
{
name: "case3",
carbon: Parse("2011-08-05"),
want: 10,
},
{
name: "case4",
carbon: Parse("2020-08-05"),
want: 20,
},
{
name: "case5",
carbon: Parse("2021-08-05"),
want: 20,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.Decade(), "Decade()")
})
}
}
func TestCarbon_Year(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want int
}{
{
name: "case1",
carbon: Parse(""),
want: 0,
},
{
name: "case2",
carbon: Parse("2020-08-05"),
want: 2020,
},
{
name: "case3",
carbon: Parse("2021-08-05"),
want: 2021,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.Year(), "Year()")
})
}
}
func TestCarbon_Quarter(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want int
}{
{
name: "case1",
carbon: Parse(""),
want: 0,
},
{
name: "case2",
carbon: Parse("2020-01-05"),
want: 1,
},
{
name: "case3",
carbon: Parse("2020-04-05"),
want: 2,
},
{
name: "case4",
carbon: Parse("2020-08-05"),
want: 3,
},
{
name: "case5",
carbon: Parse("2020-11-05"),
want: 4,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.Quarter(), "Quarter()")
})
}
}
func TestCarbon_Month(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want int
}{
{
name: "case1",
carbon: Parse(""),
want: 0,
},
{
name: "case2",
carbon: Parse("2020-08-05"),
want: 8,
},
{
name: "case3",
carbon: Parse("2020-08-05 13:14:15"),
want: 8,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.Month(), "Month()")
})
}
}
func TestCarbon_Week(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want int
}{
{
name: "case1",
carbon: Parse(""),
want: -1,
},
{
name: "case2",
carbon: Parse("xxx"),
want: -1,
},
{
name: "case3",
carbon: Parse("2020-08-03"),
want: 1,
},
{
name: "case4",
carbon: Parse("2020-08-04"),
want: 2,
},
{
name: "case5",
carbon: Parse("2020-08-05"),
want: 3,
},
{
name: "case6",
carbon: Parse("2020-08-09"),
want: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.Week(), "Week()")
})
}
}
func TestCarbon_Day(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want int
}{
{
name: "case1",
carbon: Parse(""),
want: 0,
},
{
name: "case2",
carbon: Parse("xxx"),
want: 0,
},
{
name: "case3",
carbon: Parse("2020-08-05"),
want: 5,
},
{
name: "case4",
carbon: Parse("2020-08-05 13:14:15"),
want: 5,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.Day(), "Day()")
})
}
}
func TestCarbon_Hour(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want int
}{
{
name: "case1",
carbon: Parse(""),
want: 0,
},
{
name: "case2",
carbon: Parse("xxx"),
want: 0,
},
{
name: "case3",
carbon: Parse("2020-08-05"),
want: 0,
},
{
name: "case4",
carbon: Parse("2020-08-05 13:14:15"),
want: 13,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.Hour(), "Hour()")
})
}
}
func TestCarbon_Minute(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want int
}{
{
name: "case1",
carbon: Parse(""),
want: 0,
},
{
name: "case2",
carbon: Parse("xxx"),
want: 0,
},
{
name: "case3",
carbon: Parse("2020-08-05"),
want: 0,
},
{
name: "case4",
carbon: Parse("2020-08-05 13:14:15"),
want: 14,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.Minute(), "Minute()")
})
}
}
func TestCarbon_Second(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want int
}{
{
name: "case1",
carbon: Parse(""),
want: 0,
},
{
name: "case2",
carbon: Parse("xxx"),
want: 0,
},
{
name: "case3",
carbon: Parse("2020-08-05"),
want: 0,
},
{
name: "case4",
carbon: Parse("2020-08-05 13:14:15"),
want: 15,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.Second(), "Second()")
})
}
}
func TestCarbon_Millisecond(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want int
}{
{
name: "case1",
carbon: Parse(""),
want: 0,
},
{
name: "case2",
carbon: Parse("xxx"),
want: 0,
},
{
name: "case3",
carbon: Parse("2020-08-05 13:14:15"),
want: 0,
},
{
name: "case4",
carbon: Parse("2020-08-05 13:14:15.999"),
want: 999,
},
{
name: "case5",
carbon: Parse("2020-08-05 13:14:15.999999"),
want: 999,
},
{
name: "case6",
carbon: Parse("2020-08-05 13:14:15.999999999"),
want: 999,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.Millisecond(), "Millisecond()")
})
}
}
func TestCarbon_Microsecond(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want int
}{
{
name: "case1",
carbon: Parse(""),
want: 0,
},
{
name: "case2",
carbon: Parse("xxx"),
want: 0,
},
{
name: "case3",
carbon: Parse("2020-08-05 13:14:15"),
want: 0,
},
{
name: "case4",
carbon: Parse("2020-08-05 13:14:15.999"),
want: 999000,
},
{
name: "case5",
carbon: Parse("2020-08-05 13:14:15.999999"),
want: 999999,
},
{
name: "case6",
carbon: Parse("2020-08-05 13:14:15.999999999"),
want: 999999,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.Microsecond(), "Microsecond()")
})
}
}
func TestCarbon_Nanosecond(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want int
}{
{
name: "case1",
carbon: Parse(""),
want: 0,
},
{
name: "case2",
carbon: Parse("xxx"),
want: 0,
},
{
name: "case3",
carbon: Parse("2020-08-05 13:14:15"),
want: 0,
},
{
name: "case4",
carbon: Parse("2020-08-05 13:14:15.999"),
want: 999000000,
},
{
name: "case5",
carbon: Parse("2020-08-05 13:14:15.999999"),
want: 999999000,
},
{
name: "case6",
carbon: Parse("2020-08-05 13:14:15.999999999"),
want: 999999999,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.Nanosecond(), "Nanosecond()")
})
}
}
func TestCarbon_Timestamp(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want int64
}{
{
name: "case1",
carbon: Parse(""),
want: 0,
},
{
name: "case2",
carbon: Parse("xxx"),
want: 0,
},
{
name: "case3",
carbon: Parse("2020-01-01 13:14:15"),
want: 1577855655,
},
{
name: "case4",
carbon: Parse("2020-01-31 13:14:15"),
want: 1580447655,
},
{
name: "case5",
carbon: Parse("2020-02-01 13:14:15"),
want: 1580534055,
},
{
name: "case6",
carbon: Parse("2020-02-28 13:14:15"),
want: 1582866855,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.Timestamp(), "Timestamp()")
})
}
}
func TestCarbon_TimestampMilli(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want int64
}{
{
name: "case1",
carbon: Parse(""),
want: 0,
},
{
name: "case2",
carbon: Parse("xxx"),
want: 0,
},
{
name: "case3",
carbon: Parse("2020-01-01 13:14:15"),
want: 1577855655000,
},
{
name: "case4",
carbon: Parse("2020-01-31 13:14:15"),
want: 1580447655000,
},
{
name: "case5",
carbon: Parse("2020-02-01 13:14:15"),
want: 1580534055000,
},
{
name: "case6",
carbon: Parse("2020-02-28 13:14:15"),
want: 1582866855000,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.TimestampMilli(), "TimestampMilli()")
})
}
}
func TestCarbon_TimestampMicro(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want int64
}{
{
name: "case1",
carbon: Parse(""),
want: 0,
},
{
name: "case2",
carbon: Parse("xxx"),
want: 0,
},
{
name: "case3",
carbon: Parse("2020-01-01 13:14:15"),
want: 1577855655000000,
},
{
name: "case4",
carbon: Parse("2020-01-31 13:14:15"),
want: 1580447655000000,
},
{
name: "case5",
carbon: Parse("2020-02-01 13:14:15"),
want: 1580534055000000,
},
{
name: "case6",
carbon: Parse("2020-02-28 13:14:15"),
want: 1582866855000000,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.TimestampMicro(), "TimestampMicro()")
})
}
}
func TestCarbon_TimestampNano(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want int64
}{
{
name: "case1",
carbon: Parse(""),
want: 0,
},
{
name: "case2",
carbon: Parse("xxx"),
want: 0,
},
{
name: "case3",
carbon: Parse("2020-01-01 13:14:15"),
want: 1577855655000000000,
},
{
name: "case4",
carbon: Parse("2020-01-31 13:14:15"),
want: 1580447655000000000,
},
{
name: "case5",
carbon: Parse("2020-02-01 13:14:15"),
want: 1580534055000000000,
},
{
name: "case6",
carbon: Parse("2020-02-28 13:14:15"),
want: 1582866855000000000,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.TimestampNano(), "TimestampNano()")
})
}
}
func TestCarbon_Timezone(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want string
}{
{
name: "case1",
carbon: Parse(""),
want: "",
},
{
name: "case2",
carbon: Parse("xxx"),
want: "",
},
{
name: "case3",
carbon: Now(PRC),
want: "CST",
},
{
name: "case4",
carbon: Now(Tokyo),
want: "JST",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.Timezone(), "Timezone()")
})
}
}
func TestCarbon_Location(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want string
}{
{
name: "case1",
carbon: Parse(""),
want: "",
},
{
name: "case2",
carbon: Parse("xxx"),
want: "",
},
{
name: "case3",
carbon: Now(PRC),
want: PRC,
},
{
name: "case4",
carbon: Now(Tokyo),
want: Tokyo,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.Location(), "Location()")
})
}
}
func TestCarbon_Offset(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want int
}{
{
name: "case1",
carbon: Parse(""),
want: 0,
},
{
name: "case2",
carbon: Parse("xxx"),
want: 0,
},
{
name: "case3",
carbon: Now(PRC),
want: 28800,
},
{
name: "case4",
carbon: Now(Tokyo),
want: 32400,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.Offset(), "Offset()")
})
}
}
func TestCarbon_Locale(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want string
}{
{
name: "case1",
carbon: Now().SetLocale(""),
want: "",
},
{
name: "case2",
carbon: Now().SetLocale("en"),
want: "en",
},
{
name: "case3",
carbon: Now().SetLocale("zh-CN"),
want: "zh-CN",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.Locale(), "Locale()")
})
}
}
func TestCarbon_Age(t *testing.T) {
tests := []struct {
name string
carbon Carbon
want int
}{
{
name: "case1",
carbon: Parse(""),
want: 0,
},
{
name: "case2",
carbon: Parse("xxx"),
want: 0,
},
{
name: "case3",
carbon: Now().AddYears(18),
want: 0,
},
{
name: "case4",
carbon: Now().SubYears(18),
want: 18,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.carbon.Age(), "Age()")
})
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/latke/carbon.git
git@gitee.com:latke/carbon.git
latke
carbon
carbon
master

搜索帮助