2023-07-18 16:09:45 +00:00
|
|
|
package format
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func assertEqual(t *testing.T, a interface{}, b interface{}) {
|
|
|
|
if a != b {
|
|
|
|
t.Errorf("Assert failed, expected %v, got %v", b, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHumanTime(t *testing.T) {
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
t.Run("zero value", func(t *testing.T) {
|
|
|
|
assertEqual(t, HumanTime(time.Time{}, "never"), "never")
|
|
|
|
})
|
2023-10-11 18:05:39 +00:00
|
|
|
|
2023-07-18 16:09:45 +00:00
|
|
|
t.Run("time in the future", func(t *testing.T) {
|
|
|
|
v := now.Add(48 * time.Hour)
|
|
|
|
assertEqual(t, HumanTime(v, ""), "2 days from now")
|
|
|
|
})
|
2023-10-11 18:05:39 +00:00
|
|
|
|
2023-07-18 16:09:45 +00:00
|
|
|
t.Run("time in the past", func(t *testing.T) {
|
|
|
|
v := now.Add(-48 * time.Hour)
|
|
|
|
assertEqual(t, HumanTime(v, ""), "2 days ago")
|
|
|
|
})
|
|
|
|
|
2023-10-11 18:05:39 +00:00
|
|
|
t.Run("soon", func(t *testing.T) {
|
|
|
|
v := now.Add(800*time.Millisecond)
|
|
|
|
assertEqual(t, HumanTime(v, ""), "Less than a second from now")
|
|
|
|
})
|
2023-07-18 16:09:45 +00:00
|
|
|
}
|