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.

124 lines
6.1 KiB

  1. package humanize
  2. import (
  3. "math"
  4. "testing"
  5. "time"
  6. )
  7. func TestPast(t *testing.T) {
  8. now := time.Now()
  9. testList{
  10. {"now", Time(now), "now"},
  11. {"1 second ago", Time(now.Add(-1 * time.Second)), "1 second ago"},
  12. {"12 seconds ago", Time(now.Add(-12 * time.Second)), "12 seconds ago"},
  13. {"30 seconds ago", Time(now.Add(-30 * time.Second)), "30 seconds ago"},
  14. {"45 seconds ago", Time(now.Add(-45 * time.Second)), "45 seconds ago"},
  15. {"1 minute ago", Time(now.Add(-63 * time.Second)), "1 minute ago"},
  16. {"15 minutes ago", Time(now.Add(-15 * time.Minute)), "15 minutes ago"},
  17. {"1 hour ago", Time(now.Add(-63 * time.Minute)), "1 hour ago"},
  18. {"2 hours ago", Time(now.Add(-2 * time.Hour)), "2 hours ago"},
  19. {"21 hours ago", Time(now.Add(-21 * time.Hour)), "21 hours ago"},
  20. {"1 day ago", Time(now.Add(-26 * time.Hour)), "1 day ago"},
  21. {"2 days ago", Time(now.Add(-49 * time.Hour)), "2 days ago"},
  22. {"3 days ago", Time(now.Add(-3 * Day)), "3 days ago"},
  23. {"1 week ago (1)", Time(now.Add(-7 * Day)), "1 week ago"},
  24. {"1 week ago (2)", Time(now.Add(-12 * Day)), "1 week ago"},
  25. {"2 weeks ago", Time(now.Add(-15 * Day)), "2 weeks ago"},
  26. {"1 month ago", Time(now.Add(-39 * Day)), "1 month ago"},
  27. {"3 months ago", Time(now.Add(-99 * Day)), "3 months ago"},
  28. {"1 year ago (1)", Time(now.Add(-365 * Day)), "1 year ago"},
  29. {"1 year ago (1)", Time(now.Add(-400 * Day)), "1 year ago"},
  30. {"2 years ago (1)", Time(now.Add(-548 * Day)), "2 years ago"},
  31. {"2 years ago (2)", Time(now.Add(-725 * Day)), "2 years ago"},
  32. {"2 years ago (3)", Time(now.Add(-800 * Day)), "2 years ago"},
  33. {"3 years ago", Time(now.Add(-3 * Year)), "3 years ago"},
  34. {"long ago", Time(now.Add(-LongTime)), "a long while ago"},
  35. }.validate(t)
  36. }
  37. func TestReltimeOffbyone(t *testing.T) {
  38. testList{
  39. {"1w-1", RelTime(time.Unix(0, 0), time.Unix(7*24*60*60, -1), "ago", ""), "6 days ago"},
  40. {"1w±0", RelTime(time.Unix(0, 0), time.Unix(7*24*60*60, 0), "ago", ""), "1 week ago"},
  41. {"1w+1", RelTime(time.Unix(0, 0), time.Unix(7*24*60*60, 1), "ago", ""), "1 week ago"},
  42. {"2w-1", RelTime(time.Unix(0, 0), time.Unix(14*24*60*60, -1), "ago", ""), "1 week ago"},
  43. {"2w±0", RelTime(time.Unix(0, 0), time.Unix(14*24*60*60, 0), "ago", ""), "2 weeks ago"},
  44. {"2w+1", RelTime(time.Unix(0, 0), time.Unix(14*24*60*60, 1), "ago", ""), "2 weeks ago"},
  45. }.validate(t)
  46. }
  47. func TestFuture(t *testing.T) {
  48. // Add a little time so that these things properly line up in
  49. // the future.
  50. now := time.Now().Add(time.Millisecond * 250)
  51. testList{
  52. {"now", Time(now), "now"},
  53. {"1 second from now", Time(now.Add(+1 * time.Second)), "1 second from now"},
  54. {"12 seconds from now", Time(now.Add(+12 * time.Second)), "12 seconds from now"},
  55. {"30 seconds from now", Time(now.Add(+30 * time.Second)), "30 seconds from now"},
  56. {"45 seconds from now", Time(now.Add(+45 * time.Second)), "45 seconds from now"},
  57. {"15 minutes from now", Time(now.Add(+15 * time.Minute)), "15 minutes from now"},
  58. {"2 hours from now", Time(now.Add(+2 * time.Hour)), "2 hours from now"},
  59. {"21 hours from now", Time(now.Add(+21 * time.Hour)), "21 hours from now"},
  60. {"1 day from now", Time(now.Add(+26 * time.Hour)), "1 day from now"},
  61. {"2 days from now", Time(now.Add(+49 * time.Hour)), "2 days from now"},
  62. {"3 days from now", Time(now.Add(+3 * Day)), "3 days from now"},
  63. {"1 week from now (1)", Time(now.Add(+7 * Day)), "1 week from now"},
  64. {"1 week from now (2)", Time(now.Add(+12 * Day)), "1 week from now"},
  65. {"2 weeks from now", Time(now.Add(+15 * Day)), "2 weeks from now"},
  66. {"1 month from now", Time(now.Add(+30 * Day)), "1 month from now"},
  67. {"1 year from now", Time(now.Add(+365 * Day)), "1 year from now"},
  68. {"2 years from now", Time(now.Add(+2 * Year)), "2 years from now"},
  69. {"a while from now", Time(now.Add(+LongTime)), "a long while from now"},
  70. }.validate(t)
  71. }
  72. func TestRange(t *testing.T) {
  73. start := time.Time{}
  74. end := time.Unix(math.MaxInt64, math.MaxInt64)
  75. x := RelTime(start, end, "ago", "from now")
  76. if x != "a long while from now" {
  77. t.Errorf("Expected a long while from now, got %q", x)
  78. }
  79. }
  80. func TestCustomRelTime(t *testing.T) {
  81. now := time.Now().Add(time.Millisecond * 250)
  82. magnitudes := []RelTimeMagnitude{
  83. {time.Second, "now", time.Second},
  84. {2 * time.Second, "1 second %s", 1},
  85. {time.Minute, "%d seconds %s", time.Second},
  86. {Day - time.Second, "%d minutes %s", time.Minute},
  87. {Day, "%d hours %s", time.Hour},
  88. {2 * Day, "1 day %s", 1},
  89. {Week, "%d days %s", Day},
  90. {2 * Week, "1 week %s", 1},
  91. {6 * Month, "%d weeks %s", Week},
  92. {Year, "%d months %s", Month},
  93. }
  94. customRelTime := func(then time.Time) string {
  95. return CustomRelTime(then, time.Now(), "ago", "from now", magnitudes)
  96. }
  97. testList{
  98. {"now", customRelTime(now), "now"},
  99. {"1 second from now", customRelTime(now.Add(+1 * time.Second)), "1 second from now"},
  100. {"12 seconds from now", customRelTime(now.Add(+12 * time.Second)), "12 seconds from now"},
  101. {"30 seconds from now", customRelTime(now.Add(+30 * time.Second)), "30 seconds from now"},
  102. {"45 seconds from now", customRelTime(now.Add(+45 * time.Second)), "45 seconds from now"},
  103. {"15 minutes from now", customRelTime(now.Add(+15 * time.Minute)), "15 minutes from now"},
  104. {"2 hours from now", customRelTime(now.Add(+2 * time.Hour)), "120 minutes from now"},
  105. {"21 hours from now", customRelTime(now.Add(+21 * time.Hour)), "1260 minutes from now"},
  106. {"1 day from now", customRelTime(now.Add(+26 * time.Hour)), "1 day from now"},
  107. {"2 days from now", customRelTime(now.Add(+49 * time.Hour)), "2 days from now"},
  108. {"3 days from now", customRelTime(now.Add(+3 * Day)), "3 days from now"},
  109. {"1 week from now (1)", customRelTime(now.Add(+7 * Day)), "1 week from now"},
  110. {"1 week from now (2)", customRelTime(now.Add(+12 * Day)), "1 week from now"},
  111. {"2 weeks from now", customRelTime(now.Add(+15 * Day)), "2 weeks from now"},
  112. {"1 month from now", customRelTime(now.Add(+30 * Day)), "4 weeks from now"},
  113. {"6 months from now", customRelTime(now.Add(+6*Month - time.Second)), "25 weeks from now"},
  114. {"1 year from now", customRelTime(now.Add(+365 * Day)), "12 months from now"},
  115. {"2 years from now", customRelTime(now.Add(+2 * Year)), "24 months from now"},
  116. {"a while from now", customRelTime(now.Add(+LongTime)), "444 months from now"},
  117. }.validate(t)
  118. }