You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
5.2 KiB

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