Skip to content

Commit e2e9b2e

Browse files
committed
fix tests
1 parent 2222e30 commit e2e9b2e

File tree

5 files changed

+165
-153
lines changed

5 files changed

+165
-153
lines changed

models/gpg_key_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"testing"
99
"time"
1010

11-
"code.gitea.io/gitea/modules/util"
11+
"code.gitea.io/gitea/modules/timeutil"
1212

1313
"github.com/stretchr/testify/assert"
1414
)

models/issue_stopwatch_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package models
33
import (
44
"testing"
55

6-
"code.gitea.io/gitea/modules/util"
6+
"code.gitea.io/gitea/modules/timeutil"
77

88
"github.com/stretchr/testify/assert"
99
)

modules/base/tool_test.go

Lines changed: 0 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,13 @@ package base
22

33
import (
44
"net/url"
5-
"os"
65
"testing"
7-
"time"
86

97
"code.gitea.io/gitea/modules/setting"
108

11-
"github.com/Unknwon/i18n"
12-
macaroni18n "github.com/go-macaron/i18n"
139
"github.com/stretchr/testify/assert"
1410
)
1511

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-
4212
func TestEncodeMD5(t *testing.T) {
4313
assert.Equal(t,
4414
"3858f62230ac3c915f300c664312c63f",
@@ -131,125 +101,6 @@ func TestAvatarLink(t *testing.T) {
131101
)
132102
}
133103

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-
253104
func TestFileSize(t *testing.T) {
254105
var size int64 = 512
255106
assert.Equal(t, "512B", FileSize(size))

modules/timeutil/since.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ func TimeSince(then time.Time, lang string) template.HTML {
147147
}
148148

149149
func htmlTimeSince(then, now time.Time, lang string) template.HTML {
150-
fmt.Printf("2----- %v ------- %v\n", then, then.In(setting.DefaultUILocation).Format(GetTimeFormat(lang)))
151150
return template.HTML(fmt.Sprintf(`<span class="time-since" title="%s">%s</span>`,
152151
then.In(setting.DefaultUILocation).Format(GetTimeFormat(lang)),
153152
timeSince(then, now, lang)))
@@ -159,7 +158,6 @@ func TimeSinceUnix(then TimeStamp, lang string) template.HTML {
159158
}
160159

161160
func htmlTimeSinceUnix(then, now TimeStamp, lang string) template.HTML {
162-
fmt.Printf("1----- %v ------- %v\n", then, then.FormatInLocation(GetTimeFormat(lang), setting.DefaultUILocation))
163161
return template.HTML(fmt.Sprintf(`<span class="time-since" title="%s">%s</span>`,
164162
then.FormatInLocation(GetTimeFormat(lang), setting.DefaultUILocation),
165163
timeSinceUnix(int64(then), int64(now), lang)))

modules/timeutil/since_test.go

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
// Copyright 2019 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package timeutil
6+
7+
import (
8+
"os"
9+
"testing"
10+
"time"
11+
12+
"code.gitea.io/gitea/modules/setting"
13+
14+
"github.com/Unknwon/i18n"
15+
macaroni18n "github.com/go-macaron/i18n"
16+
"github.com/stretchr/testify/assert"
17+
)
18+
19+
var BaseDate time.Time
20+
21+
// time durations
22+
const (
23+
DayDur = 24 * time.Hour
24+
WeekDur = 7 * DayDur
25+
MonthDur = 30 * DayDur
26+
YearDur = 12 * MonthDur
27+
)
28+
29+
func TestMain(m *testing.M) {
30+
// setup
31+
macaroni18n.I18n(macaroni18n.Options{
32+
Directory: "../../options/locale/",
33+
DefaultLang: "en-US",
34+
Langs: []string{"en-US"},
35+
Names: []string{"english"},
36+
})
37+
BaseDate = time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC)
38+
39+
// run the tests
40+
retVal := m.Run()
41+
42+
os.Exit(retVal)
43+
}
44+
45+
func TestTimeSince(t *testing.T) {
46+
assert.Equal(t, "now", timeSince(BaseDate, BaseDate, "en"))
47+
48+
// test that each diff in `diffs` yields the expected string
49+
test := func(expected string, diffs ...time.Duration) {
50+
for _, diff := range diffs {
51+
actual := timeSince(BaseDate, BaseDate.Add(diff), "en")
52+
assert.Equal(t, i18n.Tr("en", "tool.ago", expected), actual)
53+
actual = timeSince(BaseDate.Add(diff), BaseDate, "en")
54+
assert.Equal(t, i18n.Tr("en", "tool.from_now", expected), actual)
55+
}
56+
}
57+
test("1 second", time.Second, time.Second+50*time.Millisecond)
58+
test("2 seconds", 2*time.Second, 2*time.Second+50*time.Millisecond)
59+
test("1 minute", time.Minute, time.Minute+30*time.Second)
60+
test("2 minutes", 2*time.Minute, 2*time.Minute+30*time.Second)
61+
test("1 hour", time.Hour, time.Hour+30*time.Minute)
62+
test("2 hours", 2*time.Hour, 2*time.Hour+30*time.Minute)
63+
test("1 day", DayDur, DayDur+12*time.Hour)
64+
test("2 days", 2*DayDur, 2*DayDur+12*time.Hour)
65+
test("1 week", WeekDur, WeekDur+3*DayDur)
66+
test("2 weeks", 2*WeekDur, 2*WeekDur+3*DayDur)
67+
test("1 month", MonthDur, MonthDur+15*DayDur)
68+
test("2 months", 2*MonthDur, 2*MonthDur+15*DayDur)
69+
test("1 year", YearDur, YearDur+6*MonthDur)
70+
test("2 years", 2*YearDur, 2*YearDur+6*MonthDur)
71+
}
72+
73+
func TestTimeSincePro(t *testing.T) {
74+
assert.Equal(t, "now", timeSincePro(BaseDate, BaseDate, "en"))
75+
76+
// test that a difference of `diff` yields the expected string
77+
test := func(expected string, diff time.Duration) {
78+
actual := timeSincePro(BaseDate, BaseDate.Add(diff), "en")
79+
assert.Equal(t, expected, actual)
80+
assert.Equal(t, "future", timeSincePro(BaseDate.Add(diff), BaseDate, "en"))
81+
}
82+
test("1 second", time.Second)
83+
test("2 seconds", 2*time.Second)
84+
test("1 minute", time.Minute)
85+
test("1 minute, 1 second", time.Minute+time.Second)
86+
test("1 minute, 59 seconds", time.Minute+59*time.Second)
87+
test("2 minutes", 2*time.Minute)
88+
test("1 hour", time.Hour)
89+
test("1 hour, 1 second", time.Hour+time.Second)
90+
test("1 hour, 59 minutes, 59 seconds", time.Hour+59*time.Minute+59*time.Second)
91+
test("2 hours", 2*time.Hour)
92+
test("1 day", DayDur)
93+
test("1 day, 23 hours, 59 minutes, 59 seconds",
94+
DayDur+23*time.Hour+59*time.Minute+59*time.Second)
95+
test("2 days", 2*DayDur)
96+
test("1 week", WeekDur)
97+
test("2 weeks", 2*WeekDur)
98+
test("1 month", MonthDur)
99+
test("3 months", 3*MonthDur)
100+
test("1 year", YearDur)
101+
test("2 years, 3 months, 1 week, 2 days, 4 hours, 12 minutes, 17 seconds",
102+
2*YearDur+3*MonthDur+WeekDur+2*DayDur+4*time.Hour+
103+
12*time.Minute+17*time.Second)
104+
}
105+
106+
func TestHtmlTimeSince(t *testing.T) {
107+
setting.TimeFormat = time.UnixDate
108+
setting.DefaultUILocation = time.UTC
109+
// test that `diff` yields a result containing `expected`
110+
test := func(expected string, diff time.Duration) {
111+
actual := htmlTimeSince(BaseDate, BaseDate.Add(diff), "en")
112+
assert.Contains(t, actual, `title="Sat Jan 1 00:00:00 UTC 2000"`)
113+
assert.Contains(t, actual, expected)
114+
}
115+
test("1 second", time.Second)
116+
test("3 minutes", 3*time.Minute+5*time.Second)
117+
test("1 day", DayDur+18*time.Hour)
118+
test("1 week", WeekDur+6*DayDur)
119+
test("3 months", 3*MonthDur+3*WeekDur)
120+
test("2 years", 2*YearDur)
121+
test("3 years", 3*YearDur+11*MonthDur+4*WeekDur)
122+
}
123+
124+
func TestComputeTimeDiff(t *testing.T) {
125+
// test that for each offset in offsets,
126+
// computeTimeDiff(base + offset) == (offset, str)
127+
test := func(base int64, str string, offsets ...int64) {
128+
for _, offset := range offsets {
129+
diff, diffStr := computeTimeDiff(base+offset, "en")
130+
assert.Equal(t, offset, diff)
131+
assert.Equal(t, str, diffStr)
132+
}
133+
}
134+
test(0, "now", 0)
135+
test(1, "1 second", 0)
136+
test(2, "2 seconds", 0)
137+
test(Minute, "1 minute", 0, 1, 30, Minute-1)
138+
test(2*Minute, "2 minutes", 0, Minute-1)
139+
test(Hour, "1 hour", 0, 1, Hour-1)
140+
test(5*Hour, "5 hours", 0, Hour-1)
141+
test(Day, "1 day", 0, 1, Day-1)
142+
test(5*Day, "5 days", 0, Day-1)
143+
test(Week, "1 week", 0, 1, Week-1)
144+
test(3*Week, "3 weeks", 0, 4*Day+25000)
145+
test(Month, "1 month", 0, 1, Month-1)
146+
test(10*Month, "10 months", 0, Month-1)
147+
test(Year, "1 year", 0, Year-1)
148+
test(3*Year, "3 years", 0, Year-1)
149+
}
150+
151+
func TestMinutesToFriendly(t *testing.T) {
152+
// test that a number of minutes yields the expected string
153+
test := func(expected string, minutes int) {
154+
actual := MinutesToFriendly(minutes, "en")
155+
assert.Equal(t, expected, actual)
156+
}
157+
test("1 minute", 1)
158+
test("2 minutes", 2)
159+
test("1 hour", 60)
160+
test("1 hour, 1 minute", 61)
161+
test("1 hour, 2 minutes", 62)
162+
test("2 hours", 120)
163+
}

0 commit comments

Comments
 (0)