github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/timeutil/since_test.go (about) 1 // Copyright 2023 The GitBundle Inc. All rights reserved. 2 // Copyright 2017 The Gitea Authors. All rights reserved. 3 // Use of this source code is governed by a MIT-style 4 // license that can be found in the LICENSE file. 5 6 package timeutil_test 7 8 import ( 9 "fmt" 10 "html/template" 11 "os" 12 "testing" 13 "time" 14 15 _ "unsafe" 16 17 "github.com/gitbundle/modules/log" 18 "github.com/gitbundle/modules/setting" 19 "github.com/gitbundle/modules/timeutil" 20 "github.com/gitbundle/modules/translation" 21 "github.com/gitbundle/modules/translation/i18n" 22 23 "github.com/stretchr/testify/assert" 24 ) 25 26 var BaseDate time.Time 27 28 // time durations 29 const ( 30 DayDur = 24 * time.Hour 31 WeekDur = 7 * DayDur 32 MonthDur = 30 * DayDur 33 YearDur = 12 * MonthDur 34 ) 35 36 func TestMain(m *testing.M) { 37 setting.StaticRootPath = os.Getenv("MAGIT_ROOT_PATH") 38 if setting.StaticRootPath == "" { 39 log.Fatal("Must specify `MAGIT_ROOT_PATH` environment variable first") 40 } 41 setting.Names = []string{"english"} 42 setting.Langs = []string{"en-US"} 43 // setup 44 translation.InitLocales(setting.Dir, setting.Locale) 45 BaseDate = time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC) 46 47 // run the tests 48 retVal := m.Run() 49 50 os.Exit(retVal) 51 } 52 53 //go:linkname timeSince github.com/gitbundle/modules/timeutil.timeSince 54 func timeSince(then, now time.Time, lang string) string 55 56 //go:linkname timeSincePro github.com/gitbundle/modules/timeutil.timeSincePro 57 func timeSincePro(then, now time.Time, lang string) string 58 59 func TestTimeSince(t *testing.T) { 60 assert.Equal(t, "now", timeSince(BaseDate, BaseDate, "en")) 61 62 // test that each diff in `diffs` yields the expected string 63 test := func(expected string, diffs ...time.Duration) { 64 t.Run(expected, func(t *testing.T) { 65 for _, diff := range diffs { 66 actual := timeSince(BaseDate, BaseDate.Add(diff), "en") 67 assert.Equal(t, i18n.Tr("en", "tool.ago", expected), actual) 68 actual = timeSince(BaseDate.Add(diff), BaseDate, "en") 69 assert.Equal(t, i18n.Tr("en", "tool.from_now", expected), actual) 70 } 71 }) 72 } 73 test("1 second", time.Second, time.Second+50*time.Millisecond) 74 test("2 seconds", 2*time.Second, 2*time.Second+50*time.Millisecond) 75 test("1 minute", time.Minute, time.Minute+29*time.Second) 76 test("2 minutes", 2*time.Minute, time.Minute+30*time.Second) 77 test("2 minutes", 2*time.Minute, 2*time.Minute+29*time.Second) 78 test("1 hour", time.Hour, time.Hour+29*time.Minute) 79 test("2 hours", 2*time.Hour, time.Hour+30*time.Minute) 80 test("2 hours", 2*time.Hour, 2*time.Hour+29*time.Minute) 81 test("3 hours", 3*time.Hour, 2*time.Hour+30*time.Minute) 82 test("1 day", DayDur, DayDur+11*time.Hour) 83 test("2 days", 2*DayDur, DayDur+12*time.Hour) 84 test("2 days", 2*DayDur, 2*DayDur+11*time.Hour) 85 test("3 days", 3*DayDur, 2*DayDur+12*time.Hour) 86 test("1 week", WeekDur, WeekDur+3*DayDur) 87 test("2 weeks", 2*WeekDur, WeekDur+4*DayDur) 88 test("2 weeks", 2*WeekDur, 2*WeekDur+3*DayDur) 89 test("3 weeks", 3*WeekDur, 2*WeekDur+4*DayDur) 90 test("1 month", MonthDur, MonthDur+14*DayDur) 91 test("2 months", 2*MonthDur, MonthDur+15*DayDur) 92 test("2 months", 2*MonthDur, 2*MonthDur+14*DayDur) 93 test("1 year", YearDur, YearDur+5*MonthDur) 94 test("2 years", 2*YearDur, YearDur+6*MonthDur) 95 test("2 years", 2*YearDur, 2*YearDur+5*MonthDur) 96 test("3 years", 3*YearDur, 2*YearDur+6*MonthDur) 97 } 98 99 func TestTimeSincePro(t *testing.T) { 100 assert.Equal(t, "now", timeSincePro(BaseDate, BaseDate, "en")) 101 102 // test that a difference of `diff` yields the expected string 103 test := func(expected string, diff time.Duration) { 104 actual := timeSincePro(BaseDate, BaseDate.Add(diff), "en") 105 assert.Equal(t, expected, actual) 106 assert.Equal(t, "future", timeSincePro(BaseDate.Add(diff), BaseDate, "en")) 107 } 108 test("1 second", time.Second) 109 test("2 seconds", 2*time.Second) 110 test("1 minute", time.Minute) 111 test("1 minute, 1 second", time.Minute+time.Second) 112 test("1 minute, 59 seconds", time.Minute+59*time.Second) 113 test("2 minutes", 2*time.Minute) 114 test("1 hour", time.Hour) 115 test("1 hour, 1 second", time.Hour+time.Second) 116 test("1 hour, 59 minutes, 59 seconds", time.Hour+59*time.Minute+59*time.Second) 117 test("2 hours", 2*time.Hour) 118 test("1 day", DayDur) 119 test("1 day, 23 hours, 59 minutes, 59 seconds", 120 DayDur+23*time.Hour+59*time.Minute+59*time.Second) 121 test("2 days", 2*DayDur) 122 test("1 week", WeekDur) 123 test("2 weeks", 2*WeekDur) 124 test("1 month", MonthDur) 125 test("3 months", 3*MonthDur) 126 test("1 year", YearDur) 127 test("2 years, 3 months, 1 week, 2 days, 4 hours, 12 minutes, 17 seconds", 128 2*YearDur+3*MonthDur+WeekDur+2*DayDur+4*time.Hour+ 129 12*time.Minute+17*time.Second) 130 } 131 132 //go:linkname htmlTimeSince github.com/gitbundle/modules/timeutil.htmlTimeSince 133 func htmlTimeSince(then, now time.Time, lang string) template.HTML 134 135 func TestHtmlTimeSince(t *testing.T) { 136 setting.TimeFormat = time.UnixDate 137 setting.DefaultUILocation = time.UTC 138 // test that `diff` yields a result containing `expected` 139 test := func(expected string, diff time.Duration) { 140 actual := htmlTimeSince(BaseDate, BaseDate.Add(diff), "en") 141 assert.Contains(t, actual, `title="Sat Jan 1 00:00:00 UTC 2000"`) 142 assert.Contains(t, actual, expected) 143 } 144 test("1 second", time.Second) 145 test("3 minutes", 3*time.Minute+5*time.Second) 146 test("1 day", DayDur+11*time.Hour) 147 test("1 week", WeekDur+3*DayDur) 148 test("3 months", 3*MonthDur+2*WeekDur) 149 test("2 years", 2*YearDur) 150 test("3 years", 2*YearDur+11*MonthDur+4*WeekDur) 151 } 152 153 //go:linkname computeTimeDiff github.com/gitbundle/modules/timeutil.computeTimeDiff 154 func computeTimeDiff(diff int64, lang string) (int64, string) 155 156 func TestComputeTimeDiff(t *testing.T) { 157 // test that for each offset in offsets, 158 // computeTimeDiff(base + offset) == (offset, str) 159 test := func(base int64, str string, offsets ...int64) { 160 for _, offset := range offsets { 161 t.Run(fmt.Sprintf("%s:%d", str, offset), func(t *testing.T) { 162 diff, diffStr := computeTimeDiff(base+offset, "en") 163 assert.Equal(t, offset, diff) 164 assert.Equal(t, str, diffStr) 165 }) 166 } 167 } 168 test(0, "now", 0) 169 test(1, "1 second", 0) 170 test(2, "2 seconds", 0) 171 test(timeutil.Minute, "1 minute", 0, 1, 29) 172 test(timeutil.Minute, "2 minutes", 30, timeutil.Minute-1) 173 test(2*timeutil.Minute, "2 minutes", 0, 29) 174 test(2*timeutil.Minute, "3 minutes", 30, timeutil.Minute-1) 175 test(timeutil.Hour, "1 hour", 0, 1, 29*timeutil.Minute) 176 test(timeutil.Hour, "2 hours", 30*timeutil.Minute, timeutil.Hour-1) 177 test(5*timeutil.Hour, "5 hours", 0, 29*timeutil.Minute) 178 test(timeutil.Day, "1 day", 0, 1, 11*timeutil.Hour) 179 test(timeutil.Day, "2 days", 12*timeutil.Hour, timeutil.Day-1) 180 test(5*timeutil.Day, "5 days", 0, 11*timeutil.Hour) 181 test(timeutil.Week, "1 week", 0, 1, 3*timeutil.Day) 182 test(timeutil.Week, "2 weeks", 4*timeutil.Day, timeutil.Week-1) 183 test(3*timeutil.Week, "3 weeks", 0, 3*timeutil.Day) 184 test(timeutil.Month, "1 month", 0, 1) 185 test(timeutil.Month, "2 months", 16*timeutil.Day, timeutil.Month-1) 186 test(10*timeutil.Month, "10 months", 0, 13*timeutil.Day) 187 test(timeutil.Year, "1 year", 0, 179*timeutil.Day) 188 test(timeutil.Year, "2 years", 180*timeutil.Day, timeutil.Year-1) 189 test(3*timeutil.Year, "3 years", 0, 179*timeutil.Day) 190 } 191 192 func TestMinutesToFriendly(t *testing.T) { 193 // test that a number of minutes yields the expected string 194 test := func(expected string, minutes int) { 195 actual := timeutil.MinutesToFriendly(minutes, "en") 196 assert.Equal(t, expected, actual) 197 } 198 test("1 minute", 1) 199 test("2 minutes", 2) 200 test("1 hour", 60) 201 test("1 hour, 1 minute", 61) 202 test("1 hour, 2 minutes", 62) 203 test("2 hours", 120) 204 }