@@ -676,12 +676,155 @@ func TestCarbon_WeekOfMonth(t *testing.T) {
676676 }
677677}
678678
679+ func TestCarbon_Age (t * testing.T ) {
680+ Tests := []struct {
681+ input string // 输入值
682+ output int // 期望输出值
683+ }{
684+ {"0000-00-00" , 0 },
685+ {Now ().SubYears (18 ).ToDateTimeString (), 18 },
686+ }
687+
688+ for _ , v := range Tests {
689+ output := Parse (v .input ).Age ()
690+
691+ if output != v .output {
692+ t .Fatalf ("Input %s, expected %d, but got %d" , v .input , v .output , output )
693+ }
694+ }
695+ }
696+
697+ func TestCarbon_Year (t * testing.T ) {
698+ Tests := []struct {
699+ input string // 输入值
700+ output int // 期望输出值
701+ }{
702+ {"" , 0 },
703+ {"0000-00-00" , 0 },
704+ {"2020-08-05" , 2020 },
705+ }
706+
707+ for _ , v := range Tests {
708+ output := Parse (v .input ).Year ()
709+
710+ if output != v .output {
711+ t .Fatalf ("Input %s, expected %d, but got %d" , v .input , v .output , output )
712+ }
713+ }
714+ }
715+
716+ func TestCarbon_Month (t * testing.T ) {
717+ Tests := []struct {
718+ input string // 输入值
719+ output int // 期望输出值
720+ }{
721+ {"" , 0 },
722+ {"0000-00-00 00:00:00" , 0 },
723+ {"0000-00-00" , 0 },
724+ {"2020-08-05 13:14:15" , 8 },
725+ {"2020-08-05" , 8 },
726+ }
727+
728+ for _ , v := range Tests {
729+ output := Parse (v .input ).Month ()
730+
731+ if output != v .output {
732+ t .Fatalf ("Input %s, expected %d, but got %d" , v .input , v .output , output )
733+ }
734+ }
735+ }
736+
737+ func TestCarbon_Day (t * testing.T ) {
738+ Tests := []struct {
739+ input string // 输入值
740+ output int // 期望输出值
741+ }{
742+ {"" , 0 },
743+ {"0000-00-00 00:00:00" , 0 },
744+ {"0000-00-00" , 0 },
745+ {"2020-08-05 13:14:15" , 5 },
746+ {"2020-08-05" , 5 },
747+ }
748+
749+ for _ , v := range Tests {
750+ output := Parse (v .input ).Day ()
751+
752+ if output != v .output {
753+ t .Fatalf ("Input %s, expected %d, but got %d" , v .input , v .output , output )
754+ }
755+ }
756+ }
757+
758+ func TestCarbon_Hour (t * testing.T ) {
759+ Tests := []struct {
760+ input string // 输入值
761+ output int // 期望输出值
762+ }{
763+ {"" , 0 },
764+ {"0000-00-00 00:00:00" , 0 },
765+ {"0000-00-00" , 0 },
766+ {"2020-08-05 13:14:15" , 13 },
767+ {"2020-08-05" , 0 },
768+ }
769+
770+ for _ , v := range Tests {
771+ output := Parse (v .input ).Hour ()
772+
773+ if output != v .output {
774+ t .Fatalf ("Input %s, expected %d, but got %d" , v .input , v .output , output )
775+ }
776+ }
777+ }
778+
779+ func TestCarbon_Minute (t * testing.T ) {
780+ Tests := []struct {
781+ input string // 输入值
782+ output int // 期望输出值
783+ }{
784+ {"" , 0 },
785+ {"0000-00-00 00:00:00" , 0 },
786+ {"0000-00-00" , 0 },
787+ {"2020-08-05 13:14:15" , 14 },
788+ {"2020-08-05" , 0 },
789+ }
790+
791+ for _ , v := range Tests {
792+ output := Parse (v .input ).Minute ()
793+
794+ if output != v .output {
795+ t .Fatalf ("Input %s, expected %d, but got %d" , v .input , v .output , output )
796+ }
797+ }
798+ }
799+
800+ func TestCarbon_Second (t * testing.T ) {
801+ Tests := []struct {
802+ input string // 输入值
803+ output int // 期望输出值
804+ }{
805+ {"" , 0 },
806+ {"0000-00-00 00:00:00" , 0 },
807+ {"0000-00-00" , 0 },
808+ {"2020-08-05 13:14:15" , 15 },
809+ {"2020-08-05" , 0 },
810+ }
811+
812+ for _ , v := range Tests {
813+ output := Parse (v .input ).Second ()
814+
815+ if output != v .output {
816+ t .Fatalf ("Input %s, expected %d, but got %d" , v .input , v .output , output )
817+ }
818+ }
819+ }
820+
679821func TestCarbon_IsZero (t * testing.T ) {
680822 Tests := []struct {
681823 input string // 输入值
682824 output bool // 期望输出值
683825 }{
684826 {"0000-00-00 00:00:00" , true },
827+ {"0000-00-00" , true },
685828 {"2020-08-05 00:00:00" , false },
686829 {"2020-08-05" , false },
687830 }
0 commit comments