@@ -2,43 +2,13 @@ package base
2
2
3
3
import (
4
4
"net/url"
5
- "os"
6
5
"testing"
7
- "time"
8
6
9
7
"code.gitea.io/gitea/modules/setting"
10
8
11
- "github.com/Unknwon/i18n"
12
- macaroni18n "github.com/go-macaron/i18n"
13
9
"github.com/stretchr/testify/assert"
14
10
)
15
11
16
- var BaseDate time.Time
17
-
18
- // time durations
19
- const (
20
- DayDur = 24 * time .Hour
21
- WeekDur = 7 * DayDur
22
- MonthDur = 30 * DayDur
23
- YearDur = 12 * MonthDur
24
- )
25
-
26
- func TestMain (m * testing.M ) {
27
- // setup
28
- macaroni18n .I18n (macaroni18n.Options {
29
- Directory : "../../options/locale/" ,
30
- DefaultLang : "en-US" ,
31
- Langs : []string {"en-US" },
32
- Names : []string {"english" },
33
- })
34
- BaseDate = time .Date (2000 , time .January , 1 , 0 , 0 , 0 , 0 , time .UTC )
35
-
36
- // run the tests
37
- retVal := m .Run ()
38
-
39
- os .Exit (retVal )
40
- }
41
-
42
12
func TestEncodeMD5 (t * testing.T ) {
43
13
assert .Equal (t ,
44
14
"3858f62230ac3c915f300c664312c63f" ,
@@ -131,125 +101,6 @@ func TestAvatarLink(t *testing.T) {
131
101
)
132
102
}
133
103
134
- func TestComputeTimeDiff (t * testing.T ) {
135
- // test that for each offset in offsets,
136
- // computeTimeDiff(base + offset) == (offset, str)
137
- test := func (base int64 , str string , offsets ... int64 ) {
138
- for _ , offset := range offsets {
139
- diff , diffStr := computeTimeDiff (base + offset , "en" )
140
- assert .Equal (t , offset , diff )
141
- assert .Equal (t , str , diffStr )
142
- }
143
- }
144
- test (0 , "now" , 0 )
145
- test (1 , "1 second" , 0 )
146
- test (2 , "2 seconds" , 0 )
147
- test (Minute , "1 minute" , 0 , 1 , 30 , Minute - 1 )
148
- test (2 * Minute , "2 minutes" , 0 , Minute - 1 )
149
- test (Hour , "1 hour" , 0 , 1 , Hour - 1 )
150
- test (5 * Hour , "5 hours" , 0 , Hour - 1 )
151
- test (Day , "1 day" , 0 , 1 , Day - 1 )
152
- test (5 * Day , "5 days" , 0 , Day - 1 )
153
- test (Week , "1 week" , 0 , 1 , Week - 1 )
154
- test (3 * Week , "3 weeks" , 0 , 4 * Day + 25000 )
155
- test (Month , "1 month" , 0 , 1 , Month - 1 )
156
- test (10 * Month , "10 months" , 0 , Month - 1 )
157
- test (Year , "1 year" , 0 , Year - 1 )
158
- test (3 * Year , "3 years" , 0 , Year - 1 )
159
- }
160
-
161
- func TestMinutesToFriendly (t * testing.T ) {
162
- // test that a number of minutes yields the expected string
163
- test := func (expected string , minutes int ) {
164
- actual := MinutesToFriendly (minutes , "en" )
165
- assert .Equal (t , expected , actual )
166
- }
167
- test ("1 minute" , 1 )
168
- test ("2 minutes" , 2 )
169
- test ("1 hour" , 60 )
170
- test ("1 hour, 1 minute" , 61 )
171
- test ("1 hour, 2 minutes" , 62 )
172
- test ("2 hours" , 120 )
173
- }
174
-
175
- func TestTimeSince (t * testing.T ) {
176
- assert .Equal (t , "now" , timeSince (BaseDate , BaseDate , "en" ))
177
-
178
- // test that each diff in `diffs` yields the expected string
179
- test := func (expected string , diffs ... time.Duration ) {
180
- for _ , diff := range diffs {
181
- actual := timeSince (BaseDate , BaseDate .Add (diff ), "en" )
182
- assert .Equal (t , i18n .Tr ("en" , "tool.ago" , expected ), actual )
183
- actual = timeSince (BaseDate .Add (diff ), BaseDate , "en" )
184
- assert .Equal (t , i18n .Tr ("en" , "tool.from_now" , expected ), actual )
185
- }
186
- }
187
- test ("1 second" , time .Second , time .Second + 50 * time .Millisecond )
188
- test ("2 seconds" , 2 * time .Second , 2 * time .Second + 50 * time .Millisecond )
189
- test ("1 minute" , time .Minute , time .Minute + 30 * time .Second )
190
- test ("2 minutes" , 2 * time .Minute , 2 * time .Minute + 30 * time .Second )
191
- test ("1 hour" , time .Hour , time .Hour + 30 * time .Minute )
192
- test ("2 hours" , 2 * time .Hour , 2 * time .Hour + 30 * time .Minute )
193
- test ("1 day" , DayDur , DayDur + 12 * time .Hour )
194
- test ("2 days" , 2 * DayDur , 2 * DayDur + 12 * time .Hour )
195
- test ("1 week" , WeekDur , WeekDur + 3 * DayDur )
196
- test ("2 weeks" , 2 * WeekDur , 2 * WeekDur + 3 * DayDur )
197
- test ("1 month" , MonthDur , MonthDur + 15 * DayDur )
198
- test ("2 months" , 2 * MonthDur , 2 * MonthDur + 15 * DayDur )
199
- test ("1 year" , YearDur , YearDur + 6 * MonthDur )
200
- test ("2 years" , 2 * YearDur , 2 * YearDur + 6 * MonthDur )
201
- }
202
-
203
- func TestTimeSincePro (t * testing.T ) {
204
- assert .Equal (t , "now" , timeSincePro (BaseDate , BaseDate , "en" ))
205
-
206
- // test that a difference of `diff` yields the expected string
207
- test := func (expected string , diff time.Duration ) {
208
- actual := timeSincePro (BaseDate , BaseDate .Add (diff ), "en" )
209
- assert .Equal (t , expected , actual )
210
- assert .Equal (t , "future" , timeSincePro (BaseDate .Add (diff ), BaseDate , "en" ))
211
- }
212
- test ("1 second" , time .Second )
213
- test ("2 seconds" , 2 * time .Second )
214
- test ("1 minute" , time .Minute )
215
- test ("1 minute, 1 second" , time .Minute + time .Second )
216
- test ("1 minute, 59 seconds" , time .Minute + 59 * time .Second )
217
- test ("2 minutes" , 2 * time .Minute )
218
- test ("1 hour" , time .Hour )
219
- test ("1 hour, 1 second" , time .Hour + time .Second )
220
- test ("1 hour, 59 minutes, 59 seconds" , time .Hour + 59 * time .Minute + 59 * time .Second )
221
- test ("2 hours" , 2 * time .Hour )
222
- test ("1 day" , DayDur )
223
- test ("1 day, 23 hours, 59 minutes, 59 seconds" ,
224
- DayDur + 23 * time .Hour + 59 * time .Minute + 59 * time .Second )
225
- test ("2 days" , 2 * DayDur )
226
- test ("1 week" , WeekDur )
227
- test ("2 weeks" , 2 * WeekDur )
228
- test ("1 month" , MonthDur )
229
- test ("3 months" , 3 * MonthDur )
230
- test ("1 year" , YearDur )
231
- test ("2 years, 3 months, 1 week, 2 days, 4 hours, 12 minutes, 17 seconds" ,
232
- 2 * YearDur + 3 * MonthDur + WeekDur + 2 * DayDur + 4 * time .Hour +
233
- 12 * time .Minute + 17 * time .Second )
234
- }
235
-
236
- func TestHtmlTimeSince (t * testing.T ) {
237
- setting .TimeFormat = time .UnixDate
238
- // test that `diff` yields a result containing `expected`
239
- test := func (expected string , diff time.Duration ) {
240
- actual := htmlTimeSince (BaseDate , BaseDate .Add (diff ), "en" )
241
- assert .Contains (t , actual , `title="Sat Jan 1 00:00:00 UTC 2000"` )
242
- assert .Contains (t , actual , expected )
243
- }
244
- test ("1 second" , time .Second )
245
- test ("3 minutes" , 3 * time .Minute + 5 * time .Second )
246
- test ("1 day" , DayDur + 18 * time .Hour )
247
- test ("1 week" , WeekDur + 6 * DayDur )
248
- test ("3 months" , 3 * MonthDur + 3 * WeekDur )
249
- test ("2 years" , 2 * YearDur )
250
- test ("3 years" , 3 * YearDur + 11 * MonthDur + 4 * WeekDur )
251
- }
252
-
253
104
func TestFileSize (t * testing.T ) {
254
105
var size int64 = 512
255
106
assert .Equal (t , "512B" , FileSize (size ))
0 commit comments