@@ -13,7 +13,7 @@ func TestCarbon_Parse(t *testing.T) {
13
13
tests := []struct {
14
14
id int // 测试id
15
15
input string // 输入值
16
- output string // 期望输出值
16
+ output string // 期望值
17
17
}{
18
18
{1 , "" , "" },
19
19
{2 , "0" , "" },
@@ -29,20 +29,35 @@ func TestCarbon_Parse(t *testing.T) {
29
29
}
30
30
31
31
for _ , test := range tests {
32
- c := Parse (test .input )
32
+ c := SetTimezone ( PRC ). Parse (test .input )
33
33
assert .Nil (c .Error )
34
- assert .Equal (c .ToDateTimeStringWithTimezone (PRC ), test .output , "Current test id is " + strconv .Itoa (test .id ))
34
+ assert .Equal (test .output , c .ToDateTimeString (), "Current test id is " + strconv .Itoa (test .id ))
35
+ }
36
+
37
+ for _ , test := range tests {
38
+ c := Parse (test .input , PRC )
39
+ assert .Nil (c .Error )
40
+ assert .Equal (test .output , c .ToDateTimeString (), "Current test id is " + strconv .Itoa (test .id ))
35
41
}
36
42
}
37
43
44
+ func TestError_Parse (t * testing.T ) {
45
+ value , timezone := "2020-13-50" , "xxx"
46
+ c1 := Parse (value )
47
+ assert .Equal (t , invalidValueError (value ), c1 .Error , "Should catch an exception in Parse()" )
48
+
49
+ c2 := Parse (value , timezone )
50
+ assert .Equal (t , invalidTimezoneError (timezone ), c2 .Error , "Should catch an exception in Parse()" )
51
+ }
52
+
38
53
func TestCarbon_ParseByFormat (t * testing.T ) {
39
54
assert := assert .New (t )
40
55
41
56
tests := []struct {
42
57
id int // 测试id
43
58
input string // 输入值
44
59
format string // 输入参数
45
- output string // 期望输出值
60
+ output string // 期望值
46
61
}{
47
62
{1 , "" , "Y|m|d H:i:s" , "" },
48
63
{2 , "0" , "Y|m|d H:i:s" , "" },
@@ -56,20 +71,35 @@ func TestCarbon_ParseByFormat(t *testing.T) {
56
71
}
57
72
58
73
for _ , test := range tests {
59
- c := ParseByFormat (test .input , test .format )
74
+ c := SetTimezone (PRC ).ParseByFormat (test .input , test .format )
75
+ assert .Nil (c .Error )
76
+ assert .Equal (test .output , c .ToDateTimeString (), "Current test id is " + strconv .Itoa (test .id ))
77
+ }
78
+
79
+ for _ , test := range tests {
80
+ c := ParseByFormat (test .input , test .format , PRC )
60
81
assert .Nil (c .Error )
61
- assert .Equal (c .ToDateTimeString (), test . output , "Current test id is " + strconv .Itoa (test .id ))
82
+ assert .Equal (test . output , c .ToDateTimeString (), "Current test id is " + strconv .Itoa (test .id ))
62
83
}
63
84
}
64
85
86
+ func TestError_ParseByFormat (t * testing.T ) {
87
+ value , format , timezone := "2020-08-50" , "Y-m-d" , "xxx"
88
+ c1 := ParseByFormat (value , format )
89
+ assert .Equal (t , invalidFormatError (value , format ), c1 .Error , "Should catch an exception in ParseByFormat()" )
90
+
91
+ c2 := ParseByFormat (value , format , timezone )
92
+ assert .Equal (t , invalidTimezoneError (timezone ), c2 .Error , "Should catch an exception in ParseByFormat()" )
93
+ }
94
+
65
95
func TestCarbon_ParseByLayout (t * testing.T ) {
66
96
assert := assert .New (t )
67
97
68
98
tests := []struct {
69
99
id int // 测试id
70
100
input string // 输入值
71
- format string // 输入参数
72
- output string // 期望输出值
101
+ layout string // 输入参数
102
+ output string // 期望值
73
103
}{
74
104
{1 , "" , "2006-01-02" , "" },
75
105
{2 , "0" , "2006-01-02" , "" },
@@ -83,8 +113,23 @@ func TestCarbon_ParseByLayout(t *testing.T) {
83
113
}
84
114
85
115
for _ , test := range tests {
86
- c := ParseByLayout (test .input , test .format )
116
+ c := SetTimezone ( PRC ). ParseByLayout (test .input , test .layout )
87
117
assert .Nil (c .Error )
88
- assert .Equal (c .ToDateTimeString (), test . output , "Current test id is " + strconv .Itoa (test .id ))
118
+ assert .Equal (test . output , c .ToDateTimeString (), "Current test id is " + strconv .Itoa (test .id ))
89
119
}
120
+
121
+ for _ , test := range tests {
122
+ c := ParseByLayout (test .input , test .layout , PRC )
123
+ assert .Nil (c .Error )
124
+ assert .Equal (test .output , c .ToDateTimeString (), "Current test id is " + strconv .Itoa (test .id ))
125
+ }
126
+ }
127
+
128
+ func TestError_ParseByLayout (t * testing.T ) {
129
+ value , layout , timezone := "2020-08-50" , "2006-01-02" , "xxx"
130
+ c1 := ParseByLayout (value , layout )
131
+ assert .Equal (t , invalidLayoutError (value , layout ), c1 .Error , "Should catch an exception in ParseByLayout()" )
132
+
133
+ c2 := ParseByLayout (value , layout , timezone )
134
+ assert .Equal (t , invalidTimezoneError (timezone ), c2 .Error , "Should catch an exception in ParseByLayout()" )
90
135
}
0 commit comments